-
Notifications
You must be signed in to change notification settings - Fork 0
/
guard_test.go
124 lines (107 loc) · 3.3 KB
/
guard_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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
type (
tCase struct {
command string
additionalArgs []string
content string
}
)
func guard(t *testing.T, additionalArgs []string, command string, want string) (err error) {
fmt.Printf("running %s\n", command)
tempFile, err := ioutil.TempFile("", "guard")
if err != nil {
return fmt.Errorf("unable to create tmp file (%s): %s", tempFile.Name(), err)
}
os.Args = []string{
"_",
"-errfile-no-uuid",
"-errfile-quiet",
"-name", "test",
"-errfile", tempFile.Name(),
}
os.Args = append(os.Args, additionalArgs...)
os.Args = append(os.Args, command)
main()
got, err := ioutil.ReadAll(tempFile)
if err != nil {
return
}
if strings.ContainsRune(string(got), '\x00') {
return fmt.Errorf("Nullbyte")
}
if diff := cmp.Diff(want, string(got)); diff != "" {
return fmt.Errorf("mismatch (-want +got):\n%s", diff)
}
tempFile.Close()
err = os.Remove(tempFile.Name())
if err != nil && !os.IsNotExist(err) {
return nil
}
return err
}
func TestOutput(t *testing.T) {
var err error
// test normal
cases := []tCase{
// check exit statuss
{"true", []string{}, ""},
{"false", []string{}, "// error: exit status 1\n"},
{"exit 2", []string{}, "// error: exit status 2\n"},
// check output
{"echo fail", []string{}, "fail\n// error: bad keyword in command output: fail\n"},
{"echo failure", []string{}, "failure\n// error: bad keyword in command output: failure\n"},
{"echo ERR", []string{}, "ERR\n// error: bad keyword in command output: ERR\n"},
{"echo ERROR", []string{}, "ERROR\n// error: bad keyword in command output: ERROR\n"},
{"echo Crit", []string{}, "Crit\n// error: bad keyword in command output: Crit\n"},
{"echo Critical", []string{}, "Critical\n// error: bad keyword in command output: Critical\n"},
{`echo -e "err\ngood line\n"`, []string{}, "err\ngood line\n\n// error: bad keyword in command output: err\n"},
// check err output
{"echo Hi there 1>&2", []string{}, "Hi there\n// error: stderr is not empty\n"},
// check asci boundaries
{"echo transferred", []string{}, ""},
{"echo transferred error", []string{}, "transferred error\n// error: bad keyword in command output: transferred error\n"},
// quiet tests
{"false", []string{"-quiet-times", "0 * * * *:1h"}, ""},
{"false", []string{"-quiet-times", "0 0 * * *:0s"}, "// error: exit status 1\n"},
// timeout tests
{"sleep 1", []string{"-timeout", "2s"}, ""},
{"sleep 2", []string{"-timeout", "500ms"}, "// error: context deadline exceeded\n"},
}
for i, c := range cases {
t.Logf("running case %d: %+v", i+1, c)
err = guard(t, c.additionalArgs, c.command, c.content)
if err != nil {
t.Error(err)
break
}
}
// paralell tests
cases = []tCase{
// lockfile tests
{"sleep 2; echo failed", []string{"-lockfile", "/tmp/guard.lock"}, "failed\n// error: bad keyword in command output: failed\n"},
{"echo failed this should not run; sleep 3", []string{"-lockfile", "/tmp/guard.lock"}, ""},
}
errChan := make(chan error)
for _, c := range cases {
<-time.After(1 * time.Second)
go func(c tCase) {
err := guard(t, c.additionalArgs, c.command, c.content)
errChan <- err
}(c)
}
for i := 0; i < len(cases); i++ {
err = <-errChan
if err != nil {
t.Error(err)
}
}
}