-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
83 lines (79 loc) · 1.92 KB
/
main_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
package main
import (
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/mackerelio/checkers"
"github.com/stretchr/testify/assert"
)
func TestRun(t *testing.T) {
cases := map[string]struct {
r *Runner
st checkers.Status
msg string
rebootRequired *string
rebootRequiredPerm fs.FileMode
}{
"OK if reboot-required file does not exist": {
r: &Runner{
Dir: t.TempDir(), Warning: true,
},
st: checkers.OK,
msg: "",
},
"Warning if reboot-required file exists": {
r: &Runner{
Dir: t.TempDir(),
},
st: checkers.WARNING,
msg: "System restart required",
rebootRequired: toPtr("a"),
rebootRequiredPerm: 0666,
},
"Warning if reboot-required file exists and warning flag is true": {
r: &Runner{
Dir: t.TempDir(), Warning: true,
},
st: checkers.WARNING,
msg: "System restart required",
rebootRequired: toPtr("a"),
rebootRequiredPerm: 0666,
},
"Critical if reboot-required file exists and critical flag is true": {
r: &Runner{
Dir: t.TempDir(), Critical: true,
},
st: checkers.CRITICAL,
msg: "System restart required",
rebootRequired: toPtr("a"),
rebootRequiredPerm: 0666,
},
"Unknown if both warning & critical flag is true": {
r: &Runner{
Dir: "", Warning: true, Critical: true,
},
st: checkers.UNKNOWN,
msg: "cannot specify both -critical and -warning",
},
}
for name, tt := range cases {
if tt.rebootRequired != nil {
if err := os.WriteFile(
filepath.Join(tt.r.Dir, "reboot-required"),
[]byte(*tt.rebootRequired),
tt.rebootRequiredPerm,
); err != nil {
t.Error(err)
}
}
t.Run(name, func(t *testing.T) {
st, msg := tt.r.Run()
assert.Equal(t, tt.st, st)
assert.Equal(t, tt.msg, msg)
})
}
}
func toPtr[T any](v T) *T {
return &v
}