diff --git a/docs/Gemfile.lock b/docs/Gemfile.lock index 149c5999f..4ccf4247a 100644 --- a/docs/Gemfile.lock +++ b/docs/Gemfile.lock @@ -231,7 +231,7 @@ GEM rb-inotify (0.10.1) ffi (~> 1.0) ref (2.0.0) - rexml (3.3.6) + rexml (3.3.3) strscan rouge (3.26.0) rubyzip (2.3.0) diff --git a/modules/helm/cmd.go b/modules/helm/cmd.go index d4cd41b11..d259c66c5 100644 --- a/modules/helm/cmd.go +++ b/modules/helm/cmd.go @@ -1,6 +1,8 @@ package helm import ( + "slices" + "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/terratest/modules/shell" "github.com/gruntwork-io/terratest/modules/testing" @@ -66,7 +68,10 @@ func RunHelmCommandAndGetStdOutE(t testing.TestingT, options *Options, cmd strin func prepareHelmCommand(t testing.TestingT, options *Options, cmd string, additionalArgs ...string) shell.Command { args := []string{cmd} args = getCommonArgs(options, args...) - args = append(args, getNamespaceArgs(options)...) + // name space arg only append if it is not there + if !slices.Contains(additionalArgs, "--namespace") { + args = append(args, getNamespaceArgs(options)...) + } args = append(args, additionalArgs...) helmCmd := shell.Command{ diff --git a/modules/helm/cmd_test.go b/modules/helm/cmd_test.go new file mode 100644 index 000000000..06cf1bc12 --- /dev/null +++ b/modules/helm/cmd_test.go @@ -0,0 +1,50 @@ +package helm + +import ( + "testing" + + "github.com/gruntwork-io/terratest/modules/k8s" + "github.com/gruntwork-io/terratest/modules/logger" + "github.com/stretchr/testify/assert" +) + +func TestPrepareHelmCommand(t *testing.T) { + t.Parallel() + + options := &Options{ + KubectlOptions: &k8s.KubectlOptions{ + Namespace: "test-namespace", + }, + EnvVars: map[string]string{"SampleEnv": "test_value"}, + Logger: logger.Default, + } + t.Run("command without additional args", func(t *testing.T) { + cmd := prepareHelmCommand(t, options, "install") + assert.Equal(t, "helm", cmd.Command) + assert.Contains(t, cmd.Args, "install") + assert.Contains(t, cmd.Args, "--namespace") + assert.Contains(t, cmd.Args, "test-namespace") + assert.Equal(t, ".", cmd.WorkingDir) + assert.Equal(t, options.EnvVars, cmd.Env) + assert.Equal(t, options.Logger, cmd.Logger) + }) + t.Run("Command with additional args", func(t *testing.T) { + cmd := prepareHelmCommand(t, options, "upgrade", "--install", "my-release", "my-chart") + assert.Equal(t, "helm", cmd.Command) + assert.Contains(t, cmd.Args, "upgrade") + assert.Contains(t, cmd.Args, "--install") + assert.Contains(t, cmd.Args, "my-release") + assert.Contains(t, cmd.Args, "my-chart") + assert.Contains(t, cmd.Args, "--namespace") + assert.Contains(t, cmd.Args, "test-namespace") + }) + + t.Run("Command with namespace in additional args", func(t *testing.T) { + cmd := prepareHelmCommand(t, options, "install", "--namespace", "custom-namespace") + assert.Equal(t, "helm", cmd.Command) + assert.Contains(t, cmd.Args, "install") + assert.Contains(t, cmd.Args, "--namespace") + assert.Contains(t, cmd.Args, "custom-namespace") + assert.NotContains(t, cmd.Args, "test-namespace") + }) +} diff --git a/modules/terraform/output.go b/modules/terraform/output.go index b14295bc1..6e294e7a2 100644 --- a/modules/terraform/output.go +++ b/modules/terraform/output.go @@ -6,7 +6,6 @@ import ( "fmt" "reflect" "strconv" - "strings" "github.com/gruntwork-io/terratest/modules/testing" "github.com/stretchr/testify/require" @@ -301,27 +300,10 @@ func OutputStructE(t testing.TestingT, options *Options, key string, v interface if err != nil { return err } - out = cleanOutput(out) return json.Unmarshal([]byte(out), &v) } -// cleanOutput removes lines containing "INFO" and non-printable ASCII characters from the output. -func cleanOutput(out string) string { - var result []rune - for _, line := range strings.Split(out, "\n") { - if strings.Contains(line, "INFO") { - continue - } - for _, r := range line { - if r >= 32 && r < 127 { // Keep printable ASCII characters only - result = append(result, r) - } - } - } - return string(result) -} - // OutputForKeysE calls terraform output for the given key list and returns values as a map. // The returned values are of type interface{} and need to be type casted as necessary. Refer to output_test.go func OutputForKeysE(t testing.TestingT, options *Options, keys []string) (map[string]interface{}, error) { @@ -329,7 +311,6 @@ func OutputForKeysE(t testing.TestingT, options *Options, keys []string) (map[st if err != nil { return nil, err } - out = cleanOutput(out) outputMap := map[string]map[string]interface{}{} if err := json.Unmarshal([]byte(out), &outputMap); err != nil {