forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goss_test.go
155 lines (126 loc) · 3.41 KB
/
goss_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
package goss
import (
"bytes"
"encoding/json"
"os"
"testing"
"github.com/goss-org/goss/outputs"
"github.com/goss-org/goss/resource"
"github.com/goss-org/goss/util"
)
func checkErr(t *testing.T, err error, format string, a ...any) {
t.Helper()
if err == nil {
return
}
t.Fatalf(format+": "+err.Error(), a...)
}
func TestConfigMerge(t *testing.T) {
var g1json = `file:
/etc/passwd:
exists: true
mode: "0644"
size: 1722
owner: root
group: root
filetype: file
contains: []`
var g2json = `service:
sshd:
enabled: true
running: true
`
g1, err := ReadJSONData([]byte(g1json), true)
checkErr(t, err, "reading g1 failed")
_, ok := g1.Services["sshd"]
if ok {
t.Fatalf("did not expect sshd service")
}
g2, err := ReadJSONData([]byte(g2json), true)
checkErr(t, err, "reading g1 failed")
g1.Merge(g2)
_, ok = g1.Files["/etc/passwd"]
if !ok {
t.Fatalf("expected passwd file, got none")
}
_, ok = g1.Services["sshd"]
if !ok {
t.Fatalf("expected sshd service, got none")
}
}
func TestUseAsPackage(t *testing.T) {
output := &bytes.Buffer{}
// temp spec file
fh, err := os.CreateTemp("", "*.yaml")
checkErr(t, err, "temp file failed")
fh.Close()
// new config that doesnt spam output etc
cfg, err := util.NewConfig(util.WithFormatOptions("pretty"), util.WithResultWriter(output), util.WithSpecFile(fh.Name()))
checkErr(t, err, "new config failed")
// adds the os tmp dir to the goss spec file
err = AddResources(fh.Name(), "File", []string{os.TempDir()}, cfg)
checkErr(t, err, "could not add resource %q", os.TempDir())
// validate and sanity check, compare structured vs direct results etc
results, err := ValidateResults(cfg)
checkErr(t, err, "check failed")
found := 0
passed := 0
for rg := range results {
for _, r := range rg {
found++
if r.Result == resource.SUCCESS {
passed++
}
}
}
code, err := Validate(cfg)
checkErr(t, err, "check failed")
if code != 0 {
t.Fatalf("check failed, expected 0 got %d", code)
}
res := &outputs.StructuredOutput{}
err = json.Unmarshal(output.Bytes(), res)
checkErr(t, err, "unmarshal failed")
if res.Summary.Failed != 0 {
t.Fatalf("expected 0 failed, got %d", res.Summary.Failed)
}
if len(res.Results) != found {
t.Fatalf("expected %d results for %d", found, len(res.Results))
}
okcount := 0
for _, r := range res.Results {
if r.Result == resource.SUCCESS {
okcount++
}
}
if okcount != passed {
t.Fatalf("expected %d passed but got %d", passed, okcount)
}
}
func TestSkipResourcesByType(t *testing.T) {
output := &bytes.Buffer{}
// temp spec file
fh, err := os.CreateTemp("", "*.yaml")
checkErr(t, err, "temp file failed")
fh.Close()
// new config that doesnt spam output etc
cfg, err := util.NewConfig(util.WithFormatOptions("pretty"), util.WithResultWriter(output), util.WithSpecFile(fh.Name()), util.WithDisabledResourceTypes("file"))
checkErr(t, err, "new config failed")
// adds the os tmp dir to the goss spec file
err = AddResources(fh.Name(), "File", []string{os.TempDir()}, cfg)
checkErr(t, err, "could not add resource %q", os.TempDir())
// validate and sanity check, compare structured vs direct results etc
results, err := ValidateResults(cfg)
checkErr(t, err, "check failed")
skipped := 0
for rg := range results {
for _, r := range rg {
if r.Skipped {
skipped++
}
}
}
if skipped != 5 {
t.Fatalf("Expected to skip 5 tests, skipped %d", skipped)
}
}