From eb56965296e52617de7d4bfb608b349bf9469130 Mon Sep 17 00:00:00 2001 From: Gerd Oberlechner Date: Wed, 4 Dec 2024 12:58:45 +0100 Subject: [PATCH] bash flag tests Signed-off-by: Gerd Oberlechner --- tooling/templatize/pkg/pipeline/run_test.go | 2 +- tooling/templatize/pkg/pipeline/shell.go | 5 +- tooling/templatize/pkg/pipeline/shell_test.go | 53 ++++++++++++++++--- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/tooling/templatize/pkg/pipeline/run_test.go b/tooling/templatize/pkg/pipeline/run_test.go index 0b90c46b4..b53be7648 100644 --- a/tooling/templatize/pkg/pipeline/run_test.go +++ b/tooling/templatize/pkg/pipeline/run_test.go @@ -133,7 +133,7 @@ func TestResourceGroupError(t *testing.T) { }, } err := rg.run(context.Background(), &PipelineRunOptions{}, &executionTargetImpl{}) - assert.Error(t, err, "failed to execute shell command: /bin/bash: line 3: faaaaafffaa: command not found\n exit status 127") + assert.Error(t, err, "failed to execute shell command: /bin/bash: line 1: faaaaafffaa: command not found\n exit status 127") // Test processing ends after first error assert.Equal(t, len(tmpVals), 1) } diff --git a/tooling/templatize/pkg/pipeline/shell.go b/tooling/templatize/pkg/pipeline/shell.go index b3600391e..48730b94e 100644 --- a/tooling/templatize/pkg/pipeline/shell.go +++ b/tooling/templatize/pkg/pipeline/shell.go @@ -31,10 +31,7 @@ func (s *Step) createCommand(ctx context.Context, dryRun bool, envVars map[strin } func buildBashScript(command string) string { - return "set -o errexit\n" + - "set -o nounset\n" + - "set -o pipefail\n" + - command + return fmt.Sprintf("set -o errexit -o nounset -o pipefail\n%s", command) } func (s *Step) runShellStep(ctx context.Context, kubeconfigFile string, options *PipelineRunOptions) error { diff --git a/tooling/templatize/pkg/pipeline/shell_test.go b/tooling/templatize/pkg/pipeline/shell_test.go index b4c2b2c8f..8ad2dfda1 100644 --- a/tooling/templatize/pkg/pipeline/shell_test.go +++ b/tooling/templatize/pkg/pipeline/shell_test.go @@ -152,13 +152,52 @@ func TestMapStepVariables(t *testing.T) { } func TestRunShellStep(t *testing.T) { - expectedOutput := "hello\n" - s := &Step{ - Command: "echo hello", - outputFunc: func(output string) { - assert.Equal(t, output, expectedOutput) + testCases := []struct { + name string + vars config.Variables + step *Step + err string + }{ + { + name: "basic", + vars: config.Variables{}, + step: &Step{ + Command: "echo hello", + }, + }, + { + name: "test nounset", + vars: config.Variables{}, + step: &Step{ + Command: "echo $DOES_NOT_EXIST", + }, + err: "failed to execute shell command: /bin/bash: line 1: DOES_NOT_EXIST: unbound variable\n exit status 1", }, + { + name: "test errexit", + vars: config.Variables{}, + step: &Step{ + Command: "ls /does-not-exist ; echo hello", + }, + err: "failed to execute shell command: ls: /does-not-exist: No such file or directory\n exit status 1", + }, + { + name: "test pipefail", + vars: config.Variables{}, + step: &Step{ + Command: "ls /does-not-exist | echo", + }, + err: "failed to execute shell command: \nls: /does-not-exist: No such file or directory\n exit status 1", + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := tc.step.runShellStep(context.Background(), "", &PipelineRunOptions{}) + if tc.err != "" { + assert.Error(t, err, tc.err) + } else { + assert.NilError(t, err) + } + }) } - err := s.runShellStep(context.Background(), "", &PipelineRunOptions{}) - assert.NilError(t, err) }