update args_test.go unittest

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2022-04-05 05:03:06 +08:00
parent 4a52ce0ac2
commit 8e5c779626
2 changed files with 75 additions and 27 deletions

View File

@ -18,7 +18,12 @@ type argMap struct {
flags []string
}
// SetArg sets a flag and value in the map
func (a *argMap) SetArg(flag, arg string, isSpace bool) {
// if flag is empty, return
if len(flag) == 0 {
return
}
if _, exists := a.m[flag]; !exists {
keyarg := &keyVal{key: flag, val: arg, spaceFlag: isSpace}
a.m[flag] = append(a.m[flag], keyarg)
@ -29,6 +34,7 @@ func (a *argMap) SetArg(flag, arg string, isSpace bool) {
}
}
// newArgMap creates a new argMap
func newArgMap() *argMap {
return &argMap{m: map[string][]*keyVal{}}
}

View File

@ -5,45 +5,87 @@ import (
"testing"
"github.com/roboll/helmfile/pkg/state"
"github.com/stretchr/testify/require"
)
// TestGetArgs tests the GetArgs function
func TestGetArgs(t *testing.T) {
args := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false --tiller-namespace ns"
defaultArgs := []string{"--recreate-pods", "--force"}
Helmdefaults := state.HelmSpec{KubeContext: "test", TillerNamespace: "test-namespace", Args: defaultArgs}
testState := &state.HelmState{
ReleaseSetSpec: state.ReleaseSetSpec{
HelmDefaults: Helmdefaults,
tests := []struct {
args string
expected string
}{
{
args: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false --tiller-namespace ns",
expected: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false --tiller-namespace ns --recreate-pods --force",
},
{
args: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --tiller-namespace ns",
expected: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --tiller-namespace ns --recreate-pods --force",
},
}
receivedArgs := GetArgs(args, testState)
for _, test := range tests {
defaultArgs := []string{"--recreate-pods", "--force"}
Helmdefaults := state.HelmSpec{KubeContext: "test", TillerNamespace: "test-namespace", Args: defaultArgs}
testState := &state.HelmState{
ReleaseSetSpec: state.ReleaseSetSpec{
HelmDefaults: Helmdefaults,
},
}
receivedArgs := GetArgs(test.args, testState)
expectedOutput := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false --tiller-namespace ns --recreate-pods --force"
if compareArgs(expectedOutput, receivedArgs) == false {
t.Errorf("expected %s, got %s", expectedOutput, strings.Join(receivedArgs, " "))
require.Equalf(t, test.expected, strings.Join(receivedArgs, " "), "expected args %s, received args %s", test.expected, strings.Join(receivedArgs, " "))
}
}
func Test2(t *testing.T) {
args := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --tiller-namespace ns"
defaultArgs := []string{"--recreate-pods", "--force"}
Helmdefaults := state.HelmSpec{KubeContext: "test", TillerNamespace: "test-namespace", Args: defaultArgs}
testState := &state.HelmState{
ReleaseSetSpec: state.ReleaseSetSpec{
HelmDefaults: Helmdefaults,
// TestSetArg tests the SetArg function
func TestSetArg(t *testing.T) {
ap := newArgMap()
tests := []struct {
// check if changes have been made to the map
change bool
flag string
arg string
isSpace bool
}{
{
flag: "--set",
arg: "app1.bootstrap=true",
isSpace: false,
change: true,
},
{
flag: "--timeout",
arg: "3600",
isSpace: false,
change: true,
},
{
flag: "--force",
arg: "",
isSpace: false,
change: true,
},
{
flag: "",
arg: "",
isSpace: false,
change: false,
},
}
receivedArgs := GetArgs(args, testState)
expectedOutput := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --tiller-namespace ns --recreate-pods --force"
for _, test := range tests {
ap.SetArg(test.flag, test.arg, test.isSpace)
if test.change {
require.Containsf(t, ap.flags, test.flag, "expected flag %s to be set", test.flag)
require.Containsf(t, ap.m, test.flag, "expected m %s to be set", test.flag)
kv := &keyVal{key: test.flag, val: test.arg, spaceFlag: test.isSpace}
require.Containsf(t, ap.m[test.flag], kv, "expected %v in m[%s]", kv, test.flag)
} else {
require.NotContainsf(t, ap.flags, test.flag, "expected flag %s to be not set", test.flag)
require.NotContainsf(t, ap.m, test.flag, "expected m %s to be not set", test.flag)
}
if compareArgs(expectedOutput, receivedArgs) == false {
t.Errorf("expected %s, got %s", expectedOutput, strings.Join(receivedArgs, " "))
}
}
func compareArgs(expectedArgs string, args []string) bool {
return strings.Compare(strings.Join(args, " "), expectedArgs) == 0
}