fix: fix miss makezero bug (#125132)

* fix: fix miss makezero bug

Signed-off-by: alingse <alingse@foxmail.com>

* add testcase for new verb

* Update create_role_test.go

---------

Signed-off-by: alingse <alingse@foxmail.com>

Kubernetes-commit: 5b06498cb24ee68dbc2815a1d9fa505da3452d98
This commit is contained in:
alingse 2024-12-12 10:56:37 +08:00 committed by Kubernetes Publisher
parent beeec3d7a0
commit 92bb3cdeda
2 changed files with 14 additions and 1 deletions

View File

@ -116,7 +116,7 @@ var (
func AddSpecialVerb(verb string, gr schema.GroupResource) {
resources, ok := specialVerbs[verb]
if !ok {
resources = make([]schema.GroupResource, 1)
resources = make([]schema.GroupResource, 0, 1)
}
resources = append(resources, gr)
specialVerbs[verb] = resources

View File

@ -684,14 +684,17 @@ func TestAddSpecialVerb(t *testing.T) {
testCases := map[string]struct {
verb string
resource schema.GroupResource
isNew bool
}{
"existing verb": {
verb: "use",
resource: schema.GroupResource{Group: "my.custom.io", Resource: "one"},
isNew: false,
},
"new verb": {
verb: "new",
resource: schema.GroupResource{Group: "my.custom.io", Resource: "two"},
isNew: true,
},
}
@ -703,6 +706,16 @@ func TestAddSpecialVerb(t *testing.T) {
t.Errorf("missing expected verb: %s", tc.verb)
}
if tc.isNew {
if len(resources) != 1 {
t.Errorf("new verb should only contain one resource resources:%#v", resources)
}
if !reflect.DeepEqual(tc.resource, resources[0]) {
t.Errorf("miss expected resource:%#v", tc.resource)
}
return
}
for _, res := range resources {
if reflect.DeepEqual(tc.resource, res) {
return