Skip to content

Commit

Permalink
Improve health check image.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Jul 7, 2024
1 parent ff412f4 commit 216b93a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tests/images/healthcheck/is-healthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 26 additions & 6 deletions tests/images/healthcheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion tests/images/healthcheck/make-healthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func main() {
} else {
state = []byte("unhealthy")
}
os.WriteFile("health.txt", state, 0644)
os.WriteFile("/health.txt", state, 0644)
}
1 change: 1 addition & 0 deletions tests/integration/targets/setup_docker/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 216b93a

Please sign in to comment.