Skip to content

Commit f6f1803

Browse files
authored
Merge pull request fluxcd#414 from darkowlzz/gitrepo-reconciler-artifact-tests
controllers: Add more tests for reconcileArtifact
2 parents 29442ba + be4e85b commit f6f1803

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

controllers/gitrepository_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,8 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, obj *so
404404
ctrl.LoggerFrom(ctx).Error(err, "failed to stat source path")
405405
return ctrl.Result{}, err
406406
} else if !f.IsDir() {
407-
ctrl.LoggerFrom(ctx).Error(err, fmt.Sprintf("source path '%s' is not a directory", dir))
407+
err := fmt.Errorf("source path '%s' is not a directory", dir)
408+
ctrl.LoggerFrom(ctx).Error(err, "invalid target path")
408409
return ctrl.Result{}, err
409410
}
410411

controllers/gitrepository_controller_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
563563
tests := []struct {
564564
name string
565565
dir string
566+
includes artifactSet
566567
beforeFunc func(obj *sourcev1.GitRepository)
567568
afterFunc func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact)
568569
want ctrl.Result
@@ -578,6 +579,42 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
578579
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
579580
t.Expect(obj.GetArtifact()).ToNot(BeNil())
580581
t.Expect(obj.GetArtifact().Checksum).To(Equal("b1fab897a1a0fb8094ce3ae0e9743a4b72bd7268"))
582+
t.Expect(obj.Status.URL).ToNot(BeEmpty())
583+
},
584+
want: ctrl.Result{RequeueAfter: interval},
585+
assertConditions: []metav1.Condition{
586+
*conditions.TrueCondition(meta.ReadyCondition, meta.SucceededReason, "Stored artifact for revision 'main/revision'"),
587+
},
588+
},
589+
{
590+
name: "Archiving artifact to storage with includes makes Ready=True",
591+
dir: "testdata/git/repository",
592+
includes: artifactSet{&sourcev1.Artifact{Revision: "main/revision"}},
593+
beforeFunc: func(obj *sourcev1.GitRepository) {
594+
obj.Spec.Interval = metav1.Duration{Duration: interval}
595+
},
596+
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
597+
t.Expect(obj.GetArtifact()).ToNot(BeNil())
598+
t.Expect(obj.GetArtifact().Checksum).To(Equal("b1fab897a1a0fb8094ce3ae0e9743a4b72bd7268"))
599+
t.Expect(obj.Status.IncludedArtifacts).ToNot(BeEmpty())
600+
t.Expect(obj.Status.URL).ToNot(BeEmpty())
601+
},
602+
want: ctrl.Result{RequeueAfter: interval},
603+
assertConditions: []metav1.Condition{
604+
*conditions.TrueCondition(meta.ReadyCondition, meta.SucceededReason, "Stored artifact for revision 'main/revision'"),
605+
},
606+
},
607+
{
608+
name: "Up-to-date artifact should not update status",
609+
dir: "testdata/git/repository",
610+
includes: artifactSet{&sourcev1.Artifact{Revision: "main/revision"}},
611+
beforeFunc: func(obj *sourcev1.GitRepository) {
612+
obj.Spec.Interval = metav1.Duration{Duration: interval}
613+
obj.Status.Artifact = &sourcev1.Artifact{Revision: "main/revision"}
614+
obj.Status.IncludedArtifacts = []*sourcev1.Artifact{{Revision: "main/revision"}}
615+
},
616+
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
617+
t.Expect(obj.Status.URL).To(BeEmpty())
581618
},
582619
want: ctrl.Result{RequeueAfter: interval},
583620
assertConditions: []metav1.Condition{
@@ -600,6 +637,16 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
600637
*conditions.TrueCondition(meta.ReadyCondition, meta.SucceededReason, "Stored artifact for revision 'main/revision'"),
601638
},
602639
},
640+
{
641+
name: "Target path does not exists",
642+
dir: "testdata/git/foo",
643+
wantErr: true,
644+
},
645+
{
646+
name: "Target path is not a directory",
647+
dir: "testdata/git/repository/foo.txt",
648+
wantErr: true,
649+
},
603650
}
604651

605652
for _, tt := range tests {
@@ -624,7 +671,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
624671

625672
artifact := testStorage.NewArtifactFor(obj.Kind, obj, "main/revision", "checksum.tar.gz")
626673

627-
got, err := r.reconcileArtifact(ctx, obj, artifact, nil, tt.dir)
674+
got, err := r.reconcileArtifact(ctx, obj, artifact, tt.includes, tt.dir)
628675
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))
629676
g.Expect(err != nil).To(Equal(tt.wantErr))
630677
g.Expect(got).To(Equal(tt.want))

0 commit comments

Comments
 (0)