-
Notifications
You must be signed in to change notification settings - Fork 15
/
analyze_test.go
78 lines (70 loc) · 2.4 KB
/
analyze_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
package crossplane
import (
"strings"
"testing"
)
func TestAnalyze(t *testing.T) {
fname := "/path/to/nginx.conf"
ctx := blockCtx{"events"}
// Checks that the `state` directive should only be in certain contexts.
t.Run("state-directive", func(t *testing.T) {
stmt := Directive{
Directive: "state",
Args: []string{"/path/to/state/file.conf"},
Line: 5, // this is arbitrary
}
// the state directive should not cause errors if it"s in these contexts
goodCtxs := []blockCtx{
blockCtx{"http", "upstream"},
blockCtx{"stream", "upstream"},
blockCtx{"some_third_party_context"},
}
for _, ctx := range goodCtxs {
if err := analyze(fname, stmt, ";", ctx, &ParseOptions{}); err != nil {
t.Fatalf("expected err to be nil: %v", err)
}
}
goodMap := map[string]bool{}
for _, c := range goodCtxs {
goodMap[c.key()] = true
}
for key := range contexts {
// the state directive should only be in the "good" contexts
if _, ok := goodMap[key]; !ok {
ctx := blockCtx(strings.Split(key, ">"))
if err := analyze(fname, stmt, ";", ctx, &ParseOptions{}); err == nil {
t.Fatalf("expected error to not be nil: %v", err)
} else if e, ok := err.(ParseError); !ok {
t.Fatalf("error was not a ParseError: %v", err)
} else if !strings.HasSuffix(e.what, `directive is not allowed here`) {
t.Fatalf("unexpected error message: %q", e.what)
}
}
}
})
// Check which arguments are valid for flag directives.
t.Run("flag-args", func(t *testing.T) {
stmt := Directive{
Directive: "accept_mutex",
Line: 2, // this is arbitrary
}
goodArgs := [][]string{[]string{"on"}, []string{"off"}, []string{"On"}, []string{"Off"}, []string{"ON"}, []string{"OFF"}}
for _, args := range goodArgs {
stmt.Args = args
if err := analyze(fname, stmt, ";", ctx, &ParseOptions{}); err != nil {
t.Fatalf("expected err to be nil: %v", err)
}
}
badArgs := [][]string{[]string{"1"}, []string{"0"}, []string{"true"}, []string{"okay"}, []string{""}}
for _, args := range badArgs {
stmt.Args = args
if err := analyze(fname, stmt, ";", ctx, &ParseOptions{}); err == nil {
t.Fatalf("expected error to not be nil: %v", err)
} else if e, ok := err.(ParseError); !ok {
t.Fatalf("error was not a ParseError: %v", err)
} else if !strings.HasSuffix(e.what, `it must be "on" or "off"`) {
t.Fatalf("unexpected error message: %q", e.what)
}
}
})
}