update args_test.go unittest
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
parent
4a52ce0ac2
commit
8e5c779626
|
|
@ -18,7 +18,12 @@ type argMap struct {
|
||||||
flags []string
|
flags []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetArg sets a flag and value in the map
|
||||||
func (a *argMap) SetArg(flag, arg string, isSpace bool) {
|
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 {
|
if _, exists := a.m[flag]; !exists {
|
||||||
keyarg := &keyVal{key: flag, val: arg, spaceFlag: isSpace}
|
keyarg := &keyVal{key: flag, val: arg, spaceFlag: isSpace}
|
||||||
a.m[flag] = append(a.m[flag], keyarg)
|
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 {
|
func newArgMap() *argMap {
|
||||||
return &argMap{m: map[string][]*keyVal{}}
|
return &argMap{m: map[string][]*keyVal{}}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,45 +5,87 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/roboll/helmfile/pkg/state"
|
"github.com/roboll/helmfile/pkg/state"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestGetArgs tests the GetArgs function
|
||||||
func TestGetArgs(t *testing.T) {
|
func TestGetArgs(t *testing.T) {
|
||||||
args := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false --tiller-namespace ns"
|
|
||||||
defaultArgs := []string{"--recreate-pods", "--force"}
|
tests := []struct {
|
||||||
Helmdefaults := state.HelmSpec{KubeContext: "test", TillerNamespace: "test-namespace", Args: defaultArgs}
|
args string
|
||||||
testState := &state.HelmState{
|
expected string
|
||||||
ReleaseSetSpec: state.ReleaseSetSpec{
|
}{
|
||||||
HelmDefaults: Helmdefaults,
|
{
|
||||||
|
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"
|
require.Equalf(t, test.expected, strings.Join(receivedArgs, " "), "expected args %s, received args %s", test.expected, strings.Join(receivedArgs, " "))
|
||||||
|
|
||||||
if compareArgs(expectedOutput, receivedArgs) == false {
|
|
||||||
t.Errorf("expected %s, got %s", expectedOutput, strings.Join(receivedArgs, " "))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test2(t *testing.T) {
|
// TestSetArg tests the SetArg function
|
||||||
args := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --tiller-namespace ns"
|
func TestSetArg(t *testing.T) {
|
||||||
defaultArgs := []string{"--recreate-pods", "--force"}
|
ap := newArgMap()
|
||||||
Helmdefaults := state.HelmSpec{KubeContext: "test", TillerNamespace: "test-namespace", Args: defaultArgs}
|
|
||||||
testState := &state.HelmState{
|
tests := []struct {
|
||||||
ReleaseSetSpec: state.ReleaseSetSpec{
|
// check if changes have been made to the map
|
||||||
HelmDefaults: Helmdefaults,
|
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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue