controllers: Add more tests for reconcileArtifact
Fixes error returned from target path validation check and adds more test cases for TestGitRepositoryReconciler_reconcileArtifact. Signed-off-by: Sunny <darkowlzz@protonmail.com>
This commit is contained in:
parent
e34f79203d
commit
be4e85b422
|
@ -402,7 +402,8 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, obj *so
|
|||
ctrl.LoggerFrom(ctx).Error(err, "failed to stat source path")
|
||||
return ctrl.Result{}, err
|
||||
} else if !f.IsDir() {
|
||||
ctrl.LoggerFrom(ctx).Error(err, fmt.Sprintf("source path '%s' is not a directory", dir))
|
||||
err := fmt.Errorf("source path '%s' is not a directory", dir)
|
||||
ctrl.LoggerFrom(ctx).Error(err, "invalid target path")
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
|
|
|
@ -563,6 +563,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
|
|||
tests := []struct {
|
||||
name string
|
||||
dir string
|
||||
includes artifactSet
|
||||
beforeFunc func(obj *sourcev1.GitRepository)
|
||||
afterFunc func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact)
|
||||
want ctrl.Result
|
||||
|
@ -578,6 +579,42 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
|
|||
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
|
||||
t.Expect(obj.GetArtifact()).ToNot(BeNil())
|
||||
t.Expect(obj.GetArtifact().Checksum).To(Equal("b1fab897a1a0fb8094ce3ae0e9743a4b72bd7268"))
|
||||
t.Expect(obj.Status.URL).ToNot(BeEmpty())
|
||||
},
|
||||
want: ctrl.Result{RequeueAfter: interval},
|
||||
assertConditions: []metav1.Condition{
|
||||
*conditions.TrueCondition(meta.ReadyCondition, meta.SucceededReason, "Stored artifact for revision 'main/revision'"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Archiving artifact to storage with includes makes Ready=True",
|
||||
dir: "testdata/git/repository",
|
||||
includes: artifactSet{&sourcev1.Artifact{Revision: "main/revision"}},
|
||||
beforeFunc: func(obj *sourcev1.GitRepository) {
|
||||
obj.Spec.Interval = metav1.Duration{Duration: interval}
|
||||
},
|
||||
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
|
||||
t.Expect(obj.GetArtifact()).ToNot(BeNil())
|
||||
t.Expect(obj.GetArtifact().Checksum).To(Equal("b1fab897a1a0fb8094ce3ae0e9743a4b72bd7268"))
|
||||
t.Expect(obj.Status.IncludedArtifacts).ToNot(BeEmpty())
|
||||
t.Expect(obj.Status.URL).ToNot(BeEmpty())
|
||||
},
|
||||
want: ctrl.Result{RequeueAfter: interval},
|
||||
assertConditions: []metav1.Condition{
|
||||
*conditions.TrueCondition(meta.ReadyCondition, meta.SucceededReason, "Stored artifact for revision 'main/revision'"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Up-to-date artifact should not update status",
|
||||
dir: "testdata/git/repository",
|
||||
includes: artifactSet{&sourcev1.Artifact{Revision: "main/revision"}},
|
||||
beforeFunc: func(obj *sourcev1.GitRepository) {
|
||||
obj.Spec.Interval = metav1.Duration{Duration: interval}
|
||||
obj.Status.Artifact = &sourcev1.Artifact{Revision: "main/revision"}
|
||||
obj.Status.IncludedArtifacts = []*sourcev1.Artifact{{Revision: "main/revision"}}
|
||||
},
|
||||
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
|
||||
t.Expect(obj.Status.URL).To(BeEmpty())
|
||||
},
|
||||
want: ctrl.Result{RequeueAfter: interval},
|
||||
assertConditions: []metav1.Condition{
|
||||
|
@ -600,6 +637,16 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
|
|||
*conditions.TrueCondition(meta.ReadyCondition, meta.SucceededReason, "Stored artifact for revision 'main/revision'"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Target path does not exists",
|
||||
dir: "testdata/git/foo",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Target path is not a directory",
|
||||
dir: "testdata/git/repository/foo.txt",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
@ -624,7 +671,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
|
|||
|
||||
artifact := testStorage.NewArtifactFor(obj.Kind, obj, "main/revision", "checksum.tar.gz")
|
||||
|
||||
got, err := r.reconcileArtifact(ctx, obj, artifact, nil, tt.dir)
|
||||
got, err := r.reconcileArtifact(ctx, obj, artifact, tt.includes, tt.dir)
|
||||
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))
|
||||
g.Expect(err != nil).To(Equal(tt.wantErr))
|
||||
g.Expect(got).To(Equal(tt.want))
|
||||
|
|
Loading…
Reference in New Issue