Skip to content

Commit 648beef

Browse files
somtochiamastefanprodan
authored andcommitted
Add test for reconcileArtifact
Signed-off-by: Somtochi Onyekwere <[email protected]>
1 parent e42e9d0 commit 648beef

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

controllers/ocirepository_controller_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,152 @@ func TestOCIRepository_getArtifactURL(t *testing.T) {
765765
}
766766
}
767767

768+
func TestOCIRepository_reconcileStorage(t *testing.T) {
769+
g := NewWithT(t)
770+
771+
tests := []struct {
772+
name string
773+
beforeFunc func(obj *sourcev1.OCIRepository) error
774+
want sreconcile.Result
775+
wantErr bool
776+
assertConditions []metav1.Condition
777+
assertArtifact *sourcev1.Artifact
778+
assertPaths []string
779+
}{
780+
{
781+
name: "garbage collects",
782+
beforeFunc: func(obj *sourcev1.OCIRepository) error {
783+
revisions := []string{"a", "b", "c", "d"}
784+
785+
for n := range revisions {
786+
v := revisions[n]
787+
obj.Status.Artifact = &sourcev1.Artifact{
788+
Path: fmt.Sprintf("/oci-reconcile-storage/%s.txt", v),
789+
Revision: v,
790+
}
791+
if err := testStorage.MkdirAll(*obj.Status.Artifact); err != nil {
792+
return err
793+
}
794+
795+
if err := testStorage.AtomicWriteFile(obj.Status.Artifact, strings.NewReader(v), 0o640); err != nil {
796+
return err
797+
}
798+
799+
if n != len(revisions)-1 {
800+
time.Sleep(time.Second)
801+
}
802+
}
803+
804+
testStorage.SetArtifactURL(obj.Status.Artifact)
805+
return nil
806+
},
807+
assertArtifact: &sourcev1.Artifact{
808+
Path: "/oci-reconcile-storage/d.txt",
809+
Revision: "d",
810+
Checksum: "18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4",
811+
URL: testStorage.Hostname + "/oci-reconcile-storage/d.txt",
812+
Size: int64p(int64(len("d"))),
813+
},
814+
assertPaths: []string{
815+
"/oci-reconcile-storage/d.txt",
816+
"/oci-reconcile-storage/c.txt",
817+
"!/oci-reconcile-storage/b.txt",
818+
"!/oci-reconcile-storage/a.txt",
819+
},
820+
want: sreconcile.ResultSuccess,
821+
},
822+
{
823+
name: "notices missing artifact in storage",
824+
beforeFunc: func(obj *sourcev1.OCIRepository) error {
825+
obj.Status.Artifact = &sourcev1.Artifact{
826+
Path: "/oci-reconcile-storage/invalid.txt",
827+
Revision: "e",
828+
}
829+
testStorage.SetArtifactURL(obj.Status.Artifact)
830+
return nil
831+
},
832+
want: sreconcile.ResultSuccess,
833+
assertPaths: []string{
834+
"!/oci-reconcile-storage/invalid.txt",
835+
},
836+
assertConditions: []metav1.Condition{
837+
*conditions.TrueCondition(meta.ReconcilingCondition, "NoArtifact", "no artifact for resource in storage"),
838+
},
839+
},
840+
{
841+
name: "updates hostname on diff from current",
842+
beforeFunc: func(obj *sourcev1.OCIRepository) error {
843+
obj.Status.Artifact = &sourcev1.Artifact{
844+
Path: "/oci-reconcile-storage/hostname.txt",
845+
Revision: "f",
846+
Checksum: "3b9c358f36f0a31b6ad3e14f309c7cf198ac9246e8316f9ce543d5b19ac02b80",
847+
URL: "http://outdated.com/oci-reconcile-storage/hostname.txt",
848+
}
849+
if err := testStorage.MkdirAll(*obj.Status.Artifact); err != nil {
850+
return err
851+
}
852+
if err := testStorage.AtomicWriteFile(obj.Status.Artifact, strings.NewReader("file"), 0o640); err != nil {
853+
return err
854+
}
855+
return nil
856+
},
857+
want: sreconcile.ResultSuccess,
858+
assertPaths: []string{
859+
"/oci-reconcile-storage/hostname.txt",
860+
},
861+
assertArtifact: &sourcev1.Artifact{
862+
Path: "/oci-reconcile-storage/hostname.txt",
863+
Revision: "f",
864+
Checksum: "3b9c358f36f0a31b6ad3e14f309c7cf198ac9246e8316f9ce543d5b19ac02b80",
865+
URL: testStorage.Hostname + "/oci-reconcile-storage/hostname.txt",
866+
Size: int64p(int64(len("file"))),
867+
},
868+
},
869+
}
870+
871+
for _, tt := range tests {
872+
t.Run(tt.name, func(t *testing.T) {
873+
builder := fakeclient.NewClientBuilder().WithScheme(testEnv.GetScheme())
874+
r := &OCIRepositoryReconciler{
875+
Client: builder.Build(),
876+
EventRecorder: record.NewFakeRecorder(32),
877+
Storage: testStorage,
878+
}
879+
obj := &sourcev1.OCIRepository{
880+
ObjectMeta: metav1.ObjectMeta{
881+
GenerateName: "test-",
882+
},
883+
}
884+
885+
g.Expect(tt.beforeFunc(obj)).To(Succeed())
886+
got, err := r.reconcileStorage(ctx, obj, &sourcev1.Artifact{}, "")
887+
if tt.wantErr {
888+
g.Expect(err).To(HaveOccurred())
889+
} else {
890+
g.Expect(err).ToNot(HaveOccurred())
891+
}
892+
893+
g.Expect(got).To(Equal(tt.want))
894+
g.Expect(obj.Status.Artifact).To(MatchArtifact(tt.assertArtifact))
895+
if tt.assertArtifact != nil && tt.assertArtifact.URL != "" {
896+
g.Expect(obj.Status.Artifact.URL).To(Equal(tt.assertArtifact.URL))
897+
}
898+
899+
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))
900+
901+
for _, p := range tt.assertPaths {
902+
absoluteP := filepath.Join(testStorage.BasePath, p)
903+
if !strings.HasPrefix(p, "!") {
904+
g.Expect(absoluteP).To(BeAnExistingFile())
905+
continue
906+
}
907+
908+
g.Expect(absoluteP).ToNot(BeAnExistingFile())
909+
}
910+
})
911+
}
912+
}
913+
768914
func TestOCIRepository_CertSecret(t *testing.T) {
769915
g := NewWithT(t)
770916

d.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ func mustInitStorage(path string, storageAdvAddr string, artifactRetentionTTL ti
357357
os.MkdirAll(path, 0o700)
358358
}
359359

360+
fmt.Println("PARHHHH", path)
360361
storage, err := controllers.NewStorage(path, storageAdvAddr, artifactRetentionTTL, artifactRetentionRecords)
361362
if err != nil {
362363
l.Error(err, "unable to initialise storage")

0 commit comments

Comments
 (0)