-
Notifications
You must be signed in to change notification settings - Fork 2
/
walk_test.go
360 lines (307 loc) · 7.19 KB
/
walk_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
package codf
import (
"errors"
"fmt"
"reflect"
"testing"
)
type mapFunc func(Node) (Node, error)
var _ WalkMapper = mapFunc(nil)
func (f mapFunc) Map(n Node) (Node, error) {
return f(n)
}
func (f mapFunc) Statement(*Statement) error { return nil }
func (f mapFunc) EnterSection(*Section) (Walker, error) { return f, nil }
type docWalker struct {
name string
statements map[string]struct{}
sections map[string]*docWalker
}
func (d *docWalker) Statement(stmt *Statement) error {
if _, ok := d.statements[stmt.Name()]; !ok {
return fmt.Errorf("invalid statement: %s", stmt.Name())
}
return nil
}
func (d *docWalker) EnterSection(sec *Section) (Walker, error) {
if sub := d.sections[sec.Name()]; sub != nil {
return sub, nil
}
return nil, fmt.Errorf("invalid section: %s", sec.Name())
}
func (d *docWalker) ExitSection(_ Walker, sec *Section, parent ParentNode) error {
if sec.Name() != d.name {
return fmt.Errorf("ExitSection called with invalid section named %s", sec.Name())
}
return nil
}
func sectionWalker(name string, children ...interface{}) *docWalker {
w := &docWalker{
name: name,
statements: map[string]struct{}{},
sections: map[string]*docWalker{},
}
for _, child := range children {
switch child := child.(type) {
case *docWalker:
w.sections[child.name] = child
case string:
w.statements[child] = struct{}{}
default:
panic(fmt.Errorf("invalid walker child type: %T", child))
}
}
return w
}
func TestWalk(t *testing.T) {
defer setlogf(t)()
const DocSource = `
user http;
daemon no;
http {
server {
server_name go.spiff.io;
listen 80;
listen 443 http2 ssl;
location / {
root /var/www/public;
index index.html index.htm;
}
}
sendfile yes;
keepalive_timeout 65s;
}
`
doc := mustParseNamed(t, "root.conf", DocSource)
doc.addChild(mustParseNamed(t, "empty.conf", ``))
doc.addChild(mustParseNamed(t, "include_root.conf", `
worker_processes 1;
user httpd;
user www-data;
user nobody;
daemon yes;
daemon true;
daemon false;
`))
doc.Nodes()[2].(*Section).addChild(mustParseNamed(t, "include_nested.conf", `
sendfile no;
keepalive_timeout 10s;
cache /var/run/sv/cache 3;
cache_proxy [200 301 302 404] 5m; // defaults to cache_proxy [] 0
`))
t.Run("Valid", func(t *testing.T) {
defer setlogf(t)()
def := sectionWalker("main",
"user",
"daemon",
"worker_processes",
sectionWalker("http",
"sendfile",
"keepalive_timeout",
"cache",
"cache_proxy",
sectionWalker("server",
"server_name",
"listen",
sectionWalker("location",
"root",
"index",
),
),
),
)
if err := Walk(doc, def); err != nil {
t.Fatal(err)
}
})
t.Run("InvalidNestedSection", func(t *testing.T) {
defer setlogf(t)()
def := sectionWalker("main",
"user",
"daemon",
"worker_processes",
sectionWalker("http",
"sendfile",
),
)
if err := Walk(doc, def); err == nil {
t.Fatal("expected error on server section")
} else {
t.Log(err)
}
})
checkStatementErr := func(t *testing.T, err error, wantDoc string, wantName string) {
fail := func() {
t.Fatalf("expected error on %s statement; got %v", wantName, err)
}
we, ok := err.(*WalkError)
if !ok {
fail()
}
if stmt, ok := we.Node.(*Statement); !ok || stmt.Name() != wantName {
fail()
}
t.Log(err)
docName := ""
if we.Document != nil {
docName = we.Document.Name
}
if docName != wantDoc {
t.Errorf("expected error in document %q; got %q", wantDoc, docName)
}
}
t.Run("InvalidNestedStatement", func(t *testing.T) {
defer setlogf(t)()
def := sectionWalker("main",
"user",
"daemon",
"worker_processes",
sectionWalker("http",
"sendfile",
"cache",
"cache_proxy",
sectionWalker("server",
"server_name",
"listen",
sectionWalker("location",
"root",
"index",
),
),
),
)
err := Walk(doc, def)
checkStatementErr(t, err, "root.conf", "keepalive_timeout")
})
t.Run("InvalidNestedDocumentInDocument", func(t *testing.T) {
defer setlogf(t)()
def := sectionWalker("main",
"user",
"daemon",
sectionWalker("http",
"sendfile",
"keepalive_timeout",
"cache",
"cache_proxy",
sectionWalker("server",
"server_name",
"listen",
sectionWalker("location",
"root",
"index",
),
),
),
)
err := Walk(doc, def)
checkStatementErr(t, err, "include_root.conf", "worker_processes")
})
t.Run("InvalidNestedDocumentInSection", func(t *testing.T) {
defer setlogf(t)()
def := sectionWalker("main",
"user",
"daemon",
"worker_processes",
sectionWalker("http",
"sendfile",
"keepalive_timeout",
"cache",
sectionWalker("server",
"server_name",
"listen",
sectionWalker("location",
"root",
"index",
),
),
),
)
err := Walk(doc, def)
checkStatementErr(t, err, "include_nested.conf", "cache_proxy")
})
}
type walkDeleteNested struct {
statements []string
}
func (w *walkDeleteNested) Statement(s *Statement) error {
w.statements = append(w.statements, s.Name())
return nil
}
func (w *walkDeleteNested) EnterSection(s *Section) (Walker, error) {
return nil, fmt.Errorf("encountered section %s", s.Name())
}
func (w *walkDeleteNested) Map(n Node) (Node, error) {
switch n.(type) {
case *Section:
return nil, nil
}
return n, nil
}
func TestWalkMapper(t *testing.T) {
defer setlogf(t)()
const DocSource = `
user http;
daemon no;
http {
server {
server_name go.spiff.io;
listen 80;
listen 443 http2 ssl;
location / {
root /var/www/public;
index index.html index.htm;
}
}
sendfile yes;
keepalive_timeout 65s;
}
`
doc := mustParseNamed(t, "root.conf", DocSource)
doc.addChild(mustParseNamed(t, "child.conf", `
sub-section {}
statement true;
`))
t.Logf("-- BEFORE --\n%v", doc)
want := []Node{doc.Children[0], doc.Children[1], doc.Children[3]}
walker := new(walkDeleteNested)
err := Walk(doc, walker)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(doc.Children, want) {
t.Errorf("children = %#+v; want %#+v", doc.Children, want)
}
t.Logf("-- AFTER --\n%v", doc)
wantStatements := []string{
"user",
"daemon",
"statement",
}
if !reflect.DeepEqual(walker.statements, wantStatements) {
t.Errorf("statements = %q; want %q", walker.statements, wantStatements)
}
}
func TestMapDelete(t *testing.T) {
const DocSource = `stmt 1; section arg {}`
defer setlogf(t)()
doc := mustParseNamed(t, "root.conf", DocSource)
err := Walk(doc, mapFunc(func(Node) (Node, error) { return nil, nil }))
if err != nil {
t.Fatalf("err = %v; want nil", err)
}
if got := doc.Children; len(got) != 0 {
t.Fatalf("children = %#v; want an empty slice", got)
}
}
func TestMapError(t *testing.T) {
const wantErrMessage = `[1:1:0] root.conf: stmt in main: expected error`
const DocSource = `stmt 1; section arg {}`
defer setlogf(t)()
doc := mustParseNamed(t, "root.conf", DocSource)
doc.Children = append([]Node{nil}, doc.Children...)
wantErr := errors.New("expected error")
err := Walk(doc, mapFunc(func(Node) (Node, error) { return nil, wantErr }))
if got := err.Error(); got != wantErrMessage {
t.Fatalf("err = %q; want %q", got, wantErrMessage)
}
}