@@ -38,12 +38,14 @@ import (
3838 "k8s.io/client-go/tools/record"
3939 kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"
4040 "sigs.k8s.io/controller-runtime/pkg/client"
41+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
4142 fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
4243
4344 sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
4445 gcsmock "github.com/fluxcd/source-controller/internal/mock/gcs"
4546 s3mock "github.com/fluxcd/source-controller/internal/mock/s3"
4647 sreconcile "github.com/fluxcd/source-controller/internal/reconcile"
48+ "github.com/fluxcd/source-controller/internal/reconcile/summarize"
4749)
4850
4951// Environment variable to set the GCP Storage host for the GCP client.
@@ -886,13 +888,13 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
886888 assertConditions []metav1.Condition
887889 }{
888890 {
889- name : "Archiving artifact to storage makes Ready =True" ,
891+ name : "Archiving artifact to storage makes ArtifactInStorage =True" ,
890892 beforeFunc : func (t * WithT , obj * sourcev1.Bucket , index * etagIndex , dir string ) {
891893 obj .Spec .Interval = metav1.Duration {Duration : interval }
892894 },
893895 want : sreconcile .ResultSuccess ,
894896 assertConditions : []metav1.Condition {
895- * conditions .TrueCondition (meta . ReadyCondition , meta .SucceededReason , "stored artifact for revision 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'" ),
897+ * conditions .TrueCondition (sourcev1 . ArtifactInStorageCondition , meta .SucceededReason , "stored artifact for revision 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'" ),
896898 },
897899 },
898900 {
@@ -909,7 +911,7 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
909911 },
910912 want : sreconcile .ResultSuccess ,
911913 assertConditions : []metav1.Condition {
912- * conditions .TrueCondition (meta . ReadyCondition , meta .SucceededReason , "stored artifact for revision 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'" ),
914+ * conditions .TrueCondition (sourcev1 . ArtifactInStorageCondition , meta .SucceededReason , "stored artifact for revision 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'" ),
913915 },
914916 },
915917 {
@@ -920,7 +922,7 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
920922 },
921923 want : sreconcile .ResultSuccess ,
922924 assertConditions : []metav1.Condition {
923- * conditions .TrueCondition (meta . ReadyCondition , meta .SucceededReason , "stored artifact for revision 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'" ),
925+ * conditions .TrueCondition (sourcev1 . ArtifactInStorageCondition , meta .SucceededReason , "stored artifact for revision 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'" ),
924926 },
925927 },
926928 {
@@ -937,7 +939,7 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
937939 },
938940 want : sreconcile .ResultSuccess ,
939941 assertConditions : []metav1.Condition {
940- * conditions .TrueCondition (meta . ReadyCondition , meta .SucceededReason , "stored artifact for revision 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'" ),
942+ * conditions .TrueCondition (sourcev1 . ArtifactInStorageCondition , meta .SucceededReason , "stored artifact for revision 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'" ),
941943 },
942944 },
943945 {
@@ -1070,3 +1072,94 @@ func Test_etagIndex_Revision(t *testing.T) {
10701072 })
10711073 }
10721074}
1075+
1076+ func TestBucketReconciler_statusConditions (t * testing.T ) {
1077+ tests := []struct {
1078+ name string
1079+ beforeFunc func (obj * sourcev1.Bucket )
1080+ assertConditions []metav1.Condition
1081+ }{
1082+ {
1083+ name : "positive conditions only" ,
1084+ beforeFunc : func (obj * sourcev1.Bucket ) {
1085+ conditions .MarkTrue (obj , sourcev1 .ArtifactInStorageCondition , meta .SucceededReason , "stored artifact for revision" )
1086+ },
1087+ assertConditions : []metav1.Condition {
1088+ * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "stored artifact for revision" ),
1089+ * conditions .TrueCondition (sourcev1 .ArtifactInStorageCondition , meta .SucceededReason , "stored artifact for revision" ),
1090+ },
1091+ },
1092+ {
1093+ name : "multiple failures" ,
1094+ beforeFunc : func (obj * sourcev1.Bucket ) {
1095+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , sourcev1 .AuthenticationFailedReason , "failed to get secret" )
1096+ conditions .MarkTrue (obj , sourcev1 .StorageOperationFailedCondition , sourcev1 .DirCreationFailedReason , "failed to create directory" )
1097+ conditions .MarkTrue (obj , sourcev1 .ArtifactOutdatedCondition , "NewRevision" , "some error" )
1098+ },
1099+ assertConditions : []metav1.Condition {
1100+ * conditions .FalseCondition (meta .ReadyCondition , sourcev1 .DirCreationFailedReason , "failed to create directory" ),
1101+ * conditions .TrueCondition (sourcev1 .FetchFailedCondition , sourcev1 .AuthenticationFailedReason , "failed to get secret" ),
1102+ * conditions .TrueCondition (sourcev1 .StorageOperationFailedCondition , sourcev1 .DirCreationFailedReason , "failed to create directory" ),
1103+ * conditions .TrueCondition (sourcev1 .ArtifactOutdatedCondition , "NewRevision" , "some error" ),
1104+ },
1105+ },
1106+ {
1107+ name : "mixed positive and negative conditions" ,
1108+ beforeFunc : func (obj * sourcev1.Bucket ) {
1109+ conditions .MarkTrue (obj , sourcev1 .ArtifactInStorageCondition , meta .SucceededReason , "stored artifact for revision" )
1110+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , sourcev1 .AuthenticationFailedReason , "failed to get secret" )
1111+ },
1112+ assertConditions : []metav1.Condition {
1113+ * conditions .FalseCondition (meta .ReadyCondition , sourcev1 .AuthenticationFailedReason , "failed to get secret" ),
1114+ * conditions .TrueCondition (sourcev1 .FetchFailedCondition , sourcev1 .AuthenticationFailedReason , "failed to get secret" ),
1115+ * conditions .TrueCondition (sourcev1 .ArtifactInStorageCondition , meta .SucceededReason , "stored artifact for revision" ),
1116+ },
1117+ },
1118+ }
1119+
1120+ for _ , tt := range tests {
1121+ t .Run (tt .name , func (t * testing.T ) {
1122+ g := NewWithT (t )
1123+
1124+ obj := & sourcev1.Bucket {
1125+ TypeMeta : metav1.TypeMeta {
1126+ Kind : sourcev1 .BucketKind ,
1127+ APIVersion : "source.toolkit.fluxcd.io/v1beta2" ,
1128+ },
1129+ ObjectMeta : metav1.ObjectMeta {
1130+ Name : "bucket" ,
1131+ Namespace : "foo" ,
1132+ },
1133+ }
1134+ clientBuilder := fake .NewClientBuilder ()
1135+ clientBuilder .WithObjects (obj )
1136+ c := clientBuilder .Build ()
1137+
1138+ patchHelper , err := patch .NewHelper (obj , c )
1139+ g .Expect (err ).ToNot (HaveOccurred ())
1140+
1141+ if tt .beforeFunc != nil {
1142+ tt .beforeFunc (obj )
1143+ }
1144+
1145+ ctx := context .TODO ()
1146+ recResult := sreconcile .ResultSuccess
1147+ var retErr error
1148+
1149+ summarizeHelper := summarize .NewHelper (record .NewFakeRecorder (32 ), patchHelper )
1150+ summarizeOpts := []summarize.Option {
1151+ summarize .WithConditions (bucketReadyCondition ),
1152+ summarize .WithReconcileResult (recResult ),
1153+ summarize .WithReconcileError (retErr ),
1154+ summarize .WithIgnoreNotFound (),
1155+ summarize .WithResultBuilder (sreconcile.AlwaysRequeueResultBuilder {RequeueAfter : obj .GetRequeueAfter ()}),
1156+ summarize .WithPatchFieldOwner ("source-controller" ),
1157+ }
1158+ _ , retErr = summarizeHelper .SummarizeAndPatch (ctx , obj , summarizeOpts ... )
1159+
1160+ key := client .ObjectKeyFromObject (obj )
1161+ g .Expect (c .Get (ctx , key , obj )).ToNot (HaveOccurred ())
1162+ g .Expect (obj .GetConditions ()).To (conditions .MatchConditions (tt .assertConditions ))
1163+ })
1164+ }
1165+ }
0 commit comments