@@ -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
@@ -216,7 +218,22 @@ func TestSummarizeAndPatch(t *testing.T) {
216
218
},
217
219
},
218
220
{
219
- name : "Success, multiple conditions summary" ,
221
+ name : "Success, multiple conditions True summary" ,
222
+ generation : 3 ,
223
+ beforeFunc : func (obj conditions.Setter ) {
224
+ conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "test-msg" )
225
+ conditions .MarkTrue (obj , "AAA" , "ZZZ" , "zzz" ) // Positive polarity True.
226
+ },
227
+ conditions : []Conditions {testReadyConditions , testFooConditions },
228
+ result : reconcile .ResultSuccess ,
229
+ assertConditions : []metav1.Condition {
230
+ * conditions .TrueCondition (meta .ReadyCondition , meta .SucceededReason , "test-msg" ),
231
+ * conditions .TrueCondition ("Foo" , "ZZZ" , "zzz" ), // True summary.
232
+ * conditions .TrueCondition ("AAA" , "ZZZ" , "zzz" ),
233
+ },
234
+ },
235
+ {
236
+ name : "Fail, multiple conditions mixed summary" ,
220
237
generation : 3 ,
221
238
beforeFunc : func (obj conditions.Setter ) {
222
239
conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "test-msg" )
@@ -231,6 +248,35 @@ func TestSummarizeAndPatch(t *testing.T) {
231
248
* conditions .TrueCondition ("BBB" , "YYY" , "yyy" ),
232
249
* conditions .TrueCondition ("AAA" , "ZZZ" , "zzz" ),
233
250
},
251
+ wantErr : true ,
252
+ },
253
+ {
254
+ name : "Fail, success result but Ready=False" ,
255
+ generation : 3 ,
256
+ beforeFunc : func (obj conditions.Setter ) {
257
+ conditions .MarkTrue (obj , sourcev1 .ArtifactOutdatedCondition , "NewRevision" , "new index revision" )
258
+ },
259
+ conditions : []Conditions {testReadyConditions },
260
+ result : reconcile .ResultSuccess ,
261
+ assertConditions : []metav1.Condition {
262
+ * conditions .FalseCondition (meta .ReadyCondition , "NewRevision" , "new index revision" ),
263
+ * conditions .TrueCondition (sourcev1 .ArtifactOutdatedCondition , "NewRevision" , "new index revision" ),
264
+ },
265
+ wantErr : true ,
266
+ },
267
+ {
268
+ name : "Success, success result but Ready=False" ,
269
+ generation : 3 ,
270
+ beforeFunc : func (obj conditions.Setter ) {
271
+ conditions .MarkTrue (obj , sourcev1 .ArtifactOutdatedCondition , "NewRevision" , "new index revision" )
272
+ },
273
+ conditions : []Conditions {testReadyConditions },
274
+ result : reconcile .ResultSuccess ,
275
+ assertConditions : []metav1.Condition {
276
+ * conditions .FalseCondition (meta .ReadyCondition , "NewRevision" , "new index revision" ),
277
+ * conditions .TrueCondition (sourcev1 .ArtifactOutdatedCondition , "NewRevision" , "new index revision" ),
278
+ },
279
+ wantErr : true ,
234
280
},
235
281
}
236
282
@@ -291,6 +337,8 @@ func TestSummarizeAndPatch(t *testing.T) {
291
337
// This tests the scenario where SummarizeAndPatch is used in the middle of
292
338
// reconciliation.
293
339
func TestSummarizeAndPatch_Intermediate (t * testing.T ) {
340
+ interval := 5 * time .Second
341
+
294
342
var testStageAConditions = Conditions {
295
343
Target : "StageA" ,
296
344
Owned : []string {"StageA" , "A1" , "A2" , "A3" },
@@ -335,7 +383,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
335
383
},
336
384
},
337
385
{
338
- name : "multiple Conditions" ,
386
+ name : "multiple Conditions, mixed results " ,
339
387
conditions : []Conditions {testStageAConditions , testStageBConditions },
340
388
beforeFunc : func (obj conditions.Setter ) {
341
389
conditions .MarkTrue (obj , "A3" , "ZZZ" , "zzz" ) // Negative polarity True.
@@ -365,7 +413,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
365
413
GenerateName : "test-" ,
366
414
},
367
415
Spec : sourcev1.GitRepositorySpec {
368
- Interval : metav1.Duration {Duration : 5 * time . Second },
416
+ Interval : metav1.Duration {Duration : interval },
369
417
},
370
418
Status : sourcev1.GitRepositoryStatus {
371
419
Conditions : []metav1.Condition {
@@ -386,6 +434,7 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
386
434
summaryHelper := NewHelper (record .NewFakeRecorder (32 ), patchHelper )
387
435
summaryOpts := []Option {
388
436
WithConditions (tt .conditions ... ),
437
+ WithResultBuilder (reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval }),
389
438
}
390
439
_ , err = summaryHelper .SummarizeAndPatch (ctx , obj , summaryOpts ... )
391
440
g .Expect (err ).ToNot (HaveOccurred ())
@@ -394,3 +443,62 @@ func TestSummarizeAndPatch_Intermediate(t *testing.T) {
394
443
})
395
444
}
396
445
}
446
+
447
+ func TestIsNonStalledSuccess (t * testing.T ) {
448
+ interval := 5 * time .Second
449
+
450
+ tests := []struct {
451
+ name string
452
+ beforeFunc func (obj conditions.Setter )
453
+ rb reconcile.RuntimeResultBuilder
454
+ recResult ctrl.Result
455
+ recErr error
456
+ wantResult bool
457
+ }{
458
+ {
459
+ name : "non stalled success" ,
460
+ rb : reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval },
461
+ recResult : ctrl.Result {RequeueAfter : interval },
462
+ wantResult : true ,
463
+ },
464
+ {
465
+ name : "stalled success" ,
466
+ beforeFunc : func (obj conditions.Setter ) {
467
+ conditions .MarkStalled (obj , "FooReason" , "test-msg" )
468
+ },
469
+ rb : reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval },
470
+ recResult : ctrl.Result {RequeueAfter : interval },
471
+ wantResult : false ,
472
+ },
473
+ {
474
+ name : "error result" ,
475
+ rb : reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval },
476
+ recResult : ctrl.Result {RequeueAfter : interval },
477
+ recErr : errors .New ("some-error" ),
478
+ wantResult : false ,
479
+ },
480
+ {
481
+ name : "non success result" ,
482
+ rb : reconcile.AlwaysRequeueResultBuilder {RequeueAfter : interval },
483
+ recResult : ctrl.Result {RequeueAfter : 2 * time .Second },
484
+ wantResult : false ,
485
+ },
486
+ {
487
+ name : "no result builder" ,
488
+ recResult : ctrl.Result {RequeueAfter : interval },
489
+ wantResult : false ,
490
+ },
491
+ }
492
+
493
+ for _ , tt := range tests {
494
+ t .Run (tt .name , func (t * testing.T ) {
495
+ g := NewWithT (t )
496
+
497
+ obj := & sourcev1.GitRepository {}
498
+ if tt .beforeFunc != nil {
499
+ tt .beforeFunc (obj )
500
+ }
501
+ g .Expect (isNonStalledSuccess (obj , tt .rb , tt .recResult , tt .recErr )).To (Equal (tt .wantResult ))
502
+ })
503
+ }
504
+ }
0 commit comments