add unit test for function ParseKindArg and ParseGroupKind

This commit is contained in:
hangaoshuai 2018-03-08 09:24:18 +08:00
parent 01504f66e3
commit 177afe55c0

View File

@ -134,3 +134,47 @@ func TestKindForGroupVersionKinds(t *testing.T) {
} }
} }
} }
func TestParseKindArg(t *testing.T) {
tests := []struct {
input string
gvk *GroupVersionKind
gk GroupKind
}{
{input: "Pod", gk: GroupKind{Kind: "Pod"}},
{input: ".apps", gk: GroupKind{Group: "apps"}},
{input: "Pod.", gk: GroupKind{Kind: "Pod"}},
{input: "StatefulSet.apps", gk: GroupKind{Group: "apps", Kind: "StatefulSet"}},
{input: "StatefulSet.v1.apps", gvk: &GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}, gk: GroupKind{Group: "v1.apps", Kind: "StatefulSet"}},
}
for i, test := range tests {
t.Run(test.input, func(t *testing.T) {
gvk, gk := ParseKindArg(test.input)
if (gvk != nil && test.gvk == nil) || (gvk == nil && test.gvk != nil) || (test.gvk != nil && *gvk != *test.gvk) {
t.Errorf("%d: expected output: %#v, got: %#v", i, test.gvk, gvk)
}
if gk != test.gk {
t.Errorf("%d: expected output: %#v, got: %#v", i, test.gk, gk)
}
})
}
}
func TestParseGroupKind(t *testing.T) {
tests := []struct {
input string
out GroupKind
}{
{input: "Pod", out: GroupKind{Kind: "Pod"}},
{input: ".StatefulSet", out: GroupKind{Group: "StatefulSet"}},
{input: "StatefulSet.apps", out: GroupKind{Group: "apps", Kind: "StatefulSet"}},
}
for i, test := range tests {
t.Run(test.input, func(t *testing.T) {
out := ParseGroupKind(test.input)
if out != test.out {
t.Errorf("%d: expected output: %#v, got: %#v", i, test.out, out)
}
})
}
}