Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 51b5b89

Browse files
committed
release-note-none
Added negative test case for namespace
1 parent 303742c commit 51b5b89

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

pkg/kubectl/namespace_test.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestNamespaceGenerate(t *testing.T) {
2828
params map[string]interface{}
2929
expected *api.Namespace
3030
expectErr bool
31+
index int
3132
}{
3233
{
3334
params: map[string]interface{}{
@@ -44,15 +45,45 @@ func TestNamespaceGenerate(t *testing.T) {
4445
params: map[string]interface{}{},
4546
expectErr: true,
4647
},
48+
{
49+
params: map[string]interface{}{
50+
"name": 1,
51+
},
52+
expectErr: true,
53+
},
54+
{
55+
params: map[string]interface{}{
56+
"name": nil,
57+
},
58+
expectErr: true,
59+
},
60+
{
61+
params: map[string]interface{}{
62+
"name_wrong_key": "some_value",
63+
},
64+
expectErr: true,
65+
},
66+
{
67+
params: map[string]interface{}{
68+
"NAME": "some_value",
69+
},
70+
expectErr: true,
71+
},
4772
}
4873
generator := NamespaceGeneratorV1{}
49-
for _, test := range tests {
74+
for index, test := range tests {
5075
obj, err := generator.Generate(test.params)
51-
if !test.expectErr && err != nil {
52-
t.Errorf("unexpected error: %v", err)
53-
}
54-
if test.expectErr && err != nil {
55-
continue
76+
switch {
77+
case test.expectErr && err != nil:
78+
continue // loop, since there's no output to check
79+
case test.expectErr && err == nil:
80+
t.Errorf("%v: expected error and didn't get one", index)
81+
continue // loop, no expected output object
82+
case !test.expectErr && err != nil:
83+
t.Errorf("%v: expected error and didn't get one", index)
84+
continue // loop, no output object
85+
case !test.expectErr && err == nil:
86+
// do nothing and drop through
5687
}
5788
if !reflect.DeepEqual(obj.(*api.Namespace), test.expected) {
5889
t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected, obj.(*api.Namespace))

0 commit comments

Comments
 (0)