-
Notifications
You must be signed in to change notification settings - Fork 617
/
parse_test.go
258 lines (246 loc) · 8.94 KB
/
parse_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
package route
import (
"bytes"
"reflect"
"regexp"
"testing"
)
func TestParse(t *testing.T) {
tests := []struct {
desc string
in string
out []*RouteDef
fail bool
}{
// error flows
{"FailEmpty", ``, nil, false},
{"FailNoRoute", `bang`, nil, true},
{"FailRouteNoCmd", `route x`, nil, true},
{"FailRouteAddNoService", `route add`, nil, true},
{"FailRouteAddNoSrc", `route add svc`, nil, true},
// happy flows
{
desc: "RouteAddService",
in: `route add svc /prefix http://1.2.3.4/`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/"}},
},
{
desc: "RouteAddTCPService",
in: `route add svc :1234 tcp://1.2.3.4:5678`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: ":1234", Dst: "tcp://1.2.3.4:5678"}},
},
{
desc: "RouteAddGRPCService",
in: `route add svc :1234 grpc://1.2.3.4:5678`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: ":1234", Dst: "grpc://1.2.3.4:5678"}},
},
{
desc: "RouteAddServiceWeight",
in: `route add svc /prefix http://1.2.3.4/ weight 1.2`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/", Weight: 1.2}},
},
{
desc: "RouteAddServiceWeightTags",
in: `route add svc /prefix http://1.2.3.4/ weight 1.2 tags "a,b"`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/", Weight: 1.2, Tags: []string{"a", "b"}}},
},
{
desc: "RouteAddServiceWeightOpts",
in: `route add svc /prefix http://1.2.3.4/ weight 1.2 opts "foo=bar baz=bang blimp"`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/", Weight: 1.2, Opts: map[string]string{"foo": "bar", "baz": "bang", "blimp": ""}}},
},
{
desc: "RouteAddServiceWeightTagsOpts",
in: `route add svc /prefix http://1.2.3.4/ weight 1.2 tags "a,b" opts "foo=bar baz=bang blimp"`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/", Weight: 1.2, Tags: []string{"a", "b"}, Opts: map[string]string{"foo": "bar", "baz": "bang", "blimp": ""}}},
},
{
desc: "RouteAddServiceWeightTagsOptsMoreSpaces",
in: ` route add svc /prefix http://1.2.3.4/ weight 1.2 tags " a , b " opts "foo=bar baz=bang blimp" `,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/", Weight: 1.2, Tags: []string{"a", "b"}, Opts: map[string]string{"foo": "bar", "baz": "bang", "blimp": ""}}},
},
{
desc: "RouteAddTags",
in: `route add svc /prefix http://1.2.3.4/ tags "a,b"`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/", Tags: []string{"a", "b"}}},
},
{
desc: "RouteAddTagsOpts",
in: `route add svc /prefix http://1.2.3.4/ tags "a,b" opts "foo=bar baz=bang blimp"`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/", Tags: []string{"a", "b"}, Opts: map[string]string{"foo": "bar", "baz": "bang", "blimp": ""}}},
},
{
desc: "RouteAddOpts",
in: `route add svc /prefix http://1.2.3.4/ opts "foo=bar baz=bang blimp"`,
out: []*RouteDef{{Cmd: RouteAddCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/", Opts: map[string]string{"foo": "bar", "baz": "bang", "blimp": ""}}},
},
{
desc: "RouteDelTags",
in: `route del tags "a,b"`,
out: []*RouteDef{{Cmd: RouteDelCmd, Tags: []string{"a", "b"}}},
},
{
desc: "RouteDelTagsMoreSpaces",
in: `route del tags " a , b "`,
out: []*RouteDef{{Cmd: RouteDelCmd, Tags: []string{"a", "b"}}},
},
{
desc: "RouteDelService",
in: `route del svc`,
out: []*RouteDef{{Cmd: RouteDelCmd, Service: "svc"}},
},
{
desc: "RouteDelServiceTags",
in: `route del svc tags "a,b"`,
out: []*RouteDef{{Cmd: RouteDelCmd, Service: "svc", Tags: []string{"a", "b"}}},
},
{
desc: "RouteDelServiceTagsMoreSpaces",
in: `route del svc tags " a , b "`,
out: []*RouteDef{{Cmd: RouteDelCmd, Service: "svc", Tags: []string{"a", "b"}}},
},
{
desc: "RouteDelServiceSrc",
in: `route del svc /prefix`,
out: []*RouteDef{{Cmd: RouteDelCmd, Service: "svc", Src: "/prefix"}},
},
{
desc: "RouteDelTCPServiceSrc",
in: `route del svc :1234`,
out: []*RouteDef{{Cmd: RouteDelCmd, Service: "svc", Src: ":1234"}},
},
{
desc: "RouteDelServiceSrcDst",
in: `route del svc /prefix http://1.2.3.4/`,
out: []*RouteDef{{Cmd: RouteDelCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/"}},
},
{
desc: "RouteDelTCPServiceSrcDst",
in: `route del svc :1234 tcp://1.2.3.4:5678`,
out: []*RouteDef{{Cmd: RouteDelCmd, Service: "svc", Src: ":1234", Dst: "tcp://1.2.3.4:5678"}},
},
{
desc: "RouteDelServiceSrcDstMoreSpaces",
in: ` route del svc /prefix http://1.2.3.4/ `,
out: []*RouteDef{{Cmd: RouteDelCmd, Service: "svc", Src: "/prefix", Dst: "http://1.2.3.4/"}},
},
{
desc: "RouteWeightServiceSrc",
in: `route weight svc /prefix weight 1.2`,
out: []*RouteDef{{Cmd: RouteWeightCmd, Service: "svc", Src: "/prefix", Weight: 1.2}},
},
{
desc: "RouteWeightServiceSrcTags",
in: `route weight svc /prefix weight 1.2 tags "a,b"`,
out: []*RouteDef{{Cmd: RouteWeightCmd, Service: "svc", Src: "/prefix", Weight: 1.2, Tags: []string{"a", "b"}}},
},
{
desc: "RouteWeightServiceSrcTagsMoreSpaces",
in: ` route weight svc /prefix weight 1.2 tags " a , b " `,
out: []*RouteDef{{Cmd: RouteWeightCmd, Service: "svc", Src: "/prefix", Weight: 1.2, Tags: []string{"a", "b"}}},
},
{
desc: "RouteWeightSrcTags",
in: `route weight /prefix weight 1.2 tags "a,b"`,
out: []*RouteDef{{Cmd: RouteWeightCmd, Src: "/prefix", Weight: 1.2, Tags: []string{"a", "b"}}},
},
{
desc: "RouteWeightSrcTagsMoreSpaces",
in: ` route weight /prefix weight 1.2 tags " a , b " `,
out: []*RouteDef{{Cmd: RouteWeightCmd, Src: "/prefix", Weight: 1.2, Tags: []string{"a", "b"}}},
},
}
reSyntaxError := regexp.MustCompile(`syntax error`)
deref := func(def []*RouteDef) (defs []RouteDef) {
for _, d := range def {
defs = append(defs, *d)
}
return
}
run := func(in string, def []*RouteDef, fail bool, parseFn func(*bytes.Buffer) ([]*RouteDef, error)) {
out, err := parseFn(bytes.NewBufferString(in))
switch {
case err == nil && fail:
t.Errorf("got error nil want fail")
return
case err != nil && !fail:
t.Errorf("got error %v want nil", err)
return
case err != nil:
if !reSyntaxError.MatchString(err.Error()) {
t.Errorf("got error %q want 'syntax error.*'", err)
}
return
}
if got, want := out, def; !reflect.DeepEqual(got, want) {
t.Errorf("\ngot %#v\nwant %#v", deref(got), deref(want))
}
}
for _, tt := range tests {
t.Run("Parse-"+tt.desc, func(t *testing.T) { run(tt.in, tt.out, tt.fail, Parse) })
}
}
func TestParseAliases(t *testing.T) {
tests := []struct {
desc string
in string
out []string
fail bool
}{
// error flows
{"FailEmpty", ``, nil, false},
{"FailNoRoute", `bang`, nil, true},
{"FailRouteNoCmd", `route x`, nil, true},
{"FailRouteAddNoService", `route add`, nil, true},
{"FailRouteAddNoSrc", `route add svc`, nil, true},
// happy flows with and without aliases
{
desc: "RouteAddServiceWithoutAlias",
in: `route add alpha-be alpha/ http://1.2.3.4/ opts "strip=/path prepend=/new proto=https"`,
out: []string(nil),
},
{
desc: "RouteAddServiceWithAlias",
in: `route add alpha-be alpha/ http://1.2.3.4/ opts "strip=/path prepend=/new proto=https register=alpha"`,
out: []string{"alpha"},
},
{
desc: "RouteAddServicesWithoutAliases",
in: `route add alpha-be alpha/ http://1.2.3.4/ opts "strip=/path prepend=/new proto=tcp"
route add bravo-be bravo/ http://1.2.3.5/
route add charlie-be charlie/ http://1.2.3.6/ opts "host=charlie"`,
out: []string(nil),
},
{
desc: "RouteAddServicesWithAliases",
in: `route add alpha-be alpha/ http://1.2.3.4/ opts "register=alpha"
route add bravo-be bravo/ http://1.2.3.5/ opts "strip=/foo prepend=/new register=bravo"
route add charlie-be charlie/ http://1.2.3.5/ opts "host=charlie proto=https"
route add delta-be delta/ http://1.2.3.5/ opts "host=delta proto=https register=delta"`,
out: []string{"alpha", "bravo", "delta"},
},
}
reSyntaxError := regexp.MustCompile(`syntax error`)
run := func(in string, aliases []string, fail bool, parseFn func(string) ([]string, error)) {
out, err := parseFn(in)
switch {
case err == nil && fail:
t.Errorf("got error nil want fail")
return
case err != nil && !fail:
t.Errorf("got error %v want nil", err)
return
case err != nil:
if !reSyntaxError.MatchString(err.Error()) {
t.Errorf("got error %q want 'syntax error.*'", err)
}
return
}
if got, want := out, aliases; !reflect.DeepEqual(got, want) {
t.Errorf("\ngot %#v\nwant %#v", got, want)
}
}
for _, tt := range tests {
t.Run("ParseAliases-"+tt.desc, func(t *testing.T) { run(tt.in, tt.out, tt.fail, ParseAliases) })
}
}