Skip to content

Commit b49a809

Browse files
darkowlzzhiddeco
authored andcommitted
BucketReconciler: Add reconcileArtifact tests
Add `BucketReconciler.reconcileArtifact` tests based on `GitRepositoryReconciler.reconcileArtifact` test cases. Signed-off-by: Sunny <[email protected]>
1 parent 15bc9e7 commit b49a809

File tree

1 file changed

+104
-39
lines changed

1 file changed

+104
-39
lines changed

controllers/bucket_controller_test.go

Lines changed: 104 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
164164
assertArtifact: &sourcev1.Artifact{
165165
Path: "/reconcile-storage/c.txt",
166166
Revision: "c",
167-
Checksum: "84a516841ba77a5b4648de2cd0dfcb30ea46dbb4",
167+
Checksum: "2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6",
168168
URL: testStorage.Hostname + "/reconcile-storage/c.txt",
169169
},
170170
assertPaths: []string{
@@ -197,7 +197,7 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
197197
obj.Status.Artifact = &sourcev1.Artifact{
198198
Path: fmt.Sprintf("/reconcile-storage/hostname.txt"),
199199
Revision: "f",
200-
Checksum: "971c419dd609331343dee105fffd0f4608dc0bf2",
200+
Checksum: "3b9c358f36f0a31b6ad3e14f309c7cf198ac9246e8316f9ce543d5b19ac02b80",
201201
URL: "http://outdated.com/reconcile-storage/hostname.txt",
202202
}
203203
if err := testStorage.MkdirAll(*obj.Status.Artifact); err != nil {
@@ -214,7 +214,7 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
214214
assertArtifact: &sourcev1.Artifact{
215215
Path: "/reconcile-storage/hostname.txt",
216216
Revision: "f",
217-
Checksum: "971c419dd609331343dee105fffd0f4608dc0bf2",
217+
Checksum: "3b9c358f36f0a31b6ad3e14f309c7cf198ac9246e8316f9ce543d5b19ac02b80",
218218
URL: testStorage.Hostname + "/reconcile-storage/hostname.txt",
219219
},
220220
},
@@ -441,57 +441,113 @@ func TestBucketReconciler_reconcileSource(t *testing.T) {
441441
}
442442

443443
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+
444448
tests := []struct {
445449
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)
448452
want ctrl.Result
449453
wantErr bool
450454
assertConditions []metav1.Condition
451455
}{
452456
{
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}
456506
},
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))
459513
},
514+
want: ctrl.Result{RequeueAfter: interval},
460515
assertConditions: []metav1.Condition{
461516
*conditions.TrueCondition(meta.ReadyCondition, meta.SucceededReason, "Stored artifact for revision 'existing'"),
462517
},
463518
},
464519
{
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())
468538
},
469539
wantErr: true,
470540
},
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-
//},
489541
}
490542

491543
for _, tt := range tests {
492544
t.Run(tt.name, func(t *testing.T) {
493545
g := NewWithT(t)
494546

547+
r := &BucketReconciler{
548+
Storage: testStorage,
549+
}
550+
495551
tmpDir, err := os.MkdirTemp("", "reconcile-bucket-artifact-")
496552
g.Expect(err).ToNot(HaveOccurred())
497553
defer os.RemoveAll(tmpDir)
@@ -501,27 +557,36 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
501557
Kind: sourcev1.BucketKind,
502558
},
503559
ObjectMeta: metav1.ObjectMeta{
504-
Name: "test-bucket",
560+
GenerateName: "test-bucket-",
561+
Generation: 1,
562+
Namespace: "default",
505563
},
506564
Spec: sourcev1.BucketSpec{
507565
Timeout: &metav1.Duration{Duration: timeout},
508566
},
509567
}
510568

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
514571

515-
r := &BucketReconciler{
516-
Storage: testStorage,
572+
if tt.beforeFunc != nil {
573+
tt.beforeFunc(g, obj, artifact, tmpDir)
517574
}
518575

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)
520577
g.Expect(err != nil).To(Equal(tt.wantErr))
521578
g.Expect(got).To(Equal(tt.want))
522579

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+
}
524585
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))
586+
587+
if tt.afterFunc != nil {
588+
tt.afterFunc(g, obj, tmpDir)
589+
}
525590
})
526591
}
527592
}

0 commit comments

Comments
 (0)