Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/e2e/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func TestPreStartHookInError(t *testing.T) {
ServiceState("sample", "created"))
}

// TestPreStartHookStartFailureNamesTheHook locks docker/compose#14513: a hook
// inherits the entrypoint of the service, which may not exist in the image of
// the hook, and the engine's error then named neither the hook nor its image,
// and surfaced on the row of the service container.
func TestPreStartHookStartFailureNamesTheHook(t *testing.T) {
NewScenario(t, "a pre_start hook that cannot start must be named in the error").
Step("up fails, naming the hook and its image",
ComposeCmd("up", "-d").MayFail(),
ExitCode(1),
OutputContains(`service "app" pre_start[0] (image hello-world): failed to start the hook container`),
OutputContains("/bin/busybox"),
ServiceState("app", "created"))
}

// The output of a failing hook is what tells why it failed: it must reach the
// error, which is built from the log stream opened on the running hook.
func TestPreStartHookFailureShowsOutput(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
app:
image: alpine
# inherited by the hook, whose image has no such executable
entrypoint: ["/bin/busybox", "sh", "-c"]
command: ["echo app ran"]
pre_start:
- image: hello-world
command: ["echo", "hook ran"]
14 changes: 13 additions & 1 deletion pkg/compose/pre_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ func (s *composeService) execPreStartHook(
case <-waitRes.Result:
case <-ctx.Done():
}
return err
// The daemon's error (a missing executable, a bad mount…) names neither
// the hook nor its image, and surfaces on the row of the service
// container: say which hook it is.
return fmt.Errorf("service %q pre_start[%d]%s: failed to start the hook container: %w", service.Name, index, hookImageSuffix(service, index), err)
}

// Open the log stream once the hook is started: on a container still in
Expand Down Expand Up @@ -264,6 +267,15 @@ func (s *composeService) execPreStartHook(
return nil
}

// hookImageSuffix returns " (image <name>)" for the index-th pre_start hook of
// service, or nothing when the model does not carry the image of the hook.
func hookImageSuffix(service types.ServiceConfig, index int) string {
if index < 0 || index >= len(service.PreStart) || service.PreStart[index].Image == "" {
return ""
}
return fmt.Sprintf(" (image %s)", service.PreStart[index].Image)
}

// createPreStartContainer creates the runner container for the index-th
// pre_start hook of service, named after getHookContainerName and stamped
// with the hook labels so both the start phase (by index) and the purge (by
Expand Down
39 changes: 39 additions & 0 deletions pkg/compose/pre_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,45 @@ func TestPreStart_ContainerStartFailurePropagates(t *testing.T) {
assert.ErrorContains(t, err, "container start failed")
}

// A hook container that cannot start (docker/compose#14513: the entrypoint
// inherited from the service does not exist in the image of the hook) is
// reported with the hook and its image, not as a bare daemon error that reads
// as the failure of the service container.
func TestPreStart_ContainerStartFailureNamesTheHook(t *testing.T) {
tested, apiClient := newPreStartTestService(t)

project := &types.Project{Name: "demo"}
service := types.ServiceConfig{
Name: "web",
ContainerSpec: types.ContainerSpec{Image: "alpine"},
PreStart: []types.PreStartHook{
{ContainerSpec: types.ContainerSpec{Image: "alpine", Command: types.ShellCommand{"true"}}},
{ContainerSpec: types.ContainerSpec{Image: "debian:trixie-slim", Command: types.ShellCommand{"echo", "hook ran"}}},
},
}
daemonErr := errors.New(`exec: "/bin/busybox": stat /bin/busybox: no such file or directory`)

scan := expectRunnerScan(apiClient, runnerSummary("hook-1", 0), runnerSummary("hook-2", 1))
wait1 := apiClient.EXPECT().ContainerWait(gomock.Any(), "hook-1", gomock.Any()).
Return(waitResultExit(0)).After(scan)
start1 := apiClient.EXPECT().ContainerStart(gomock.Any(), "hook-1", gomock.Any()).
Return(client.ContainerStartResult{}, nil).After(wait1)
logs1 := apiClient.EXPECT().ContainerLogs(gomock.Any(), "hook-1", gomock.Any()).
Return(emptyLogs(), nil).After(start1)
remove1 := expectSuccessRemove(apiClient, "hook-1").After(logs1)
wait2 := apiClient.EXPECT().ContainerWait(gomock.Any(), "hook-2", gomock.Any()).
Return(waitResultExit(0)).After(remove1)
start2 := apiClient.EXPECT().ContainerStart(gomock.Any(), "hook-2", gomock.Any()).
Return(client.ContainerStartResult{}, daemonErr).After(wait2)
apiClient.EXPECT().ContainerRemove(gomock.Any(), "hook-2", client.ContainerRemoveOptions{Force: true, RemoveVolumes: true}).
Return(client.ContainerRemoveResult{}, nil).After(start2)

err := tested.runPreStart(t.Context(), project, service, func(api.ContainerEvent) {})
assert.ErrorContains(t, err, `service "web" pre_start[1] (image debian:trixie-slim): failed to start the hook container: `)
assert.ErrorContains(t, err, "stat /bin/busybox: no such file or directory")
assert.ErrorIs(t, err, daemonErr)
}

// TestPreStart_WaitResultPreferredOverNilError pins the fix for the scheduler
// race in waitPreStart: when ContainerWait closes a successful stream cleanly
// it delivers Result (exit code) AND a nil send on Error at the same time.
Expand Down
Loading