@@ -18,6 +18,7 @@ package summarize
1818
1919import (
2020 "context"
21+ "errors"
2122 "fmt"
2223 "testing"
2324 "time"
@@ -27,6 +28,7 @@ import (
2728 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829 "k8s.io/apimachinery/pkg/runtime"
2930 "k8s.io/client-go/tools/record"
31+ ctrl "sigs.k8s.io/controller-runtime"
3032 "sigs.k8s.io/controller-runtime/pkg/client"
3133 fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
3234
@@ -91,18 +93,19 @@ func TestSummarizeAndPatch(t *testing.T) {
9193 afterFunc func (t * WithT , obj client.Object )
9294 assertConditions []metav1.Condition
9395 }{
94- // Success/Fail indicates if a reconciliation succeeded or failed. On
95- // a successful reconciliation, the object generation is expected to
96- // match the observed generation in the object status .
96+ // Success/Fail indicates if a reconciliation succeeded or failed.
97+ // The object generation is expected to match the observed generation in
98+ // the object status if Ready=True or Stalled=True at the end .
9799 // All the cases have some Ready condition set, even if a test case is
98100 // unrelated to the conditions, because it's neseccary for a valid
99101 // status.
100102 {
101- name : "Success, no extra conditions " ,
103+ name : "Success, Ready=True " ,
102104 generation : 4 ,
103105 beforeFunc : func (obj conditions.Setter ) {
104106 conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "test-msg" )
105107 },
108+ result : reconcile .ResultSuccess ,
106109 conditions : []Conditions {testReadyConditions },
107110 assertConditions : []metav1.Condition {
108111 * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "test-msg" ),
@@ -111,20 +114,6 @@ func TestSummarizeAndPatch(t *testing.T) {
111114 t .Expect (obj ).To (HaveStatusObservedGeneration (4 ))
112115 },
113116 },
114- {
115- name : "Success, Ready=True" ,
116- generation : 5 ,
117- beforeFunc : func (obj conditions.Setter ) {
118- conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "created" )
119- },
120- conditions : []Conditions {testReadyConditions },
121- assertConditions : []metav1.Condition {
122- * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "created" ),
123- },
124- afterFunc : func (t * WithT , obj client.Object ) {
125- t .Expect (obj ).To (HaveStatusObservedGeneration (5 ))
126- },
127- },
128117 {
129118 name : "Success, removes reconciling for successful result" ,
130119 generation : 2 ,
@@ -216,7 +205,22 @@ func TestSummarizeAndPatch(t *testing.T) {
216205 },
217206 },
218207 {
219- name : "Success, multiple conditions summary" ,
208+ name : "Success, multiple target conditions summary" ,
209+ generation : 3 ,
210+ beforeFunc : func (obj conditions.Setter ) {
211+ conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "test-msg" )
212+ conditions .MarkTrue (obj , "AAA" , "ZZZ" , "zzz" ) // Positive polarity True.
213+ },
214+ conditions : []Conditions {testReadyConditions , testFooConditions },
215+ result : reconcile .ResultSuccess ,
216+ assertConditions : []metav1.Condition {
217+ * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "test-msg" ),
218+ * conditions .TrueCondition ("Foo" , "ZZZ" , "zzz" ), // True summary.
219+ * conditions .TrueCondition ("AAA" , "ZZZ" , "zzz" ),
220+ },
221+ },
222+ {
223+ name : "Success, multiple target conditions, False non-Ready summary don't affect result" ,
220224 generation : 3 ,
221225 beforeFunc : func (obj conditions.Setter ) {
222226 conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "test-msg" )
@@ -232,6 +236,20 @@ func TestSummarizeAndPatch(t *testing.T) {
232236 * conditions .TrueCondition ("AAA" , "ZZZ" , "zzz" ),
233237 },
234238 },
239+ {
240+ name : "Fail, success result but Ready=False" ,
241+ generation : 3 ,
242+ beforeFunc : func (obj conditions.Setter ) {
243+ conditions .MarkTrue (obj , sourcev1 .ArtifactOutdatedCondition , "NewRevision" , "new index revision" )
244+ },
245+ conditions : []Conditions {testReadyConditions },
246+ result : reconcile .ResultSuccess ,
247+ assertConditions : []metav1.Condition {
248+ * conditions .FalseCondition (meta .ReadyCondition , "NewRevision" , "new index revision" ),
249+ * conditions .TrueCondition (sourcev1 .ArtifactOutdatedCondition , "NewRevision" , "new index revision" ),
250+ },
251+ wantErr : true ,
252+ },
235253 }
236254
237255 for _ , tt := range tests {
@@ -291,6 +309,8 @@ func TestSummarizeAndPatch(t *testing.T) {
291309// This tests the scenario where SummarizeAndPatch is used in the middle of
292310// reconciliation.
293311func TestSummarizeAndPatch_Intermediate (t * testing.T ) {
312+ interval := 5 * time .Second
313+
294314 var testStageAConditions = Conditions {
295315 Target : "StageA" ,
296316 Owned : []string {"StageA" , "A1" , "A2" , "A3" },
@@ -335,7 +355,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
335355 },
336356 },
337357 {
338- name : "multiple Conditions" ,
358+ name : "multiple Conditions, mixed results " ,
339359 conditions : []Conditions {testStageAConditions , testStageBConditions },
340360 beforeFunc : func (obj conditions.Setter ) {
341361 conditions .MarkTrue (obj , "A3" , "ZZZ" , "zzz" ) // Negative polarity True.
@@ -365,7 +385,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
365385 GenerateName : "test-" ,
366386 },
367387 Spec : sourcev1.GitRepositorySpec {
368- Interval : metav1.Duration {Duration : 5 * time . Second },
388+ Interval : metav1.Duration {Duration : interval },
369389 },
370390 Status : sourcev1.GitRepositoryStatus {
371391 Conditions : []metav1.Condition {
@@ -386,6 +406,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
386406 summaryHelper := NewHelper (record .NewFakeRecorder (32 ), patchHelper )
387407 summaryOpts := []Option {
388408 WithConditions (tt .conditions ... ),
409+ WithResultBuilder (reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval }),
389410 }
390411 _ , err = summaryHelper .SummarizeAndPatch (ctx , obj , summaryOpts ... )
391412 g .Expect (err ).ToNot (HaveOccurred ())
@@ -394,3 +415,62 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
394415 })
395416 }
396417}
418+
419+ func TestIsNonStalledSuccess (t * testing.T ) {
420+ interval := 5 * time .Second
421+
422+ tests := []struct {
423+ name string
424+ beforeFunc func (obj conditions.Setter )
425+ rb reconcile.RuntimeResultBuilder
426+ recResult ctrl.Result
427+ recErr error
428+ wantResult bool
429+ }{
430+ {
431+ name : "non stalled success" ,
432+ rb : reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval },
433+ recResult : ctrl.Result {RequeueAfter : interval },
434+ wantResult : true ,
435+ },
436+ {
437+ name : "stalled success" ,
438+ beforeFunc : func (obj conditions.Setter ) {
439+ conditions .MarkStalled (obj , "FooReason" , "test-msg" )
440+ },
441+ rb : reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval },
442+ recResult : ctrl.Result {RequeueAfter : interval },
443+ wantResult : false ,
444+ },
445+ {
446+ name : "error result" ,
447+ rb : reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval },
448+ recResult : ctrl.Result {RequeueAfter : interval },
449+ recErr : errors .New ("some-error" ),
450+ wantResult : false ,
451+ },
452+ {
453+ name : "non success result" ,
454+ rb : reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval },
455+ recResult : ctrl.Result {RequeueAfter : 2 * time .Second },
456+ wantResult : false ,
457+ },
458+ {
459+ name : "no result builder" ,
460+ recResult : ctrl.Result {RequeueAfter : interval },
461+ wantResult : false ,
462+ },
463+ }
464+
465+ for _ , tt := range tests {
466+ t .Run (tt .name , func (t * testing.T ) {
467+ g := NewWithT (t )
468+
469+ obj := & sourcev1.GitRepository {}
470+ if tt .beforeFunc != nil {
471+ tt .beforeFunc (obj )
472+ }
473+ g .Expect (isNonStalledSuccess (obj , tt .rb , tt .recResult , tt .recErr )).To (Equal (tt .wantResult ))
474+ })
475+ }
476+ }
0 commit comments