-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
yaml_parser_test.go
299 lines (255 loc) · 5.57 KB
/
yaml_parser_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
//
// Our YAML parser is our single biggest potential source of
// failure - whether users give us bogus input, or puppet-versions
// change what thye submit.
//
// We should have good, thorough, and real test-cases here.
//
//
package main
import (
"regexp"
"strings"
"testing"
)
//
// Ensure that bogus YAML is caught.
//
func TestBogusYaml(t *testing.T) {
//
// Parse the bogus YAML content "`\n3'"
//
_, err := ParsePuppetReport([]byte("`\n3'"))
//
// Ensure the error is what we expect.
//
reg, _ := regexp.Compile("failed to parse YAML")
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
}
//
// Test that we can handle dates of various forms.
//
func TestYamlDates(t *testing.T) {
tests := []string{"---\ntime: '2017-03-10T10:22:33.659245699+00:00'\nhost: bart\nenvironment: production\n",
"---\ntime: 2017-03-10 10:22:33.493526494 +00:00\nhost: foo\nenvironment: production\n"}
for _, input := range tests {
//
// Error will be set here, since we only supply
// `host` + `time` we'll expect something like
//
// "Failed to get `status' from YAML
//
node, _ := ParsePuppetReport([]byte(input))
if node.At != "2017-03-10 10:22:33" {
t.Errorf("Invalid time result, got '%s'", node.At)
}
}
}
//
// Test that we can handle filter out bogus hostnames.
//
// Here we look for an exception of the form "blah invalid|missing host"
// to know whether we passed/failed.
//
func TestHostName(t *testing.T) {
//
// Test-cases
//
type HostTest struct {
hostname string
valid bool
}
//
// Possible Hostnames
//
fail := []HostTest{
{"../../../etc/passwd%00", false},
{"node1.example.com../../../etc", false},
{"steve_example com", false},
{"node1./example.com", false},
{"steve1.example.com", true},
{"steve-example.com", true},
{"example3-3_2.com", true}}
//
// For each test-case
//
for _, input := range fail {
//
// Build up YAML
//
tmp := "---\n" +
"host: " + input.hostname
//
// Parse it.
//
_, err := ParsePuppetReport([]byte(tmp))
//
// Host-regexp.
//
reg, _ := regexp.Compile("host")
//
// Do we expect this to pass/fail?
//
if input.valid {
if reg.MatchString(err.Error()) {
t.Errorf("Expected no error relating to 'host', but got one: %v", err)
}
} else {
//
// We expect this to fail. Did it?
//
if !reg.MatchString(err.Error()) {
t.Errorf("Expected an error relating to 'host', but didn't: %v", err)
}
}
}
}
//
// Test that we can detect unknown states.
//
func TestNodeStatus(t *testing.T) {
//
// Test-cases
//
type TestCase struct {
state string
valid bool
}
//
// Possible states, and whether they are valid
//
fail := []TestCase{
{"changed", true},
{"unchanged", true},
{"failed", true},
{"blah", false},
{"forced", false},
{"unknown", false}}
//
// For each test-case
//
for _, input := range fail {
//
// Build up YAML
//
tmp := "---\n" +
"host: foo.example.com\n" +
"environment: production\n" +
"time: '2017-08-07T16:37:42.659245699+00:00'\n" +
"status: " + input.state
//
// Parse it.
//
_, err := ParsePuppetReport([]byte(tmp))
//
// regexp for matching error-conditions
//
reg, _ := regexp.Compile("status")
//
// Do we expect this to pass/fail?
//
if input.valid {
if reg.MatchString(err.Error()) {
t.Errorf("Expected no error relating to 'status', but got one: %v", err)
}
} else {
//
// We expect this to fail. Did it?
//
if !reg.MatchString(err.Error()) {
t.Errorf("Expected an error relating to 'status', but didn't: %v", err)
}
}
}
}
//
// Test importing a valid YAML file.
//
// TODO: Test bogus ones too.
//
func TestValidYaml(t *testing.T) {
//
// Read the YAML file.
//
tmpl, err := getResource("data/valid.yaml")
if err != nil {
t.Fatal("Failed to load YAML asset data/valid.yaml")
}
report, err := ParsePuppetReport(tmpl)
if err != nil {
t.Fatal("Failed to parse YAML file")
}
//
// Test data from YAML
//
if report.Fqdn != "www.steve.org.uk" {
t.Errorf("Incorrect hostname: %v", report.Fqdn)
}
if report.State != "unchanged" {
t.Errorf("Incorrect state: %v", report.State)
}
if report.At != "2017-07-29 23:17:01" {
t.Errorf("Incorrect at: %v", report.At)
}
if report.Failed != "0" {
t.Errorf("Incorrect failed: %v", report.Failed)
}
if report.Changed != "0" {
t.Errorf("Incorrect changed: %v", report.Changed)
}
if report.Skipped != "2" {
t.Errorf("Incorrect skipped: %v", report.Skipped)
}
}
//
// Test a valid report which has been modified to remove fields of
// interest raises errors as expected.
//
func TestMissingResources(t *testing.T) {
//
// Various fields we remove.
//
tests := []string{"resource_statuses",
"logs",
"metrics",
"resources",
"values"}
//
// Read the YAML file.
//
tmpl, err := getResource("data/valid.yaml")
if err != nil {
t.Fatal("Failed to load YAML asset data/valid.yaml")
}
//
// For each field-test
//
for _, field := range tests {
//
// Conver the template to a string, and remove
// the bit that we should.
//
str := string(tmpl)
str = strings.Replace(str, field, "blah", -1)
//
// Now parse, which we expect to fail.
//
var b = []byte(str)
_, err = ParsePuppetReport(b)
//
// We expect an error.
//
if err == nil {
t.Fatal("We expected an error from the report!")
}
//
// The error will relate to our string, or an interface
// violation
//
if !strings.Contains(err.Error(), field) && !strings.Contains(err.Error(), "type assertion") {
t.Fatal("No reference to field/type in our error")
}
}
}