@@ -1104,6 +1104,148 @@ func TestGitRepositoryReconciler_reconcileInclude(t *testing.T) {
11041104 }
11051105}
11061106
1107+ func TestGitRepositoryReconciler_reconcileStorage (t * testing.T ) {
1108+ tests := []struct {
1109+ name string
1110+ beforeFunc func (obj * sourcev1.GitRepository , storage * Storage ) error
1111+ want sreconcile.Result
1112+ wantErr bool
1113+ assertArtifact * sourcev1.Artifact
1114+ assertConditions []metav1.Condition
1115+ assertPaths []string
1116+ }{
1117+ {
1118+ name : "garbage collects" ,
1119+ beforeFunc : func (obj * sourcev1.GitRepository , storage * Storage ) error {
1120+ revisions := []string {"a" , "b" , "c" , "d" }
1121+ for n := range revisions {
1122+ v := revisions [n ]
1123+ obj .Status .Artifact = & sourcev1.Artifact {
1124+ Path : fmt .Sprintf ("/reconcile-storage/%s.txt" , v ),
1125+ Revision : v ,
1126+ }
1127+ if err := testStorage .MkdirAll (* obj .Status .Artifact ); err != nil {
1128+ return err
1129+ }
1130+ if err := testStorage .AtomicWriteFile (obj .Status .Artifact , strings .NewReader (v ), 0o644 ); err != nil {
1131+ return err
1132+ }
1133+ if n != len (revisions )- 1 {
1134+ time .Sleep (time .Second * 1 )
1135+ }
1136+ }
1137+ testStorage .SetArtifactURL (obj .Status .Artifact )
1138+ return nil
1139+ },
1140+ assertArtifact : & sourcev1.Artifact {
1141+ Path : "/reconcile-storage/d.txt" ,
1142+ Revision : "d" ,
1143+ Checksum : "18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4" ,
1144+ URL : testStorage .Hostname + "/reconcile-storage/d.txt" ,
1145+ Size : int64p (int64 (len ("d" ))),
1146+ },
1147+ assertPaths : []string {
1148+ "/reconcile-storage/d.txt" ,
1149+ "/reconcile-storage/c.txt" ,
1150+ "!/reconcile-storage/b.txt" ,
1151+ "!/reconcile-storage/a.txt" ,
1152+ },
1153+ want : sreconcile .ResultSuccess ,
1154+ },
1155+ {
1156+ name : "notices missing artifact in storage" ,
1157+ beforeFunc : func (obj * sourcev1.GitRepository , storage * Storage ) error {
1158+ obj .Status .Artifact = & sourcev1.Artifact {
1159+ Path : "/reconcile-storage/invalid.txt" ,
1160+ Revision : "e" ,
1161+ }
1162+ testStorage .SetArtifactURL (obj .Status .Artifact )
1163+ return nil
1164+ },
1165+ want : sreconcile .ResultSuccess ,
1166+ assertPaths : []string {
1167+ "!/reconcile-storage/invalid.txt" ,
1168+ },
1169+ assertConditions : []metav1.Condition {
1170+ * conditions .TrueCondition (meta .ReconcilingCondition , "NoArtifact" , "no artifact for resource in storage" ),
1171+ },
1172+ },
1173+ {
1174+ name : "updates hostname on diff from current" ,
1175+ beforeFunc : func (obj * sourcev1.GitRepository , storage * Storage ) error {
1176+ obj .Status .Artifact = & sourcev1.Artifact {
1177+ Path : "/reconcile-storage/hostname.txt" ,
1178+ Revision : "f" ,
1179+ Checksum : "3b9c358f36f0a31b6ad3e14f309c7cf198ac9246e8316f9ce543d5b19ac02b80" ,
1180+ URL : "http://outdated.com/reconcile-storage/hostname.txt" ,
1181+ }
1182+ if err := testStorage .MkdirAll (* obj .Status .Artifact ); err != nil {
1183+ return err
1184+ }
1185+ if err := testStorage .AtomicWriteFile (obj .Status .Artifact , strings .NewReader ("file" ), 0o644 ); err != nil {
1186+ return err
1187+ }
1188+ return nil
1189+ },
1190+ want : sreconcile .ResultSuccess ,
1191+ assertPaths : []string {
1192+ "/reconcile-storage/hostname.txt" ,
1193+ },
1194+ assertArtifact : & sourcev1.Artifact {
1195+ Path : "/reconcile-storage/hostname.txt" ,
1196+ Revision : "f" ,
1197+ Checksum : "3b9c358f36f0a31b6ad3e14f309c7cf198ac9246e8316f9ce543d5b19ac02b80" ,
1198+ URL : testStorage .Hostname + "/reconcile-storage/hostname.txt" ,
1199+ Size : int64p (int64 (len ("file" ))),
1200+ },
1201+ },
1202+ }
1203+ for _ , tt := range tests {
1204+ t .Run (tt .name , func (t * testing.T ) {
1205+ g := NewWithT (t )
1206+
1207+ defer func () {
1208+ g .Expect (os .RemoveAll (filepath .Join (testStorage .BasePath , "/reconcile-storage" ))).To (Succeed ())
1209+ }()
1210+
1211+ r := & GitRepositoryReconciler {
1212+ EventRecorder : record .NewFakeRecorder (32 ),
1213+ Storage : testStorage ,
1214+ }
1215+
1216+ obj := & sourcev1.GitRepository {
1217+ ObjectMeta : metav1.ObjectMeta {
1218+ GenerateName : "test-" ,
1219+ },
1220+ }
1221+ if tt .beforeFunc != nil {
1222+ g .Expect (tt .beforeFunc (obj , testStorage )).To (Succeed ())
1223+ }
1224+
1225+ var c * git.Commit
1226+ var as artifactSet
1227+ got , err := r .reconcileStorage (context .TODO (), obj , c , & as , "" )
1228+ g .Expect (err != nil ).To (Equal (tt .wantErr ))
1229+ g .Expect (got ).To (Equal (tt .want ))
1230+
1231+ g .Expect (obj .Status .Artifact ).To (MatchArtifact (tt .assertArtifact ))
1232+ if tt .assertArtifact != nil && tt .assertArtifact .URL != "" {
1233+ g .Expect (obj .Status .Artifact .URL ).To (Equal (tt .assertArtifact .URL ))
1234+ }
1235+ g .Expect (obj .Status .Conditions ).To (conditions .MatchConditions (tt .assertConditions ))
1236+
1237+ for _ , p := range tt .assertPaths {
1238+ absoluteP := filepath .Join (testStorage .BasePath , p )
1239+ if ! strings .HasPrefix (p , "!" ) {
1240+ g .Expect (absoluteP ).To (BeAnExistingFile ())
1241+ continue
1242+ }
1243+ g .Expect (absoluteP ).NotTo (BeAnExistingFile ())
1244+ }
1245+ })
1246+ }
1247+ }
1248+
11071249func TestGitRepositoryReconciler_reconcileDelete (t * testing.T ) {
11081250 g := NewWithT (t )
11091251
0 commit comments