-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_test.go
292 lines (283 loc) · 6.38 KB
/
rules_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
package main
import (
"io/ioutil"
"log"
"testing"
)
func TestParseRules(t *testing.T) {
tests := map[string]struct {
file string
err bool
}{
"good rules": {
file: "testdata/good_rules.json",
err: false,
},
"bad rules": {
file: "testdata/bad_rules.json",
err: true,
},
}
for name, testcase := range tests {
data, err := ioutil.ReadFile(testcase.file)
if err != nil {
t.Errorf("%v failed when reading %v: %v", name, testcase.file, err)
continue
}
rules, err := parseRules(data)
if testcase.err {
if err == nil {
t.Errorf("%v should've failed but didn't when parsing rules %v", name, testcase.file)
continue
}
} else if err != nil {
t.Errorf("%v failed when parsing rules: %v", name, err)
continue
}
for _, rule := range rules {
if rule.release_mapping == "" {
t.Errorf("%v failed: no mapping found for rule: %v", name, rule)
break
}
}
}
}
// TestFindMatchingRule is not necessarily the ideal way to test
// findMatchingRule, as we don't fully isolate each individual
// property under test - we test the entire function at once.
// In reality, there are never rules with just one property set,
// and it's probably better to test using realistic rules than
// fully isolate each property under test.
// The main downside here is that a bug in evaluating one
// property may cause seemingly unrelated tests to fail.
func TestFindMatchingRule(t *testing.T) {
data, err := ioutil.ReadFile("testdata/good_rules.json")
if err != nil {
t.Errorf("Couldn't read rules to run rule matching tests")
return
}
rules, err := parseRules(data)
if err != nil {
t.Errorf("Couldn't parse rules to run rule matching tests")
return
}
tests := map[string]struct {
req gothmogFields
want Rule
}{
"simple string matches": {
req: gothmogFields{
product: "Firefox",
channel: "aurora",
instructionSet: "SSE",
osVersion: "Linux",
buildid: "2020101010101010",
},
want: rules[46],
},
"simple string no matches": {
req: gothmogFields{
product: "NotFirefox",
channel: "fake",
osVersion: "fake",
distribution: "fake",
},
want: Rule{
priority: -1,
},
},
"version <": {
req: gothmogFields{
product: "Firefox",
channel: "esr",
version: "78.0",
},
want: rules[5],
},
// TODO: test for version <= & >
// But we don't have rules that use these right now
"version > matches higher": {
req: gothmogFields{
product: "Firefox",
channel: "beta*",
version: "45.0",
osVersion: "GTK 2,GTK 3.0.,GTK 3.1.,GTK 3.2.,GTK 3.3.",
},
want: rules[120],
},
"version > matches exact": {
req: gothmogFields{
product: "Firefox",
channel: "beta*",
version: "44.0",
osVersion: "GTK 2,GTK 3.0.,GTK 3.1.,GTK 3.2.,GTK 3.3.",
},
want: rules[120],
},
"version matches csv": {
req: gothmogFields{
product: "Firefox",
channel: "release*",
version: "56.0",
osVersion: "Windows_NT",
},
want: rules[52],
},
"version matches csv2": {
req: gothmogFields{
product: "Firefox",
channel: "release*",
version: "56.0.1",
osVersion: "Windows_NT",
},
want: rules[52],
},
"version no match csv": {
req: gothmogFields{
product: "Firefox",
channel: "release*",
version: "99.0",
osVersion: "Windows_NT",
},
want: Rule{priority: -1},
},
"buildid <": {
req: gothmogFields{
product: "Firefox",
channel: "nightly*",
buildid: "20151209095499",
osVersion: "Windows_NT",
},
want: rules[108],
},
// TODO: tests for other operators for buildID, when
// we have rules with them in it
"buildTarget csv": {
req: gothmogFields{
product: "Firefox",
channel: "bhrelease-localtest",
version: "56.0",
buildTarget: "WINNT_x86-msvc-x64",
memory: ">2048",
},
want: rules[47],
},
"buildTarget csv2": {
req: gothmogFields{
product: "Firefox",
channel: "bhrelease-localtest",
version: "56.0",
buildTarget: "WINNT_x86_64-msvc",
memory: ">2048",
},
want: rules[47],
},
"locale csv": {
req: gothmogFields{
product: "Firefox",
channel: "release*",
version: "56.0",
locale: "as",
},
want: rules[33],
},
"locale csv2": {
req: gothmogFields{
product: "Firefox",
channel: "release*",
version: "56.0",
locale: "mai",
},
want: rules[33],
},
"locale csv3": {
req: gothmogFields{
product: "Firefox",
channel: "release*",
version: "56.0",
locale: "bn-IN",
},
want: rules[33],
},
"memory >": {
req: gothmogFields{
product: "Firefox",
channel: "bhrelease-localtest",
buildTarget: "WINNT_x86_64-msvc",
version: "56.0",
memory: "2049",
},
want: rules[47],
},
// TODO: test other operators when we have rules with them
"osVersion csv": {
req: gothmogFields{
product: "Firefox",
channel: "aurora",
buildid: "20170123004000",
osVersion: "Windows_NT 5.1",
},
want: rules[58],
},
"osVersion csv2": {
req: gothmogFields{
product: "Firefox",
channel: "aurora",
buildid: "20170123004000",
osVersion: "Windows_NT 5.2",
},
want: rules[58],
},
"osVersion csv3": {
req: gothmogFields{
product: "Firefox",
channel: "aurora",
buildid: "20170123004000",
osVersion: "Windows_NT 6.0",
},
want: rules[58],
},
"osVersion substring match": {
req: gothmogFields{
product: "Firefox",
channel: "esr*",
osVersion: "Windows_NT 6.0",
version: "38.0",
},
want: rules[114],
},
"channel exact match": {
req: gothmogFields{
product: "Firefox",
channel: "date-localtest",
},
want: rules[1],
},
"channel match glob": {
req: gothmogFields{
product: "Firefox",
channel: "esr",
version: "38.0.0",
osVersion: "Windows_NT",
},
want: rules[114],
},
"channel match glob2": {
req: gothmogFields{
product: "Firefox",
channel: "esr-localtest",
version: "38.0.0",
osVersion: "Windows_NT",
},
want: rules[114],
},
}
for name, testcase := range tests {
log.Printf("Running test: %v", name)
got := findMatchingRule(&rules, testcase.req)
if got != testcase.want {
t.Errorf("%v failed. wanted: %v, got: %v", name, testcase.want, got)
continue
}
}
}