@@ -199,14 +199,16 @@ func Test_pulledRecordMergeNewCreds(t *testing.T) {
199
199
200
200
func TestFileBasedImagePullManager_MustAttemptImagePull (t * testing.T ) {
201
201
tests := []struct {
202
- name string
203
- imagePullPolicy ImagePullPolicyEnforcer
204
- podSecrets []kubeletconfiginternal.ImagePullSecret
205
- image string
206
- imageRef string
207
- pulledFiles []string
208
- pullingFiles []string
209
- want bool
202
+ name string
203
+ imagePullPolicy ImagePullPolicyEnforcer
204
+ podSecrets []kubeletconfiginternal.ImagePullSecret
205
+ image string
206
+ imageRef string
207
+ pulledFiles []string
208
+ pullingFiles []string
209
+ expectedPullRecord * kubeletconfiginternal.ImagePulledRecord
210
+ want bool
211
+ expectedCacheWrite bool
210
212
}{
211
213
{
212
214
name : "image exists and is recorded with pod's exact secret" ,
@@ -240,7 +242,18 @@ func TestFileBasedImagePullManager_MustAttemptImagePull(t *testing.T) {
240
242
image : "docker.io/testing/test:latest" ,
241
243
imageRef : "testimageref" ,
242
244
pulledFiles : []string {"sha256-b3c0cc4278800b03a308ceb2611161430df571ca733122f0a40ac8b9792a9064" },
243
- want : false ,
245
+ expectedPullRecord : & kubeletconfiginternal.ImagePulledRecord {
246
+ ImageRef : "testimageref" ,
247
+ CredentialMapping : map [string ]kubeletconfiginternal.ImagePullCredentials {
248
+ "docker.io/testing/test" : {
249
+ KubernetesSecrets : []kubeletconfiginternal.ImagePullSecret {
250
+ {UID : "testsecretuid" , Namespace : "default" , Name : "pull-secret" , CredentialHash : "differenthash" },
251
+ },
252
+ },
253
+ },
254
+ },
255
+ want : false ,
256
+ expectedCacheWrite : true ,
244
257
},
245
258
{
246
259
name : "image exists and is recorded with a different secret with a different UID" ,
@@ -279,7 +292,19 @@ func TestFileBasedImagePullManager_MustAttemptImagePull(t *testing.T) {
279
292
image : "docker.io/testing/test:latest" ,
280
293
imageRef : "testimageref" ,
281
294
pulledFiles : []string {"sha256-b3c0cc4278800b03a308ceb2611161430df571ca733122f0a40ac8b9792a9064" },
282
- want : false ,
295
+ expectedPullRecord : & kubeletconfiginternal.ImagePulledRecord {
296
+ ImageRef : "testimageref" ,
297
+ CredentialMapping : map [string ]kubeletconfiginternal.ImagePullCredentials {
298
+ "docker.io/testing/test" : {
299
+ KubernetesSecrets : []kubeletconfiginternal.ImagePullSecret {
300
+ {UID : "testsecretuid" , Namespace : "default" , Name : "pull-secret" , CredentialHash : "testsecrethash" },
301
+ {UID : "testsecretuid" , Namespace : "differentns" , Name : "pull-secret" , CredentialHash : "testsecrethash" },
302
+ },
303
+ },
304
+ },
305
+ },
306
+ want : false ,
307
+ expectedCacheWrite : true ,
283
308
},
284
309
{
285
310
name : "image exists but the pull is recorded with a different image name but with the exact same secret" ,
@@ -409,11 +434,13 @@ func TestFileBasedImagePullManager_MustAttemptImagePull(t *testing.T) {
409
434
copyTestData (t , pullingDir , "pulling" , tt .pullingFiles )
410
435
copyTestData (t , pulledDir , "pulled" , tt .pulledFiles )
411
436
412
- fsRecordAccessor := & fsPullRecordsAccessor {
413
- pullingDir : pullingDir ,
414
- pulledDir : pulledDir ,
415
- encoder : encoder ,
416
- decoder : decoder ,
437
+ fsRecordAccessor := & testWriteCountingFSPullRecordsAccessor {
438
+ fsPullRecordsAccessor : fsPullRecordsAccessor {
439
+ pullingDir : pullingDir ,
440
+ pulledDir : pulledDir ,
441
+ encoder : encoder ,
442
+ decoder : decoder ,
443
+ },
417
444
}
418
445
419
446
f := & PullManager {
@@ -426,10 +453,37 @@ func TestFileBasedImagePullManager_MustAttemptImagePull(t *testing.T) {
426
453
if got := f .MustAttemptImagePull (tt .image , tt .imageRef , tt .podSecrets ); got != tt .want {
427
454
t .Errorf ("FileBasedImagePullManager.MustAttemptImagePull() = %v, want %v" , got , tt .want )
428
455
}
456
+
457
+ if tt .expectedCacheWrite != (fsRecordAccessor .imagePulledRecordsWrites != 0 ) {
458
+ t .Errorf ("expected zero cache writes, got: %v" , fsRecordAccessor .imagePulledRecordsWrites )
459
+ }
460
+
461
+ if tt .expectedPullRecord != nil {
462
+ got , found , err := fsRecordAccessor .GetImagePulledRecord (tt .imageRef )
463
+ if err != nil && ! found {
464
+ t .Fatalf ("failed to get an expected ImagePulledRecord" )
465
+ }
466
+ got .LastUpdatedTime = tt .expectedPullRecord .LastUpdatedTime
467
+
468
+ if ! reflect .DeepEqual (got , tt .expectedPullRecord ) {
469
+ t .Errorf ("expected ImagePulledRecord != got; diff: %s" , cmp .Diff (tt .expectedPullRecord , got ))
470
+ }
471
+ }
429
472
})
430
473
}
431
474
}
432
475
476
+ type testWriteCountingFSPullRecordsAccessor struct {
477
+ imagePulledRecordsWrites int
478
+
479
+ fsPullRecordsAccessor
480
+ }
481
+
482
+ func (a * testWriteCountingFSPullRecordsAccessor ) WriteImagePulledRecord (pulledRecord * kubeletconfiginternal.ImagePulledRecord ) error {
483
+ a .imagePulledRecordsWrites += 1
484
+ return a .fsPullRecordsAccessor .WriteImagePulledRecord (pulledRecord )
485
+ }
486
+
433
487
func TestFileBasedImagePullManager_RecordPullIntent (t * testing.T ) {
434
488
tests := []struct {
435
489
name string
0 commit comments