@@ -18,6 +18,7 @@ package summarize
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"testing"
23
24
"time"
@@ -27,6 +28,7 @@ import (
27
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
29
"k8s.io/apimachinery/pkg/runtime"
29
30
"k8s.io/client-go/tools/record"
31
+ ctrl "sigs.k8s.io/controller-runtime"
30
32
"sigs.k8s.io/controller-runtime/pkg/client"
31
33
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
32
34
@@ -91,18 +93,19 @@ func TestSummarizeAndPatch(t *testing.T) {
91
93
afterFunc func (t * WithT , obj client.Object )
92
94
assertConditions []metav1.Condition
93
95
}{
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 .
97
99
// All the cases have some Ready condition set, even if a test case is
98
100
// unrelated to the conditions, because it's neseccary for a valid
99
101
// status.
100
102
{
101
- name : "Success, no extra conditions " ,
103
+ name : "Success, Ready=True " ,
102
104
generation : 4 ,
103
105
beforeFunc : func (obj conditions.Setter ) {
104
106
conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "test-msg" )
105
107
},
108
+ result : reconcile .ResultSuccess ,
106
109
conditions : []Conditions {testReadyConditions },
107
110
assertConditions : []metav1.Condition {
108
111
* conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "test-msg" ),
@@ -111,20 +114,6 @@ func TestSummarizeAndPatch(t *testing.T) {
111
114
t .Expect (obj ).To (HaveStatusObservedGeneration (4 ))
112
115
},
113
116
},
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
- },
128
117
{
129
118
name : "Success, removes reconciling for successful result" ,
130
119
generation : 2 ,
@@ -216,7 +205,22 @@ func TestSummarizeAndPatch(t *testing.T) {
216
205
},
217
206
},
218
207
{
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" ,
220
224
generation : 3 ,
221
225
beforeFunc : func (obj conditions.Setter ) {
222
226
conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "test-msg" )
@@ -232,6 +236,20 @@ func TestSummarizeAndPatch(t *testing.T) {
232
236
* conditions .TrueCondition ("AAA" , "ZZZ" , "zzz" ),
233
237
},
234
238
},
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
+ },
235
253
}
236
254
237
255
for _ , tt := range tests {
@@ -291,6 +309,8 @@ func TestSummarizeAndPatch(t *testing.T) {
291
309
// This tests the scenario where SummarizeAndPatch is used in the middle of
292
310
// reconciliation.
293
311
func TestSummarizeAndPatch_Intermediate (t * testing.T ) {
312
+ interval := 5 * time .Second
313
+
294
314
var testStageAConditions = Conditions {
295
315
Target : "StageA" ,
296
316
Owned : []string {"StageA" , "A1" , "A2" , "A3" },
@@ -335,7 +355,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
335
355
},
336
356
},
337
357
{
338
- name : "multiple Conditions" ,
358
+ name : "multiple Conditions, mixed results " ,
339
359
conditions : []Conditions {testStageAConditions , testStageBConditions },
340
360
beforeFunc : func (obj conditions.Setter ) {
341
361
conditions .MarkTrue (obj , "A3" , "ZZZ" , "zzz" ) // Negative polarity True.
@@ -365,7 +385,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
365
385
GenerateName : "test-" ,
366
386
},
367
387
Spec : sourcev1.GitRepositorySpec {
368
- Interval : metav1.Duration {Duration : 5 * time . Second },
388
+ Interval : metav1.Duration {Duration : interval },
369
389
},
370
390
Status : sourcev1.GitRepositoryStatus {
371
391
Conditions : []metav1.Condition {
@@ -386,6 +406,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
386
406
summaryHelper := NewHelper (record .NewFakeRecorder (32 ), patchHelper )
387
407
summaryOpts := []Option {
388
408
WithConditions (tt .conditions ... ),
409
+ WithResultBuilder (reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval }),
389
410
}
390
411
_ , err = summaryHelper .SummarizeAndPatch (ctx , obj , summaryOpts ... )
391
412
g .Expect (err ).ToNot (HaveOccurred ())
@@ -394,3 +415,62 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
394
415
})
395
416
}
396
417
}
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