diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_role.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_role.go index 1b4b2edd482..cb2a6aac7f3 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_role.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_role.go @@ -114,10 +114,12 @@ var ( // AddSpecialVerb allows the addition of items to the `specialVerbs` map for non-k8s native resources. func AddSpecialVerb(verb string, gr schema.GroupResource) { - if resources, ok := specialVerbs[verb]; ok { - resources = append(resources, gr) - specialVerbs[verb] = resources + resources, ok := specialVerbs[verb] + if !ok { + resources = make([]schema.GroupResource, 1) } + resources = append(resources, gr) + specialVerbs[verb] = resources } // ResourceOptions holds the related options for '--resource' option diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_role_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_role_test.go index 8cc55037137..e723de5c61c 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_role_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_role_test.go @@ -680,27 +680,34 @@ func TestComplete(t *testing.T) { } func TestAddSpecialVerb(t *testing.T) { - resource := schema.GroupResource{ - Group: "my.custom.io", - Resource: "things", + testCases := map[string]struct { + verb string + resource schema.GroupResource + }{ + "existing verb": { + verb: "use", + resource: schema.GroupResource{Group: "my.custom.io", Resource: "one"}, + }, + "new verb": { + verb: "new", + resource: schema.GroupResource{Group: "my.custom.io", Resource: "two"}, + }, } - AddSpecialVerb("use", resource) + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + AddSpecialVerb(tc.verb, tc.resource) + resources, ok := specialVerbs[tc.verb] + if !ok { + t.Errorf("missing expected verb: %s", tc.verb) + } - found := false - resources, ok := specialVerbs["use"] - if !ok { - t.Errorf("expected resources for verb: use, found none\n") - } - - for _, res := range resources { - if reflect.DeepEqual(resource, res) { - found = true - break - } - } - - if !found { - t.Errorf("expected resource:\n%v\nnot found", resource) + for _, res := range resources { + if reflect.DeepEqual(tc.resource, res) { + return + } + } + t.Errorf("missing expected resource:%#v", tc.resource) + }) } }