@@ -20,6 +20,9 @@ import (
20
20
"fmt"
21
21
"testing"
22
22
23
+ "github.com/stretchr/testify/assert"
24
+ "github.com/stretchr/testify/require"
25
+
23
26
v1 "k8s.io/api/core/v1"
24
27
resourceapi "k8s.io/api/resource/v1alpha3"
25
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -112,15 +115,219 @@ func TestResourceClaimIsForPod(t *testing.T) {
112
115
t .Run (name , func (t * testing.T ) {
113
116
err := IsForPod (tc .pod , tc .claim )
114
117
if tc .expectedError == "" {
115
- if err != nil {
116
- t .Errorf ("expected no error, got %v" , err )
117
- }
118
+ require .NoError (t , err )
119
+ } else {
120
+ require .Error (t , err )
121
+ require .EqualError (t , err , tc .expectedError )
122
+ }
123
+ })
124
+ }
125
+ }
126
+
127
+ func TestIsReservedForPod (t * testing.T ) {
128
+ uid := 0
129
+ newUID := func () types.UID {
130
+ uid ++
131
+ return types .UID (fmt .Sprintf ("%d" , uid ))
132
+ }
133
+
134
+ podNotReserved := & v1.Pod {
135
+ ObjectMeta : metav1.ObjectMeta {
136
+ Namespace : "kube-system" ,
137
+ Name : "podNotReserved" ,
138
+ UID : newUID (),
139
+ },
140
+ }
141
+ podReserved := & v1.Pod {
142
+ ObjectMeta : metav1.ObjectMeta {
143
+ Namespace : "kube-system" ,
144
+ Name : "podReserved" ,
145
+ UID : newUID (),
146
+ },
147
+ }
148
+ podOtherReserved := & v1.Pod {
149
+ ObjectMeta : metav1.ObjectMeta {
150
+ Namespace : "kube-system" ,
151
+ Name : "podOtherReserved" ,
152
+ UID : newUID (),
153
+ },
154
+ }
155
+
156
+ claimNoReservation := & resourceapi.ResourceClaim {
157
+ ObjectMeta : metav1.ObjectMeta {
158
+ Namespace : "kube-system" ,
159
+ Name : "claimNoReservation" ,
160
+ UID : newUID (),
161
+ },
162
+ Status : resourceapi.ResourceClaimStatus {},
163
+ }
164
+
165
+ claimWithReservation := & resourceapi.ResourceClaim {
166
+ ObjectMeta : metav1.ObjectMeta {
167
+ Namespace : "kube-system" ,
168
+ Name : "claimWithReservation" ,
169
+ UID : newUID (),
170
+ },
171
+ Status : resourceapi.ResourceClaimStatus {
172
+ ReservedFor : []resourceapi.ResourceClaimConsumerReference {
173
+ {
174
+ UID : podReserved .UID ,
175
+ },
176
+ },
177
+ },
178
+ }
179
+
180
+ claimWithMultipleReservations := & resourceapi.ResourceClaim {
181
+ ObjectMeta : metav1.ObjectMeta {
182
+ Namespace : "kube-system" ,
183
+ Name : "claimWithMultipleReservations" ,
184
+ UID : newUID (),
185
+ },
186
+ Status : resourceapi.ResourceClaimStatus {
187
+ ReservedFor : []resourceapi.ResourceClaimConsumerReference {
188
+ {
189
+ UID : podReserved .UID ,
190
+ },
191
+ {
192
+ UID : podOtherReserved .UID ,
193
+ },
194
+ },
195
+ },
196
+ }
197
+
198
+ testcases := map [string ]struct {
199
+ pod * v1.Pod
200
+ claim * resourceapi.ResourceClaim
201
+ expectedResult bool
202
+ }{
203
+ "not-reserved" : {
204
+ pod : podNotReserved ,
205
+ claim : claimNoReservation ,
206
+ expectedResult : false ,
207
+ },
208
+ "reserved-for-pod" : {
209
+ pod : podReserved ,
210
+ claim : claimWithReservation ,
211
+ expectedResult : true ,
212
+ },
213
+ "reserved-for-other-pod" : {
214
+ pod : podNotReserved ,
215
+ claim : claimWithReservation ,
216
+ expectedResult : false ,
217
+ },
218
+ "multiple-reservations-including-pod" : {
219
+ pod : podReserved ,
220
+ claim : claimWithMultipleReservations ,
221
+ expectedResult : true ,
222
+ },
223
+ "multiple-reservations-excluding-pod" : {
224
+ pod : podNotReserved ,
225
+ claim : claimWithMultipleReservations ,
226
+ expectedResult : false ,
227
+ },
228
+ }
229
+
230
+ for name , tc := range testcases {
231
+ t .Run (name , func (t * testing.T ) {
232
+ result := IsReservedForPod (tc .pod , tc .claim )
233
+ assert .Equal (t , tc .expectedResult , result )
234
+ })
235
+ }
236
+ }
237
+
238
+ func TestName (t * testing.T ) {
239
+ testcases := map [string ]struct {
240
+ pod * v1.Pod
241
+ podClaim * v1.PodResourceClaim
242
+ expectedName * string
243
+ expectedCheck bool
244
+ expectedError error
245
+ }{
246
+ "resource-claim-name-set" : {
247
+ pod : & v1.Pod {
248
+ ObjectMeta : metav1.ObjectMeta {
249
+ Namespace : "default" ,
250
+ Name : "test-pod" ,
251
+ },
252
+ },
253
+ podClaim : & v1.PodResourceClaim {
254
+ ResourceClaimName : func () * string { s := "existing-claim" ; return & s }(),
255
+ },
256
+ expectedName : func () * string { s := "existing-claim" ; return & s }(),
257
+ expectedCheck : false ,
258
+ expectedError : nil ,
259
+ },
260
+ "resource-claim-template-name-set-and-status-found" : {
261
+ pod : & v1.Pod {
262
+ ObjectMeta : metav1.ObjectMeta {
263
+ Namespace : "default" ,
264
+ Name : "test-pod" ,
265
+ },
266
+ Status : v1.PodStatus {
267
+ ResourceClaimStatuses : []v1.PodResourceClaimStatus {
268
+ {
269
+ Name : "template-claim" ,
270
+ ResourceClaimName : func () * string { s := "created-claim" ; return & s }(),
271
+ },
272
+ },
273
+ },
274
+ },
275
+ podClaim : & v1.PodResourceClaim {
276
+ Name : "template-claim" ,
277
+ ResourceClaimTemplateName : func () * string { s := "template-claim-template" ; return & s }(),
278
+ },
279
+ expectedName : func () * string { s := "created-claim" ; return & s }(),
280
+ expectedCheck : true ,
281
+ expectedError : nil ,
282
+ },
283
+ "resource-claim-template-name-set-but-status-not-found" : {
284
+ pod : & v1.Pod {
285
+ ObjectMeta : metav1.ObjectMeta {
286
+ Namespace : "default" ,
287
+ Name : "test-pod" ,
288
+ },
289
+ Status : v1.PodStatus {
290
+ ResourceClaimStatuses : []v1.PodResourceClaimStatus {
291
+ {
292
+ Name : "other-claim" ,
293
+ ResourceClaimName : func () * string { s := "other-created-claim" ; return & s }(),
294
+ },
295
+ },
296
+ },
297
+ },
298
+ podClaim : & v1.PodResourceClaim {
299
+ Name : "template-claim" ,
300
+ ResourceClaimTemplateName : func () * string { s := "template-claim-template" ; return & s }(),
301
+ },
302
+ expectedName : nil ,
303
+ expectedCheck : false ,
304
+ expectedError : fmt .Errorf (`pod "default/test-pod": %w` , ErrClaimNotFound ),
305
+ },
306
+ "neither-resource-claim-name-nor-template-name-set" : {
307
+ pod : & v1.Pod {
308
+ ObjectMeta : metav1.ObjectMeta {
309
+ Namespace : "default" ,
310
+ Name : "test-pod" ,
311
+ },
312
+ },
313
+ podClaim : & v1.PodResourceClaim {
314
+ Name : "invalid-claim" ,
315
+ },
316
+ expectedName : nil ,
317
+ expectedCheck : false ,
318
+ expectedError : fmt .Errorf (`pod "default/test-pod", spec.resourceClaim "invalid-claim": %w` , ErrAPIUnsupported ),
319
+ },
320
+ }
321
+
322
+ for name , tc := range testcases {
323
+ t .Run (name , func (t * testing.T ) {
324
+ name , check , err := Name (tc .pod , tc .podClaim )
325
+ if tc .expectedError == nil {
326
+ require .NoError (t , err )
327
+ assert .Equal (t , tc .expectedName , name )
328
+ assert .Equal (t , tc .expectedCheck , check )
118
329
} else {
119
- if err == nil {
120
- t .Errorf ("expected error %q, got nil" , tc .expectedError )
121
- } else if tc .expectedError != err .Error () {
122
- t .Errorf ("expected error %q, got %v" , tc .expectedError , err )
123
- }
330
+ require .EqualError (t , err , tc .expectedError .Error ())
124
331
}
125
332
})
126
333
}
0 commit comments