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
62 changes: 62 additions & 0 deletions container/container.run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down
68 changes: 66 additions & 2 deletions container/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))

Expand All @@ -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...)
Expand Down
32 changes: 32 additions & 0 deletions container/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down
Loading