diff --git a/tests/images/healthcheck/is-healthy.go b/tests/images/healthcheck/is-healthy.go index a002ddce5..2b4069c85 100644 --- a/tests/images/healthcheck/is-healthy.go +++ b/tests/images/healthcheck/is-healthy.go @@ -13,7 +13,7 @@ import ( ) func main() { - data, err := os.ReadFile("health.txt") + data, err := os.ReadFile("/health.txt") if err != nil { fmt.Fprintf(os.Stderr, "Error while reading health status: %s\n", err) os.Exit(1) diff --git a/tests/images/healthcheck/main.go b/tests/images/healthcheck/main.go index 842729db3..9d62b1b7d 100644 --- a/tests/images/healthcheck/main.go +++ b/tests/images/healthcheck/main.go @@ -14,18 +14,38 @@ import ( func main() { os.WriteFile("health.txt", []byte("starting"), 0644) - if len(os.Args) != 2 { - fmt.Fprintf(os.Stderr, "%s must have 1 argument, not %d arguments\n", os.Args[0], len(os.Args)) + if len(os.Args) > 3 || len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, "%s must have 1 or 2 arguments, not %d arguments\n", os.Args[0], len(os.Args)) os.Exit(1) } - delay, err := time.ParseDuration(os.Args[1]) + runtimeDelay, err := time.ParseDuration(os.Args[1]) if err != nil { - fmt.Fprintf(os.Stderr, "Cannot parse delay duration: %s\n", err) + fmt.Fprintf(os.Stderr, "Cannot parse runtime duration: %s\n", err) os.Exit(1) } - if delay.Microseconds() <= 0 { + if runtimeDelay.Microseconds() <= 0 { fmt.Fprintf(os.Stderr, "Delay must be positive!\n") os.Exit(1) } - time.Sleep(delay) + var healthyDelay time.Duration + if len(os.Args) == 3 { + healthyDelay, err = time.ParseDuration(os.Args[2]) + if err != nil { + fmt.Fprintf(os.Stderr, "Cannot parse healthy delay: %s\n", err) + os.Exit(1) + } + if healthyDelay.Microseconds() <= 0 { + fmt.Fprintf(os.Stderr, "Healthy delay must not be negative!\n") + os.Exit(1) + } + } + if healthyDelay.Microseconds() > 0 { + fmt.Fprintf(os.Stderr, "Waiting %s until setting to healthy...\n", healthyDelay) + time.Sleep(healthyDelay) + os.WriteFile("/health.txt", []byte("healthy"), 0644) + fmt.Fprintf(os.Stderr, "Set state to healthy.\n") + } + fmt.Fprintf(os.Stderr, "Waiting %s until quitting...\n", runtimeDelay) + time.Sleep(runtimeDelay) + fmt.Fprintf(os.Stderr, "Goodbye.\n") } diff --git a/tests/images/healthcheck/make-healthy.go b/tests/images/healthcheck/make-healthy.go index b046f21ee..fdece9e1a 100644 --- a/tests/images/healthcheck/make-healthy.go +++ b/tests/images/healthcheck/make-healthy.go @@ -31,5 +31,5 @@ func main() { } else { state = []byte("unhealthy") } - os.WriteFile("health.txt", state, 0644) + os.WriteFile("/health.txt", state, 0644) } diff --git a/tests/integration/targets/setup_docker/vars/main.yml b/tests/integration/targets/setup_docker/vars/main.yml index 494dcc2fe..61d74293e 100644 --- a/tests/integration/targets/setup_docker/vars/main.yml +++ b/tests/integration/targets/setup_docker/vars/main.yml @@ -19,3 +19,4 @@ docker_test_image_registry_nginx: quay.io/ansible/docker-test-containers:nginx-a docker_test_image_registry: registry:2.6.1 docker_test_image_simple_1: ghcr.io/ansible-collections/simple-1:tag docker_test_image_simple_2: ghcr.io/ansible-collections/simple-2:tag +docker_test_image_healthcheck: ghcr.io/ansible-collections/healthcheck:check