diff --git a/pkg/compose/dependency_wait_test.go b/pkg/compose/dependency_wait_test.go index 5c64ac5d8..65136cd7d 100644 --- a/pkg/compose/dependency_wait_test.go +++ b/pkg/compose/dependency_wait_test.go @@ -795,3 +795,235 @@ func TestCheckDependencyDoneContext(t *testing.T) { assert.Assert(t, !done) }) } + +// TestShouldWaitForDependency pins which depends_on entries are worth a wait, +// the gate the plan's wait nodes and the waits outside a plan (restart, run, +// --wait) share: only a condition other than service_started, on a declared +// service that really runs containers of its own. +func TestShouldWaitForDependency(t *testing.T) { + project := &types.Project{ + Name: "myproject", + Services: types.Services{ + "db": {Name: "db"}, + "idle": {Name: "idle", Scale: intPtr(0)}, + "provider": {Name: "provider", Provider: &types.ServiceProviderConfig{Type: "test"}}, + }, + DisabledServices: types.Services{"cache": {Name: "cache"}}, + } + healthy := types.ServiceDependency{Condition: types.ServiceConditionHealthy, Required: true} + + tests := []struct { + name string + dependency string + config types.ServiceDependency + wait bool + err string + }{ + {name: "a started condition is the dependency order itself", dependency: "db", config: types.ServiceDependency{Condition: types.ServiceConditionStarted, Required: true}}, + {name: "a healthy dependency is waited for", dependency: "db", config: healthy, wait: true}, + {name: "a service completing is waited for", dependency: "db", config: types.ServiceDependency{Condition: types.ServiceConditionCompletedSuccessfully, Required: true}, wait: true}, + {name: "a dependency with no container (scale 0) is not", dependency: "idle", config: healthy}, + {name: "a provider service is not", dependency: "provider", config: healthy}, + {name: "a disabled service (--no-deps) is not", dependency: "cache", config: healthy}, + {name: "an undeclared service is an error", dependency: "ghost", config: healthy, err: "ghost"}, + {name: "an undeclared service with a started condition is not even looked up", dependency: "ghost", config: types.ServiceDependency{Condition: types.ServiceConditionStarted}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + wait, err := shouldWaitForDependency(tt.dependency, tt.config, project) + if tt.err != "" { + assert.ErrorContains(t, err, tt.err) + return + } + assert.NilError(t, err) + assert.Equal(t, wait, tt.wait) + }) + } +} + +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, +// unhealthy, no health state at all, a status the daemon should not report -- +// is a definitive failure naming the container. +func TestIsServiceHealthyStatuses(t *testing.T) { + check := &container.HealthConfig{Test: []string{"CMD", "true"}} + tests := []struct { + name string + inspect client.ContainerInspectResult + 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"`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + svc, apiClient, _ := newStartPhaseTestService(t) + apiClient.EXPECT().ContainerInspect(gomock.Any(), "db-id", gomock.Any()).Return(tt.inspect, nil) + + healthy, err := svc.isServiceHealthy(t.Context(), Containers{{ID: "db-id"}}, false) + + if tt.err != "" { + assert.ErrorContains(t, err, tt.err) + } else { + assert.NilError(t, err) + } + assert.Equal(t, healthy, tt.healthy) + }) + } + + 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) + apiClient.EXPECT().ContainerInspect(gomock.Any(), "db-2", gomock.Any()). + Return(inspectOf(container.StateRunning, &container.Health{Status: container.Starting}, check, 0), nil) + + healthy, err := svc.isServiceHealthy(t.Context(), Containers{{ID: "db-1"}, {ID: "db-2"}}, false) + assert.NilError(t, err) + assert.Assert(t, !healthy) + }) +} + +// TestCheckDependencyCompleted pins service_completed_successfully: a +// dependency that exited with 0 satisfies it, one still running does not yet, +// one that exited with another code fails a required dependency -- naming the +// service and the code, and marking its rows in error -- and is skipped, with a warning, +// when the dependency is optional. +func TestCheckDependencyCompleted(t *testing.T) { + waitingFor := Containers{{ID: "db-id", Names: []string{"/test-db-1"}, Labels: map[string]string{api.ServiceLabel: "db"}}} + const row = "Container test-db-1" + + tests := []struct { + name string + inspect client.ContainerInspectResult + required bool + done bool + err string + events []string + }{ + { + name: "exited with 0", inspect: inspectOf(container.StateExited, nil, nil, 0), required: true, + done: true, events: []string{api.StatusExited}, + }, + { + name: "still running", inspect: inspectOf(container.StateRunning, nil, nil, 0), required: true, + }, + { + name: "required, exited with an error code", inspect: inspectOf(container.StateExited, nil, nil, 3), 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), + done: true, + events: []string{`Skipped: optional dependency "db" didn't complete successfully: exit 3`}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + svc, apiClient, recorder := newStartPhaseTestService(t) + apiClient.EXPECT().ContainerInspect(gomock.Any(), "db-id", gomock.Any()).Return(tt.inspect, nil) + + done, err := svc.checkDependencyCompleted(t.Context(), "db", types.ServiceDependency{Required: tt.required}, waitingFor) + + if tt.err != "" { + assert.ErrorContains(t, err, tt.err) + } else { + assert.NilError(t, err) + } + assert.Equal(t, done, tt.done) + assert.DeepEqual(t, recorder.byID[row], tt.events) + }) + } + + t.Run("a dry run never sees a dependency exit: it is taken as completed", func(t *testing.T) { + svc, _, _ := newStartPhaseTestService(t) + svc.dryRun = true + + done, err := svc.checkDependencyCompleted(t.Context(), "db", types.ServiceDependency{Required: true}, waitingFor) + assert.NilError(t, err) + assert.Assert(t, done) + }) +} + +// TestWaitDependenciesReportsTheWait pins what a wait outside a plan (restart, +// run, the --wait verification) shows of a dependency's containers: Waiting +// first, then Healthy once the condition holds -- the progression a plan's wait +// node gives the same rows -- and only the dependency's own containers, never +// a one-off or a hook runner of the service. +func TestWaitDependenciesReportsTheWait(t *testing.T) { + svc, apiClient, recorder := newStartPhaseTestService(t) + project := &types.Project{Name: "test", Services: types.Services{"db": {Name: "db"}}} + labels := func(oneOff string) map[string]string { + return map[string]string{api.ServiceLabel: "db", api.OneoffLabel: oneOff} + } + 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")}, + } + 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) + + 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") +} + +// TestWaitDependencyEndOfContextWinsOverATick pins that a wait whose context +// ended is never run through one more check because a tick happened to be +// ready as well: select picks at random between the two, so the wait looks at +// its context after it, and the end of the context wins. Here a check is in +// flight while the context ends and a tick is pending; the scenario is run +// repeatedly, as a wait that let the tick win would only miss it one time in +// two. The one check allowed is the first, on entry. +func TestWaitDependencyEndOfContextWinsOverATick(t *testing.T) { + waitingFor := Containers{{ID: "db-id", Names: []string{"/test-db-1"}, Labels: map[string]string{api.ServiceLabel: "db"}}} + dependency := types.ServiceDependency{Condition: types.ServiceConditionHealthy, Required: true} + + for range 40 { + synctest.Test(t, func(t *testing.T) { + svc, apiClient, _ := newStartPhaseTestService(t) + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + entered, release := make(chan struct{}), make(chan struct{}) + apiClient.EXPECT().ContainerInspect(gomock.Any(), "db-id", gomock.Any()).Times(1). + 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 + }) + result := make(chan error, 1) + go func() { + reached, err := svc.waitDependencyReached(ctx, ctx, "web", "db", dependency, waitingFor) + if err == nil && reached { + err = errors.New("the wait reported the dependency as reached") + } + result <- err + }() + + <-entered + time.Sleep(time.Second) // the ticker fires while the check is in flight + cancel() + close(release) + + assert.NilError(t, <-result) + }) + } +} diff --git a/pkg/compose/reconcile_start_test.go b/pkg/compose/reconcile_start_test.go index 768f44210..6d087d353 100644 --- a/pkg/compose/reconcile_start_test.go +++ b/pkg/compose/reconcile_start_test.go @@ -1307,3 +1307,84 @@ func TestPlanStart_DefaultScopeIsInert(t *testing.T) { } assert.Equal(t, plan.String(), "[] -> #1 service:app:1, CreateContainer, no existing container\n") } + +// A wait node is ordered after the start of the dependency it waits for: the +// condition is only worth checking once the dependency's container has been +// started. Here a single dependent uses the wait, so nothing but the node's +// own prerequisites can order it (a second dependent would add them to the +// shared node). +func TestPlanStart_WaitFollowsTheDependencyStart(t *testing.T) { + project := &types.Project{ + Name: "myproject", + Services: types.Services{ + "db": {Name: "db"}, + "web": serviceWithDeps("web", types.DependsOnConfig{ + "db": {Condition: types.ServiceConditionHealthy, Required: true}, + }), + }, + } + + plan, err := reconcile(t.Context(), project, emptyObserved(), startScopeOptions(ScopeCreateStart), noPrompt) + assert.NilError(t, err) + + assert.Equal(t, plan.String(), strings.TrimSpace(` +[] -> #1 service:db:1, CreateContainer, no existing container +[1] -> #2 service:web:1, CreateContainer, no existing container +[1] -> #3 service:db:1, StartContainer, start [start:db:1] {start} +[3] -> #4 wait:db:service_healthy, WaitCondition, depends_on condition {start} +[2,4] -> #5 service:web:1, StartContainer, start [start:web:1] {start} +`)+"\n") +} + +// A project whose services all have scale: 0 has no container to start, and +// that is not an error: `compose start` has nothing to do. +func TestPlanStart_ScaleZeroServiceWithoutAnyContainerIsNotAnError(t *testing.T) { + zero := 0 + project := &types.Project{ + Name: "myproject", + Services: types.Services{"app": {Name: "app", Scale: &zero}}, + } + + plan, err := reconcile(t.Context(), project, emptyObserved(), startScopeOptions(ScopeStart), noPrompt) + assert.NilError(t, err) + assert.Equal(t, len(plan.Nodes), 0, "nothing to plan:\n%s", plan) +} + +// The Start phase covers every service of the project the plan is built for, +// whichever services the create options select: Services only decides the +// services whose containers may be recreated (see api.ScaleOptions.Services), +// so scaling a service starts, with the replicas it creates, the stopped +// containers of the project's other services. The CLI hands the plan a +// project narrowed to the services it names and their dependencies, which is +// why a user doesn't meet it. Starting only what was selected would be a +// decision of its own (#14081, "scale start scope"), not the side effect of +// another change: this pins the behavior as it stands. +func TestPlanStart_StartPhaseCoversEveryServiceOfTheProject(t *testing.T) { + two := 2 + db := types.ServiceConfig{Name: "db", Scale: &two} + web := serviceWithDeps("web", types.DependsOnConfig{"db": {Condition: types.ServiceConditionStarted, Required: true}}) + other := types.ServiceConfig{Name: "other"} + project := &types.Project{Name: "myproject", Services: types.Services{"db": db, "web": web, "other": other}} + observed := emptyObserved() + for _, service := range []types.ServiceConfig{db, web, other} { + hash, err := serviceHashWithResolvedRefs(service, nil) + assert.NilError(t, err) + state := container.StateExited + if service.Name == "db" { + state = container.StateRunning + } + observed.Containers[service.Name] = []ObservedContainer{observedServiceContainer(service.Name, 1, state, hash)} + } + options := startScopeOptions(ScopeCreateStart) + options.Services = []string{"db"} + + plan, err := reconcile(t.Context(), project, observed, options, noPrompt) + assert.NilError(t, err) + + assert.Equal(t, plan.String(), strings.TrimSpace(` +[] -> #1 service:db:2, CreateContainer, no existing container +[1] -> #2 service:db:2, StartContainer, start [start:db:2] {start} +[] -> #3 service:other:1, StartContainer, start [start:other:1] {start} +[2] -> #4 service:web:1, StartContainer, start [start:web:1] {start} +`)+"\n") +}