@@ -164,7 +164,7 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
164
164
assertArtifact : & sourcev1.Artifact {
165
165
Path : "/reconcile-storage/c.txt" ,
166
166
Revision : "c" ,
167
- Checksum : "84a516841ba77a5b4648de2cd0dfcb30ea46dbb4 " ,
167
+ Checksum : "2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6 " ,
168
168
URL : testStorage .Hostname + "/reconcile-storage/c.txt" ,
169
169
},
170
170
assertPaths : []string {
@@ -197,7 +197,7 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
197
197
obj .Status .Artifact = & sourcev1.Artifact {
198
198
Path : fmt .Sprintf ("/reconcile-storage/hostname.txt" ),
199
199
Revision : "f" ,
200
- Checksum : "971c419dd609331343dee105fffd0f4608dc0bf2 " ,
200
+ Checksum : "3b9c358f36f0a31b6ad3e14f309c7cf198ac9246e8316f9ce543d5b19ac02b80 " ,
201
201
URL : "http://outdated.com/reconcile-storage/hostname.txt" ,
202
202
}
203
203
if err := testStorage .MkdirAll (* obj .Status .Artifact ); err != nil {
@@ -214,7 +214,7 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
214
214
assertArtifact : & sourcev1.Artifact {
215
215
Path : "/reconcile-storage/hostname.txt" ,
216
216
Revision : "f" ,
217
- Checksum : "971c419dd609331343dee105fffd0f4608dc0bf2 " ,
217
+ Checksum : "3b9c358f36f0a31b6ad3e14f309c7cf198ac9246e8316f9ce543d5b19ac02b80 " ,
218
218
URL : testStorage .Hostname + "/reconcile-storage/hostname.txt" ,
219
219
},
220
220
},
@@ -441,57 +441,113 @@ func TestBucketReconciler_reconcileSource(t *testing.T) {
441
441
}
442
442
443
443
func TestBucketReconciler_reconcileArtifact (t * testing.T ) {
444
+ // testChecksum is the checksum value of the artifacts created in this
445
+ // test.
446
+ const testChecksum = "4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1"
447
+
444
448
tests := []struct {
445
449
name string
446
- artifact sourcev1.Artifact
447
- beforeFunc func (obj * sourcev1. Bucket , artifact sourcev1.Artifact , dir string )
450
+ beforeFunc func ( t * WithT , obj * sourcev1.Bucket , artifact sourcev1. Artifact , dir string )
451
+ afterFunc func (t * WithT , obj * sourcev1.Bucket , dir string )
448
452
want ctrl.Result
449
453
wantErr bool
450
454
assertConditions []metav1.Condition
451
455
}{
452
456
{
453
- name : "artifact revision up-to-date" ,
454
- artifact : sourcev1.Artifact {
455
- Revision : "existing" ,
457
+ name : "Archiving artifact to storage makes Ready=True" ,
458
+ beforeFunc : func (t * WithT , obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
459
+ obj .Spec .Interval = metav1.Duration {Duration : interval }
460
+ },
461
+ want : ctrl.Result {RequeueAfter : interval },
462
+ assertConditions : []metav1.Condition {
463
+ * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "Stored artifact for revision 'existing'" ),
464
+ },
465
+ },
466
+ {
467
+ name : "Up-to-date artifact should not update status" ,
468
+ beforeFunc : func (t * WithT , obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
469
+ obj .Spec .Interval = metav1.Duration {Duration : interval }
470
+ obj .Status .Artifact = artifact .DeepCopy ()
471
+ },
472
+ afterFunc : func (t * WithT , obj * sourcev1.Bucket , dir string ) {
473
+ t .Expect (obj .Status .URL ).To (BeEmpty ())
474
+ },
475
+ want : ctrl.Result {RequeueAfter : interval },
476
+ assertConditions : []metav1.Condition {
477
+ * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "Stored artifact for revision 'existing'" ),
478
+ },
479
+ },
480
+ {
481
+ name : "Removes ArtifactUnavailableCondition after creating artifact" ,
482
+ beforeFunc : func (t * WithT , obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
483
+ obj .Spec .Interval = metav1.Duration {Duration : interval }
484
+ conditions .MarkTrue (obj , sourcev1 .ArtifactUnavailableCondition , "Foo" , "" )
485
+ },
486
+ want : ctrl.Result {RequeueAfter : interval },
487
+ assertConditions : []metav1.Condition {
488
+ * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "Stored artifact for revision 'existing'" ),
489
+ },
490
+ },
491
+ {
492
+ name : "Removes ArtifactOutdatedCondition after creating a new artifact" ,
493
+ beforeFunc : func (t * WithT , obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
494
+ obj .Spec .Interval = metav1.Duration {Duration : interval }
495
+ conditions .MarkTrue (obj , sourcev1 .ArtifactOutdatedCondition , "Foo" , "" )
496
+ },
497
+ want : ctrl.Result {RequeueAfter : interval },
498
+ assertConditions : []metav1.Condition {
499
+ * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "Stored artifact for revision 'existing'" ),
500
+ },
501
+ },
502
+ {
503
+ name : "Creates latest symlink to the created artifact" ,
504
+ beforeFunc : func (t * WithT , obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
505
+ obj .Spec .Interval = metav1.Duration {Duration : interval }
456
506
},
457
- beforeFunc : func (obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
458
- obj .Status .Artifact = & artifact
507
+ afterFunc : func (t * WithT , obj * sourcev1.Bucket , dir string ) {
508
+ localPath := testStorage .LocalPath (* obj .GetArtifact ())
509
+ symlinkPath := filepath .Join (filepath .Dir (localPath ), "latest.tar.gz" )
510
+ targetFile , err := os .Readlink (symlinkPath )
511
+ t .Expect (err ).NotTo (HaveOccurred ())
512
+ t .Expect (localPath ).To (Equal (targetFile ))
459
513
},
514
+ want : ctrl.Result {RequeueAfter : interval },
460
515
assertConditions : []metav1.Condition {
461
516
* conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "Stored artifact for revision 'existing'" ),
462
517
},
463
518
},
464
519
{
465
- name : "dir path deleted" ,
466
- beforeFunc : func (obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
467
- _ = os .RemoveAll (dir )
520
+ name : "Dir path deleted" ,
521
+ beforeFunc : func (t * WithT , obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
522
+ t .Expect (os .RemoveAll (dir )).ToNot (HaveOccurred ())
523
+ },
524
+ wantErr : true ,
525
+ },
526
+ {
527
+ name : "Dir path is not a directory" ,
528
+ beforeFunc : func (t * WithT , obj * sourcev1.Bucket , artifact sourcev1.Artifact , dir string ) {
529
+ // Remove the given directory and create a file for the same
530
+ // path.
531
+ t .Expect (os .RemoveAll (dir )).ToNot (HaveOccurred ())
532
+ f , err := os .Create (dir )
533
+ defer f .Close ()
534
+ t .Expect (err ).ToNot (HaveOccurred ())
535
+ },
536
+ afterFunc : func (t * WithT , obj * sourcev1.Bucket , dir string ) {
537
+ t .Expect (os .RemoveAll (dir )).ToNot (HaveOccurred ())
468
538
},
469
539
wantErr : true ,
470
540
},
471
- //{
472
- // name: "dir path empty",
473
- //},
474
- //{
475
- // name: "success",
476
- // artifact: sourcev1.Artifact{
477
- // Revision: "existing",
478
- // },
479
- // beforeFunc: func(obj *sourcev1.Bucket, artifact sourcev1.Artifact, dir string) {
480
- // obj.Status.Artifact = &artifact
481
- // },
482
- // assertConditions: []metav1.Condition{
483
- // *conditions.TrueCondition(sourcev1.ArtifactAvailableCondition, meta.SucceededReason, "Compressed source to artifact with revision 'existing'"),
484
- // },
485
- //},
486
- //{
487
- // name: "symlink",
488
- //},
489
541
}
490
542
491
543
for _ , tt := range tests {
492
544
t .Run (tt .name , func (t * testing.T ) {
493
545
g := NewWithT (t )
494
546
547
+ r := & BucketReconciler {
548
+ Storage : testStorage ,
549
+ }
550
+
495
551
tmpDir , err := os .MkdirTemp ("" , "reconcile-bucket-artifact-" )
496
552
g .Expect (err ).ToNot (HaveOccurred ())
497
553
defer os .RemoveAll (tmpDir )
@@ -501,27 +557,36 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
501
557
Kind : sourcev1 .BucketKind ,
502
558
},
503
559
ObjectMeta : metav1.ObjectMeta {
504
- Name : "test-bucket" ,
560
+ GenerateName : "test-bucket-" ,
561
+ Generation : 1 ,
562
+ Namespace : "default" ,
505
563
},
506
564
Spec : sourcev1.BucketSpec {
507
565
Timeout : & metav1.Duration {Duration : timeout },
508
566
},
509
567
}
510
568
511
- if tt .beforeFunc != nil {
512
- tt .beforeFunc (obj , tt .artifact , tmpDir )
513
- }
569
+ artifact := testStorage .NewArtifactFor (obj .Kind , obj , "existing" , "foo.tar.gz" )
570
+ artifact .Checksum = testChecksum
514
571
515
- r := & BucketReconciler {
516
- Storage : testStorage ,
572
+ if tt . beforeFunc != nil {
573
+ tt . beforeFunc ( g , obj , artifact , tmpDir )
517
574
}
518
575
519
- got , err := r .reconcileArtifact (logr .NewContext (ctx , log.NullLogger {}), obj , tt . artifact , tmpDir )
576
+ got , err := r .reconcileArtifact (logr .NewContext (ctx , log.NullLogger {}), obj , artifact , tmpDir )
520
577
g .Expect (err != nil ).To (Equal (tt .wantErr ))
521
578
g .Expect (got ).To (Equal (tt .want ))
522
579
523
- //g.Expect(artifact).To(MatchArtifact(tt.assertArtifact.DeepCopy()))
580
+ // On error, artifact is empty. Check artifacts only on successful
581
+ // reconcile.
582
+ if ! tt .wantErr {
583
+ g .Expect (obj .Status .Artifact ).To (MatchArtifact (artifact .DeepCopy ()))
584
+ }
524
585
g .Expect (obj .Status .Conditions ).To (conditions .MatchConditions (tt .assertConditions ))
586
+
587
+ if tt .afterFunc != nil {
588
+ tt .afterFunc (g , obj , tmpDir )
589
+ }
525
590
})
526
591
}
527
592
}
0 commit comments