From d0b477de9a93c04d38421e65468cbe8ea2695a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 26 Sep 2022 20:14:07 +0200 Subject: [PATCH 01/31] docs: add a basic layout for wait strategies in docs --- docs/features/startup_and_waits.md | 16 ++++++++ docs/features/wait/host_port.md | 26 ++++++++++++ docs/features/wait/http.md | 64 ++++++++++++++++++++++++++++++ mkdocs.yml | 4 ++ 4 files changed, 110 insertions(+) create mode 100644 docs/features/startup_and_waits.md create mode 100644 docs/features/wait/host_port.md create mode 100644 docs/features/wait/http.md diff --git a/docs/features/startup_and_waits.md b/docs/features/startup_and_waits.md new file mode 100644 index 0000000000..58ccee8690 --- /dev/null +++ b/docs/features/startup_and_waits.md @@ -0,0 +1,16 @@ +# Wait Strategies + +!!! info "Wait strategies vs Startup strategies" + + **Wait strategy:** is the container in a state that is useful for testing. This is generally approximated as 'can we talk to this container over the network'. However, there are quite a few variations and nuances. + + **Startup strategy:** did a container reach the desired running state. *Almost always* this just means 'wait until the container is running' - for a daemon process in a container this is the goal. Sometimes we need to wait until the container reaches a running state and then exits - this is the 'one shot startup' strategy, only used for cases where we need to run a one off command in a container but not a daemon. + +When defining a wait strategy, Testcontainers will create a cancel context with 60 seconds defined as timeout. + +If the default 60s timeout is not sufficient, it can be updated with the `WithStartupTimeout(startupTimeout time.Duration)` function, present at each wait struct. + +Below you can find a list of the available wait strategies that you can use: + +- [HostPort](./wait/host_port.md) +- [HTTP](./wait/http.md) diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md new file mode 100644 index 0000000000..30575c4b0e --- /dev/null +++ b/docs/features/wait/host_port.md @@ -0,0 +1,26 @@ +# HostPort wait strategy + +You can choose to wait for: + +- a port to be listening in the container. The port and protocol to be used, which is represented by a string containing port number and protocol in the format "80/tcp" +- the first exposed port in the container. + +## Waiting for a port in the container + +```golang +req := ContainerRequest{ + Image: "docker.io/nginx:alpine", + ExposedPorts: []string{"80/tcp"}, + WaitingFor: wait.ForListeningPort("80/tcp"), +} +``` + +## Waiting for the first exposed port in the container + +```golang +req := ContainerRequest{ + Image: "docker.io/nginx:alpine", + ExposedPorts: []string{"80/tcp"}, + WaitingFor: wait.ForExposedPort(), +} +``` diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md new file mode 100644 index 0000000000..fb87687d49 --- /dev/null +++ b/docs/features/wait/http.md @@ -0,0 +1,64 @@ +# HTTP(S) Wait strategy + +You can choose to wait for an HTTP(S) endpoint that runs in the container, being able to set the following conditions: + +- the port to be used +- the path to be used +- the HTTP method to be used +- the HTTP request body to be sent +- the HTTP status code as a function to resolve a matcher +- the HTTP response as a function to resolve a matcher +- the TLS config to be used for HTTPS +- the PollInterval to be used, default is 100 milliseconds + +Variations on the HTTP wait strategy are supported, including: + +## Waiting for an HTTP endpoint matching an HTTP method + +```golang +req := ContainerRequest{ + Image: "influxdb:1.8.10-alpine", + ExposedPorts: []string{"8086/tcp"}, + WaitingFor: wait.ForAll( + wait.ForHTTP("/ping").WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))), + ), + } +``` + +## Waiting for an HTTP endpoint matching an HTTP status code + +```golang +req := ContainerRequest{ + Image: "influxdb:1.8.10-alpine", + ExposedPorts: []string{"8086/tcp"}, + WaitingFor: wait.ForAll( + wait.ForHTTP("/ping").WithPort("8086/tcp").WithStatusCodeMatcher( + func(status int) bool { + return status == http.StatusNoContent + }, + ), + ), + } +``` + +## Waiting for an HTTPS endpoint including an HTTPS status code and a response matcher + +```golang +req := testcontainers.ContainerRequest{ + FromDockerfile: testcontainers.FromDockerfile{ + Context: workdir + "/testdata", + }, + ExposedPorts: []string{"80/tcp"}, + WaitingFor: wait.NewHTTPStrategy("/ping"). + WithStartupTimeout(time.Second * 10).WithPort("80/tcp"). + WithResponseMatcher(func(body io.Reader) bool { + data, _ := ioutil.ReadAll(body) + return bytes.Equal(data, []byte("pong")) + }). + WithStatusCodeMatcher(func(status int) bool { + i++ // always fail the first try in order to force the polling loop to be re-run + return i > 1 && status == 200 + }). + WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))), +} +``` diff --git a/mkdocs.yml b/mkdocs.yml index 9ff8f892ac..39b81cc7bc 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -37,6 +37,10 @@ nav: - features/override_container_command.md - features/copy_file.md - features/using_podman.md + - Wait Strategies: + - Introduction: features/startup_and_waits.md + - HostPort: features/wait/host_port.md + - HTTP: features/wait/http.md - Examples: - examples/cockroachdb.md - examples/nginx.md From 6c6247951494c35a0fb3c699f450cca1735a51e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 26 Sep 2022 20:21:29 +0200 Subject: [PATCH 02/31] fix: bump docs version --- mkdocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index 39b81cc7bc..e19102da11 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -50,4 +50,4 @@ nav: - contributing_docs.md - Getting help: getting_help.md extra: - latest_version: 0.13.0 + latest_version: 0.14.0 From a85f628c9e2ff4dca4af189a0c4247d0cafcd6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 07:28:15 +0200 Subject: [PATCH 03/31] docs: remove exposed ports --- docs/features/wait/host_port.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md index 30575c4b0e..1225b52ac3 100644 --- a/docs/features/wait/host_port.md +++ b/docs/features/wait/host_port.md @@ -17,10 +17,11 @@ req := ContainerRequest{ ## Waiting for the first exposed port in the container +The wait strategy will use the first exposed port from the image configuration. + ```golang req := ContainerRequest{ Image: "docker.io/nginx:alpine", - ExposedPorts: []string{"80/tcp"}, WaitingFor: wait.ForExposedPort(), } ``` From 57aba3ce04f3c07aab74df7e6f6ea36668c6fc98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 07:28:36 +0200 Subject: [PATCH 04/31] docs: rename intro file --- docs/features/{startup_and_waits.md => wait/introduction.md} | 0 mkdocs.yml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename docs/features/{startup_and_waits.md => wait/introduction.md} (100%) diff --git a/docs/features/startup_and_waits.md b/docs/features/wait/introduction.md similarity index 100% rename from docs/features/startup_and_waits.md rename to docs/features/wait/introduction.md diff --git a/mkdocs.yml b/mkdocs.yml index e19102da11..d5361650f3 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -38,7 +38,7 @@ nav: - features/copy_file.md - features/using_podman.md - Wait Strategies: - - Introduction: features/startup_and_waits.md + - Introduction: features/wait/introduction.md - HostPort: features/wait/host_port.md - HTTP: features/wait/http.md - Examples: From c449248dbc74f22ab89e6bbe718dafb3f1e6a1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 07:51:37 +0200 Subject: [PATCH 05/31] chor: use simpler image --- docs/features/wait/http.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md index fb87687d49..98c643ee66 100644 --- a/docs/features/wait/http.md +++ b/docs/features/wait/http.md @@ -17,7 +17,7 @@ Variations on the HTTP wait strategy are supported, including: ```golang req := ContainerRequest{ - Image: "influxdb:1.8.10-alpine", + Image: "docker.io/nginx:alpine", ExposedPorts: []string{"8086/tcp"}, WaitingFor: wait.ForAll( wait.ForHTTP("/ping").WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))), @@ -29,7 +29,7 @@ req := ContainerRequest{ ```golang req := ContainerRequest{ - Image: "influxdb:1.8.10-alpine", + Image: "docker.io/nginx:alpine", ExposedPorts: []string{"8086/tcp"}, WaitingFor: wait.ForAll( wait.ForHTTP("/ping").WithPort("8086/tcp").WithStatusCodeMatcher( From c44d50fcadbe964b7e814283de77763f44af9bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 07:53:30 +0200 Subject: [PATCH 06/31] chore: simplify waitFor http examples --- docs/features/wait/http.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md index 98c643ee66..9751a325ec 100644 --- a/docs/features/wait/http.md +++ b/docs/features/wait/http.md @@ -19,9 +19,7 @@ Variations on the HTTP wait strategy are supported, including: req := ContainerRequest{ Image: "docker.io/nginx:alpine", ExposedPorts: []string{"8086/tcp"}, - WaitingFor: wait.ForAll( - wait.ForHTTP("/ping").WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))), - ), + WaitingFor: wait.ForHTTP("/ping").WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))), } ``` @@ -31,13 +29,11 @@ req := ContainerRequest{ req := ContainerRequest{ Image: "docker.io/nginx:alpine", ExposedPorts: []string{"8086/tcp"}, - WaitingFor: wait.ForAll( - wait.ForHTTP("/ping").WithPort("8086/tcp").WithStatusCodeMatcher( - func(status int) bool { - return status == http.StatusNoContent - }, - ), - ), + WaitingFor: wait.ForHTTP("/ping").WithPort("8086/tcp").WithStatusCodeMatcher( + func(status int) bool { + return status == http.StatusNoContent + }, + ), } ``` From 2d821463826bbf71f55693957199e8c94bdaebb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 07:59:22 +0200 Subject: [PATCH 07/31] docs: add wait.ForExec --- docs/features/wait/exec.md | 18 ++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 19 insertions(+) create mode 100644 docs/features/wait/exec.md diff --git a/docs/features/wait/exec.md b/docs/features/wait/exec.md new file mode 100644 index 0000000000..f51c1095c2 --- /dev/null +++ b/docs/features/wait/exec.md @@ -0,0 +1,18 @@ +# Exec Wait strategy + +You can choose to wait for a process to be executed in the container, being able to set the following conditions: + +- the command and arguments to be executed, as an array of strings +- the exit code as a function to resolve a matcher, being the default one `0`. +- the PollInterval to be used, default is 100 milliseconds + +## Waiting for a command matching an exit code + +```golang +req := ContainerRequest{ + Image: "docker.io/nginx:alpine", + WaitingFor: wait.NewExecStrategy([]string{"git", "version"}).WithExitCodeMatcher(func(exitCode int) bool { + return exitCode == 10 + }), + } +``` diff --git a/mkdocs.yml b/mkdocs.yml index d5361650f3..a10f9043b6 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -39,6 +39,7 @@ nav: - features/using_podman.md - Wait Strategies: - Introduction: features/wait/introduction.md + - Exec: features/wait/exec.md - HostPort: features/wait/host_port.md - HTTP: features/wait/http.md - Examples: From d83e5f135b29d9a9cb6d69650c01997941efeee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 10:39:28 +0200 Subject: [PATCH 08/31] docs: simplify wording --- docs/features/wait/exec.md | 12 ++++++------ docs/features/wait/host_port.md | 6 ++++-- docs/features/wait/http.md | 6 +++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/docs/features/wait/exec.md b/docs/features/wait/exec.md index f51c1095c2..851b62fad1 100644 --- a/docs/features/wait/exec.md +++ b/docs/features/wait/exec.md @@ -6,13 +6,13 @@ You can choose to wait for a process to be executed in the container, being able - the exit code as a function to resolve a matcher, being the default one `0`. - the PollInterval to be used, default is 100 milliseconds -## Waiting for a command matching an exit code +## Match an exit code ```golang req := ContainerRequest{ - Image: "docker.io/nginx:alpine", - WaitingFor: wait.NewExecStrategy([]string{"git", "version"}).WithExitCodeMatcher(func(exitCode int) bool { - return exitCode == 10 - }), - } + Image: "docker.io/nginx:alpine", + WaitingFor: wait.NewExecStrategy([]string{"git", "version"}).WithExitCodeMatcher(func(exitCode int) bool { + return exitCode == 10 + }), +} ``` diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md index 1225b52ac3..3a761f24da 100644 --- a/docs/features/wait/host_port.md +++ b/docs/features/wait/host_port.md @@ -5,7 +5,9 @@ You can choose to wait for: - a port to be listening in the container. The port and protocol to be used, which is represented by a string containing port number and protocol in the format "80/tcp" - the first exposed port in the container. -## Waiting for a port in the container +Variations on the HosPort wait strategy are supported, including: + +## Listening port in the container ```golang req := ContainerRequest{ @@ -15,7 +17,7 @@ req := ContainerRequest{ } ``` -## Waiting for the first exposed port in the container +## First exposed port in the container The wait strategy will use the first exposed port from the image configuration. diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md index 9751a325ec..7ee6df0fe8 100644 --- a/docs/features/wait/http.md +++ b/docs/features/wait/http.md @@ -13,7 +13,7 @@ You can choose to wait for an HTTP(S) endpoint that runs in the container, being Variations on the HTTP wait strategy are supported, including: -## Waiting for an HTTP endpoint matching an HTTP method +## Match an HTTP method ```golang req := ContainerRequest{ @@ -23,7 +23,7 @@ req := ContainerRequest{ } ``` -## Waiting for an HTTP endpoint matching an HTTP status code +## Match an HTTP status code ```golang req := ContainerRequest{ @@ -37,7 +37,7 @@ req := ContainerRequest{ } ``` -## Waiting for an HTTPS endpoint including an HTTPS status code and a response matcher +## Match an HTTPS status code and a response matcher ```golang req := testcontainers.ContainerRequest{ From 7944e88869be620e2e4d56acac932054cef33202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 10:41:35 +0200 Subject: [PATCH 09/31] docs: link to exec --- docs/features/wait/introduction.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 58ccee8690..53f4f60b58 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -12,5 +12,6 @@ If the default 60s timeout is not sufficient, it can be updated with the `WithSt Below you can find a list of the available wait strategies that you can use: +- [Exec](./wait/exec.md) - [HostPort](./wait/host_port.md) - [HTTP](./wait/http.md) From 199235b47c8495c64565bbfdad0213312b432377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 10:49:21 +0200 Subject: [PATCH 10/31] docs: add exit wait strategy --- docs/features/wait/exit.md | 15 +++++++++++++++ docs/features/wait/introduction.md | 1 + mkdocs.yml | 1 + 3 files changed, 17 insertions(+) create mode 100644 docs/features/wait/exit.md diff --git a/docs/features/wait/exit.md b/docs/features/wait/exit.md new file mode 100644 index 0000000000..40d1026e5e --- /dev/null +++ b/docs/features/wait/exit.md @@ -0,0 +1,15 @@ +# Exit Wait strategy + +The exit wait strategy will check the container is not in the running state, being able to set the following conditions: + +- the exit timeout, default is `0`. +- the PollInterval to be used, default is 100 milliseconds + +## Match an exit code + +```golang +req := ContainerRequest{ + Image: "docker.io/alpine:latest", + WaitingFor: wait.ForExit(), +} +``` diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 53f4f60b58..526f240d23 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -13,5 +13,6 @@ If the default 60s timeout is not sufficient, it can be updated with the `WithSt Below you can find a list of the available wait strategies that you can use: - [Exec](./wait/exec.md) +- [Exit](./wait/exit.md) - [HostPort](./wait/host_port.md) - [HTTP](./wait/http.md) diff --git a/mkdocs.yml b/mkdocs.yml index a10f9043b6..63fee0c668 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -40,6 +40,7 @@ nav: - Wait Strategies: - Introduction: features/wait/introduction.md - Exec: features/wait/exec.md + - Exit: features/wait/exit.md - HostPort: features/wait/host_port.md - HTTP: features/wait/http.md - Examples: From 28974664c376d699e432a25b612eb268262d8059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 10:52:02 +0200 Subject: [PATCH 11/31] chore: wording --- docs/features/wait/exec.md | 2 +- docs/features/wait/host_port.md | 2 +- docs/features/wait/http.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/features/wait/exec.md b/docs/features/wait/exec.md index 851b62fad1..15ce89ff38 100644 --- a/docs/features/wait/exec.md +++ b/docs/features/wait/exec.md @@ -1,6 +1,6 @@ # Exec Wait strategy -You can choose to wait for a process to be executed in the container, being able to set the following conditions: +The exec wait strategy will check the exit code of a process to be executed in the container, being able to set the following conditions: - the command and arguments to be executed, as an array of strings - the exit code as a function to resolve a matcher, being the default one `0`. diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md index 3a761f24da..65034dac7e 100644 --- a/docs/features/wait/host_port.md +++ b/docs/features/wait/host_port.md @@ -1,6 +1,6 @@ # HostPort wait strategy -You can choose to wait for: +The host-port wait strategy will check if a port is listening in the container, being able to set the following conditions: - a port to be listening in the container. The port and protocol to be used, which is represented by a string containing port number and protocol in the format "80/tcp" - the first exposed port in the container. diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md index 7ee6df0fe8..ba9682ed0c 100644 --- a/docs/features/wait/http.md +++ b/docs/features/wait/http.md @@ -1,6 +1,6 @@ # HTTP(S) Wait strategy -You can choose to wait for an HTTP(S) endpoint that runs in the container, being able to set the following conditions: +The HTTP wait strategy will check the result of an HTTP(S) request that is available in the container, being able to set the following conditions: - the port to be used - the path to be used From afdcec250289f45dae010de2b4b1c54b541ecb16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 10:53:20 +0200 Subject: [PATCH 12/31] fix: relative links --- docs/features/wait/introduction.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 526f240d23..4e44aee222 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -12,7 +12,7 @@ If the default 60s timeout is not sufficient, it can be updated with the `WithSt Below you can find a list of the available wait strategies that you can use: -- [Exec](./wait/exec.md) -- [Exit](./wait/exit.md) -- [HostPort](./wait/host_port.md) -- [HTTP](./wait/http.md) +- [Exec](./exec.md) +- [Exit](./exit.md) +- [HostPort](./host_port.md) +- [HTTP](./http.md) From 5e7711352f3a8dc9d58cd90baf13599a3dece3c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 11:01:38 +0200 Subject: [PATCH 13/31] feat: support setting poll interval in host port --- wait/host_port.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/wait/host_port.go b/wait/host_port.go index 98be362901..53de3405ba 100644 --- a/wait/host_port.go +++ b/wait/host_port.go @@ -21,6 +21,7 @@ type HostPortStrategy struct { Port nat.Port // all WaitStrategies should have a startupTimeout to avoid waiting infinitely startupTimeout time.Duration + PollInterval time.Duration } // NewHostPortStrategy constructs a default host port strategy @@ -28,6 +29,7 @@ func NewHostPortStrategy(port nat.Port) *HostPortStrategy { return &HostPortStrategy{ Port: port, startupTimeout: defaultStartupTimeout(), + PollInterval: defaultPollInterval(), } } @@ -52,6 +54,12 @@ func (hp *HostPortStrategy) WithStartupTimeout(startupTimeout time.Duration) *Ho return hp } +// WithPollInterval can be used to override the default polling interval of 100 milliseconds +func (hp *HostPortStrategy) WithPollInterval(pollInterval time.Duration) *HostPortStrategy { + hp.PollInterval = pollInterval + return hp +} + // WaitUntilReady implements Strategy.WaitUntilReady func (hp *HostPortStrategy) WaitUntilReady(ctx context.Context, target StrategyTarget) (err error) { // limit context to startupTimeout @@ -63,7 +71,7 @@ func (hp *HostPortStrategy) WaitUntilReady(ctx context.Context, target StrategyT return } - var waitInterval = 100 * time.Millisecond + var waitInterval = hp.PollInterval internalPort := hp.Port if internalPort == "" { From 340282f06659bcc359c590b70be6a8cce49bd129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 11:09:18 +0200 Subject: [PATCH 14/31] chore: include startup timeout --- docs/features/wait/exec.md | 3 ++- docs/features/wait/exit.md | 2 +- docs/features/wait/host_port.md | 2 ++ docs/features/wait/http.md | 3 ++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/features/wait/exec.md b/docs/features/wait/exec.md index 15ce89ff38..2d3ac99ee1 100644 --- a/docs/features/wait/exec.md +++ b/docs/features/wait/exec.md @@ -4,7 +4,8 @@ The exec wait strategy will check the exit code of a process to be executed in t - the command and arguments to be executed, as an array of strings - the exit code as a function to resolve a matcher, being the default one `0`. -- the PollInterval to be used, default is 100 milliseconds +- the startup timeout to be used, default is 60 seconds +- the poll interval to be used, default is 100 milliseconds ## Match an exit code diff --git a/docs/features/wait/exit.md b/docs/features/wait/exit.md index 40d1026e5e..d9c155e9fc 100644 --- a/docs/features/wait/exit.md +++ b/docs/features/wait/exit.md @@ -3,7 +3,7 @@ The exit wait strategy will check the container is not in the running state, being able to set the following conditions: - the exit timeout, default is `0`. -- the PollInterval to be used, default is 100 milliseconds +- the poll interval to be used, default is 100 milliseconds ## Match an exit code diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md index 65034dac7e..b486a5260e 100644 --- a/docs/features/wait/host_port.md +++ b/docs/features/wait/host_port.md @@ -4,6 +4,8 @@ The host-port wait strategy will check if a port is listening in the container, - a port to be listening in the container. The port and protocol to be used, which is represented by a string containing port number and protocol in the format "80/tcp" - the first exposed port in the container. +- the startup timeout to be used, default is 60 seconds +- the poll interval to be used, default is 100 milliseconds Variations on the HosPort wait strategy are supported, including: diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md index ba9682ed0c..084c5a615e 100644 --- a/docs/features/wait/http.md +++ b/docs/features/wait/http.md @@ -9,7 +9,8 @@ The HTTP wait strategy will check the result of an HTTP(S) request that is avail - the HTTP status code as a function to resolve a matcher - the HTTP response as a function to resolve a matcher - the TLS config to be used for HTTPS -- the PollInterval to be used, default is 100 milliseconds +- the startup timeout to be used, default is 60 seconds +- the poll interval to be used, default is 100 milliseconds Variations on the HTTP wait strategy are supported, including: From 63e4321536bda8fd905f6ac277c5ceb806b9a07d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 11:11:51 +0200 Subject: [PATCH 15/31] docs: add health strategy --- docs/features/wait/health.md | 13 +++++++++++++ docs/features/wait/introduction.md | 1 + mkdocs.yml | 1 + 3 files changed, 15 insertions(+) create mode 100644 docs/features/wait/health.md diff --git a/docs/features/wait/health.md b/docs/features/wait/health.md new file mode 100644 index 0000000000..bc2036df24 --- /dev/null +++ b/docs/features/wait/health.md @@ -0,0 +1,13 @@ +# Health Wait strategy + +The health wait strategy will check the container is in the healthy state, being able to set the following conditions: + +- the startupTimeout to be used, default is 60 seconds +- the PollInterval to be used, default is 100 milliseconds + +```golang +req := ContainerRequest{ + Image: "docker.io/alpine:latest", + WaitingFor: wait.ForHealthCheck(), +} +``` diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 4e44aee222..5663d9ef38 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -14,5 +14,6 @@ Below you can find a list of the available wait strategies that you can use: - [Exec](./exec.md) - [Exit](./exit.md) +- [Health](./health.md) - [HostPort](./host_port.md) - [HTTP](./http.md) diff --git a/mkdocs.yml b/mkdocs.yml index 63fee0c668..d7b55904e5 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -41,6 +41,7 @@ nav: - Introduction: features/wait/introduction.md - Exec: features/wait/exec.md - Exit: features/wait/exit.md + - Health: features/wait/health.md - HostPort: features/wait/host_port.md - HTTP: features/wait/http.md - Examples: From b5f4aacc72477eb7fca724d4924e4c41466ca660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 11:24:36 +0200 Subject: [PATCH 16/31] fix: add dots --- docs/features/wait/exec.md | 6 +++--- docs/features/wait/exit.md | 2 +- docs/features/wait/health.md | 4 ++-- docs/features/wait/host_port.md | 6 +++--- docs/features/wait/http.md | 18 +++++++++--------- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/features/wait/exec.md b/docs/features/wait/exec.md index 2d3ac99ee1..2406d6fe3c 100644 --- a/docs/features/wait/exec.md +++ b/docs/features/wait/exec.md @@ -2,10 +2,10 @@ The exec wait strategy will check the exit code of a process to be executed in the container, being able to set the following conditions: -- the command and arguments to be executed, as an array of strings +- the command and arguments to be executed, as an array of strings. - the exit code as a function to resolve a matcher, being the default one `0`. -- the startup timeout to be used, default is 60 seconds -- the poll interval to be used, default is 100 milliseconds +- the startup timeout to be used, default is 60 seconds. +- the poll interval to be used, default is 100 milliseconds. ## Match an exit code diff --git a/docs/features/wait/exit.md b/docs/features/wait/exit.md index d9c155e9fc..5d5086236f 100644 --- a/docs/features/wait/exit.md +++ b/docs/features/wait/exit.md @@ -3,7 +3,7 @@ The exit wait strategy will check the container is not in the running state, being able to set the following conditions: - the exit timeout, default is `0`. -- the poll interval to be used, default is 100 milliseconds +- the poll interval to be used, default is 100 milliseconds. ## Match an exit code diff --git a/docs/features/wait/health.md b/docs/features/wait/health.md index bc2036df24..db1c55c656 100644 --- a/docs/features/wait/health.md +++ b/docs/features/wait/health.md @@ -2,8 +2,8 @@ The health wait strategy will check the container is in the healthy state, being able to set the following conditions: -- the startupTimeout to be used, default is 60 seconds -- the PollInterval to be used, default is 100 milliseconds +- the startupTimeout to be used, default is 60 seconds. +- the PollInterval to be used, default is 100 milliseconds. ```golang req := ContainerRequest{ diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md index b486a5260e..df9c67babc 100644 --- a/docs/features/wait/host_port.md +++ b/docs/features/wait/host_port.md @@ -2,10 +2,10 @@ The host-port wait strategy will check if a port is listening in the container, being able to set the following conditions: -- a port to be listening in the container. The port and protocol to be used, which is represented by a string containing port number and protocol in the format "80/tcp" +- a port to be listening in the container. The port and protocol to be used, which is represented by a string containing port number and protocol in the format "80/tcp". - the first exposed port in the container. -- the startup timeout to be used, default is 60 seconds -- the poll interval to be used, default is 100 milliseconds +- the startup timeout to be used, default is 60 seconds. +- the poll interval to be used, default is 100 milliseconds. Variations on the HosPort wait strategy are supported, including: diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md index 084c5a615e..3efd48803d 100644 --- a/docs/features/wait/http.md +++ b/docs/features/wait/http.md @@ -2,15 +2,15 @@ The HTTP wait strategy will check the result of an HTTP(S) request that is available in the container, being able to set the following conditions: -- the port to be used -- the path to be used -- the HTTP method to be used -- the HTTP request body to be sent -- the HTTP status code as a function to resolve a matcher -- the HTTP response as a function to resolve a matcher -- the TLS config to be used for HTTPS -- the startup timeout to be used, default is 60 seconds -- the poll interval to be used, default is 100 milliseconds +- the port to be used. +- the path to be used. +- the HTTP method to be used. +- the HTTP request body to be sent. +- the HTTP status code as a function to resolve a matcher. +- the HTTP response as a function to resolve a matcher. +- the TLS config to be used for HTTPS. +- the startup timeout to be used, default is 60 seconds. +- the poll interval to be used, default is 100 milliseconds. Variations on the HTTP wait strategy are supported, including: From 05861242464cddeac6de7165d3fe856e890fb47c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 11:28:14 +0200 Subject: [PATCH 17/31] docs: add wait for log --- docs/features/wait/introduction.md | 1 + docs/features/wait/log.md | 20 ++++++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 22 insertions(+) create mode 100644 docs/features/wait/log.md diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 5663d9ef38..a243059a86 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -17,3 +17,4 @@ Below you can find a list of the available wait strategies that you can use: - [Health](./health.md) - [HostPort](./host_port.md) - [HTTP](./http.md) +- [Log](./log.md) diff --git a/docs/features/wait/log.md b/docs/features/wait/log.md new file mode 100644 index 0000000000..fc6c6f0d9b --- /dev/null +++ b/docs/features/wait/log.md @@ -0,0 +1,20 @@ +# Log Wait strategy + +The Log wait strategy will check if a string is present in the container logs with a desired number of ocurrences, being able to set the following conditions: + +- the string to be searched in the container log. +- the number of ocurrences of the searched string, default is `1`. +- the startup timeout to be used, default is 60 seconds. +- the poll interval to be used, default is 100 milliseconds. + +```golang +req := ContainerRequest{ + Image: "docker.io/mysql:latest", + ExposedPorts: []string{"3306/tcp", "33060/tcp"}, + Env: map[string]string{ + "MYSQL_ROOT_PASSWORD": "password", + "MYSQL_DATABASE": "database", + }, + WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"), +} +``` diff --git a/mkdocs.yml b/mkdocs.yml index d7b55904e5..33594c8bf8 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -44,6 +44,7 @@ nav: - Health: features/wait/health.md - HostPort: features/wait/host_port.md - HTTP: features/wait/http.md + - Log: features/wait/log.md - Examples: - examples/cockroachdb.md - examples/nginx.md From ce6170b0af5f5451508184e040a4a510126acab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 11:34:38 +0200 Subject: [PATCH 18/31] docs: add wait for multiple --- docs/features/wait/introduction.md | 1 + docs/features/wait/multi.md | 20 ++++++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 22 insertions(+) create mode 100644 docs/features/wait/multi.md diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index a243059a86..0e8db94b3d 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -18,3 +18,4 @@ Below you can find a list of the available wait strategies that you can use: - [HostPort](./host_port.md) - [HTTP](./http.md) - [Log](./log.md) +- [Multi](./multi.md) diff --git a/docs/features/wait/multi.md b/docs/features/wait/multi.md new file mode 100644 index 0000000000..c563f9963e --- /dev/null +++ b/docs/features/wait/multi.md @@ -0,0 +1,20 @@ +# Multi Wait strategy + +The Multi wait strategy will hold a list of wait strategies, in order to wait for all of them. It's possible to set the following conditions: + +- the startup timeout to be used, default is 60 seconds. + +```golang +req := ContainerRequest{ + Image: "docker.io/mysql:latest", + ExposedPorts: []string{"3306/tcp", "33060/tcp"}, + Env: map[string]string{ + "MYSQL_ROOT_PASSWORD": "password", + "MYSQL_DATABASE": "database", + }, + WaitingFor: wait.ForAll( + wait.ForLog("port: 3306 MySQL Community Server - GPL"), + wait.ForListeningPort("3306/tcp"), + ).WithStartupTimeout(10*time.Second), +} +``` diff --git a/mkdocs.yml b/mkdocs.yml index 33594c8bf8..14568aece4 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -45,6 +45,7 @@ nav: - HostPort: features/wait/host_port.md - HTTP: features/wait/http.md - Log: features/wait/log.md + - Multi: features/wait/multi.md - Examples: - examples/cockroachdb.md - examples/nginx.md From d9e060b65c123abf72d73a2e7d6f5ae897043317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 11:44:01 +0200 Subject: [PATCH 19/31] chore: consistency with startup timeout method in SQL wait --- wait/sql.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wait/sql.go b/wait/sql.go index 09f5fd7590..ff305fd8f2 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -33,8 +33,14 @@ type waitForSql struct { } //Timeout sets the maximum waiting time for the strategy after which it'll give up and return an error +// Deprecated: Use WithStartupTimeout func (w *waitForSql) Timeout(duration time.Duration) *waitForSql { - w.startupTimeout = duration + return w.WithStartupTimeout(duration) +} + +// WithStartupTimeout can be used to change the default startup timeout +func (w *waitForSql) WithStartupTimeout(startupTimeout time.Duration) *waitForSql { + w.startupTimeout = startupTimeout return w } From 74318796b331fd87980eb0c9a6a833b7d38bbb73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 11:50:16 +0200 Subject: [PATCH 20/31] docs: add wait for SQL --- docs/features/wait/introduction.md | 1 + docs/features/wait/sql.md | 22 ++++++++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 24 insertions(+) create mode 100644 docs/features/wait/sql.md diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 0e8db94b3d..a7e6d4b9a8 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -19,3 +19,4 @@ Below you can find a list of the available wait strategies that you can use: - [HTTP](./http.md) - [Log](./log.md) - [Multi](./multi.md) +- [SQL](./sql.md) diff --git a/docs/features/wait/sql.md b/docs/features/wait/sql.md new file mode 100644 index 0000000000..c190a02f5f --- /dev/null +++ b/docs/features/wait/sql.md @@ -0,0 +1,22 @@ +# SQL Wait strategy + +The SQL wait strategy will check the result of an HTTP(S) request that is available in the container, being able to set the following conditions: + +- the SQL query to be used, default is `SELECT 1`. +- the port to be used. +- the database driver to be used, as a string. +- the URL of the database to be used, as a function returning the URL string. +- the startup timeout to be used, default is 60 seconds. +- the poll interval to be used, default is 100 milliseconds. + +```golang +req := ContainerRequest{ + Image: "postgres:14.1-alpine", + ExposedPorts: []string{port}, + Cmd: []string{"postgres", "-c", "fsync=off"}, + Env: env, + WaitingFor: wait.ForSQL(nat.Port(port), "postgres", dbURL). + WithStartupTimeout(time.Second * 5). + WithQuery("SELECT 10"), +} +``` diff --git a/mkdocs.yml b/mkdocs.yml index 14568aece4..0d1a7574ad 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -46,6 +46,7 @@ nav: - HTTP: features/wait/http.md - Log: features/wait/log.md - Multi: features/wait/multi.md + - SQL: features/wait/sql.md - Examples: - examples/cockroachdb.md - examples/nginx.md From 2bdbf457f0b196b37cd952065e53c86a12af2288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 14:33:40 +0200 Subject: [PATCH 21/31] chore: apply suggestions from code review Co-authored-by: Kevin Wittek --- docs/features/wait/exec.md | 8 ++++---- docs/features/wait/exit.md | 6 +++--- docs/features/wait/health.md | 6 +++--- docs/features/wait/host_port.md | 8 ++++---- docs/features/wait/http.md | 10 +++++----- docs/features/wait/introduction.md | 2 +- docs/features/wait/log.md | 10 +++++----- docs/features/wait/multi.md | 2 +- docs/features/wait/sql.md | 4 ++-- 9 files changed, 28 insertions(+), 28 deletions(-) diff --git a/docs/features/wait/exec.md b/docs/features/wait/exec.md index 2406d6fe3c..871bdad55b 100644 --- a/docs/features/wait/exec.md +++ b/docs/features/wait/exec.md @@ -1,11 +1,11 @@ -# Exec Wait strategy +# Exec Wait Strategy The exec wait strategy will check the exit code of a process to be executed in the container, being able to set the following conditions: - the command and arguments to be executed, as an array of strings. -- the exit code as a function to resolve a matcher, being the default one `0`. -- the startup timeout to be used, default is 60 seconds. -- the poll interval to be used, default is 100 milliseconds. +- a function to match a specific exit code, with the default matching `0`. +- the startup timeout to be used in seconds, default is 60 seconds. +- the poll interval to be used in milliseconds, default is 100 milliseconds. ## Match an exit code diff --git a/docs/features/wait/exit.md b/docs/features/wait/exit.md index 5d5086236f..3487cb2d21 100644 --- a/docs/features/wait/exit.md +++ b/docs/features/wait/exit.md @@ -1,9 +1,9 @@ # Exit Wait strategy -The exit wait strategy will check the container is not in the running state, being able to set the following conditions: +The exit wait strategy will check that the container is not in the running state, and allows to set the following conditions: -- the exit timeout, default is `0`. -- the poll interval to be used, default is 100 milliseconds. +- the exit timeout in seconds, default is `0`. +- the poll interval to be used in milliseconds, default is 100 milliseconds. ## Match an exit code diff --git a/docs/features/wait/health.md b/docs/features/wait/health.md index db1c55c656..d4756f47bd 100644 --- a/docs/features/wait/health.md +++ b/docs/features/wait/health.md @@ -1,9 +1,9 @@ # Health Wait strategy -The health wait strategy will check the container is in the healthy state, being able to set the following conditions: +The health wait strategy will check that the container is in the healthy state and allows to set the following conditions: -- the startupTimeout to be used, default is 60 seconds. -- the PollInterval to be used, default is 100 milliseconds. +- the startup timeout to be used in seconds, default is 60 seconds. +- the poll interval to be used in milliseconds, default is 100 milliseconds. ```golang req := ContainerRequest{ diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md index df9c67babc..baf09ea9e8 100644 --- a/docs/features/wait/host_port.md +++ b/docs/features/wait/host_port.md @@ -1,13 +1,13 @@ -# HostPort wait strategy +# HostPort Wait strategy -The host-port wait strategy will check if a port is listening in the container, being able to set the following conditions: +The host-port wait strategy will check if the container is listening to a specific port and allows to set the following conditions: -- a port to be listening in the container. The port and protocol to be used, which is represented by a string containing port number and protocol in the format "80/tcp". +- a port exposed by the container. The port and protocol to be used, which is represented by a string containing the port number and protocol in the format "80/tcp". - the first exposed port in the container. - the startup timeout to be used, default is 60 seconds. - the poll interval to be used, default is 100 milliseconds. -Variations on the HosPort wait strategy are supported, including: +Variations on the HostPort wait strategy are supported, including: ## Listening port in the container diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md index 3efd48803d..91d5aefd3e 100644 --- a/docs/features/wait/http.md +++ b/docs/features/wait/http.md @@ -1,16 +1,16 @@ # HTTP(S) Wait strategy -The HTTP wait strategy will check the result of an HTTP(S) request that is available in the container, being able to set the following conditions: +The HTTP wait strategy will check the result of an HTTP(S) request against the container and allows to set the following conditions: - the port to be used. - the path to be used. - the HTTP method to be used. - the HTTP request body to be sent. -- the HTTP status code as a function to resolve a matcher. -- the HTTP response as a function to resolve a matcher. +- the HTTP status code matcher as a function. +- the HTTP response matcher as a function. - the TLS config to be used for HTTPS. -- the startup timeout to be used, default is 60 seconds. -- the poll interval to be used, default is 100 milliseconds. +- the startup timeout to be used in seconds, default is 60 seconds. +- the poll interval to be used in milliseconds, default is 100 milliseconds. Variations on the HTTP wait strategy are supported, including: diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index a7e6d4b9a8..fd41aaae65 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -2,7 +2,7 @@ !!! info "Wait strategies vs Startup strategies" - **Wait strategy:** is the container in a state that is useful for testing. This is generally approximated as 'can we talk to this container over the network'. However, there are quite a few variations and nuances. + **Wait strategy:** Is the container in a state that is useful for testing? This is generally approximated as 'Can we talk to this container over the network?'. However, in practice, there are quite a few variations and nuances. **Startup strategy:** did a container reach the desired running state. *Almost always* this just means 'wait until the container is running' - for a daemon process in a container this is the goal. Sometimes we need to wait until the container reaches a running state and then exits - this is the 'one shot startup' strategy, only used for cases where we need to run a one off command in a container but not a daemon. diff --git a/docs/features/wait/log.md b/docs/features/wait/log.md index fc6c6f0d9b..2825423498 100644 --- a/docs/features/wait/log.md +++ b/docs/features/wait/log.md @@ -1,11 +1,11 @@ # Log Wait strategy -The Log wait strategy will check if a string is present in the container logs with a desired number of ocurrences, being able to set the following conditions: +The Log wait strategy will check if a string occurs in the container logs for a desired number of times, and allows to set the following conditions: -- the string to be searched in the container log. -- the number of ocurrences of the searched string, default is `1`. -- the startup timeout to be used, default is 60 seconds. -- the poll interval to be used, default is 100 milliseconds. +- the string to be waited for in the container log. +- the number of occurrences of the string to wait for, default is `1`. +- the startup timeout to be used in seconds, default is 60 seconds. +- the poll interval to be used in milliseconds, default is 100 milliseconds. ```golang req := ContainerRequest{ diff --git a/docs/features/wait/multi.md b/docs/features/wait/multi.md index c563f9963e..0511d0d449 100644 --- a/docs/features/wait/multi.md +++ b/docs/features/wait/multi.md @@ -2,7 +2,7 @@ The Multi wait strategy will hold a list of wait strategies, in order to wait for all of them. It's possible to set the following conditions: -- the startup timeout to be used, default is 60 seconds. +- the startup timeout to be used in seconds, default is 60 seconds. ```golang req := ContainerRequest{ diff --git a/docs/features/wait/sql.md b/docs/features/wait/sql.md index c190a02f5f..bd8c638b9b 100644 --- a/docs/features/wait/sql.md +++ b/docs/features/wait/sql.md @@ -6,8 +6,8 @@ The SQL wait strategy will check the result of an HTTP(S) request that is availa - the port to be used. - the database driver to be used, as a string. - the URL of the database to be used, as a function returning the URL string. -- the startup timeout to be used, default is 60 seconds. -- the poll interval to be used, default is 100 milliseconds. +- the startup timeout to be used in seconds, default is 60 seconds. +- the poll interval to be used in milliseconds, default is 100 milliseconds. ```golang req := ContainerRequest{ From dc7674ed0ea2b929d7a6812d3cb6d8e17cceb675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 14:35:31 +0200 Subject: [PATCH 22/31] fix: wrong copy & paste --- docs/features/wait/sql.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/wait/sql.md b/docs/features/wait/sql.md index bd8c638b9b..5e261cf6c6 100644 --- a/docs/features/wait/sql.md +++ b/docs/features/wait/sql.md @@ -1,6 +1,6 @@ # SQL Wait strategy -The SQL wait strategy will check the result of an HTTP(S) request that is available in the container, being able to set the following conditions: +The SQL wait strategy will check the result of a SQL query executed in a container representing a SQL database, and allows to set the following conditions: - the SQL query to be used, default is `SELECT 1`. - the port to be used. From 7c054c6e58e1fa3f8a5321e8240c824f597de610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 27 Sep 2022 14:36:25 +0200 Subject: [PATCH 23/31] chore: wording --- docs/features/wait/exec.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/wait/exec.md b/docs/features/wait/exec.md index 871bdad55b..11c0474bfa 100644 --- a/docs/features/wait/exec.md +++ b/docs/features/wait/exec.md @@ -1,6 +1,6 @@ # Exec Wait Strategy -The exec wait strategy will check the exit code of a process to be executed in the container, being able to set the following conditions: +The exec wait strategy will check the exit code of a process to be executed in the container, and allows to set the following conditions: - the command and arguments to be executed, as an array of strings. - a function to match a specific exit code, with the default matching `0`. From 39d50460e7060b3af983d16d6b40f52b2ca3259a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 28 Sep 2022 11:30:29 +0200 Subject: [PATCH 24/31] chore: explain wait.FromExposedPort and container config --- docs/features/wait/host_port.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md index baf09ea9e8..fbe4e1a993 100644 --- a/docs/features/wait/host_port.md +++ b/docs/features/wait/host_port.md @@ -21,7 +21,7 @@ req := ContainerRequest{ ## First exposed port in the container -The wait strategy will use the first exposed port from the image configuration. +The wait strategy will use the first exposed port from the container configuration. ```golang req := ContainerRequest{ @@ -29,3 +29,13 @@ req := ContainerRequest{ WaitingFor: wait.ForExposedPort(), } ``` + +Said that, it could be the case that the container request included ports to be exposed. Therefore using `wait.ForExposedPort` will wait for the first exposed port in the request, because the container configuration retrieved from Docker will already include them. + +```golang +req := ContainerRequest{ + Image: "docker.io/nginx:alpine", + ExposedPorts: []string{"80/tcp", "9080/tcp"}, + WaitingFor: wait.ForExposedPort(), +} +``` \ No newline at end of file From f50fcedc9f2ad6f55f79a4b1697e6527ad4df926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 28 Sep 2022 11:32:23 +0200 Subject: [PATCH 25/31] docs: clarify --- docs/features/wait/host_port.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/wait/host_port.md b/docs/features/wait/host_port.md index fbe4e1a993..5d52f99cc1 100644 --- a/docs/features/wait/host_port.md +++ b/docs/features/wait/host_port.md @@ -3,7 +3,7 @@ The host-port wait strategy will check if the container is listening to a specific port and allows to set the following conditions: - a port exposed by the container. The port and protocol to be used, which is represented by a string containing the port number and protocol in the format "80/tcp". -- the first exposed port in the container. +- alternatively, wait for the first exposed port in the container. - the startup timeout to be used, default is 60 seconds. - the poll interval to be used, default is 100 milliseconds. From 4671f4ddee05dd6c469cf0df8c34578b03de6cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 28 Sep 2022 12:04:15 +0200 Subject: [PATCH 26/31] chore: apply suggestions from code review Co-authored-by: Kevin Wittek --- docs/features/wait/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index fd41aaae65..42f6ceab44 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -4,7 +4,7 @@ **Wait strategy:** Is the container in a state that is useful for testing? This is generally approximated as 'Can we talk to this container over the network?'. However, in practice, there are quite a few variations and nuances. - **Startup strategy:** did a container reach the desired running state. *Almost always* this just means 'wait until the container is running' - for a daemon process in a container this is the goal. Sometimes we need to wait until the container reaches a running state and then exits - this is the 'one shot startup' strategy, only used for cases where we need to run a one off command in a container but not a daemon. + **Startup strategy:** Did a container reach the desired running state? *Almost always* this just means 'wait until the container is running' - for a daemon process in a container this is the goal. Sometimes we need to wait until the container reaches a running state and then exits - this is the 'one shot startup' strategy, only used for cases where we need to run a one off command in a container but not a daemon. When defining a wait strategy, Testcontainers will create a cancel context with 60 seconds defined as timeout. From 9c1278c2ae75333d4d601657b222626efba0f3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 28 Sep 2022 12:05:36 +0200 Subject: [PATCH 27/31] fix: replace one shot with exit --- docs/features/wait/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 42f6ceab44..bf25d3d11a 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -4,7 +4,7 @@ **Wait strategy:** Is the container in a state that is useful for testing? This is generally approximated as 'Can we talk to this container over the network?'. However, in practice, there are quite a few variations and nuances. - **Startup strategy:** Did a container reach the desired running state? *Almost always* this just means 'wait until the container is running' - for a daemon process in a container this is the goal. Sometimes we need to wait until the container reaches a running state and then exits - this is the 'one shot startup' strategy, only used for cases where we need to run a one off command in a container but not a daemon. + **Startup strategy:** Did a container reach the desired running state? *Almost always* this just means 'wait until the container is running' - for a daemon process in a container this is the goal. Sometimes we need to wait until the container reaches a running state and then exits - this is the `wait.ForExit` strategy, only used for cases where we need to run a one off command in a container but not a daemon. When defining a wait strategy, Testcontainers will create a cancel context with 60 seconds defined as timeout. From 6a26f096f2978c93627e7e2f6da05e3197afbb72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 28 Sep 2022 12:41:21 +0200 Subject: [PATCH 28/31] fix: refine introduction to wait strategies --- docs/features/wait/introduction.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index bf25d3d11a..8d1302e009 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -1,10 +1,8 @@ # Wait Strategies -!!! info "Wait strategies vs Startup strategies" +There are scenarios where your tests need the external services they rely on to reach a specific state that is particularly useful for testing. This is generally approximated as 'Can we talk to this container over the network?' or 'Let's wait until the container is running an reaches certain state'. - **Wait strategy:** Is the container in a state that is useful for testing? This is generally approximated as 'Can we talk to this container over the network?'. However, in practice, there are quite a few variations and nuances. - - **Startup strategy:** Did a container reach the desired running state? *Almost always* this just means 'wait until the container is running' - for a daemon process in a container this is the goal. Sometimes we need to wait until the container reaches a running state and then exits - this is the `wait.ForExit` strategy, only used for cases where we need to run a one off command in a container but not a daemon. +Testcontainers-go comes with the concept of `wait strategy`, which allows your tests to actually wait for the most useful conditions to be met, before continuing with their execution. These wait strategies are implemented in the `wait` package. When defining a wait strategy, Testcontainers will create a cancel context with 60 seconds defined as timeout. From c8a83eb5a62b1440353983243af5de5476208b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 28 Sep 2022 12:42:52 +0200 Subject: [PATCH 29/31] docs: create section for startup timeouts --- docs/features/wait/introduction.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 8d1302e009..8a7016ad01 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -4,10 +4,6 @@ There are scenarios where your tests need the external services they rely on to Testcontainers-go comes with the concept of `wait strategy`, which allows your tests to actually wait for the most useful conditions to be met, before continuing with their execution. These wait strategies are implemented in the `wait` package. -When defining a wait strategy, Testcontainers will create a cancel context with 60 seconds defined as timeout. - -If the default 60s timeout is not sufficient, it can be updated with the `WithStartupTimeout(startupTimeout time.Duration)` function, present at each wait struct. - Below you can find a list of the available wait strategies that you can use: - [Exec](./exec.md) @@ -18,3 +14,9 @@ Below you can find a list of the available wait strategies that you can use: - [Log](./log.md) - [Multi](./multi.md) - [SQL](./sql.md) + +## Startup timeout + +When defining a wait strategy, Testcontainers will create a cancel context with 60 seconds defined as timeout. + +If the default 60s timeout is not sufficient, it can be updated with the `WithStartupTimeout(startupTimeout time.Duration)` function, present at each wait struct. From b57fe603f29a80e908990eedab41b2a42a2d85d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 28 Sep 2022 12:45:03 +0200 Subject: [PATCH 30/31] docs: rephrase startup timeout --- docs/features/wait/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 8a7016ad01..24999ee337 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -17,6 +17,6 @@ Below you can find a list of the available wait strategies that you can use: ## Startup timeout -When defining a wait strategy, Testcontainers will create a cancel context with 60 seconds defined as timeout. +When defining a wait strategy, it should define a way to set the startup timeout to avoid waiting infinitely. For that, Testcontainers-go creates a cancel context with 60 seconds defined as timeout. If the default 60s timeout is not sufficient, it can be updated with the `WithStartupTimeout(startupTimeout time.Duration)` function, present at each wait struct. From fb8713958688ff482e868ecea8c4f1476b606560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 28 Sep 2022 12:50:51 +0200 Subject: [PATCH 31/31] docs: explain poll interval --- docs/features/wait/introduction.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/features/wait/introduction.md b/docs/features/wait/introduction.md index 24999ee337..59423fa153 100644 --- a/docs/features/wait/introduction.md +++ b/docs/features/wait/introduction.md @@ -15,8 +15,12 @@ Below you can find a list of the available wait strategies that you can use: - [Multi](./multi.md) - [SQL](./sql.md) -## Startup timeout +## Startup timeout and Poll interval When defining a wait strategy, it should define a way to set the startup timeout to avoid waiting infinitely. For that, Testcontainers-go creates a cancel context with 60 seconds defined as timeout. -If the default 60s timeout is not sufficient, it can be updated with the `WithStartupTimeout(startupTimeout time.Duration)` function, present at each wait struct. +If the default 60s timeout is not sufficient, it can be updated with the `WithStartupTimeout(startupTimeout time.Duration)` function. + +Besides that, it's possible to define a poll interval, which will actually stop 100 milliseconds the test execution. + +If the default 100 milliseconds poll interval is not sufficient, it can be updated with the `WithPollInterval(pollInterval time.Duration)` function.