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
)
@@ -64,6 +67,7 @@ func (suite *Suite) TestNew() {
64
67
suite .Equal (excludedTimesOfDay , chaoskube .ExcludedTimesOfDay )
65
68
suite .Equal (excludedDaysOfYear , chaoskube .ExcludedDaysOfYear )
66
69
suite .Equal (time .UTC , chaoskube .Timezone )
70
+ suite .Equal (minimumAge , chaoskube .MinimumAge )
67
71
suite .Equal (logger , chaoskube .Logger )
68
72
suite .Equal (false , chaoskube .DryRun )
69
73
}
@@ -107,6 +111,7 @@ func (suite *Suite) TestCandidates() {
107
111
[]util.TimePeriod {},
108
112
[]time.Time {},
109
113
time .UTC ,
114
+ time .Duration (0 ),
110
115
false ,
111
116
)
112
117
@@ -140,6 +145,7 @@ func (suite *Suite) TestVictim() {
140
145
[]util.TimePeriod {},
141
146
[]time.Time {},
142
147
time .UTC ,
148
+ time .Duration (0 ),
143
149
false ,
144
150
)
145
151
@@ -157,6 +163,7 @@ func (suite *Suite) TestNoVictimReturnsError() {
157
163
[]util.TimePeriod {},
158
164
[]time.Time {},
159
165
time .UTC ,
166
+ time .Duration (0 ),
160
167
false ,
161
168
)
162
169
@@ -184,6 +191,7 @@ func (suite *Suite) TestDeletePod() {
184
191
[]util.TimePeriod {},
185
192
[]time.Time {},
186
193
time .UTC ,
194
+ time .Duration (0 ),
187
195
tt .dryRun ,
188
196
)
189
197
@@ -413,6 +421,7 @@ func (suite *Suite) TestTerminateVictim() {
413
421
tt .excludedTimesOfDay ,
414
422
tt .excludedDaysOfYear ,
415
423
tt .timezone ,
424
+ time .Duration (0 ),
416
425
false ,
417
426
)
418
427
chaoskube .Now = tt .now
@@ -437,6 +446,7 @@ func (suite *Suite) TestTerminateNoVictimLogsInfo() {
437
446
[]util.TimePeriod {},
438
447
[]time.Time {},
439
448
time .UTC ,
449
+ time .Duration (0 ),
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 , minimumAge time. Duration , dryRun bool ) * 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
@@ -512,7 +523,7 @@ func (suite *Suite) setupWithPods(labelSelector labels.Selector, annotations lab
512
523
return chaoskube
513
524
}
514
525
515
- 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 {
526
+ 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 {
516
527
logOutput .Reset ()
517
528
518
529
return New (
@@ -524,6 +535,7 @@ func (suite *Suite) setup(labelSelector labels.Selector, annotations labels.Sele
524
535
excludedTimesOfDay ,
525
536
excludedDaysOfYear ,
526
537
timezone ,
538
+ minimumAge ,
527
539
logger ,
528
540
dryRun ,
529
541
)
@@ -541,3 +553,103 @@ func (t ThankGodItsFriday) Now() time.Time {
541
553
blackFriday , _ := time .Parse (time .RFC1123 , "Fri, 24 Sep 1869 15:04:05 UTC" )
542
554
return blackFriday
543
555
}
556
+
557
+ func (suite * Suite ) TestMinimumAge () {
558
+ type pod struct {
559
+ name string
560
+ namespace string
561
+ creationTime time.Time
562
+ }
563
+
564
+ for _ , tt := range []struct {
565
+ minimumAge time.Duration
566
+ now func () time.Time
567
+ pods []pod
568
+ candidates int
569
+ }{
570
+ // no minimum age set
571
+ {
572
+ time .Duration (0 ),
573
+ func () time.Time { return time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ) },
574
+ []pod {
575
+ {
576
+ name : "test1" ,
577
+ namespace : "test" ,
578
+ creationTime : time .Date (0 , 10 , 24 , 9 , 00 , 00 , 00 , time .UTC ),
579
+ },
580
+ },
581
+ 1 ,
582
+ },
583
+ // minimum age set, but pod is too young
584
+ {
585
+ time .Hour * 1 ,
586
+ func () time.Time { return time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ) },
587
+ []pod {
588
+ {
589
+ name : "test1" ,
590
+ namespace : "test" ,
591
+ creationTime : time .Date (0 , 10 , 24 , 9 , 30 , 00 , 00 , time .UTC ),
592
+ },
593
+ },
594
+ 0 ,
595
+ },
596
+ // one pod is too young, one matches
597
+ {
598
+ time .Hour * 1 ,
599
+ func () time.Time { return time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ) },
600
+ []pod {
601
+ // too young
602
+ {
603
+ name : "test1" ,
604
+ namespace : "test" ,
605
+ creationTime : time .Date (0 , 10 , 24 , 9 , 30 , 00 , 00 , time .UTC ),
606
+ },
607
+ // matches
608
+ {
609
+ name : "test2" ,
610
+ namespace : "test" ,
611
+ creationTime : time .Date (0 , 10 , 23 , 8 , 00 , 00 , 00 , time .UTC ),
612
+ },
613
+ },
614
+ 1 ,
615
+ },
616
+ // exact time - should not match
617
+ {
618
+ time .Hour * 1 ,
619
+ func () time.Time { return time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ) },
620
+ []pod {
621
+ {
622
+ name : "test1" ,
623
+ namespace : "test" ,
624
+ creationTime : time .Date (0 , 10 , 24 , 10 , 00 , 00 , 00 , time .UTC ),
625
+ },
626
+ },
627
+ 0 ,
628
+ },
629
+ } {
630
+ chaoskube := suite .setup (
631
+ labels .Everything (),
632
+ labels .Everything (),
633
+ labels .Everything (),
634
+ []time.Weekday {},
635
+ []util.TimePeriod {},
636
+ []time.Time {},
637
+ time .UTC ,
638
+ tt .minimumAge ,
639
+ false ,
640
+ )
641
+ chaoskube .Now = tt .now
642
+
643
+ for _ , p := range tt .pods {
644
+ pod := util .NewPod (p .namespace , p .name , v1 .PodRunning )
645
+ pod .ObjectMeta .CreationTimestamp = metav1.Time {Time : p .creationTime }
646
+ _ , err := chaoskube .Client .Core ().Pods (pod .Namespace ).Create (& pod )
647
+ suite .Require ().NoError (err )
648
+ }
649
+
650
+ pods , err := chaoskube .Candidates ()
651
+ suite .Require ().NoError (err )
652
+
653
+ suite .Len (pods , tt .candidates )
654
+ }
655
+ }
0 commit comments