From f9b03487b2418ec2e27d0068616b69721a96ce89 Mon Sep 17 00:00:00 2001 From: Dean Chen <862469039@qq.com> Date: Wed, 28 Oct 2026 18:45:51 +0500 Subject: [PATCH] fix(container): fail startup hooks on a non-zero command exit WithStartupCommand and WithAfterReadyCommand only returned the Docker exec error, so a command that exited non-zero still looked successful. Signed-off-by: Dean Chen <862469039@qq.com> --- container/container.run_test.go | 10 +++---- container/options.go | 13 +++++++--- container/options_test.go | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/container/container.run_test.go b/container/container.run_test.go index fa328bf8..7847277f 100644 --- a/container/container.run_test.go +++ b/container/container.run_test.go @@ -433,13 +433,9 @@ echo "done" // requested user, the dispatcher's set -e causes a non-zero // exit, AND the side-effect command never runs. // - // We invoke the dispatcher directly to observe its exit code. - // container.WithStartupCommand isn't suitable for this assertion: - // its lifecycle hook only checks the Docker-level exec error, - // not the inner process's exit code, so it silently swallows a - // non-zero dispatcher exit. Consumers who want first-create - // coverage to fail loudly should invoke the dispatcher - // themselves and check the returned exit code. + // Invoke the dispatcher directly so this test can see the exit + // code and the missing marker. WithStartupCommand would fail + // Run on that non-zero exit, but it does not return the code. ctx := context.Background() c, err := container.Run(ctx, diff --git a/container/options.go b/container/options.go index 72be7c21..83beddfb 100644 --- a/container/options.go +++ b/container/options.go @@ -327,11 +327,18 @@ func createExecutableHooks(execs []Executable, hookCreator func([]ContainerHook) for _, exec := range execs { execFn := func(ctx context.Context, c ContainerInfo) error { - if executor, ok := c.(ContainerExecutor); ok { - _, _, err := executor.Exec(ctx, exec.AsCommand(), exec.Options()...) + executor, ok := c.(ContainerExecutor) + if !ok { + return errors.New("container does not support execution") + } + code, _, err := executor.Exec(ctx, exec.AsCommand(), exec.Options()...) + if err != nil { return err } - return errors.New("container does not support execution") + if code != 0 { + return fmt.Errorf("command %v exited with code %d", exec.AsCommand(), code) + } + return nil } hooks = append(hooks, execFn) } diff --git a/container/options_test.go b/container/options_test.go index b62c56fb..d22b83e1 100644 --- a/container/options_test.go +++ b/container/options_test.go @@ -2,8 +2,10 @@ package container import ( "bytes" + "context" "errors" "io" + "log/slog" "testing" "time" @@ -132,6 +134,31 @@ func TestWithStartupCommand(t *testing.T) { require.Len(t, def.lifecycleHooks, 1) require.Len(t, def.lifecycleHooks[0].PostStarts, 1) + + t.Run("nonzero-exit", func(t *testing.T) { + fake := &hookExecTarget{code: 7} + err := def.lifecycleHooks[0].PostStarts[0](context.Background(), fake) + require.Error(t, err) + require.ErrorContains(t, err, "exited with code 7") + require.Equal(t, []string{"touch", ".go-sdk"}, fake.cmd) + }) + + t.Run("zero-exit", func(t *testing.T) { + fake := &hookExecTarget{code: 0} + err := def.lifecycleHooks[0].PostStarts[0](context.Background(), fake) + require.NoError(t, err) + }) + + t.Run("exec-error", func(t *testing.T) { + fake := &hookExecTarget{err: errors.New("daemon went away")} + err := def.lifecycleHooks[0].PostStarts[0](context.Background(), fake) + require.ErrorContains(t, err, "daemon went away") + }) + + t.Run("no-executor", func(t *testing.T) { + err := def.lifecycleHooks[0].PostStarts[0](context.Background(), infoOnlyTarget{}) + require.ErrorContains(t, err, "does not support execution") + }) } func TestWithAfterReadyCommand(t *testing.T) { @@ -714,3 +741,22 @@ func TestWithCredentialsFn(t *testing.T) { require.NoError(t, opt.Customize(&def)) require.Len(t, def.pullOptions, 1) } + +type infoOnlyTarget struct{} + +func (infoOnlyTarget) ID() string { return "id" } +func (infoOnlyTarget) Image() string { return "img" } +func (infoOnlyTarget) ShortID() string { return "short" } +func (infoOnlyTarget) Logger() *slog.Logger { return slog.Default() } + +type hookExecTarget struct { + infoOnlyTarget + code int + err error + cmd []string +} + +func (f *hookExecTarget) Exec(_ context.Context, cmd []string, _ ...exec.ProcessOption) (int, io.Reader, error) { + f.cmd = cmd + return f.code, nil, f.err +}