-
Notifications
You must be signed in to change notification settings - Fork 6
/
runner_test.go
733 lines (584 loc) · 19 KB
/
runner_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Copyright 2015 PagerDuty, Inc., et al.
// Copyright 2016-2017 Tim Heckman
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"regexp"
"runtime"
"strconv"
"time"
"github.com/theckman/go-flock"
. "gopkg.in/check.v1"
)
func (t *TestSuite) Test_handleCommand(c *C) {
//
// Test a command that finishes in 0.3 seconds
//
t.h.cmd = exec.Command("/usr/bin/time", "-p", "/bin/sleep", "0.3")
retCode, r, runTime, err := handleCommand(t.h)
c.Assert(err, IsNil)
c.Check(retCode, Equals, 0)
stat, ok := <-t.out
c.Assert(ok, Equals, true)
timeStatRegex := regexp.MustCompile("^cronner.testCmd.time:([0-9\\.]+)\\|ms$")
match := timeStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
statFloat, err := strconv.ParseFloat(match[0][1], 64)
c.Assert(err, IsNil)
c.Check(statFloat, Equals, runTime)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
retStatRegex := regexp.MustCompile("^cronner.testCmd.exit_code:([0-9\\.]+)\\|g$")
match = retStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
retFloat, err := strconv.ParseFloat(match[0][1], 64)
c.Assert(err, IsNil)
c.Check(retFloat, Equals, float64(0))
var timely bool
// assume the command run time will be within 20ms of correct,
// note sure how tight we can make this window without incurring
// false-failures.
if runTime > 300 && runTime < 320 {
timely = true
}
c.Assert(timely, Equals, true)
timeRegex := regexp.MustCompile("((?m)^real[[:space:]]+([0-9\\.]+)$)")
match = timeRegex.FindAllStringSubmatch(string(r), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 3)
c.Check(match[0][2], Equals, "0.30")
//
// Test a command that finishes in 1 second
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
timely = false
retCode = -512
timeRegex = nil
t.h.cmd = exec.Command("/usr/bin/time", "-p", "/bin/sleep", "1")
retCode, r, runTime, err = handleCommand(t.h)
c.Assert(err, IsNil)
c.Check(retCode, Equals, 0)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
match = timeStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
statFloat, err = strconv.ParseFloat(match[0][1], 64)
c.Assert(err, IsNil)
c.Check(statFloat, Equals, runTime)
if runTime > 1000 && runTime < 1020 {
timely = true
}
c.Check(timely, Equals, true)
timeRegex = regexp.MustCompile("((?m)^real[[:space:]]+([0-9\\.]+)$)")
match = timeRegex.FindAllStringSubmatch(string(r), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 3)
c.Check(match[0][2], Equals, "1.00")
stat, ok = <-t.out
c.Assert(ok, Equals, true)
match = retStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
retFloat, err = strconv.ParseFloat(match[0][1], 64)
c.Assert(err, IsNil)
c.Check(retFloat, Equals, float64(0))
//
// Test a valid return code is given
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
retCode = -512
switch runtime.GOOS {
case "linux":
t.h.cmd = exec.Command("/bin/false")
case "darwin":
t.h.cmd = exec.Command("/usr/bin/false")
}
retCode, r, runTime, err = handleCommand(t.h)
c.Assert(err, Not(IsNil))
c.Check(retCode, Equals, 1)
_, ok = <-t.out
c.Assert(ok, Equals, true)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
match = retStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
retFloat, err = strconv.ParseFloat(match[0][1], 64)
c.Assert(err, IsNil)
c.Check(retFloat, Equals, float64(1))
//
// Test that DD events work
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
retCode = -512
t.h.cmd = exec.Command("/bin/echo", "somevalue")
t.h.opts.AllEvents = true
retCode, r, runTime, err = handleCommand(t.h)
c.Assert(err, IsNil)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{35,44}:Cron testCmd starting on brainbox01|UUID: %v\n|k:%v|s:cronner|t:info|#source_type:cronner,cronner_label_name:testCmd`, t.h.uuid, t.h.uuid),
)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
match = timeStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
c.Check(strconv.FormatFloat(runTime, 'f', -1, 64), Equals, match[0][1])
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(string(stat), Equals, "cronner.testCmd.exit_code:0|g")
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{55,77}:Cron testCmd succeeded in %.5f seconds on brainbox01|UUID: %v\nexit code: 0\noutput: somevalue\n|k:%v|s:cronner|t:success|#source_type:cronner,cronner_label_name:testCmd`, runTime/1000, t.h.uuid, t.h.uuid),
)
//
// Test that DD events contain the cronner_group tag
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
t.h.cmd = exec.Command("/bin/echo", "somevalue")
t.h.opts.EventGroup = "testgroup"
_, r, runTime, err = handleCommand(t.h)
c.Assert(err, IsNil)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{35,44}:Cron testCmd starting on brainbox01|UUID: %v\n|k:%v|s:cronner|t:info|#source_type:cronner,cronner_label_name:testCmd,cronner_group:testgroup`, t.h.uuid, t.h.uuid),
)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
match = timeStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
c.Check(strconv.FormatFloat(runTime, 'f', -1, 64), Equals, match[0][1])
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(string(stat), Equals, "cronner.testCmd.exit_code:0|g")
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{55,77}:Cron testCmd succeeded in %.5f seconds on brainbox01|UUID: %v\nexit code: 0\noutput: somevalue\n|k:%v|s:cronner|t:success|#source_type:cronner,cronner_label_name:testCmd,cronner_group:testgroup`, runTime/1000, t.h.uuid, t.h.uuid),
)
//
// Test that DD metrics contain the cronner_group tag
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
t.h.cmd = exec.Command("/bin/echo", "somevalue")
t.h.opts.Group = "metricgroup"
t.h.opts.EventGroup = ""
_, r, runTime, err = handleCommand(t.h)
c.Assert(err, IsNil)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{35,44}:Cron testCmd starting on brainbox01|UUID: %v\n|k:%v|s:cronner|t:info|#source_type:cronner,cronner_label_name:testCmd`, t.h.uuid, t.h.uuid),
)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
timeStatTagRegex := regexp.MustCompile("^cronner.testCmd.time:([0-9\\.]+)\\|ms\\|#cronner_group:([a-z]+)$")
match = timeStatTagRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 3)
c.Check(strconv.FormatFloat(runTime, 'f', -1, 64), Equals, match[0][1])
c.Check("metricgroup", Equals, match[0][2])
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(string(stat), Equals, "cronner.testCmd.exit_code:0|g|#cronner_group:metricgroup")
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{55,77}:Cron testCmd succeeded in %.5f seconds on brainbox01|UUID: %v\nexit code: 0\noutput: somevalue\n|k:%v|s:cronner|t:success|#source_type:cronner,cronner_label_name:testCmd`, runTime/1000, t.h.uuid, t.h.uuid),
)
//
// Test that parent tags are emitted
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
t.h.cmd = exec.Command("/bin/echo", "somevalue")
t.h.opts.Group = "metricgroup"
t.h.opts.EventGroup = ""
t.h.opts.Parent = true
t.h.parentEventTags = []string{"cronner_parent_uuid:" + testCronnerUUID, "cronner_parent_event_group:testParentEventGroup"}
t.h.parentMetricTags = []string{"cronner_parent_group:testParentGroup", "cronner_parent_label:testParent"}
_, r, runTime, err = handleCommand(t.h)
c.Assert(err, IsNil)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(
`_e{35,44}:Cron testCmd starting on brainbox01|UUID: %v\n|k:%v|s:cronner|t:info|#source_type:cronner,cronner_label_name:testCmd,cronner_parent_uuid:%s,cronner_parent_event_group:testParentEventGroup`,
t.h.uuid, t.h.uuid, testCronnerUUID,
),
)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
timeStatTagRegex = regexp.MustCompile("^cronner.testCmd.time:([0-9\\.]+)\\|ms\\|#cronner_group:([a-z]+),cronner_parent_group:testParentGroup,cronner_parent_label:testParent$")
match = timeStatTagRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 3)
c.Check(strconv.FormatFloat(runTime, 'f', -1, 64), Equals, match[0][1])
c.Check("metricgroup", Equals, match[0][2])
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(string(stat), Equals, "cronner.testCmd.exit_code:0|g|#cronner_group:metricgroup,cronner_parent_group:testParentGroup,cronner_parent_label:testParent")
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(
`_e{55,77}:Cron testCmd succeeded in %.5f seconds on brainbox01|UUID: %v\nexit code: 0\noutput: somevalue\n|k:%v|s:cronner|t:success|#source_type:cronner,cronner_label_name:testCmd,cronner_parent_uuid:%s,cronner_parent_event_group:testParentEventGroup`,
runTime/1000, t.h.uuid, t.h.uuid, testCronnerUUID,
),
)
//
// Test that DD events contain additional tags
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
t.h.cmd = exec.Command("/bin/echo", "somevalue")
t.h.opts.EventGroup = "testgroup"
t.h.opts.Group = ""
t.h.opts.Parent = false
t.h.parentEventTags = nil
t.h.parentMetricTags = nil
t.h.opts.Tags = append(t.h.opts.Tags, "tag1:val1")
_, r, runTime, err = handleCommand(t.h)
c.Assert(err, IsNil)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{35,44}:Cron testCmd starting on brainbox01|UUID: %v\n|k:%v|s:cronner|t:info|#source_type:cronner,cronner_label_name:testCmd,cronner_group:testgroup,%v`, t.h.uuid, t.h.uuid, t.h.opts.Tags[0]),
)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
timeStatRegex = regexp.MustCompile("^cronner.testCmd.time:([0-9\\.]+)\\|ms\\|#tag1:val1$")
match = timeStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
c.Check(strconv.FormatFloat(runTime, 'f', -1, 64), Equals, match[0][1])
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(string(stat), Equals, fmt.Sprintf("cronner.testCmd.exit_code:0|g|#%v", t.h.opts.Tags[0]))
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{55,77}:Cron testCmd succeeded in %.5f seconds on brainbox01|UUID: %v\nexit code: 0\noutput: somevalue\n|k:%v|s:cronner|t:success|#source_type:cronner,cronner_label_name:testCmd,cronner_group:testgroup,%v`, runTime/1000, t.h.uuid, t.h.uuid, t.h.opts.Tags[0]),
)
//
// Test that DD metrics contain additional tags
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
t.h.cmd = exec.Command("/bin/echo", "somevalue")
t.h.opts.EventGroup = ""
t.h.opts.Group = "metricgroup"
t.h.opts.Tags = append(t.h.opts.Tags, "tag1:val1")
_, r, runTime, err = handleCommand(t.h)
c.Assert(err, IsNil)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{35,44}:Cron testCmd starting on brainbox01|UUID: %v\n|k:%v|s:cronner|t:info|#source_type:cronner,cronner_label_name:testCmd,%v`, t.h.uuid, t.h.uuid,t.h.opts.Tags[0]),
)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
timeStatTagRegex = regexp.MustCompile("^cronner.testCmd.time:([0-9\\.]+)\\|ms\\|#cronner_group:([a-z]+),tag1:val1$")
match = timeStatTagRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 3)
c.Check(strconv.FormatFloat(runTime, 'f', -1, 64), Equals, match[0][1])
c.Check("metricgroup", Equals, match[0][2])
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(string(stat), Equals, "cronner.testCmd.exit_code:0|g|#cronner_group:metricgroup,tag1:val1")
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{55,77}:Cron testCmd succeeded in %.5f seconds on brainbox01|UUID: %v\nexit code: 0\noutput: somevalue\n|k:%v|s:cronner|t:success|#source_type:cronner,cronner_label_name:testCmd,tag1:val1`, runTime/1000, t.h.uuid, t.h.uuid),
)
//
// Test that no output is given
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
t.h.cmd = exec.Command("/bin/echo", "something")
t.h.opts.EventGroup = ""
t.h.opts.Group = ""
t.h.opts.LogFail = false
t.h.opts.Lock = true
t.h.opts.AllEvents = false
t.h.opts.Parent = false
t.h.parentEventTags = nil
t.h.parentMetricTags = nil
t.h.opts.Tags = nil
retCode, r, _, err = handleCommand(t.h)
c.Assert(err, IsNil)
c.Check(retCode, Equals, 0)
c.Check(len(r), Equals, 0)
// clear the statsd return channel
_, ok = <-t.out
c.Assert(ok, Equals, true)
_, ok = <-t.out
c.Assert(ok, Equals, true)
//
// Test that locking fails properly when unable to acquire lock
//
// Reset variables used
err = nil
retCode = -512
lf := flock.NewFlock(t.lockFile)
c.Assert(lf, Not(IsNil))
locked, err := lf.TryLock()
c.Assert(err, IsNil)
c.Assert(locked, Equals, true)
retCode, _, _, err = handleCommand(t.h)
c.Assert(err, Not(IsNil))
c.Check(err.Error(), Equals, fmt.Sprintf("failed to obtain lock on '%v': locked by another process", t.lockFile))
c.Check(retCode, Equals, 200)
//
// Test that locking succeeds with a timeout
//
// Reset variables used
err = nil
retCode = -512
t.h.opts.WaitSeconds = 5
t.h.cmd = exec.Command("/bin/echo", "something")
go func() {
time.Sleep(time.Second * 3)
lf.Unlock()
}()
retCode, _, _, err = handleCommand(t.h)
c.Assert(err, IsNil)
c.Check(retCode, Equals, 0)
// clear the statsd return channel
_, ok = <-t.out
c.Assert(ok, Equals, true)
_, ok = <-t.out
c.Assert(ok, Equals, true)
//
// Test that locking fails when exceeding the timeout
//
// Reset variables used
err = nil
retCode = -512
t.h.opts.WaitSeconds = 1
t.h.cmd = exec.Command("/bin/echo", "something")
locked, err = lf.TryLock()
c.Assert(err, IsNil)
c.Assert(locked, Equals, true)
go func() {
time.Sleep(time.Second * 3)
lf.Unlock()
}()
retCode, _, _, err = handleCommand(t.h)
c.Assert(err, Not(IsNil))
c.Check(err.Error(), Equals, "timeout exceeded (1s) waiting for the file lock")
c.Check(retCode, Equals, 200)
//
// Test that warning Dogstatsd events are emitted if a
// command is taking too long to run
//
// Reset variables used
err = nil
retCode = -512
t.h.opts.Lock = false
t.h.opts.WarnAfter = 2
t.h.cmd = exec.Command("/bin/sleep", "3")
retCode, r, runTime, err = handleCommand(t.h)
c.Assert(err, IsNil)
c.Assert(retCode, Equals, 0)
c.Check(len(r), Equals, 0)
// clear the statsd return channel
stat, ok = <-t.out
c.Assert(ok, Equals, true)
c.Check(
string(stat),
Equals,
fmt.Sprintf(`_e{56,65}:Cron testCmd still running after 2 seconds on brainbox01|UUID: %v\nrunning for 2 seconds|k:%v|s:cronner|t:warning|#source_type:cronner,cronner_label_name:testCmd`, t.h.uuid, t.h.uuid),
)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
timeStatRegex = regexp.MustCompile("^cronner.testCmd.time:([0-9\\.]+)\\|ms$")
match = timeStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
statFloat, err = strconv.ParseFloat(match[0][1], 64)
c.Assert(err, IsNil)
c.Check(statFloat, Equals, runTime)
stat, ok = <-t.out
c.Assert(ok, Equals, true)
match = retStatRegex.FindAllStringSubmatch(string(stat), -1)
c.Assert(len(match), Equals, 1)
c.Assert(len(match[0]), Equals, 2)
retFloat, err = strconv.ParseFloat(match[0][1], 64)
c.Assert(err, IsNil)
c.Check(retFloat, Equals, float64(0))
//
// Test passthru to stdout/stderr
//
// Reset variables used
r = nil
err = nil
runTime = 0
match = nil
retCode = -512
t.h.cmd = exec.Command("/bin/bash", "testdata/echo.sh")
t.h.opts.Passthru = true
// Capture stdout/stderr
oldStdout := os.Stdout
oldStderr := os.Stderr
oReader, oWriter, err := os.Pipe()
if err != nil {
panic(err)
}
eReader, eWriter, err := os.Pipe()
if err != nil {
panic(err)
}
os.Stdout = oWriter
os.Stderr = eWriter
// copy the output in a separate goroutine (non-blocking)
outC := make(chan string)
errC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, oReader)
outC <- buf.String()
}()
go func() {
var buf bytes.Buffer
io.Copy(&buf, eReader)
errC <- buf.String()
}()
retCode, r, runTime, err = handleCommand(t.h)
// restore stdout/stderr back to normal state
oWriter.Close()
eWriter.Close()
os.Stdout = oldStdout
os.Stderr = oldStderr
stdout := <-outC
stderr := <-errC
// Check the output
c.Assert(err, IsNil)
c.Check(retCode, Equals, 0)
c.Assert(len(r), Equals, 0)
c.Assert(stdout, Equals, "stdout\nstdout\nstdout\nstdout\n")
c.Assert(stderr, Equals, "stderr\nstderr\nstderr\nstderr\n")
_, ok = <-t.out
c.Assert(ok, Equals, true)
}
func (t *TestSuite) Test_emitEvent(c *C) {
title := "TE"
body := "B"
label := "urmom"
alertType := "info"
t.h.opts.EventGroup = "testing"
emitEvent(title, body, label, alertType, t.h)
event, ok := <-t.out
c.Assert(ok, Equals, true)
eventStub := fmt.Sprintf("_e{%d,%d}:%v|%v|k:%v|s:cronner|t:%v|#source_type:cronner,cronner_label_name:urmom,cronner_group:testing", len(title), len(body), title, body, t.h.uuid, alertType)
eventStr := string(event)
c.Check(eventStr, Equals, eventStub)
//
// Test truncation
//
// generate a body that will be truncated
body = randString(4100)
title = "TE2"
label = "awwyiss"
alertType = "success"
t.h.opts.EventGroup = ""
emitEvent(title, body, label, alertType, t.h)
event, ok = <-t.out
c.Assert(ok, Equals, true)
// simulate truncation and addition of the truncation messsage
truncatedBody := fmt.Sprintf("%v...\\n=== OUTPUT TRUNCATED ===\\n%v", body[0:MaxBody/2], body[len(body)-((MaxBody/2)+1):len(body)-1])
eventStub = fmt.Sprintf("_e{%d,%d}:%v|%v|k:%v|s:cronner|t:%v|#source_type:cronner,cronner_label_name:awwyiss", len(title), len(truncatedBody), title, truncatedBody, t.h.uuid, alertType)
eventStr = string(event)
c.Check(eventStr, Equals, eventStub)
}
func (t *TestSuite) Test_writeOutput(c *C) {
tmpDir, err := ioutil.TempDir("/tmp", "cronner_test")
c.Assert(err, IsNil)
defer os.RemoveAll(tmpDir)
filename := path.Join(tmpDir, fmt.Sprintf("outfile-%v.out", randString(8)))
out := []byte("this is a test!")
ok := writeOutput(filename, out, false)
c.Assert(ok, Equals, true)
stat, err := os.Stat(filename)
c.Assert(err, IsNil)
c.Check(stat.Mode(), Equals, os.FileMode(0400))
file, err := os.Open(filename)
c.Assert(err, IsNil)
contents, err := ioutil.ReadAll(file)
c.Assert(err, IsNil)
c.Check(string(out), Equals, string(contents))
}