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.
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

View File

@ -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)
})
}
}