health_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/client"
  10. "github.com/docker/docker/integration/internal/container"
  11. "gotest.tools/v3/assert"
  12. "gotest.tools/v3/poll"
  13. "gotest.tools/v3/skip"
  14. )
  15. // TestHealthCheckWorkdir verifies that health-checks inherit the containers'
  16. // working-dir.
  17. func TestHealthCheckWorkdir(t *testing.T) {
  18. skip.If(t, testEnv.OSType == "windows", "FIXME")
  19. defer setupTest(t)()
  20. ctx := context.Background()
  21. client := testEnv.APIClient()
  22. cID := container.Run(ctx, t, client, container.WithTty(true), container.WithWorkingDir("/foo"), func(c *container.TestContainerConfig) {
  23. c.Config.Healthcheck = &containertypes.HealthConfig{
  24. Test: []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
  25. Interval: 50 * time.Millisecond,
  26. Retries: 3,
  27. }
  28. })
  29. poll.WaitOn(t, pollForHealthStatus(ctx, client, cID, types.Healthy), poll.WithDelay(100*time.Millisecond))
  30. }
  31. // GitHub #37263
  32. // Do not stop healthchecks just because we sent a signal to the container
  33. func TestHealthKillContainer(t *testing.T) {
  34. skip.If(t, testEnv.OSType == "windows", "Windows only supports SIGKILL and SIGTERM? See https://github.com/moby/moby/issues/39574")
  35. defer setupTest(t)()
  36. ctx := context.Background()
  37. client := testEnv.APIClient()
  38. id := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
  39. cmd := `
  40. # Set the initial HEALTH value so the healthcheck passes
  41. HEALTH="1"
  42. echo $HEALTH > /health
  43. # Any time doHealth is run we flip the value
  44. # This lets us use kill signals to determine when healtchecks have run.
  45. doHealth() {
  46. case "$HEALTH" in
  47. "0")
  48. HEALTH="1"
  49. ;;
  50. "1")
  51. HEALTH="0"
  52. ;;
  53. esac
  54. echo $HEALTH > /health
  55. }
  56. trap 'doHealth' USR1
  57. while true; do sleep 1; done
  58. `
  59. c.Config.Cmd = []string{"/bin/sh", "-c", cmd}
  60. c.Config.Healthcheck = &containertypes.HealthConfig{
  61. Test: []string{"CMD-SHELL", `[ "$(cat /health)" = "1" ]`},
  62. Interval: time.Second,
  63. Retries: 5,
  64. }
  65. })
  66. ctxPoll, cancel := context.WithTimeout(ctx, 30*time.Second)
  67. defer cancel()
  68. poll.WaitOn(t, pollForHealthStatus(ctxPoll, client, id, "healthy"), poll.WithDelay(100*time.Millisecond))
  69. err := client.ContainerKill(ctx, id, "SIGUSR1")
  70. assert.NilError(t, err)
  71. ctxPoll, cancel = context.WithTimeout(ctx, 30*time.Second)
  72. defer cancel()
  73. poll.WaitOn(t, pollForHealthStatus(ctxPoll, client, id, "unhealthy"), poll.WithDelay(100*time.Millisecond))
  74. err = client.ContainerKill(ctx, id, "SIGUSR1")
  75. assert.NilError(t, err)
  76. ctxPoll, cancel = context.WithTimeout(ctx, 30*time.Second)
  77. defer cancel()
  78. poll.WaitOn(t, pollForHealthStatus(ctxPoll, client, id, "healthy"), poll.WithDelay(100*time.Millisecond))
  79. }
  80. // TestHealthCheckProcessKilled verifies that health-checks exec get killed on time-out.
  81. func TestHealthCheckProcessKilled(t *testing.T) {
  82. defer setupTest(t)()
  83. ctx := context.Background()
  84. apiClient := testEnv.APIClient()
  85. cID := container.Run(ctx, t, apiClient, func(c *container.TestContainerConfig) {
  86. c.Config.Healthcheck = &containertypes.HealthConfig{
  87. Test: []string{"CMD", "sh", "-c", `echo "logs logs logs"; sleep 60`},
  88. Interval: 100 * time.Millisecond,
  89. Timeout: 50 * time.Millisecond,
  90. Retries: 1,
  91. }
  92. })
  93. poll.WaitOn(t, pollForHealthCheckLog(ctx, apiClient, cID, "Health check exceeded timeout (50ms): logs logs logs\n"))
  94. }
  95. func pollForHealthCheckLog(ctx context.Context, client client.APIClient, containerID string, expected string) func(log poll.LogT) poll.Result {
  96. return func(log poll.LogT) poll.Result {
  97. inspect, err := client.ContainerInspect(ctx, containerID)
  98. if err != nil {
  99. return poll.Error(err)
  100. }
  101. healthChecksTotal := len(inspect.State.Health.Log)
  102. if healthChecksTotal > 0 {
  103. output := inspect.State.Health.Log[healthChecksTotal-1].Output
  104. if output == expected {
  105. return poll.Success()
  106. }
  107. return poll.Error(fmt.Errorf("expected %q, got %q", expected, output))
  108. }
  109. return poll.Continue("waiting for container healthcheck logs")
  110. }
  111. }
  112. func pollForHealthStatus(ctx context.Context, client client.APIClient, containerID string, healthStatus string) func(log poll.LogT) poll.Result {
  113. return func(log poll.LogT) poll.Result {
  114. inspect, err := client.ContainerInspect(ctx, containerID)
  115. switch {
  116. case err != nil:
  117. return poll.Error(err)
  118. case inspect.State.Health.Status == healthStatus:
  119. return poll.Success()
  120. default:
  121. return poll.Continue("waiting for container to become %s", healthStatus)
  122. }
  123. }
  124. }