Skip to content

Commit

Permalink
fix: [TKC-1611] execute post run script for container executors (#5101)
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasv authored Mar 5, 2024
1 parent 66fb242 commit e7f03c0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
13 changes: 12 additions & 1 deletion contrib/executor/init/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions contrib/executor/init/pkg/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
})

}

0 comments on commit e7f03c0

Please sign in to comment.