diff --git a/contrib/executor/init/pkg/runner/runner.go b/contrib/executor/init/pkg/runner/runner.go index 4c90871f661..93ef65adb56 100755 --- a/contrib/executor/init/pkg/runner/runner.go +++ b/contrib/executor/init/pkg/runner/runner.go @@ -81,27 +81,38 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res } shebang := "#!" + shell + "\nset -e\n" - entrypoint := shebang + // No set -e so that we can run the post-run script even if the command fails + entrypoint := "#!" + shell + "\n" command := shebang preRunScript := shebang postRunScript := shebang if execution.PreRunScript != "" { entrypoint += strconv.Quote(filepath.Join(r.Params.DataDir, preRunScriptName)) + "\n" + entrypoint += "prerun_exit_code=$?\nif [ $prerun_exit_code -ne 0 ]; then\n exit $prerun_exit_code\nfi\n" preRunScript += execution.PreRunScript } if len(execution.Command) != 0 { entrypoint += strconv.Quote(filepath.Join(r.Params.DataDir, commandScriptName)) + " $@\n" + entrypoint += "command_exit_code=$?\n" command += strings.Join(execution.Command, " ") command += " \"$@\"\n" } if execution.PostRunScript != "" { entrypoint += strconv.Quote(filepath.Join(r.Params.DataDir, postRunScriptName)) + "\n" + entrypoint += "postrun_exit_code=$?\n" postRunScript += execution.PostRunScript } + if len(execution.Command) != 0 { + entrypoint += "if [ $command_exit_code -ne 0 ]; then\n exit $command_exit_code\nfi\n" + } + + if execution.PostRunScript != "" { + entrypoint += "exit $postrun_exit_code\n" + } var scripts = []struct { dir string file string diff --git a/contrib/executor/init/pkg/runner/runner_test.go b/contrib/executor/init/pkg/runner/runner_test.go index 977c20213f7..ac06bfaed26 100755 --- a/contrib/executor/init/pkg/runner/runner_test.go +++ b/contrib/executor/init/pkg/runner/runner_test.go @@ -2,6 +2,7 @@ package runner import ( "context" + "os" "testing" "github.com/stretchr/testify/assert" @@ -31,4 +32,45 @@ func TestRun(t *testing.T) { assert.Equal(t, result.Status, testkube.ExecutionStatusRunning) }) + t.Run("runner with pre and post run scripts should run test", func(t *testing.T) { + t.Parallel() + + params := envs.Params{DataDir: "./testdir"} + runner := NewRunner(params) + execution := testkube.NewQueuedExecution() + execution.Content = testkube.NewStringTestContent("hello I'm test content") + execution.PreRunScript = "echo \"===== pre-run script\"" + execution.Command = []string{"command.sh"} + execution.PostRunScript = "echo \"===== pre-run script\"" + + // when + result, err := runner.Run(ctx, *execution) + + // then + assert.NoError(t, err) + assert.Equal(t, result.Status, testkube.ExecutionStatusRunning) + + expected := `#!/bin/sh +"testdir/prerun.sh" +prerun_exit_code=$? +if [ $prerun_exit_code -ne 0 ]; then + exit $prerun_exit_code +fi +"testdir/command.sh" $@ +command_exit_code=$? +"testdir/postrun.sh" +postrun_exit_code=$? +if [ $command_exit_code -ne 0 ]; then + exit $command_exit_code +fi +exit $postrun_exit_code +` + + data, err := os.ReadFile("testdir/entrypoint.sh") + if err != nil { + t.Fatalf("Failed to read file: %v", err) + } + assert.Equal(t, string(data), expected) + }) + }