Browse Source

Don't wait forever for unhealthy dependencies

The previous code would wait for dependencies to become healthy forever,
even if they'd become unhealthy in the meantime. I can't find an issue
report for this bug, but it was described in a comment on the PR that
introduced the `--wait` flag [0].

[0]: https://github.com/docker/compose/pull/8777#issuecomment-965643839

Signed-off-by: Nikhil Benesch <[email protected]>
Nikhil Benesch 3 years ago
parent
commit
c5cdce0b60
3 changed files with 55 additions and 1 deletions
  1. 8 1
      pkg/compose/convergence.go
  2. 14 0
      pkg/e2e/fixtures/start-fail/compose.yaml
  3. 33 0
      pkg/e2e/start_fail_test.go

+ 8 - 1
pkg/compose/convergence.go

@@ -596,8 +596,15 @@ func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Pr
 		if container.State == nil || container.State.Health == nil {
 			return false, fmt.Errorf("container for service %q has no healthcheck configured", service)
 		}
-		if container.State.Health.Status != moby.Healthy {
+		switch container.State.Health.Status {
+		case moby.Healthy:
+			// Continue by checking the next container.
+		case moby.Unhealthy:
+			return false, fmt.Errorf("container for service %q is unhealthy", service)
+		case moby.Starting:
 			return false, nil
+		default:
+			return false, fmt.Errorf("container for service %q had unexpected health status %q", service, container.State.Health.Status)
 		}
 	}
 	return true, nil

+ 14 - 0
pkg/e2e/fixtures/start-fail/compose.yaml

@@ -0,0 +1,14 @@
+services:
+  fail:
+    image: alpine
+    command: sleep infinity
+    healthcheck:
+      test: "false"
+      interval: 1s
+      retries: 3
+  depends:
+    image: alpine
+    command: sleep infinity
+    depends_on:
+      fail:
+        condition: service_healthy

+ 33 - 0
pkg/e2e/start_fail_test.go

@@ -0,0 +1,33 @@
+/*
+   Copyright 2020 Docker Compose CLI authors
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package e2e
+
+import (
+	"testing"
+
+	"gotest.tools/v3/icmd"
+)
+
+func TestStartFail(t *testing.T) {
+	c := NewParallelE2eCLI(t, binDir)
+	const projectName = "e2e-start-fail"
+
+	res := c.RunDockerOrExitError("compose", "-f", "fixtures/start-fail/compose.yaml", "--project-name", projectName, "up", "-d")
+	res.Assert(t, icmd.Expected{ExitCode: 1, Err: `container for service "fail" is unhealthy`})
+
+	c.RunDockerComposeCmd("--project-name", projectName, "down")
+}