From 2df698025ca681199d5e4333efca94ca308a3109 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 14 Oct 2023 18:43:34 +0200 Subject: [PATCH] HealthCheck: add validation for minimum accepted start-interval This is a follow-up to 2216d3ca8d65c5b0c85e297a3eb351a05db3a4d2, which implemented the StartInterval for health-checks, but did not add validation for the minimum accepted interval; > The time to wait between checks in nanoseconds during the start period. > It should be 0 or at least 1000000 (1 ms). 0 means inherit. This patch adds validation for the minimum accepted interval (1ms). Signed-off-by: Sebastiaan van Stijn --- daemon/container.go | 3 +++ integration/container/create_test.go | 29 +++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/daemon/container.go b/daemon/container.go index e9b26cccb5..d2494481b6 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -333,6 +333,9 @@ func validateHealthCheck(healthConfig *containertypes.HealthConfig) error { if healthConfig.StartPeriod != 0 && healthConfig.StartPeriod < containertypes.MinimumDuration { return errors.Errorf("StartPeriod in Healthcheck cannot be less than %s", containertypes.MinimumDuration) } + if healthConfig.StartInterval != 0 && healthConfig.StartInterval < containertypes.MinimumDuration { + return errors.Errorf("StartInterval in Healthcheck cannot be less than %s", containertypes.MinimumDuration) + } return nil } diff --git a/integration/container/create_test.go b/integration/container/create_test.go index 7dd2b4993e..f30f9cdf07 100644 --- a/integration/container/create_test.go +++ b/integration/container/create_test.go @@ -343,12 +343,13 @@ func TestCreateWithInvalidHealthcheckParams(t *testing.T) { apiClient := testEnv.APIClient() testCases := []struct { - doc string - interval time.Duration - timeout time.Duration - retries int - startPeriod time.Duration - expectedErr string + doc string + interval time.Duration + timeout time.Duration + retries int + startPeriod time.Duration + startInterval time.Duration + expectedErr string }{ { doc: "test invalid Interval in Healthcheck: less than 0s", @@ -386,6 +387,15 @@ func TestCreateWithInvalidHealthcheckParams(t *testing.T) { startPeriod: 100 * time.Microsecond, expectedErr: fmt.Sprintf("StartPeriod in Healthcheck cannot be less than %s", container.MinimumDuration), }, + { + doc: "test invalid StartInterval in Healthcheck: not 0 and less than 1ms", + interval: time.Second, + timeout: time.Second, + retries: 1000, + startPeriod: time.Second, + startInterval: 100 * time.Microsecond, + expectedErr: fmt.Sprintf("StartInterval in Healthcheck cannot be less than %s", container.MinimumDuration), + }, } for _, tc := range testCases { @@ -396,9 +406,10 @@ func TestCreateWithInvalidHealthcheckParams(t *testing.T) { cfg := container.Config{ Image: "busybox", Healthcheck: &container.HealthConfig{ - Interval: tc.interval, - Timeout: tc.timeout, - Retries: tc.retries, + Interval: tc.interval, + Timeout: tc.timeout, + Retries: tc.retries, + StartInterval: tc.startInterval, }, } if tc.startPeriod != 0 {