9
9
"github.com/sirupsen/logrus/hooks/test"
10
10
11
11
"k8s.io/api/core/v1"
12
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
13
"k8s.io/apimachinery/pkg/labels"
13
14
"k8s.io/client-go/kubernetes/fake"
14
15
@@ -40,6 +41,7 @@ func (suite *Suite) TestNew() {
40
41
excludedWeekdays = []time.Weekday {time .Friday }
41
42
excludedTimesOfDay = []util.TimePeriod {util.TimePeriod {}}
42
43
excludedDaysOfYear = []time.Time {time .Now ()}
44
+ minimumAge = time .Duration (42 )
43
45
)
44
46
45
47
chaoskube := New (
@@ -51,6 +53,7 @@ func (suite *Suite) TestNew() {
51
53
excludedTimesOfDay ,
52
54
excludedDaysOfYear ,
53
55
time .UTC ,
56
+ minimumAge ,
54
57
logger ,
55
58
false ,
56
59
)
@@ -66,6 +69,7 @@ func (suite *Suite) TestNew() {
66
69
suite .Equal (time .UTC , chaoskube .Timezone )
67
70
suite .Equal (logger , chaoskube .Logger )
68
71
suite .Equal (false , chaoskube .DryRun )
72
+ suite .Equal (minimumAge , chaoskube .MinimumAge )
69
73
}
70
74
71
75
func (suite * Suite ) TestCandidates () {
@@ -108,6 +112,7 @@ func (suite *Suite) TestCandidates() {
108
112
[]time.Time {},
109
113
time .UTC ,
110
114
false ,
115
+ time .Duration (42 ),
111
116
)
112
117
113
118
suite .assertCandidates (chaoskube , tt .pods )
@@ -141,6 +146,7 @@ func (suite *Suite) TestVictim() {
141
146
[]time.Time {},
142
147
time .UTC ,
143
148
false ,
149
+ time .Duration (42 ),
144
150
)
145
151
146
152
suite .assertVictim (chaoskube , tt .victim )
@@ -157,6 +163,7 @@ func (suite *Suite) TestNoVictimReturnsError() {
157
163
[]util.TimePeriod {},
158
164
[]time.Time {},
159
165
time .UTC ,
166
+ time .Duration (42 ),
160
167
false ,
161
168
)
162
169
@@ -185,6 +192,7 @@ func (suite *Suite) TestDeletePod() {
185
192
[]time.Time {},
186
193
time .UTC ,
187
194
tt .dryRun ,
195
+ time .Duration (42 ),
188
196
)
189
197
190
198
victim := util .NewPod ("default" , "foo" )
@@ -414,6 +422,7 @@ func (suite *Suite) TestTerminateVictim() {
414
422
tt .excludedDaysOfYear ,
415
423
tt .timezone ,
416
424
false ,
425
+ time .Duration (42 ),
417
426
)
418
427
chaoskube .Now = tt .now
419
428
@@ -437,6 +446,7 @@ func (suite *Suite) TestTerminateNoVictimLogsInfo() {
437
446
[]util.TimePeriod {},
438
447
[]time.Time {},
439
448
time .UTC ,
449
+ time .Duration (42 ),
440
450
false ,
441
451
)
442
452
@@ -486,7 +496,7 @@ func (suite *Suite) assertLog(level log.Level, msg string, fields log.Fields) {
486
496
}
487
497
}
488
498
489
- func (suite * Suite ) setupWithPods (labelSelector labels.Selector , annotations labels.Selector , namespaces labels.Selector , excludedWeekdays []time.Weekday , excludedTimesOfDay []util.TimePeriod , excludedDaysOfYear []time.Time , timezone * time.Location , dryRun bool ) * Chaoskube {
499
+ func (suite * Suite ) setupWithPods (labelSelector labels.Selector , annotations labels.Selector , namespaces labels.Selector , excludedWeekdays []time.Weekday , excludedTimesOfDay []util.TimePeriod , excludedDaysOfYear []time.Time , timezone * time.Location , dryRun bool , minimumAge time. Duration ) * Chaoskube {
490
500
chaoskube := suite .setup (
491
501
labelSelector ,
492
502
annotations ,
@@ -495,6 +505,7 @@ func (suite *Suite) setupWithPods(labelSelector labels.Selector, annotations lab
495
505
excludedTimesOfDay ,
496
506
excludedDaysOfYear ,
497
507
timezone ,
508
+ minimumAge ,
498
509
dryRun ,
499
510
)
500
511
@@ -511,7 +522,7 @@ func (suite *Suite) setupWithPods(labelSelector labels.Selector, annotations lab
511
522
return chaoskube
512
523
}
513
524
514
- func (suite * Suite ) setup (labelSelector labels.Selector , annotations labels.Selector , namespaces labels.Selector , excludedWeekdays []time.Weekday , excludedTimesOfDay []util.TimePeriod , excludedDaysOfYear []time.Time , timezone * time.Location , dryRun bool ) * Chaoskube {
525
+ func (suite * Suite ) setup (labelSelector labels.Selector , annotations labels.Selector , namespaces labels.Selector , excludedWeekdays []time.Weekday , excludedTimesOfDay []util.TimePeriod , excludedDaysOfYear []time.Time , timezone * time.Location , minimumAge time. Duration , dryRun bool ) * Chaoskube {
515
526
logOutput .Reset ()
516
527
517
528
return New (
@@ -523,6 +534,7 @@ func (suite *Suite) setup(labelSelector labels.Selector, annotations labels.Sele
523
534
excludedTimesOfDay ,
524
535
excludedDaysOfYear ,
525
536
timezone ,
537
+ minimumAge ,
526
538
logger ,
527
539
dryRun ,
528
540
)
@@ -540,3 +552,103 @@ func (t ThankGodItsFriday) Now() time.Time {
540
552
blackFriday , _ := time .Parse (time .RFC1123 , "Fri, 24 Sep 1869 15:04:05 UTC" )
541
553
return blackFriday
542
554
}
555
+
556
+ func (suite * Suite ) TestMinimumAge () {
557
+ type pod struct {
558
+ name string
559
+ namespace string
560
+ creationTime time.Time
561
+ }
562
+
563
+ for _ , tt := range []struct {
564
+ minimumAge time.Duration
565
+ now func () time.Time
566
+ pods []pod
567
+ candidates int
568
+ }{
569
+ // no minimum age set
570
+ {
571
+ time .Duration (0 ),
572
+ func () time.Time { return time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ) },
573
+ []pod {
574
+ {
575
+ name : "test1" ,
576
+ namespace : "test" ,
577
+ creationTime : time .Date (0 , 10 , 24 , 9 , 00 , 00 , 00 , time .UTC ),
578
+ },
579
+ },
580
+ 1 ,
581
+ },
582
+ // minimum age set, but pod is too young
583
+ {
584
+ time .Hour * 1 ,
585
+ func () time.Time { return time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ) },
586
+ []pod {
587
+ {
588
+ name : "test1" ,
589
+ namespace : "test" ,
590
+ creationTime : time .Date (0 , 10 , 24 , 9 , 30 , 00 , 00 , time .UTC ),
591
+ },
592
+ },
593
+ 0 ,
594
+ },
595
+ // one pod is too young, one matches
596
+ {
597
+ time .Hour * 1 ,
598
+ func () time.Time { return time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ) },
599
+ []pod {
600
+ // too young
601
+ {
602
+ name : "test1" ,
603
+ namespace : "test" ,
604
+ creationTime : time .Date (0 , 10 , 24 , 9 , 30 , 00 , 00 , time .UTC ),
605
+ },
606
+ // matches
607
+ {
608
+ name : "test2" ,
609
+ namespace : "test" ,
610
+ creationTime : time .Date (0 , 10 , 23 , 8 , 00 , 00 , 00 , time .UTC ),
611
+ },
612
+ },
613
+ 1 ,
614
+ },
615
+ // exact time - should not match
616
+ {
617
+ time .Hour * 1 ,
618
+ func () time.Time { return time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ) },
619
+ []pod {
620
+ {
621
+ name : "test1" ,
622
+ namespace : "test" ,
623
+ creationTime : time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ),
624
+ },
625
+ },
626
+ 0 ,
627
+ },
628
+ } {
629
+ chaoskube := suite .setup (
630
+ labels .Everything (),
631
+ labels .Everything (),
632
+ labels .Everything (),
633
+ []time.Weekday {},
634
+ []util.TimePeriod {},
635
+ []time.Time {},
636
+ time .UTC ,
637
+ tt .minimumAge ,
638
+ false ,
639
+ )
640
+ chaoskube .Now = tt .now
641
+
642
+ for _ , p := range tt .pods {
643
+ pod := util .NewPod (p .namespace , p .name )
644
+ pod .ObjectMeta .CreationTimestamp = metav1.Time {Time : p .creationTime }
645
+ _ , err := chaoskube .Client .Core ().Pods (pod .Namespace ).Create (& pod )
646
+ suite .Require ().NoError (err )
647
+ }
648
+
649
+ pods , err := chaoskube .Candidates ()
650
+ suite .Require ().NoError (err )
651
+
652
+ suite .Len (pods , tt .candidates )
653
+ }
654
+ }
0 commit comments