Skip to content
Open
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
10 changes: 3 additions & 7 deletions container/container.run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 10 additions & 3 deletions container/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
46 changes: 46 additions & 0 deletions container/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package container

import (
"bytes"
"context"
"errors"
"io"
"log/slog"
"testing"
"time"

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Loading