-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares_test.go
327 lines (291 loc) · 9.53 KB
/
middlewares_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"regexp"
"testing"
"time"
"gopkg.in/check.v1"
)
func Test(t *testing.T) { check.TestingT(t) }
type Suite struct{}
var _ = check.Suite(&Suite{})
func (s *Suite) TestSetupLogs(c *check.C) {
mockCases := newMockCases()
// setupLogs consumes stdout
mockCases[0].validate(c, setupLogs, mockStdout(""), mockCombined(""))
// setupLogs consumes stdout
mockCases[1].validate(c, setupLogs, mockStdout(""), mockCombined(""))
// setupLogs consumes stderr
mockCases[2].validate(c, setupLogs, mockStderr(""), mockCombined(""))
// setupLogs consumes errors
mockCases[3].validate(c, setupLogs, mockNoError{})
}
func (s *Suite) TestWriteSyslog(c *check.C) {
mockCases := newMockCases()
for _, cse := range mockCases {
cse.validate(c, writeSyslog)
}
}
func (s *Suite) TestInsertUUID(c *check.C) {
mockCases := newMockCases()
mockCases[0].validate(c, insertUUID, mockCombinedCheck(&check.Matches), mockCombined(".................... I am happy and ok\n"))
mockCases[1].validate(c, insertUUID, mockCombinedCheck(&check.Matches), mockCombined(".................... ERR: something went wrong\n"))
mockCases[2].validate(c, insertUUID, mockCombinedCheck(&check.Matches), mockCombined(".................... oops\n"))
for _, cse := range mockCases[3:4] {
c.Logf("test: %+v", cse)
cse.validate(c, insertUUID)
}
}
func (s *Suite) TestCombineLogs(c *check.C) {
// no cases since its already always inlined
}
func (s *Suite) TestHeaderize(c *check.C) {
mockCases := newMockCases()
base := "(?s)// start.*%s.*exitcode: 0\n"
mockCases[0].validate(c, headerize, mockCombinedCheck(&check.Matches), mockCombined(fmt.Sprintf(base, "I am happy and ok")))
mockCases[1].validate(c, headerize, mockCombinedCheck(&check.Matches), mockCombined(fmt.Sprintf(base, "ERR: something went wrong")))
mockCases[2].validate(c, headerize, mockCombinedCheck(&check.Matches), mockCombined(fmt.Sprintf(base, "oops")))
mockCases[3].validate(c, headerize, mockCombinedCheck(&check.Matches), mockCombined("(?s).*exitcode: 1\n$"))
mockCases[4].validate(c, headerize, mockCombinedCheck(&check.Matches), mockCombined("(?s).*error: problems\n$"))
}
func (s *Suite) TestSentryHandler(c *check.C) {
// disabled
mockCases := newMockCases()
for _, cse := range mockCases {
cse.validate(c, sentryHandler)
}
// http testserver
mux := http.NewServeMux()
mux.HandleFunc("/api/1/store/", func(w http.ResponseWriter, r *http.Request) {
var payload map[string]interface{}
json.NewDecoder(r.Body).Decode(&payload)
message, ok := payload["message"].(string)
c.Assert(ok, check.Equals, true)
c.Assert(message, check.Matches, `.*: echo running go tests \(problems\)`)
extra, ok := payload["extra"].(map[string]interface{})
c.Assert(ok, check.Equals, true)
command, ok := extra["command"].(string)
c.Assert(ok, check.Equals, true)
c.Assert(command, check.Equals, "echo running go tests")
out_combined, ok := extra["out_combined"].(string)
c.Assert(ok, check.Equals, true)
c.Assert(out_combined, check.Equals, "hi")
})
mux.HandleFunc("/api/3/store/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "called")
<-time.After(35 * time.Second)
})
server := httptest.NewServer(mux)
// enabled working
os.Setenv("CRONGUARD_SENTRY_DSN", fmt.Sprintf("http://testuser@%s/1", server.Listener.Addr().String()))
mockCases = newMockCases()
for _, cse := range mockCases[0:3] {
cse.validate(c, sentryHandler)
}
mockCases[4].validate(c, sentryHandler, mockNoError{})
// enabled broken
os.Setenv("CRONGUARD_SENTRY_DSN", fmt.Sprintf("http://%s/2", server.Listener.Addr().String()))
mockCases = newMockCases()
for _, cse := range mockCases {
cse.validate(c, sentryHandler, mockStderrCheck(&check.Matches), mockStderr("(?s).*empty username.*"))
}
// timeout
os.Setenv("CRONGUARD_SENTRY_DSN", fmt.Sprintf("http://testuser@%s/3", server.Listener.Addr().String()))
SentryTimeout = 2 * time.Second
mockCases = newMockCases()
mockCases[0].validate(c, sentryHandler)
mockCases[1].validate(c, sentryHandler)
mockCases[2].validate(c, sentryHandler)
mockCases[3].validate(c, sentryHandler)
mockCases[4].validate(c, sentryHandler)
}
func (s *Suite) TestQuietIgnore(c *check.C) {
mockCases := newMockCases()
for _, cse := range mockCases {
cse.validate(c, quietIgnore)
}
}
func (s *Suite) TestValidateStderr(c *check.C) {
mockCases := newMockCases()
mockCases[0].validate(c, validateStderr)
mockCases[1].validate(c, validateStderr)
mockCases[2].validate(c, validateStderr, mockError(fmt.Errorf("stderr is not empty")))
mockCases[3].validate(c, validateStderr)
mockCases[4].validate(c, validateStderr)
}
func (s *Suite) TestValidateStdout(c *check.C) {
mockCases := newMockCases()
mockCases[0].validate(c, validateStdout)
mockCases[1].validate(c, validateStdout, mockError(fmt.Errorf("bad keyword in command output: ERR: something went wrong")))
mockCases[2].validate(c, validateStdout)
mockCases[3].validate(c, validateStdout)
mockCases[4].validate(c, validateStdout)
}
func (s *Suite) TestTimeout(c *check.C) {
mockCases := newMockCases()
for _, cse := range mockCases {
cse.validate(c, timeout)
}
}
type (
mockStdout string
mockStderr string
mockCombined string
mockExitcode int
mockError error
mockNoError struct{}
mockStdoutCheck *check.Checker
mockStderrCheck *check.Checker
mockCombinedCheck *check.Checker
mockExitcodeCheck *check.Checker
mockErrorCheck *check.Checker
mockCase struct {
cr *CmdRequest
name string
f GuardFunc
defaultStdout string
defaultStderr string
defaultCombined string
defaultExitcode int
defaultError error
}
)
func noop() GuardFunc {
return func(context.Context, *CmdRequest) error {
return nil
}
}
func (mc mockCase) validate(c *check.C, gf func(GuardFunc) GuardFunc, overwrites ...interface{}) {
f, _ := ioutil.TempFile("", "")
defer f.Close()
defer os.Remove(f.Name())
cr := &CmdRequest{}
cr.Command = "echo running go tests"
cr.ErrFile = f.Name()
cr.Status = &CmdStatus{}
cr.Regex = regexp.MustCompile(`(?im)\b(err|fail|crit)`)
combined := bytes.NewBuffer([]byte{})
cr.Status.Combined = combined
stdout := bytes.NewBuffer([]byte{})
cr.Status.Stdout = stdout
stderr := bytes.NewBuffer([]byte{})
cr.Status.Stderr = stderr
err := gf(combineLogs(mc.f))(context.Background(), cr)
shouldStdout := mc.defaultStdout
shouldStderr := mc.defaultStderr
shouldCombined := mc.defaultCombined
shouldExitcode := mc.defaultExitcode
shouldError := mc.defaultError
stdoutCheck := check.DeepEquals
stderrCheck := check.DeepEquals
combinedCheck := check.DeepEquals
exitcodeCheck := check.DeepEquals
errorCheck := check.DeepEquals
for _, overwrite := range overwrites {
switch casted := overwrite.(type) {
case mockStdout:
shouldStdout = string(casted)
case mockStderr:
shouldStderr = string(casted)
case mockCombined:
shouldCombined = string(casted)
case mockExitcode:
shouldExitcode = int(casted)
case mockError:
shouldError = error(casted)
case mockNoError:
shouldError = nil
case mockStdoutCheck:
stdoutCheck = *casted
case mockStderrCheck:
stderrCheck = *casted
case mockCombinedCheck:
combinedCheck = *casted
case mockExitcodeCheck:
exitcodeCheck = *casted
case mockErrorCheck:
errorCheck = *casted
default:
c.Fatalf("unsupported overwrite: %T %+v", overwrite, overwrite)
}
}
c.Assert(stdout.String(), stdoutCheck, shouldStdout)
c.Assert(stderr.String(), stderrCheck, shouldStderr)
c.Assert(combined.String(), combinedCheck, shouldCombined)
c.Assert(cr.Status.ExitCode, exitcodeCheck, shouldExitcode)
c.Assert(err, errorCheck, shouldError)
}
// mockCases are default tests that expect the result is the same as the input
// NOTE: a GuardFunc that implements fail/nonfail logic should have testcases adapted to its logic
func newMockCases() []mockCase {
return []mockCase{
{
name: "everything ok",
f: mockRunner("I am happy and ok\n", "", 0, nil),
defaultStdout: "I am happy and ok\n",
defaultStderr: "",
defaultCombined: "I am happy and ok\n",
defaultExitcode: 0,
defaultError: nil,
},
{
name: "bad keyword",
f: mockRunner("ERR: something went wrong\n", "", 0, nil),
defaultStdout: "ERR: something went wrong\n",
defaultStderr: "",
defaultCombined: "ERR: something went wrong\n",
defaultExitcode: 0,
defaultError: nil,
},
{
name: "stderr not empty",
f: mockRunner("", "oops\n", 0, nil),
defaultStdout: "",
defaultStderr: "oops\n",
defaultCombined: "oops\n",
defaultExitcode: 0,
defaultError: nil,
},
{
name: "exitcode node zero",
f: mockRunner("", "", 1, nil),
defaultStdout: "",
defaultStderr: "",
defaultCombined: "",
defaultExitcode: 1,
defaultError: nil,
},
{
name: "an error was returned",
f: mockRunner("hi", "", 0, fmt.Errorf("problems")),
defaultStdout: "hi",
defaultStderr: "",
defaultCombined: "hi",
defaultExitcode: 0,
defaultError: fmt.Errorf("problems"),
},
}
}
func mockRunner(stdout, stderr string, exitcode int, mockErr error) GuardFunc {
return func(ctx context.Context, cr *CmdRequest) error {
err := error(nil)
_, err = io.WriteString(cr.Status.Stdout, stdout)
if err != nil {
return err
}
_, err = io.WriteString(cr.Status.Stderr, stderr)
if err != nil {
return err
}
cr.Status.ExitCode = exitcode
return mockErr
}
}