-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernary_test.go
215 lines (196 loc) · 5.27 KB
/
kubernary_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package kubernary
import (
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/pkg/errors"
)
const testWillTimeout string = "timeout"
type predictableChecker struct {
name string
m sync.Mutex
err error
r int
do func()
}
func (c *predictableChecker) Check() error {
if c.do != nil {
c.do()
}
c.run()
return c.err
}
func (c *predictableChecker) Name() string {
return c.name
}
func (c *predictableChecker) run() {
c.m.Lock()
c.r++
c.m.Unlock()
}
func (c *predictableChecker) runs() int {
c.m.Lock()
defer c.m.Unlock()
return c.r
}
var checkerTests = []struct {
cfgs []*CheckConfig
cancelImmediately bool
}{
{
cfgs: []*CheckConfig{
&CheckConfig{
Checker: &predictableChecker{name: "pass"},
Interval: 100 * time.Millisecond,
Timeout: 100 * time.Millisecond,
},
&CheckConfig{
Checker: &predictableChecker{name: "passmore"},
Interval: 100 * time.Millisecond,
Timeout: 200 * time.Millisecond,
},
},
cancelImmediately: false,
},
{
cfgs: []*CheckConfig{
&CheckConfig{
// Sleep for longer than the longest timeout of this config check (i.e. 200ms).
Checker: &predictableChecker{name: testWillTimeout, do: func() { time.Sleep(300 * time.Millisecond) }},
Interval: 100 * time.Millisecond,
Timeout: 100 * time.Millisecond,
},
&CheckConfig{
Checker: &predictableChecker{name: "suchpass"},
Interval: 100 * time.Millisecond,
Timeout: 200 * time.Millisecond,
},
},
// Cancel this immediately during the RunChecksForever test because the
// check that times out will result in less passing checks than the
// calculation expects.
cancelImmediately: true,
},
{
cfgs: []*CheckConfig{
&CheckConfig{
Checker: &predictableChecker{name: "verypass"},
Interval: 500 * time.Millisecond,
Timeout: 2 * time.Second,
},
&CheckConfig{
Checker: &predictableChecker{name: "failfailfail", err: errors.New("Boom!")},
Interval: 100 * time.Millisecond,
Timeout: 2 * time.Second,
},
},
cancelImmediately: false,
},
}
func longestIntervalOf(cfgs []*CheckConfig) time.Duration {
interval := 0 * time.Nanosecond
for _, cfg := range cfgs {
if cfg.Interval > interval {
interval = cfg.Interval
}
}
return interval
}
func TestChecker(t *testing.T) {
t.Run("RunChecksForever", func(t *testing.T) {
for _, tt := range checkerTests {
cancel := RunChecksForever(tt.cfgs)
var longestInterval time.Duration
if !tt.cancelImmediately {
longestInterval = longestIntervalOf(tt.cfgs)
t.Logf("Sleeping for longest interval %s + 20ms", longestInterval)
time.Sleep(longestInterval + 20*time.Millisecond)
}
cancel()
for _, cfg := range tt.cfgs {
expectedRuns := 0
if longestInterval > 0*time.Nanosecond && cfg.Interval > 0*time.Nanosecond {
// Expected runs should be longest interval / interval
expectedRuns = int(longestInterval / cfg.Interval)
t.Logf("Expected runs for %s: %v (%s / %s)", cfg.Checker.Name(), expectedRuns, longestInterval, cfg.Interval)
}
p, ok := cfg.Checker.(*predictableChecker)
if !ok {
t.Fatal("cfg.Checker: wanted predictableChecker")
}
if p.runs() != expectedRuns {
t.Errorf("%s p.runs(): Want %d, got %d", cfg.Checker.Name(), expectedRuns, p.runs())
continue
}
}
}
})
t.Run("CheckHandlers", func(t *testing.T) {
for _, tt := range checkerTests {
checksWillFail := false
for _, cfg := range tt.cfgs {
p, ok := cfg.Checker.(*predictableChecker)
if !ok {
t.Fatal("cfg.Checker: wanted predictableChecker")
}
if p.err != nil {
checksWillFail = true
t.Logf("Config %s has failing checks.", cfg.Checker.Name())
}
if cfg.Checker.Name() == testWillTimeout {
checksWillFail = true
}
}
w := httptest.NewRecorder()
ChecksHandler(tt.cfgs)(w, httptest.NewRequest("GET", "/", nil))
expectedStatus := http.StatusOK
if checksWillFail {
expectedStatus = http.StatusServiceUnavailable
t.Log("This check config set should fail.")
}
if w.Code != expectedStatus {
t.Errorf("w.Code: want %v, got %v", expectedStatus, w.Code)
continue
}
results := &map[string]*e{}
if err := json.Unmarshal(w.Body.Bytes(), results); err != nil {
t.Errorf("json.Unmarshal(%v, %s): %v", w.Body, results, err)
}
}
})
t.Run("CheckHandler", func(t *testing.T) {
for _, tt := range checkerTests {
cfg := tt.cfgs[0]
p, ok := cfg.Checker.(*predictableChecker)
if !ok {
t.Fatal("cfg.Checker: wanted predictableChecker")
}
checksWillFail := false
if p.err != nil {
checksWillFail = true
t.Logf("Config %s has failing checks.", cfg.Checker.Name())
}
if cfg.Checker.Name() == testWillTimeout {
checksWillFail = true
}
w := httptest.NewRecorder()
CheckHandler(tt.cfgs[0])(w, httptest.NewRequest("GET", "/", nil))
expectedStatus := http.StatusOK
if checksWillFail {
expectedStatus = http.StatusServiceUnavailable
t.Log("This check config set should fail.")
}
if w.Code != expectedStatus {
t.Errorf("w.Code: want %v, got %v", expectedStatus, w.Code)
continue
}
result := &e{}
if err := json.Unmarshal(w.Body.Bytes(), result); err != nil {
t.Errorf("json.Unmarshal(%v, %s): %v", w.Body, result, err)
}
}
})
}