diff --git a/container/container.run_test.go b/container/container.run_test.go index fa328bf8..b80d4ce2 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,37 @@ 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 diff --git a/container/options.go b/container/options.go index 72be7c21..921414c9 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. @@ -320,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)) @@ -339,6 +375,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...) 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()