@@ -28,6 +28,8 @@ func TestResourceResolver_Resolve(t *testing.T) {
28
28
podsGR := schema.GroupResource {Resource : "pods" }
29
29
deploymentsGVR := schema.GroupVersionResource {Group : "extensions" , Version : "v1beta1" , Resource : "deployments" }
30
30
deploymentsGR := schema.GroupResource {Group : "extensions" , Resource : "deployments" }
31
+ pspGVR := schema.GroupVersionResource {Group : "policy" , Version : "v1beta1" , Resource : "podsecuritypolicies" }
32
+ pspGV := schema.GroupResource {Group : "policy" , Resource : "podsecuritypolicies" }
31
33
32
34
client := fake .NewSimpleClientset ()
33
35
@@ -47,6 +49,12 @@ func TestResourceResolver_Resolve(t *testing.T) {
47
49
{Group : "extensions" , Version : "v1beta1" , Name : "deployments/scale" , Verbs : []string {"update" , "patch" }},
48
50
},
49
51
},
52
+ {
53
+ GroupVersion : "policy/v1beta1" ,
54
+ APIResources : []apismeta.APIResource {
55
+ {Group : "policy" , Version : "v1beta1" , Name : "podsecuritypolicies" , Verbs : []string {"list" , "get" }},
56
+ },
57
+ },
50
58
}
51
59
52
60
type mappingResult struct {
@@ -56,111 +64,125 @@ func TestResourceResolver_Resolve(t *testing.T) {
56
64
returnError error
57
65
}
58
66
59
- type expected struct {
60
- gr schema.GroupResource
61
- err error
62
- }
63
-
64
- data := []struct {
65
- scenario string
67
+ testCases := []struct {
68
+ name string
66
69
action Action
67
70
mappingResult * mappingResult
68
- expected
71
+ expectedGR schema.GroupResource
72
+ expectedError error
69
73
}{
70
74
{
71
- scenario : "A" ,
72
- action : Action {Verb : "list" , Resource : "pods" },
75
+ name : "A" ,
76
+ action : Action {Verb : "list" , Resource : "pods" },
73
77
mappingResult : & mappingResult {
74
78
argGVR : schema.GroupVersionResource {Resource : "pods" },
75
79
returnGVR : podsGVR ,
76
80
},
77
- expected : expected { gr : podsGR } ,
81
+ expectedGR : podsGR ,
78
82
},
79
83
{
80
- scenario : "B" ,
81
- action : Action {Verb : "list" , Resource : "po" },
84
+ name : "B" ,
85
+ action : Action {Verb : "list" , Resource : "po" },
82
86
mappingResult : & mappingResult {
83
87
argGVR : schema.GroupVersionResource {Resource : "po" },
84
88
returnGVR : podsGVR ,
85
89
},
86
- expected : expected { gr : podsGR } ,
90
+ expectedGR : podsGR ,
87
91
},
88
92
{
89
- scenario : "C" ,
90
- action : Action {Verb : "eat" , Resource : "pods" },
93
+ name : "C" ,
94
+ action : Action {Verb : "eat" , Resource : "pods" },
91
95
mappingResult : & mappingResult {
92
96
argGVR : schema.GroupVersionResource {Resource : "pods" },
93
97
returnGVR : podsGVR ,
94
98
},
95
- expected : expected { err : errors .New ("the \" pods\" resource does not support the \" eat\" verb, only [list create delete]" )} ,
99
+ expectedError : errors .New ("the \" pods\" resource does not support the \" eat\" verb, only [list create delete]" ),
96
100
},
97
101
{
98
- scenario : "D" ,
99
- action : Action {Verb : "list" , Resource : "deployments.extensions" },
102
+ name : "D" ,
103
+ action : Action {Verb : "list" , Resource : "deployments.extensions" },
100
104
mappingResult : & mappingResult {
101
105
argGVR : schema.GroupVersionResource {Group : "extensions" , Version : "" , Resource : "deployments" },
102
106
returnGVR : deploymentsGVR ,
103
107
},
104
- expected : expected { gr : deploymentsGR } ,
108
+ expectedGR : deploymentsGR ,
105
109
},
106
110
{
107
- scenario : "E" ,
108
- action : Action {Verb : "get" , Resource : "pods" , SubResource : "log" },
111
+ name : "E" ,
112
+ action : Action {Verb : "get" , Resource : "pods" , SubResource : "log" },
109
113
mappingResult : & mappingResult {
110
114
argGVR : schema.GroupVersionResource {Resource : "pods" },
111
115
returnGVR : podsGVR ,
112
116
},
113
- expected : expected { gr : podsGR } ,
117
+ expectedGR : podsGR ,
114
118
},
115
119
{
116
- scenario : "F" ,
117
- action : Action {Verb : "get" , Resource : "pods" , SubResource : "logz" },
120
+ name : "F" ,
121
+ action : Action {Verb : "get" , Resource : "pods" , SubResource : "logz" },
118
122
mappingResult : & mappingResult {
119
123
argGVR : schema.GroupVersionResource {Resource : "pods" },
120
124
returnGVR : podsGVR ,
121
125
},
122
- expected : expected { err : errors .New ("the server doesn't have a resource type \" pods/logz\" " )} ,
126
+ expectedError : errors .New ("the server doesn't have a resource type \" pods/logz\" " ),
123
127
},
124
128
{
125
- scenario : "G" ,
126
- action : Action {Verb : "list" , Resource : "bees" },
129
+ name : "G" ,
130
+ action : Action {Verb : "list" , Resource : "bees" },
127
131
mappingResult : & mappingResult {
128
132
argGVR : schema.GroupVersionResource {Resource : "bees" },
129
133
returnError : errors .New ("mapping failed" ),
130
134
},
131
- expected : expected { err : errors .New ("the server doesn't have a resource type \" bees\" " )} ,
135
+ expectedError : errors .New ("the server doesn't have a resource type \" bees\" " ),
132
136
},
133
137
{
134
- scenario : "H" ,
135
- action : Action {Verb : rbac .VerbAll , Resource : "pods" },
138
+ name : "H" ,
139
+ action : Action {Verb : rbac .VerbAll , Resource : "pods" },
136
140
mappingResult : & mappingResult {
137
141
argGVR : schema.GroupVersionResource {Resource : "pods" },
138
142
returnGVR : podsGVR ,
139
143
},
140
- expected : expected {gr : podsGR },
144
+ expectedGR : podsGR ,
145
+ },
146
+ {
147
+ name : "I" ,
148
+ action : Action {Verb : "list" , Resource : rbac .ResourceAll },
149
+ expectedGR : schema.GroupResource {Resource : rbac .ResourceAll },
150
+ },
151
+ {
152
+ name : "Should resolve psp" ,
153
+ action : Action {Verb : "use" , Resource : "psp" },
154
+ mappingResult : & mappingResult {
155
+ argGVR : schema.GroupVersionResource {Resource : "psp" },
156
+ returnGVR : pspGVR ,
157
+ },
158
+ expectedGR : pspGV ,
141
159
},
142
160
{
143
- scenario : "I" ,
144
- action : Action {Verb : "list" , Resource : rbac .ResourceAll },
145
- expected : expected {gr : schema.GroupResource {Resource : rbac .ResourceAll }},
161
+ name : "Should return error when psp verb is not supported" ,
162
+ action : Action {Verb : "cook" , Resource : "psp" },
163
+ mappingResult : & mappingResult {
164
+ argGVR : schema.GroupVersionResource {Resource : "psp" },
165
+ returnGVR : pspGVR ,
166
+ },
167
+ expectedError : errors .New ("the \" podsecuritypolicies\" resource does not support the \" cook\" verb, only [list get]" ),
146
168
},
147
169
}
148
170
149
- for _ , tt := range data {
150
- t .Run (tt . scenario , func (t * testing.T ) {
171
+ for _ , tc := range testCases {
172
+ t .Run (tc . name , func (t * testing.T ) {
151
173
mapper := new (mapperMock )
152
174
153
- if tt .mappingResult != nil {
154
- mapper .On ("ResourceFor" , tt .mappingResult .argGVR ).
155
- Return (tt .mappingResult .returnGVR , tt .mappingResult .returnError )
175
+ if tc .mappingResult != nil {
176
+ mapper .On ("ResourceFor" , tc .mappingResult .argGVR ).
177
+ Return (tc .mappingResult .returnGVR , tc .mappingResult .returnError )
156
178
}
157
179
158
180
resolver := NewResourceResolver (client .Discovery (), mapper )
159
181
160
- resource , err := resolver .Resolve (tt .action .Verb , tt .action .Resource , tt .action .SubResource )
182
+ resource , err := resolver .Resolve (tc .action .Verb , tc .action .Resource , tc .action .SubResource )
161
183
162
- assert .Equal (t , tt . expected . err , err )
163
- assert .Equal (t , tt . expected . gr , resource )
184
+ assert .Equal (t , tc . expectedError , err )
185
+ assert .Equal (t , tc . expectedGR , resource )
164
186
165
187
mapper .AssertExpectations (t )
166
188
})
0 commit comments