Skip to content

Commit

Permalink
Merge pull request #1452 from gruntwork-io/fix/namespace-show-twice
Browse files Browse the repository at this point in the history
fix: issue --namespace showing twice
  • Loading branch information
james03160927 authored Oct 17, 2024
2 parents 6fd2ca3 + 7bf6d85 commit 754233f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion modules/helm/cmd.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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{
Expand Down
50 changes: 50 additions & 0 deletions modules/helm/cmd_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}
19 changes: 0 additions & 19 deletions modules/terraform/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -301,35 +300,17 @@ 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) {
out, err := OutputJsonE(t, options, "")
if err != nil {
return nil, err
}
out = cleanOutput(out)

outputMap := map[string]map[string]interface{}{}
if err := json.Unmarshal([]byte(out), &outputMap); err != nil {
Expand Down

0 comments on commit 754233f

Please sign in to comment.