From 581beb7b4c96d99af737c1dbc751fb801dfb57ff Mon Sep 17 00:00:00 2001 From: kw-00 Date: Wed, 28 Oct 2026 14:00:32 +0100 Subject: [PATCH 1/5] feat: add WithStrictStartupCommand and WithStrictAfterReadyCommand These two functions are variants to WithStartupCommand and WithAfterReadyCommand, respectively. As opposed to the latter, they treat non-zero exit codes from executable commands as errors. The variants are implemented just like the original functions, except that they use createStrictExecutableHooks instead of createExecutableHooks. createStrictExecutableHooks is a variant of createExecutableHooks which treats non-zero exit codes as errors and has also been implemented as part of this commit. --- container/options.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/container/options.go b/container/options.go index 72be7c21..6c353a35 100644 --- a/container/options.go +++ b/container/options.go @@ -304,6 +304,24 @@ func WithStartupCommand(execs ...Executable) CustomizeDefinitionOption { } } +// WithStrictStartupCommand will execute the command representation of each Executable into the container. +// It will leverage the container lifecycle hooks to call the command right after the container +// is started. +// +// As opposed to WithStartupCommand, non-zero exit codes of each Executable are treated as errors. +func WithStrictStartupCommand(execs ...Executable) CustomizeDefinitionOption { + return func(def *Definition) error { + startupCommandsHook := createStrictExecutableHooks(execs, func(hooks []ContainerHook) LifecycleHooks { + return LifecycleHooks{ + PostStarts: hooks, + } + }) + + def.lifecycleHooks = append(def.lifecycleHooks, startupCommandsHook) + return nil + } +} + // WithAfterReadyCommand will execute the command representation of each Executable into the container. // It will leverage the container lifecycle hooks to call the command right after the container // is ready. @@ -339,6 +357,34 @@ func createExecutableHooks(execs []Executable, hookCreator func([]ContainerHook) return hookCreator(hooks) } +// createStrictExecutableHooks creates lifecycle hooks for a slice of executables. +// hookCreator is a function that creates the appropriate LifecycleHooks with the provided ContainerHook slice. +// +// This is a variant of createExecutableHooks; the difference is that createStrictExecutableHooks treats non-zero +// exit codes in execs as errors. +func createStrictExecutableHooks(execs []Executable, hookCreator func([]ContainerHook) LifecycleHooks) LifecycleHooks { + hooks := make([]ContainerHook, 0, len(execs)) + + for _, exec := range execs { + execFn := func(ctx context.Context, c ContainerInfo) error { + if executor, ok := c.(ContainerExecutor); ok { + code, _, err := executor.Exec(ctx, exec.AsCommand(), exec.Options()...) + if err != nil { + return err + } + if code != 0 { + return fmt.Errorf("startup command %v exited with code %d", exec.AsCommand(), code) + } + return nil + } + return errors.New("container does not support execution") + } + hooks = append(hooks, execFn) + } + + return hookCreator(hooks) +} + // WithWaitStrategy replaces the wait strategy for a container, using 60 seconds as deadline func WithWaitStrategy(strategies ...wait.Strategy) CustomizeDefinitionOption { return WithWaitStrategyAndDeadline(60*time.Second, strategies...) From eaaa203e98f6d7afae2bf41006890be407657608 Mon Sep 17 00:00:00 2001 From: kw-00 Date: Wed, 28 Oct 2026 14:06:44 +0100 Subject: [PATCH 2/5] chore: add punctuation to createExecutableHooks description for consistency --- container/options.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/container/options.go b/container/options.go index 6c353a35..921414c9 100644 --- a/container/options.go +++ b/container/options.go @@ -338,8 +338,26 @@ func WithAfterReadyCommand(execs ...Executable) CustomizeDefinitionOption { } } -// createExecutableHooks creates lifecycle hooks for a slice of executables -// hookCreator is a function that creates the appropriate LifecycleHooks with the provided ContainerHook slice +// WithStrictAfterReadyCommand will execute the command representation of each Executable into the container. +// It will leverage the container lifecycle hooks to call the command right after the container +// is ready. +// +// As opposed to WithAfterReadyCommand, non-zero exit codes of each Executable are treated as errors. +func WithStrictAfterReadyCommand(execs ...Executable) CustomizeDefinitionOption { + return func(def *Definition) error { + postReadiesHook := createStrictExecutableHooks(execs, func(hooks []ContainerHook) LifecycleHooks { + return LifecycleHooks{ + PostReadies: hooks, + } + }) + + def.lifecycleHooks = append(def.lifecycleHooks, postReadiesHook) + return nil + } +} + +// createExecutableHooks creates lifecycle hooks for a slice of executables. +// hookCreator is a function that creates the appropriate LifecycleHooks with the provided ContainerHook slice. func createExecutableHooks(execs []Executable, hookCreator func([]ContainerHook) LifecycleHooks) LifecycleHooks { hooks := make([]ContainerHook, 0, len(execs)) From 6f5b877519d1ffa38e9c4ef6210bbe737fa1357d Mon Sep 17 00:00:00 2001 From: kw-00 Date: Wed, 28 Oct 2026 14:06:59 +0100 Subject: [PATCH 3/5] test: write tests for WithStrictStartupCommand and WithStrictAfterReadyCommand For both features, the tests include the following: - command execution and io tests analogical to those found in WithStartupCommand and WithStrictAfterReadyCommand, respectively - test that expects an error when executable command finishes with a non-zero exit code - this is the new functionality that WithStrict... functions add --- container/container.run_test.go | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/container/container.run_test.go b/container/container.run_test.go index fa328bf8..987de642 100644 --- a/container/container.run_test.go +++ b/container/container.run_test.go @@ -254,6 +254,37 @@ echo "done" require.Equal(t, "/tmp/.container-test\n", string(content)) }) + t.Run("with-strict-startup-command", func(t *testing.T) { + ctx := context.Background() + { + c, err := container.Run(ctx, + container.WithImage(alpineLatest), + container.WithEntrypoint("tail", "-f", "/dev/null"), + container.WithStrictStartupCommand(exec.NewRawCommand([]string{"touch", "/tmp/.container-test"})), + ) + container.Cleanup(t, c) + require.NoError(t, err) + require.NotNil(t, c) + + _, reader, err := c.Exec(context.Background(), []string{"ls", "/tmp/.container-test"}, exec.Multiplexed()) + require.NoError(t, err) + + content, err := io.ReadAll(reader) + require.NoError(t, err) + require.Equal(t, "/tmp/.container-test\n", string(content)) + } + { + c, err := container.Run(ctx, + container.WithImage(alpineLatest), + container.WithEntrypoint("tail", "-f", "/dev/null"), + container.WithStrictStartupCommand(exec.NewRawCommand([]string{"false"})), + ) + container.Cleanup(t, c) + require.Error(t, err) + require.NotNil(t, c) + } + }) + t.Run("with-after-ready-command", func(t *testing.T) { ctx := context.Background() @@ -274,6 +305,38 @@ echo "done" require.Equal(t, "/tmp/.container-test\n", string(content)) }) + + t.Run("with-strict-after-ready-command", func(t *testing.T) { + ctx := context.Background() + { + c, err := container.Run(ctx, + container.WithImage(alpineLatest), + container.WithEntrypoint("tail", "-f", "/dev/null"), + container.WithStrictAfterReadyCommand(exec.NewRawCommand([]string{"touch", "/tmp/.container-test"})), + ) + container.Cleanup(t, c) + require.NoError(t, err) + require.NotNil(t, c) + + _, reader, err := c.Exec(context.Background(), []string{"ls", "/tmp/.container-test"}, exec.Multiplexed()) + require.NoError(t, err) + + content, err := io.ReadAll(reader) + require.NoError(t, err) + require.Equal(t, "/tmp/.container-test\n", string(content)) + } + { + c, err := container.Run(ctx, + container.WithImage(alpineLatest), + container.WithEntrypoint("tail", "-f", "/dev/null"), + container.WithStrictAfterReadyCommand(exec.NewRawCommand([]string{"false"})), + ) + container.Cleanup(t, c) + require.Error(t, err) + require.NotNil(t, c) + } + }) + t.Run("with-durable-startup-command", func(t *testing.T) { // Load-bearing integration test: the rendered scripts are copied // into the container via the SDK's own file-copy path, the From 51a5a73e452f066f38339af16e4645f702fab3bd Mon Sep 17 00:00:00 2001 From: kw-00 Date: Wed, 28 Oct 2026 14:58:34 +0100 Subject: [PATCH 4/5] chore: apply linter (single newline removed) --- container/container.run_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/container/container.run_test.go b/container/container.run_test.go index 987de642..b80d4ce2 100644 --- a/container/container.run_test.go +++ b/container/container.run_test.go @@ -305,7 +305,6 @@ echo "done" require.Equal(t, "/tmp/.container-test\n", string(content)) }) - t.Run("with-strict-after-ready-command", func(t *testing.T) { ctx := context.Background() { From 9339d3da264cdb80d590a1fd73eeaf1ee11c3bfa Mon Sep 17 00:00:00 2001 From: kw-00 Date: Wed, 28 Oct 2026 18:37:55 +0100 Subject: [PATCH 5/5] test: add TestWithStrictStartupCommand and TestWithStrictAfterReadyCommand These tests merely check whether no error occurs when WithStrictStartupCommand and WithStrictAfterReadyCommand are used to register hooks and also whether the hooks actually gets registered. I am adding them because tests using the same logic already exist for the original WithStartupCommand and WithAfterReadyCommand, so it might be expected that the variants added in this PR are also tested for this. These tests do not test any new functionality. The new functionality of treating non-zero exit codes as errors is a feature used when running containers, and I have already added tests for this in container/container.run_test.go. --- container/options_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/container/options_test.go b/container/options_test.go index b62c56fb..330d0d20 100644 --- a/container/options_test.go +++ b/container/options_test.go @@ -134,6 +134,22 @@ func TestWithStartupCommand(t *testing.T) { require.Len(t, def.lifecycleHooks[0].PostStarts, 1) } +func TestWithStrictStartupCommand(t *testing.T) { + def := Definition{ + image: "alpine", + entrypoint: []string{"tail", "-f", "/dev/null"}, + started: true, + } + + testExec := exec.NewRawCommand([]string{"touch", ".go-sdk"}, exec.WithWorkingDir("/tmp")) + + err := WithStartupCommand(testExec)(&def) + require.NoError(t, err) + + require.Len(t, def.lifecycleHooks, 1) + require.Len(t, def.lifecycleHooks[0].PostStarts, 1) +} + func TestWithAfterReadyCommand(t *testing.T) { def := Definition{ image: "alpine", @@ -150,6 +166,22 @@ func TestWithAfterReadyCommand(t *testing.T) { require.Len(t, def.lifecycleHooks[0].PostReadies, 1) } +func TestWithStrictAfterReadyCommand(t *testing.T) { + def := Definition{ + image: "alpine", + entrypoint: []string{"tail", "-f", "/dev/null"}, + started: true, + } + + testExec := exec.NewRawCommand([]string{"touch", "/tmp/.go-sdk"}) + + err := WithStrictAfterReadyCommand(testExec)(&def) + require.NoError(t, err) + + require.Len(t, def.lifecycleHooks, 1) + require.Len(t, def.lifecycleHooks[0].PostReadies, 1) +} + func TestWithEnv(t *testing.T) { testEnv := func(t *testing.T, initial map[string]string, add map[string]string, expected map[string]string) { t.Helper()