diff --git a/pkg/compose/dependency_wait_test.go b/pkg/compose/dependency_wait_test.go index 65136cd7d..267c100b4 100644 --- a/pkg/compose/dependency_wait_test.go +++ b/pkg/compose/dependency_wait_test.go @@ -841,15 +841,6 @@ func TestShouldWaitForDependency(t *testing.T) { } } -func inspectOf(status container.ContainerState, health *container.Health, healthcheck *container.HealthConfig, exitCode int) client.ContainerInspectResult { - return client.ContainerInspectResult{Container: container.InspectResponse{ - ID: "db-id", - Name: "/test-db-1", - Config: &container.Config{Healthcheck: healthcheck}, - State: &container.State{Status: status, Health: health, ExitCode: exitCode}, - }} -} - // TestIsServiceHealthyStatuses pins how each state of a container with a // healthcheck reads for service_healthy: healthy is the only way to satisfy // the condition, starting means keep polling, and anything else -- exited, @@ -863,12 +854,16 @@ func TestIsServiceHealthyStatuses(t *testing.T) { healthy bool err string }{ - {name: "healthy", inspect: inspectOf(container.StateRunning, &container.Health{Status: container.Healthy}, check, 0), healthy: true}, - {name: "still starting", inspect: inspectOf(container.StateRunning, &container.Health{Status: container.Starting}, check, 0)}, - {name: "unhealthy", inspect: inspectOf(container.StateRunning, &container.Health{Status: container.Unhealthy}, check, 0), err: "container test-db-1 is unhealthy"}, - {name: "exited", inspect: inspectOf(container.StateExited, nil, check, 3), err: "container test-db-1 exited (3)"}, - {name: "no health state", inspect: inspectOf(container.StateRunning, nil, check, 0), err: "container test-db-1 has no healthcheck configured"}, - {name: "unexpected status", inspect: inspectOf(container.StateRunning, &container.Health{Status: "weird"}, check, 0), err: `container test-db-1 had unexpected health status "weird"`}, + {name: "healthy", inspect: inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, &container.Health{Status: container.Healthy}, check), healthy: true}, + {name: "still starting", inspect: inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, &container.Health{Status: container.Starting}, check)}, + {name: "unhealthy", inspect: inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, &container.Health{Status: container.Unhealthy}, check), err: "container test-db-1 is unhealthy"}, + {name: "exited", inspect: inspectResultOf("db-id", "test-db-1", container.StateExited, 3, nil, check), err: "container test-db-1 exited (3)"}, + {name: "no health state", inspect: inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, nil, check), err: "container test-db-1 has no healthcheck configured"}, + { + name: "unexpected status", + inspect: inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, &container.Health{Status: "weird"}, check), + err: `container test-db-1 had unexpected health status "weird"`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -889,9 +884,9 @@ func TestIsServiceHealthyStatuses(t *testing.T) { t.Run("every container of the service has to be healthy", func(t *testing.T) { svc, apiClient, _ := newStartPhaseTestService(t) apiClient.EXPECT().ContainerInspect(gomock.Any(), "db-1", gomock.Any()). - Return(inspectOf(container.StateRunning, &container.Health{Status: container.Healthy}, check, 0), nil) + Return(inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, &container.Health{Status: container.Healthy}, check), nil) apiClient.EXPECT().ContainerInspect(gomock.Any(), "db-2", gomock.Any()). - Return(inspectOf(container.StateRunning, &container.Health{Status: container.Starting}, check, 0), nil) + Return(inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, &container.Health{Status: container.Starting}, check), nil) healthy, err := svc.isServiceHealthy(t.Context(), Containers{{ID: "db-1"}, {ID: "db-2"}}, false) assert.NilError(t, err) @@ -917,19 +912,19 @@ func TestCheckDependencyCompleted(t *testing.T) { events []string }{ { - name: "exited with 0", inspect: inspectOf(container.StateExited, nil, nil, 0), required: true, + name: "exited with 0", inspect: inspectResultOf("db-id", "test-db-1", container.StateExited, 0, nil, nil), required: true, done: true, events: []string{api.StatusExited}, }, { - name: "still running", inspect: inspectOf(container.StateRunning, nil, nil, 0), required: true, + name: "still running", inspect: inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, nil, nil), required: true, }, { - name: "required, exited with an error code", inspect: inspectOf(container.StateExited, nil, nil, 3), required: true, + name: "required, exited with an error code", inspect: inspectResultOf("db-id", "test-db-1", container.StateExited, 3, nil, nil), required: true, err: `service "db" didn't complete successfully: exit 3`, events: []string{api.StatusError}, }, { - name: "optional, exited with an error code", inspect: inspectOf(container.StateExited, nil, nil, 3), + name: "optional, exited with an error code", inspect: inspectResultOf("db-id", "test-db-1", container.StateExited, 3, nil, nil), done: true, events: []string{`Skipped: optional dependency "db" didn't complete successfully: exit 3`}, }, @@ -975,15 +970,17 @@ func TestWaitDependenciesReportsTheWait(t *testing.T) { containers := Containers{ {ID: "db-id", Names: []string{"/test-db-1"}, Labels: labels("False")}, {ID: "run-id", Names: []string{"/test-db-run-1"}, Labels: labels("True")}, + {ID: "hook-id", Names: []string{"/test-db-hook-1"}, Labels: map[string]string{api.ServiceLabel: "db", api.OneoffLabel: "False", api.HookLabel: "post_start"}}, } apiClient.EXPECT().ContainerInspect(gomock.Any(), "db-id", gomock.Any()). - Return(inspectOf(container.StateRunning, &container.Health{Status: container.Healthy}, &container.HealthConfig{Test: []string{"CMD", "true"}}, 0), nil) + Return(inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, &container.Health{Status: container.Healthy}, &container.HealthConfig{Test: []string{"CMD", "true"}}), nil) dependencies := types.DependsOnConfig{"db": {Condition: types.ServiceConditionHealthy, Required: true}} assert.NilError(t, svc.waitDependencies(t.Context(), project, "web", dependencies, containers)) assert.DeepEqual(t, recorder.byID["Container test-db-1"], []string{api.StatusWaiting, api.StatusHealthy}) assert.Equal(t, len(recorder.byID["Container test-db-run-1"]), 0, "a one-off container is not waited for") + assert.Equal(t, len(recorder.byID["Container test-db-hook-1"]), 0, "a hook runner is not waited for") } // TestWaitDependencyEndOfContextWinsOverATick pins that a wait whose context @@ -1007,7 +1004,7 @@ func TestWaitDependencyEndOfContextWinsOverATick(t *testing.T) { DoAndReturn(func(context.Context, string, client.ContainerInspectOptions) (client.ContainerInspectResult, error) { close(entered) <-release - return inspectOf(container.StateRunning, &container.Health{Status: container.Starting}, &container.HealthConfig{Test: []string{"CMD", "true"}}, 0), nil + return inspectResultOf("db-id", "test-db-1", container.StateRunning, 0, &container.Health{Status: container.Starting}, &container.HealthConfig{Test: []string{"CMD", "true"}}), nil }) result := make(chan error, 1) go func() { diff --git a/pkg/compose/executor_start_test.go b/pkg/compose/executor_start_test.go index 0d905f110..a8d9a6064 100644 --- a/pkg/compose/executor_start_test.go +++ b/pkg/compose/executor_start_test.go @@ -2300,12 +2300,10 @@ func TestExecutePlanBoundedConcurrencyNeverBlocksAPrerequisite(t *testing.T) { svc, apiClient, _ := newStartPhaseTestService(t) svc.maxConcurrency = 1 - ctrs := map[string]container.Summary{} plan := &Plan{} nodes := map[string]*PlanNode{} for _, name := range []string{"first", "second", "third"} { ctr := container.Summary{ID: name + "-id", Names: []string{"/test-" + name + "-1"}} - ctrs[name] = ctr nodes[name] = plan.addNode(Operation{Type: OpStartContainer, ResourceID: "service:" + name + ":1", Cause: "start", Container: &ctr}, "") } // the dependencies point at nodes created later diff --git a/pkg/compose/restart_test.go b/pkg/compose/restart_test.go index e2d69daca..cc3dccdbe 100644 --- a/pkg/compose/restart_test.go +++ b/pkg/compose/restart_test.go @@ -231,7 +231,7 @@ func TestRestart_WaitsForADependencyBeforeRestartingItsDependent(t *testing.T) { dbRestart := apiClient.EXPECT().ContainerRestart(gomock.Any(), dbCtr.ID, gomock.Any()). Return(client.ContainerRestartResult{}, nil) dbHealthy := apiClient.EXPECT().ContainerInspect(gomock.Any(), dbCtr.ID, gomock.Any()). - Return(inspectResultOf(dbCtr.ID, "prj-db-1", container.StateRunning, 0, &container.Health{Status: container.Healthy}), nil). + Return(inspectResultOf(dbCtr.ID, "prj-db-1", container.StateRunning, 0, &container.Health{Status: container.Healthy}, nil), nil). After(dbRestart) apiClient.EXPECT().ContainerRestart(gomock.Any(), webCtr.ID, gomock.Any()). Return(client.ContainerRestartResult{}, nil).After(dbHealthy) diff --git a/pkg/compose/start_test.go b/pkg/compose/start_test.go index 79e2c98a6..090b17590 100644 --- a/pkg/compose/start_test.go +++ b/pkg/compose/start_test.go @@ -318,9 +318,11 @@ func TestGetDependencyCondition(t *testing.T) { assert.Equal(t, getDependencyCondition(web, project), ServiceConditionRunningOrHealthy) } -func inspectResultOf(id, name string, status container.ContainerState, exitCode int, health *container.Health) client.ContainerInspectResult { - config := &container.Config{} - if health != nil { +// inspectResultOf builds the inspection of a container. Its healthcheck is the +// one given, or a trivial one when it has a health state and none is given. +func inspectResultOf(id, name string, status container.ContainerState, exitCode int, health *container.Health, healthcheck *container.HealthConfig) client.ContainerInspectResult { + config := &container.Config{Healthcheck: healthcheck} + if health != nil && healthcheck == nil { config.Healthcheck = &container.HealthConfig{Test: []string{"CMD", "true"}} } return client.ContainerInspectResult{Container: container.InspectResponse{ @@ -347,7 +349,7 @@ func TestStart_WaitVerifiesTheApplicationIsReady(t *testing.T) { apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()). Return(client.ContainerListResult{Items: []container.Summary{webCtr}}, nil).AnyTimes() apiClient.EXPECT().ContainerInspect(gomock.Any(), webCtr.ID, gomock.Any()).AnyTimes(). - Return(inspectResultOf(webCtr.ID, "prj-web-1", container.StateRunning, 0, &container.Health{Status: health}), nil) + Return(inspectResultOf(webCtr.ID, "prj-web-1", container.StateRunning, 0, &container.Health{Status: health}, nil), nil) return svc } @@ -406,9 +408,9 @@ func TestStart_WaitSatisfiesAOneShotServiceByItsCompletion(t *testing.T) { Return(client.ContainerListResult{Items: []container.Summary{initCtr, appCtr}}, nil) apiClient.EXPECT().ContainerStart(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(client.ContainerStartResult{}, nil) apiClient.EXPECT().ContainerInspect(gomock.Any(), initCtr.ID, gomock.Any()).AnyTimes(). - Return(inspectResultOf(initCtr.ID, "prj-init-1", container.StateExited, tt.exitCode, nil), nil) + Return(inspectResultOf(initCtr.ID, "prj-init-1", container.StateExited, tt.exitCode, nil, nil), nil) apiClient.EXPECT().ContainerInspect(gomock.Any(), appCtr.ID, gomock.Any()).AnyTimes(). - Return(inspectResultOf(appCtr.ID, "prj-app-1", container.StateRunning, 0, nil), nil) + Return(inspectResultOf(appCtr.ID, "prj-app-1", container.StateRunning, 0, nil, nil), nil) err := svc.start(t.Context(), "prj", api.StartOptions{Project: project, Wait: true, WaitTimeout: 5 * time.Second})