forked from estafette/estafette-gke-preemptible-killer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitelist_test.go
133 lines (116 loc) · 3.62 KB
/
whitelist_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"testing"
"time"
)
// Test data
var (
a time.Time
b time.Time
c time.Time
)
func init() {
var err error
s := "2000-01-02T03:04:00Z"
a, err = time.Parse(time.RFC3339, s)
if err != nil {
panic(fmt.Sprintf("%v cannot be parsed: %v", s, err))
}
s = "2000-01-02T04:04:00Z"
b, err = time.Parse(time.RFC3339, s)
if err != nil {
panic(fmt.Sprintf("%v cannot be parsed: %v", s, err))
}
s = "2000-05-06T07:08:00Z"
c, err = time.Parse(time.RFC3339, s)
if err != nil {
panic(fmt.Sprintf("%v cannot be parsed: %v", s, err))
}
}
func TestMergeTimespans(t *testing.T) {
var i WhitelistInstance
i.initialize()
// Check that basic scenario works.
i.mergeTimespans(a, b, "+")
i.whitelistHours.IntervalsBetween(a, c, i.updateWhitelistSecondCount)
if i.whitelistSecondCount != 3600 {
t.Errorf("Expected 3600 seconds, got '%v'", i.whitelistSecondCount)
}
i.whitelistSecondCount = 0
// Check that merging a multi-day interval works.
i.mergeTimespans(b, c, "+")
i.whitelistHours.IntervalsBetween(a, c, i.updateWhitelistSecondCount)
if i.whitelistSecondCount != 10814640 {
t.Errorf("Expected 10814640 seconds, got '%v'", i.whitelistSecondCount)
}
i.whitelistSecondCount = 0
// Check that merging a zero interval with + doesn't change second count.
i.mergeTimespans(a, a, "+")
i.whitelistHours.IntervalsBetween(a, c, i.updateWhitelistSecondCount)
if i.whitelistSecondCount != 10814640 {
t.Errorf("Expected 10814640 seconds, got '%v'", i.whitelistSecondCount)
}
i.whitelistSecondCount = 0
// Check that merging a zero interval with - doesn't change second count.
i.mergeTimespans(a, a, "-")
i.whitelistHours.IntervalsBetween(a, c, i.updateWhitelistSecondCount)
if i.whitelistSecondCount != 10814640 {
t.Errorf("Expected 10814640 seconds, got '%v'", i.whitelistSecondCount)
}
i.whitelistSecondCount = 0
// Check that merging an interval with - works.
i.mergeTimespans(b, c, "-")
i.whitelistHours.IntervalsBetween(a, c, i.updateWhitelistSecondCount)
if i.whitelistSecondCount != 3600 {
t.Errorf("Expected 3600 seconds, got '%v'", i.whitelistSecondCount)
}
i.whitelistSecondCount = 0
// Check that merging with a wrong direction panics.
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on mergeTimespans(*)")
}
}()
i.mergeTimespans(a, b, "*")
}
func TestProcessHours(t *testing.T) {
var i WhitelistInstance
i.initialize()
// Check that argument parsing works.
i.whitelist = "00:00 - 04:00, 08:00 - 12:00, 16:00 - 20:00"
i.blacklist = "01:00 - 02:00, 06:00 - 14:00, 15:00 - 17:00"
i.parseArguments()
if i.whitelistSecondCount != 21600 {
t.Errorf("Expected 21600 seconds, got '%v'", i.whitelistSecondCount)
}
}
func TestUpdateWhitelistSecondCount(t *testing.T) {
var i WhitelistInstance
i.initialize()
// Check that basic scenario works.
i.updateWhitelistSecondCount(a, b)
if i.whitelistSecondCount != 3600 {
t.Errorf("Expected 3600 seconds, got '%v'", i.whitelistSecondCount)
}
i.whitelistSecondCount = 0
// Check that adding a multi-day interval works.
i.updateWhitelistSecondCount(b, c)
if i.whitelistSecondCount != 10811040 {
t.Errorf("Expected 10811040 seconds, got '%v'", i.whitelistSecondCount)
}
i.whitelistSecondCount = 0
// Check that adding a zero interval doesn't change second count.
i.updateWhitelistSecondCount(a, a)
if i.whitelistSecondCount != 0 {
t.Errorf("Expected 0 seconds, got '%v'", i.whitelistSecondCount)
}
i.whitelistSecondCount = 0
// Check that adding a reverse interval panics.
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic when adding '%v - %v'", b, a)
}
}()
i.updateWhitelistSecondCount(b, a)
}