Skip to content

Commit

Permalink
helm: add unit tests for ParseVals
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Sauber <[email protected]>
  • Loading branch information
asauber authored and michi-covalent committed Apr 27, 2023
1 parent 41d8616 commit 37f1e28
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ func MergeVals(
return vals, nil
}

// ParseVals takes a slice of Helm values of the form
// ["some.chart.value=val1", "some.other.value=val2"]
// and returns a deeply nested map of Values of the form
// expected by Helm actions.
func ParseVals(helmStrValues []string) (map[string]interface{}, error) {
helmValStr := strings.Join(helmStrValues, ",")
helmValues := map[string]interface{}{}
Expand Down
71 changes: 71 additions & 0 deletions internal/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,74 @@ func TestResolveHelmChartVersion(t *testing.T) {
})
}
}

func TestParseVals(t *testing.T) {
tests := []struct {
name string
input []string
want map[string]interface{}
wantErr bool
}{
{
name: "simple-val",
input: []string{"simple=true"},
want: map[string]interface{}{"simple": true},
wantErr: false,
},
{
name: "two-levels",
input: []string{"two.levels=true"},
want: map[string]interface{}{"two": map[string]interface{}{"levels": true}},
wantErr: false,
},
{
name: "multiple-keys",
input: []string{"multiple=true", "keys=true"},
want: map[string]interface{}{"multiple": true, "keys": true},
wantErr: false,
},
{
name: "string-type",
input: []string{"string=testval"},
want: map[string]interface{}{"string": "testval"},
wantErr: false,
},
{
name: "mixed-type",
input: []string{"string=testval", "bool=false"},
want: map[string]interface{}{"string": "testval", "bool": false},
wantErr: false,
},
{
name: "mixed-levels",
input: []string{"two.levels=true", "three.levels.deep=true"},
want: map[string]interface{}{
"two": map[string]interface{}{"levels": true},
"three": map[string]interface{}{"levels": map[string]interface{}{"deep": true}},
},
wantErr: false,
},
{
name: "invalid-input",
input: []string{"invalid"},
wantErr: true,
},
{
name: "mixed-invalid",
input: []string{"testkey=val", "invalid"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseVals(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseVals() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseVals() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 37f1e28

Please sign in to comment.