Allow adding new verbs in AddSpecialVerb

This commit is contained in:
Maciej Szulik 2022-03-28 12:49:02 +02:00
parent d58f42961c
commit 790396caba
No known key found for this signature in database
GPG Key ID: F15E55D276FA84C4
2 changed files with 31 additions and 22 deletions

View File

@ -114,10 +114,12 @@ var (
// AddSpecialVerb allows the addition of items to the `specialVerbs` map for non-k8s native resources. // AddSpecialVerb allows the addition of items to the `specialVerbs` map for non-k8s native resources.
func AddSpecialVerb(verb string, gr schema.GroupResource) { func AddSpecialVerb(verb string, gr schema.GroupResource) {
if resources, ok := specialVerbs[verb]; ok { resources, ok := specialVerbs[verb]
if !ok {
resources = make([]schema.GroupResource, 1)
}
resources = append(resources, gr) resources = append(resources, gr)
specialVerbs[verb] = resources specialVerbs[verb] = resources
}
} }
// ResourceOptions holds the related options for '--resource' option // ResourceOptions holds the related options for '--resource' option

View File

@ -680,27 +680,34 @@ func TestComplete(t *testing.T) {
} }
func TestAddSpecialVerb(t *testing.T) { func TestAddSpecialVerb(t *testing.T) {
resource := schema.GroupResource{ testCases := map[string]struct {
Group: "my.custom.io", verb string
Resource: "things", 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) {
found := false AddSpecialVerb(tc.verb, tc.resource)
resources, ok := specialVerbs["use"] resources, ok := specialVerbs[tc.verb]
if !ok { if !ok {
t.Errorf("expected resources for verb: use, found none\n") t.Errorf("missing expected verb: %s", tc.verb)
} }
for _, res := range resources { for _, res := range resources {
if reflect.DeepEqual(resource, res) { if reflect.DeepEqual(tc.resource, res) {
found = true return
break
} }
} }
t.Errorf("missing expected resource:%#v", tc.resource)
if !found { })
t.Errorf("expected resource:\n%v\nnot found", resource)
} }
} }