test(start): pin how start() wires its plan and the provider start path
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (relay-lint) (push) Waiting to run
ci / validate (relay-test) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / relay-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / ci-ok (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / relay-image (push) Waiting to run
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run

Cover what the removed imperative tests used to: a no-op start is silent and
only lists containers, --wait-timeout bounds the dependency waits, secrets and
configs are injected before ContainerStart, a failing post_start fails the
start, pre_start is skipped on a stale relay, deploy.replicas: 0 starts
nothing, and the project-wide no-container check. On the provider path: the
restart is a chain end, a relay gets no post_start, pre_start is gated on
running replicas and runs once, before the chain, after the dependencies.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2026-10-16 10:23:47 +02:00 • committed by Guillaume Lours
parent f1ea46f4b8
commit 280f6a454d
3 changed files with 375 additions and 0 deletions

View file

@ -24,6 +24,7 @@ import (
"fmt"
"net"
"runtime"
"slices"
"sync"
"sync/atomic"
"testing"
@ -856,6 +857,93 @@ func TestExecRunPostStart_RunsHookAndStreamsToListener(t *testing.T) {
assert.DeepEqual(t, lines, []string{"post-start ok"})
}
// TestExecutePlanStartInjectsSecretsAndConfigsBeforeContainerStart pins that
// a start node copies the service's secrets and configs into the container
// before starting it, in that order.
func TestExecutePlanStartInjectsSecretsAndConfigsBeforeContainerStart(t *testing.T) {
svc, apiClient, _ := newStartPhaseTestService(t)
service := types.ServiceConfig{
Name: "web",
ContainerSpec: types.ContainerSpec{
Secrets: []types.ServiceSecretConfig{{Source: "token"}},
Configs: []types.ServiceConfigObjConfig{{Source: "settings"}},
},
}
project := &types.Project{
Name: "test",
Services: types.Services{"web": service},
Secrets: types.Secrets{"token": types.SecretConfig{Name: "token", Content: "s3cret"}},
Configs: types.Configs{"settings": types.ConfigObjConfig{Name: "settings", Content: "key=value"}},
}
ctr := container.Summary{ID: "web-id", Names: []string{"/test-web-1"}}
secret := apiClient.EXPECT().CopyToContainer(gomock.Any(), "web-id", gomock.Any()).
Return(client.CopyToContainerResult{}, nil)
config := apiClient.EXPECT().CopyToContainer(gomock.Any(), "web-id", gomock.Any()).
Return(client.CopyToContainerResult{}, nil).After(secret)
apiClient.EXPECT().ContainerStart(gomock.Any(), "web-id", gomock.Any()).
Return(client.ContainerStartResult{}, nil).After(config)
plan := &Plan{}
plan.addNode(Operation{
Type: OpStartContainer,
ResourceID: "service:web:1",
Cause: "start",
Service: &service,
Container: &ctr,
}, "start:web:1")
exec := svc.newPlanExecutor(project, emptyObservedState("test"), nil)
assert.NilError(t, exec.run(t.Context(), plan))
}
// TestExecutePlanFailedPostStartFailsTheStart pins that a failing post_start
// hook fails the plan and leaves the container's progression without its
// Started event.
func TestExecutePlanFailedPostStartFailsTheStart(t *testing.T) {
svc, apiClient, recorder := newStartPhaseTestService(t)
service := types.ServiceConfig{
Name: "web",
PostStart: []types.ServiceHook{{Command: types.ShellCommand{"fail"}}},
}
ctr := container.Summary{ID: "web-id", Names: []string{"/test-web-1"}}
apiClient.EXPECT().ContainerStart(gomock.Any(), "web-id", gomock.Any()).
Return(client.ContainerStartResult{}, nil)
apiClient.EXPECT().ExecCreate(gomock.Any(), "web-id", gomock.Any()).
Return(client.ExecCreateResult{ID: "exec-1"}, nil)
serverConn, clientConn := net.Pipe()
_ = serverConn.Close()
apiClient.EXPECT().ExecAttach(gomock.Any(), "exec-1", gomock.Any()).
Return(client.ExecAttachResult{HijackedResponse: client.NewHijackedResponse(clientConn, "")}, nil)
apiClient.EXPECT().ExecInspect(gomock.Any(), "exec-1", gomock.Any()).
Return(client.ExecInspectResult{ExitCode: 3}, nil)
plan := &Plan{}
start := plan.addNode(Operation{
Type: OpStartContainer,
ResourceID: "service:web:1",
Cause: "start",
Service: &service,
Container: &ctr,
}, "start:web:1")
plan.addNode(Operation{
Type: OpRunPostStart,
ResourceID: "service:web:1",
Cause: "post_start hooks",
Service: &service,
Container: &ctr,
}, "start:web:1", start)
exec := svc.newPlanExecutor(&types.Project{Name: "test", Services: types.Services{"web": service}}, emptyObservedState("test"), nil)
err := exec.run(t.Context(), plan)
assert.ErrorContains(t, err, "hook exited with status 3")
assert.Assert(t, !slices.Contains(recorder.byID["Container test-web-1"], api.StatusStarted),
"a failed post_start must not report Started: %v", recorder.byID["Container test-web-1"])
}
// TestExecutePlanStartPhaseEventContract drives a full wait + enriched-start
// plan through the real executor and asserts the per-resource progressions
// the event contract promises (see api.EventProcessor): a dependency wait

View file

@ -382,6 +382,90 @@ func TestPlanStart_NoContainerToStartFails(t *testing.T) {
assert.ErrorContains(t, err, `service "app" has no container to start`)
}
// Imperative parity (startService): the "no container to start" check is
// project-wide. A service without a container next to existing ones is a
// no-op for `compose start` (the other services still start); only a project
// with no container at all fails.
func TestPlanStart_NoContainerToStartIsProjectWide(t *testing.T) {
db := types.ServiceConfig{Name: "db"}
web := serviceWithDeps("web", types.DependsOnConfig{"db": {Condition: types.ServiceConditionStarted, Required: true}})
project := &types.Project{
Name: "myproject",
Services: types.Services{"db": db, "web": web},
}
t.Run("sibling service has a container", func(t *testing.T) {
observed := emptyObserved()
observed.Containers["db"] = []ObservedContainer{observedServiceContainer("db", 1, container.StateExited, "")}
plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
assert.Equal(t, plan.String(), strings.TrimSpace(`
[] -> #1 service:db:1, StartContainer, start [start:db:1] {start}
`)+"\n")
})
t.Run("dependency was never created", func(t *testing.T) {
observed := emptyObserved()
observed.Containers["web"] = []ObservedContainer{observedServiceContainer("web", 1, container.StateExited, "")}
plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
assert.Equal(t, len(plan.Nodes), 1, "web must still start:\n%s", plan)
})
t.Run("only an orphan container exists", func(t *testing.T) {
observed := emptyObserved()
observed.Orphans = []ObservedContainer{observedServiceContainer("old", 1, container.StateExited, "")}
_, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
})
}
// deploy.replicas: 0 is an unconditional no-op under scope Start: an
// observed stopped container of such a service is left alone.
func TestPlanStart_DeployReplicasZeroNeverStartsAnObservedContainer(t *testing.T) {
zero := 0
app := types.ServiceConfig{Name: "app", Deploy: &types.DeployConfig{Replicas: &zero}}
project := &types.Project{
Name: "myproject",
Services: types.Services{"app": app},
}
observed := emptyObserved()
observed.Containers["app"] = []ObservedContainer{observedServiceContainer("app", 1, container.StateExited, "")}
plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
assert.Equal(t, len(plan.Nodes), 0, "deploy.replicas: 0 must not start anything:\n%s", plan)
}
// A relay is a shell-less scratch binary: pre_start has no process for the
// hook container's VolumesFrom to share, so the hook is skipped when the
// lowest-numbered stopped container of an ordinary service is a stale relay,
// and that relay is started bare.
func TestPlanStart_OrdinaryPathSkipsPreStartWhenLowestIsRelay(t *testing.T) {
db := types.ServiceConfig{
Name: "db",
PreStart: []types.PreStartHook{{ContainerSpec: types.ContainerSpec{Command: types.ShellCommand{"init"}}}},
}
project := &types.Project{
Name: "myproject",
Services: types.Services{"db": db},
}
relay := observedServiceContainer("db", 1, container.StateExited, "")
relay.Summary.Labels[api.RelayLabel] = "relay-abc123"
observed := emptyObserved()
observed.Containers["db"] = []ObservedContainer{relay}
plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
assert.Equal(t, plan.String(), strings.TrimSpace(`
[] -> #1 service:db:1, StartContainer, start [start:db:1] {start}
`)+"\n")
assert.Assert(t, plan.Nodes[0].Operation.Service == nil, "a relay is started bare:\n%s", plan)
}
// A depends_on naming a service that is neither declared nor disabled is
// the same hard error the imperative engine raises (shouldWaitForDependency
// propagates it) — reachable via `compose start` without a compose file,
@ -1039,6 +1123,133 @@ func TestPlanStart_ProviderPathPreStartGateDoesNotRetryPastARelay(t *testing.T)
}
}
// The provider's restart node is one of its chain ends: a consumer of the
// provider orders after it, so it cannot start before the relay is running.
func TestPlanStart_ProviderRestartIsAChainEnd(t *testing.T) {
prov := types.ServiceConfig{Name: "prov"}
prov.Provider = &types.ServiceProviderConfig{Type: "test"}
web := serviceWithDeps("web", types.DependsOnConfig{"prov": {Condition: types.ServiceConditionStarted, Required: true}})
project := &types.Project{
Name: "myproject",
Services: types.Services{"prov": prov, "web": web},
}
relay := observedServiceContainer("prov", 1, container.StateExited, "")
relay.Summary.Labels[api.RelayLabel] = "relay-abc123"
observed := emptyObserved()
observed.Containers["prov"] = []ObservedContainer{relay}
observed.Containers["web"] = []ObservedContainer{observedServiceContainer("web", 1, container.StateExited, "")}
plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
var provStart, webStart *PlanNode
for _, n := range plan.Nodes {
switch n.Operation.ResourceID {
case "service:prov:1":
provStart = n
case "service:web:1":
webStart = n
}
}
assert.Assert(t, provStart != nil && webStart != nil, "both services must start:\n%s", plan)
assert.Assert(t, slices.Contains(webStart.DependsOn, provStart), "web must order after the relay restart:\n%s", plan)
}
// A relay never gets post_start: it has no shell to exec the hook in.
func TestPlanStart_ProviderRelayGetsNoPostStart(t *testing.T) {
prov := types.ServiceConfig{
Name: "prov",
PostStart: []types.ServiceHook{{Command: types.ShellCommand{"notify"}}},
}
prov.Provider = &types.ServiceProviderConfig{Type: "test"}
project := &types.Project{
Name: "myproject",
Services: types.Services{"prov": prov},
}
relay := observedServiceContainer("prov", 1, container.StateExited, "")
relay.Summary.Labels[api.RelayLabel] = "relay-abc123"
observed := emptyObserved()
observed.Containers["prov"] = []ObservedContainer{relay}
plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
assert.Equal(t, plan.String(), strings.TrimSpace(`
[] -> #1 service:prov:1, StartContainer, start [start:prov:1] {start}
`)+"\n")
}
// pre_start is gated on no replica running at observation, on the provider
// path as on the ordinary one.
func TestPlanStart_ProviderPathPreStartSkippedWhenAReplicaIsRunning(t *testing.T) {
prov := types.ServiceConfig{
Name: "prov",
PreStart: []types.PreStartHook{{ContainerSpec: types.ContainerSpec{Command: types.ShellCommand{"init"}}}},
}
prov.Provider = &types.ServiceProviderConfig{Type: "test"}
project := &types.Project{
Name: "myproject",
Services: types.Services{"prov": prov},
}
observed := emptyObserved()
observed.Containers["prov"] = []ObservedContainer{
observedServiceContainer("prov", 1, container.StateRunning, ""),
observedServiceContainer("prov", 2, container.StateExited, ""),
}
plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
assert.Equal(t, plan.String(), strings.TrimSpace(`
[] -> #1 service:prov:2, StartContainer, start [start:prov:2] {start}
`)+"\n")
}
// pre_start runs exactly once for the whole chain, on the first stale
// container, after the provider's own dependencies, and gates every start.
func TestPlanStart_ProviderPathPreStartRunsOnceBeforeTheChain(t *testing.T) {
db := types.ServiceConfig{Name: "db"}
db.HealthCheck = &types.HealthCheckConfig{Test: []string{"CMD", "true"}}
prov := serviceWithDeps("prov", types.DependsOnConfig{"db": {Condition: types.ServiceConditionHealthy, Required: true}})
prov.PreStart = []types.PreStartHook{{ContainerSpec: types.ContainerSpec{Command: types.ShellCommand{"init"}}}}
prov.Provider = &types.ServiceProviderConfig{Type: "test"}
project := &types.Project{
Name: "myproject",
Services: types.Services{"db": db, "prov": prov},
}
observed := emptyObserved()
observed.Containers["db"] = []ObservedContainer{observedServiceContainer("db", 1, container.StateRunning, "")}
// listed out of order: the hook still targets replica 1
observed.Containers["prov"] = []ObservedContainer{
observedServiceContainer("prov", 2, container.StateExited, ""),
observedServiceContainer("prov", 1, container.StateExited, ""),
}
plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt)
assert.NilError(t, err)
var wait, pre, start1, start2 *PlanNode
preStarts := 0
for _, n := range plan.Nodes {
switch {
case n.Operation.Type == OpWaitCondition:
wait = n
case n.Operation.Type == OpRunPreStart:
pre = n
preStarts++
case n.Operation.ResourceID == "service:prov:1" && n.Operation.Type == OpStartContainer:
start1 = n
case n.Operation.ResourceID == "service:prov:2" && n.Operation.Type == OpStartContainer:
start2 = n
}
}
assert.Equal(t, preStarts, 1, "pre_start must run once, not once per container:\n%s", plan)
assert.Assert(t, wait != nil && start1 != nil && start2 != nil, "unexpected plan:\n%s", plan)
assert.Assert(t, pre.Operation.Container != nil && pre.Operation.Container.ID == "myproject-prov-1-id",
"pre_start must target the first stale container:\n%s", plan)
assert.Assert(t, slices.Contains(pre.DependsOn, wait), "pre_start must wait on the provider's dependencies:\n%s", plan)
assert.Assert(t, slices.Contains(start1.DependsOn, pre), "the first start must wait on pre_start:\n%s", plan)
assert.Assert(t, slices.Contains(start2.DependsOn, start1), "the chain must continue after the first start:\n%s", plan)
}
// An optional (required: false) condition marks the shared wait node
// best-effort — a missing dependency is skipped, not fatal; one required
// dependent upgrades the node for everyone.

View file

@ -20,8 +20,10 @@ package compose
import (
"context"
"strconv"
"sync"
"testing"
"time"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/config/configfile"
@ -112,6 +114,80 @@ func serviceContainer(service string) container.Summary {
}
}
// stoppedServiceContainer builds a container summary of the given state for
// replica num of service, carrying the labels collectObservedState reads.
func stoppedServiceContainer(service string, num int, state container.ContainerState) container.Summary {
ctr := serviceContainer(service)
name := "prj-" + service + "-" + strconv.Itoa(num)
ctr.ID = name + "-id"
ctr.Names = []string{"/" + name}
ctr.State = state
ctr.Labels[api.ProjectLabel] = "prj"
ctr.Labels[api.ContainerNumberLabel] = strconv.Itoa(num)
return ctr
}
// TestStart_NoOpStartIsSilentAndOnlyListsContainers pins how start() wires
// its plan: the observation is scoped to Start (only the container listing
// reaches the daemon -- gomock fails on any network or volume listing), the
// plan is start-only, and a no-op start (everything already running) starts
// nothing and emits no event, not even the synthetic "Running" ones
// up/create report.
func TestStart_NoOpStartIsSilentAndOnlyListsContainers(t *testing.T) {
svc, apiClient, rec := newStartTestService(t)
web := types.ServiceConfig{Name: "web", ContainerSpec: types.ContainerSpec{Image: "alpine"}}
project := &types.Project{Name: "prj", Services: types.Services{"web": web}}
apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).
Return(client.ContainerListResult{Items: []container.Summary{
stoppedServiceContainer("web", 1, container.StateRunning),
}}, nil)
err := svc.start(t.Context(), "prj", api.StartOptions{Project: project}, nil)
assert.NilError(t, err)
assert.Equal(t, len(rec.summary()), 0, "a no-op start must be silent: %v", rec.summary())
}
// TestStart_WaitTimeoutBoundsDependencyWaits pins that start() hands
// StartOptions.WaitTimeout to the executor: --wait-timeout bounds a
// depends_on wait even without --wait, and expiring reports the dependency
// timeout while the dependent is never started.
func TestStart_WaitTimeoutBoundsDependencyWaits(t *testing.T) {
svc, apiClient, _ := newStartTestService(t)
db := types.ServiceConfig{Name: "db", ContainerSpec: types.ContainerSpec{Image: "alpine"}}
db.HealthCheck = &types.HealthCheckConfig{Test: []string{"CMD", "true"}}
web := types.ServiceConfig{Name: "web", ContainerSpec: types.ContainerSpec{Image: "alpine"}}
web.DependsOn = types.DependsOnConfig{"db": {Condition: types.ServiceConditionHealthy, Required: true}}
project := &types.Project{Name: "prj", Services: types.Services{"db": db, "web": web}}
dbCtr := stoppedServiceContainer("db", 1, container.StateRunning)
apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).
Return(client.ContainerListResult{Items: []container.Summary{
dbCtr,
stoppedServiceContainer("web", 1, container.StateExited),
}}, nil)
// db never becomes healthy; no ContainerStart expectation for web
apiClient.EXPECT().ContainerInspect(gomock.Any(), dbCtr.ID, gomock.Any()).
Return(client.ContainerInspectResult{Container: container.InspectResponse{
ID: dbCtr.ID,
Name: "/prj-db-1",
State: &container.State{
Status: container.StateRunning,
Health: &container.Health{Status: container.Starting},
},
Config: &container.Config{Healthcheck: &container.HealthConfig{Test: []string{"CMD", "true"}}},
}}, nil).AnyTimes()
// the outer deadline only turns a missing wait-timeout into a prompt
// failure instead of a hang
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
err := svc.start(ctx, "prj", api.StartOptions{Project: project, WaitTimeout: 200 * time.Millisecond}, nil)
assert.Error(t, err, "timeout waiting for dependencies")
}
// TestGetDependencyCondition locks the --wait condition selection: a service
// that others depend on with service_completed_successfully is waited on with
// that condition (or --wait would hang on one-shot services), anything else