@@ -23,6 +23,7 @@ import (
23
23
24
24
authenticationv1 "k8s.io/api/authentication/v1"
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
+ "k8s.io/apimachinery/pkg/types"
26
27
"k8s.io/apimachinery/pkg/util/clock"
27
28
)
28
29
@@ -175,6 +176,189 @@ func TestRequiresRefresh(t *testing.T) {
175
176
}
176
177
}
177
178
179
+ func TestDeleteServiceAccountToken (t * testing.T ) {
180
+ type request struct {
181
+ name , namespace string
182
+ tr authenticationv1.TokenRequest
183
+ shouldFail bool
184
+ }
185
+
186
+ cases := []struct {
187
+ name string
188
+ requestIndex []int
189
+ deletePodUID []types.UID
190
+ expLeftIndex []int
191
+ }{
192
+ {
193
+ name : "delete none with all success requests" ,
194
+ requestIndex : []int {0 , 1 , 2 },
195
+ expLeftIndex : []int {0 , 1 , 2 },
196
+ },
197
+ {
198
+ name : "delete one with all success requests" ,
199
+ requestIndex : []int {0 , 1 , 2 },
200
+ deletePodUID : []types.UID {"fake-uid-1" },
201
+ expLeftIndex : []int {1 , 2 },
202
+ },
203
+ {
204
+ name : "delete two with all success requests" ,
205
+ requestIndex : []int {0 , 1 , 2 },
206
+ deletePodUID : []types.UID {"fake-uid-1" , "fake-uid-3" },
207
+ expLeftIndex : []int {1 },
208
+ },
209
+ {
210
+ name : "delete all with all suceess requests" ,
211
+ requestIndex : []int {0 , 1 , 2 },
212
+ deletePodUID : []types.UID {"fake-uid-1" , "fake-uid-2" , "fake-uid-3" },
213
+ },
214
+ {
215
+ name : "delete no pod with failed requests" ,
216
+ requestIndex : []int {0 , 1 , 2 , 3 },
217
+ deletePodUID : []types.UID {},
218
+ expLeftIndex : []int {0 , 1 , 2 },
219
+ },
220
+ {
221
+ name : "delete other pod with failed requests" ,
222
+ requestIndex : []int {0 , 1 , 2 , 3 },
223
+ deletePodUID : []types.UID {"fake-uid-2" },
224
+ expLeftIndex : []int {0 , 2 },
225
+ },
226
+ {
227
+ name : "delete no pod with request which success after failure" ,
228
+ requestIndex : []int {0 , 1 , 2 , 3 , 4 },
229
+ deletePodUID : []types.UID {},
230
+ expLeftIndex : []int {0 , 1 , 2 , 4 },
231
+ },
232
+ {
233
+ name : "delete the pod which success after failure" ,
234
+ requestIndex : []int {0 , 1 , 2 , 3 , 4 },
235
+ deletePodUID : []types.UID {"fake-uid-4" },
236
+ expLeftIndex : []int {0 , 1 , 2 },
237
+ },
238
+ {
239
+ name : "delete other pod with request which success after failure" ,
240
+ requestIndex : []int {0 , 1 , 2 , 3 , 4 },
241
+ deletePodUID : []types.UID {"fake-uid-1" },
242
+ expLeftIndex : []int {1 , 2 , 4 },
243
+ },
244
+ {
245
+ name : "delete some pod not in the set" ,
246
+ requestIndex : []int {0 , 1 , 2 },
247
+ deletePodUID : []types.UID {"fake-uid-100" , "fake-uid-200" },
248
+ expLeftIndex : []int {0 , 1 , 2 },
249
+ },
250
+ }
251
+
252
+ for _ , c := range cases {
253
+ t .Run (c .name , func (t * testing.T ) {
254
+ requests := []request {
255
+ {
256
+ name : "fake-name-1" ,
257
+ namespace : "fake-namespace-1" ,
258
+ tr : authenticationv1.TokenRequest {
259
+ Spec : authenticationv1.TokenRequestSpec {
260
+ BoundObjectRef : & authenticationv1.BoundObjectReference {
261
+ UID : "fake-uid-1" ,
262
+ Name : "fake-name-1" ,
263
+ },
264
+ },
265
+ },
266
+ shouldFail : false ,
267
+ },
268
+ {
269
+ name : "fake-name-2" ,
270
+ namespace : "fake-namespace-2" ,
271
+ tr : authenticationv1.TokenRequest {
272
+ Spec : authenticationv1.TokenRequestSpec {
273
+ BoundObjectRef : & authenticationv1.BoundObjectReference {
274
+ UID : "fake-uid-2" ,
275
+ Name : "fake-name-2" ,
276
+ },
277
+ },
278
+ },
279
+ shouldFail : false ,
280
+ },
281
+ {
282
+ name : "fake-name-3" ,
283
+ namespace : "fake-namespace-3" ,
284
+ tr : authenticationv1.TokenRequest {
285
+ Spec : authenticationv1.TokenRequestSpec {
286
+ BoundObjectRef : & authenticationv1.BoundObjectReference {
287
+ UID : "fake-uid-3" ,
288
+ Name : "fake-name-3" ,
289
+ },
290
+ },
291
+ },
292
+ shouldFail : false ,
293
+ },
294
+ {
295
+ name : "fake-name-4" ,
296
+ namespace : "fake-namespace-4" ,
297
+ tr : authenticationv1.TokenRequest {
298
+ Spec : authenticationv1.TokenRequestSpec {
299
+ BoundObjectRef : & authenticationv1.BoundObjectReference {
300
+ UID : "fake-uid-4" ,
301
+ Name : "fake-name-4" ,
302
+ },
303
+ },
304
+ },
305
+ shouldFail : true ,
306
+ },
307
+ {
308
+ //exactly the same with last one, besides it will success
309
+ name : "fake-name-4" ,
310
+ namespace : "fake-namespace-4" ,
311
+ tr : authenticationv1.TokenRequest {
312
+ Spec : authenticationv1.TokenRequestSpec {
313
+ BoundObjectRef : & authenticationv1.BoundObjectReference {
314
+ UID : "fake-uid-4" ,
315
+ Name : "fake-name-4" ,
316
+ },
317
+ },
318
+ },
319
+ shouldFail : false ,
320
+ },
321
+ }
322
+ testMgr := NewManager (nil )
323
+ testMgr .clock = clock .NewFakeClock (time.Time {}.Add (30 * 24 * time .Hour ))
324
+
325
+ successGetToken := func (_ , _ string , tr * authenticationv1.TokenRequest ) (* authenticationv1.TokenRequest , error ) {
326
+ tr .Status = authenticationv1.TokenRequestStatus {
327
+ ExpirationTimestamp : metav1.Time {Time : testMgr .clock .Now ().Add (10 * time .Hour )},
328
+ }
329
+ return tr , nil
330
+ }
331
+ failGetToken := func (_ , _ string , tr * authenticationv1.TokenRequest ) (* authenticationv1.TokenRequest , error ) {
332
+ return nil , fmt .Errorf ("fail tr" )
333
+ }
334
+
335
+ for _ , index := range c .requestIndex {
336
+ req := requests [index ]
337
+ if req .shouldFail {
338
+ testMgr .getToken = failGetToken
339
+ } else {
340
+ testMgr .getToken = successGetToken
341
+ }
342
+ testMgr .GetServiceAccountToken (req .namespace , req .name , & req .tr )
343
+ }
344
+
345
+ for _ , uid := range c .deletePodUID {
346
+ testMgr .DeleteServiceAccountToken (uid )
347
+ }
348
+ if len (c .expLeftIndex ) != len (testMgr .cache ) {
349
+ t .Errorf ("%s got unexpected result: expected left cache size is %d, got %d" , c .name , len (c .expLeftIndex ), len (testMgr .cache ))
350
+ }
351
+ for _ , leftIndex := range c .expLeftIndex {
352
+ r := requests [leftIndex ]
353
+ _ , ok := testMgr .get (keyFunc (r .name , r .namespace , & r .tr ))
354
+ if ! ok {
355
+ t .Errorf ("%s got unexpected result: expected token request %v exist in cache, but not" , c .name , r )
356
+ }
357
+ }
358
+ })
359
+ }
360
+ }
361
+
178
362
type fakeTokenGetter struct {
179
363
count int
180
364
tr * authenticationv1.TokenRequest
0 commit comments