Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue --namespace showing twice #1452

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") {
james03160927 marked this conversation as resolved.
Show resolved Hide resolved
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