forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_test.go
479 lines (428 loc) · 9.77 KB
/
index_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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package ast
import (
"fmt"
"testing"
"github.com/open-policy-agent/opa/util/test"
)
type testResolver struct {
input *Term
failRef Ref
unknownRefs Set
}
func (r testResolver) Resolve(ref Ref) (Value, error) {
if r.unknownRefs != nil && r.unknownRefs.Contains(NewTerm(ref)) {
return nil, UnknownValueErr{}
}
if ref.Equal(r.failRef) {
return nil, fmt.Errorf("some error")
}
if ref.HasPrefix(InputRootRef) {
v, err := r.input.Value.Find(ref[1:])
if err != nil {
return nil, nil
}
return v, nil
}
panic("illegal value")
}
func TestBaseDocEqIndexing(t *testing.T) {
module := MustParseModule(`
package test
exact {
input.x = 1
input.y = 2
} {
input.x = 3
input.y = 4
}
scalars {
input.x = 0
input.y = 1
} {
1 = input.y # exercise ordering
input.x = 0
} {
input.y = 2
input.z = 2
} {
input.x = 2
}
vars {
input.x = 1
input.y = 2
} {
input.x = x
input.y = 3
} {
input.x = 4
input.z = 5
}
composite_arr {
input.x = 1
input.y = [1,2,3]
input.z = 1
} {
input.x = 1
input.y = [1,2,4,x]
} {
input.y = [1,2,y,5]
input.z = 3
} {
input.y = []
} {
# Must be included in all results as nested composites are not indexed.
input.y = [1,[2,3],4]
}
composite_obj {
input.y = {"foo": "bar", "bar": x}
}
equal {
input.x == 1
} {
input.x == 2
} {
input.y == 3
}
# filtering ruleset contains rules that cannot be indexed (for different reasons).
filtering {
count([], x)
} {
not input.x = 0
} {
x = [1,2,3]
x[0] = 1
} {
input.x[_] = 1
} {
input.x[input.y] = 1
} {
# include one rule that can be indexed to exercise merging of root non-indexable
# rules with other rules.
input.x = 1
}
# exercise default keyword
default allow = false
allow {
input.x = 1
} {
input.x = 0
}
`)
tests := []struct {
note string
ruleset string
input string
unknowns []string
expectedRS interface{}
expectedDR *Rule
}{
{
note: "exact match",
ruleset: "exact",
input: `{"x": 3, "y": 4}`,
expectedRS: []string{
`exact { input.x = 3; input.y = 4 }`,
},
},
{
note: "undefined match",
ruleset: "scalars",
input: `{"x": 2, "y": 2}`,
expectedRS: []string{
`scalars { input.x = 2 }`},
},
{
note: "disjoint match",
ruleset: "scalars",
input: `{"x": 2, "y": 2, "z": 2}`,
expectedRS: []string{
`scalars { input.x = 2 }`,
`scalars { input.y = 2; input.z = 2}`},
},
{
note: "ordering match",
ruleset: "scalars",
input: `{"x": 0, "y": 1}`,
expectedRS: []string{
`scalars { input.x = 0; input.y = 1 }`,
`scalars { 1 = input.y; input.x = 0 }`},
},
{
note: "type no match",
ruleset: "vars",
input: `{"y": 3, "x": {1,2,3}}`,
expectedRS: []string{
`vars { input.x = x; input.y = 3 }`,
},
},
{
note: "var match",
ruleset: "vars",
input: `{"x": 1, "y": 3}`,
expectedRS: []string{
`vars { input.x = x; input.y = 3 }`,
},
},
{
note: "var match disjoint",
ruleset: "vars",
input: `{"x": 4, "z": 5, "y": 3}`,
expectedRS: []string{
`vars { input.x = x; input.y = 3 }`,
`vars { input.x = 4; input.z = 5 }`,
},
},
{
note: "array match",
ruleset: "composite_arr",
input: `{
"x": 1,
"y": [1,2,3],
"z": 1,
}`,
expectedRS: []string{
`composite_arr { input.x = 1; input.y = [1,2,3]; input.z = 1 }`,
`composite_arr { input.y = [1,[2,3],4] }`,
},
},
{
note: "array var match",
ruleset: "composite_arr",
input: `{
"x": 1,
"y": [1,2,4,5],
}`,
expectedRS: []string{
`composite_arr { input.x = 1; input.y = [1,2,4,x] }`,
`composite_arr { input.y = [1,[2,3],4] }`,
},
},
{
note: "array var multiple match",
ruleset: "composite_arr",
input: `{
"x": 1,
"y": [1,2,4,5],
"z": 3,
}`,
expectedRS: []string{
`composite_arr { input.x = 1; input.y = [1,2,4,x] }`,
`composite_arr { input.y = [1,2,y,5]; input.z = 3 }`,
`composite_arr { input.y = [1,[2,3],4] }`,
},
},
{
note: "array nested match non-indexable rules",
ruleset: "composite_arr",
input: `{
"x": 1,
"y": [1,[2,3],4],
}`,
expectedRS: []string{
`composite_arr { input.y = [1,[2,3],4] }`,
},
},
{
note: "array empty match",
ruleset: "composite_arr",
input: `{"y": []}`,
expectedRS: []string{
`composite_arr { input.y = [] }`,
`composite_arr { input.y = [1,[2,3],4] }`,
},
},
{
note: "object match non-indexable rule",
ruleset: "composite_obj",
input: `{"y": {"foo": "bar", "bar": "baz"}}`,
expectedRS: []string{
`composite_obj { input.y = {"foo": "bar", "bar": x} }`,
},
},
{
note: "match ==",
ruleset: "equal",
input: `{"x": 2, "y": 3}`,
expectedRS: []string{
"equal { input.y == 3 }",
"equal { input.x == 2 }",
},
},
{
note: "miss ==",
ruleset: "equal",
input: `{"x": 1000, "y": 1000}`,
expectedRS: []string{},
},
{
note: "default rule only",
ruleset: "allow",
input: `{"x": 2}`,
expectedRS: []string{},
expectedDR: MustParseRule(`default allow = false`),
},
{
note: "match and default rule",
ruleset: "allow",
input: `{"x": 1}`,
expectedRS: []string{"allow { input.x = 1 }"},
expectedDR: MustParseRule(`default allow = false`),
},
{
note: "match and non-indexable rules",
ruleset: "filtering",
input: `{"x": 1}`,
expectedRS: module.RuleSet(Var("filtering")),
},
{
note: "non-indexable rules",
ruleset: "filtering",
input: `{}`,
expectedRS: module.RuleSet(Var("filtering")).Diff(NewRuleSet(MustParseRule(`filtering { input.x = 1 }`))),
},
{
note: "unknown: all",
ruleset: "composite_arr",
unknowns: []string{`input.x`, `input.y`, `input.z`},
expectedRS: module.RuleSet(Var("composite_arr")),
},
{
note: "unknown: partial",
ruleset: "composite_arr",
unknowns: []string{`input.x`, `input.y`},
input: `{"z": 3}`,
expectedRS: module.RuleSet(Var("composite_arr")).Diff(NewRuleSet(MustParseRule(`composite_arr {
input.x = 1
input.y = [1,2,3]
input.z = 1
}`))),
},
}
for _, tc := range tests {
test.Subtest(t, tc.note, func(t *testing.T) {
rules := []*Rule{}
for _, rule := range module.Rules {
if rule.Head.Name == Var(tc.ruleset) {
rules = append(rules, rule)
}
}
var input *Term
if tc.input != "" {
input = MustParseTerm(tc.input)
}
var expectedRS RuleSet
switch e := tc.expectedRS.(type) {
case []string:
for _, r := range e {
expectedRS.Add(MustParseRule(r))
}
case RuleSet:
expectedRS = e
default:
panic("Unexpected test case expected value")
}
index := newBaseDocEqIndex(func(Ref) bool {
return false
})
if !index.Build(rules) {
t.Fatalf("Expected index build to succeed")
}
var unknownRefs Set
if len(tc.unknowns) > 0 {
unknownRefs = NewSet()
for _, s := range tc.unknowns {
unknownRefs.Add(MustParseTerm(s))
}
}
result, err := index.Lookup(testResolver{input: input, unknownRefs: unknownRefs})
if err != nil {
t.Fatalf("Unexpected error during index lookup: %v", err)
}
if !NewRuleSet(result.Rules...).Equal(expectedRS) {
t.Fatalf("Expected ruleset %v but got: %v", expectedRS, result.Rules)
}
if result.Default == nil && tc.expectedDR != nil {
t.Fatalf("Expected default rule but got nil")
} else if result.Default != nil && tc.expectedDR == nil {
t.Fatalf("Unexpected default rule %v", result.Default)
} else if result.Default != nil && tc.expectedDR != nil && !result.Default.Equal(tc.expectedDR) {
t.Fatalf("Expected default rule %v but got: %v", tc.expectedDR, result.Default)
}
})
}
}
func TestBaseDocEqIndexingPriorities(t *testing.T) {
module := MustParseModule(`
package test
p { # r1
false
} else { # r2
input.x = "x1"
input.y = "y1"
} else { # r3
input.z = "z1"
}
p { # r4
input.x = "x1"
}
p { # r5
input.z = "z2"
} else { # r6
input.z = "z1"
}
`)
index := newBaseDocEqIndex(func(Ref) bool { return false })
ok := index.Build(module.Rules)
if !ok {
t.Fatalf("Expected index build to succeed")
}
input := MustParseTerm(`{"x": "x1", "y": "y1", "z": "z1"}`)
result, err := index.Lookup(testResolver{input: input})
if err != nil {
t.Fatalf("Unexpected error during index lookup: %v", err)
}
expectedRules := NewRuleSet(
module.Rules[0],
module.Rules[1],
module.Rules[2].Else)
expectedElse := map[*Rule]RuleSet{
module.Rules[0]: []*Rule{
module.Rules[0].Else,
module.Rules[0].Else.Else,
},
}
if result.Default != nil {
t.Fatalf("Expected default rule to be nil")
}
if !NewRuleSet(result.Rules...).Equal(expectedRules) {
t.Fatalf("Expected rules to be %v but got: %v", expectedRules, result.Rules)
}
r1 := module.Rules[0]
if !NewRuleSet(result.Else[r1]...).Equal(expectedElse[r1]) {
t.Fatalf("Expected else to be %v but got: %v", result.Else[r1], expectedElse[r1])
}
}
func TestBaseDocEqIndexingErrors(t *testing.T) {
index := newBaseDocEqIndex(func(Ref) bool {
return false
})
module := MustParseModule(`
package ex
p { input.raise_error = 1 }`)
if !index.Build(module.Rules) {
t.Fatalf("Expected index to build")
}
_, err := index.Lookup(testResolver{
input: MustParseTerm(`{}`),
failRef: MustParseRef("input.raise_error")})
if err == nil || err.Error() != "some error" {
t.Fatalf("Expected error but got: %v", err)
}
index = newBaseDocEqIndex(func(Ref) bool { return true })
if index.Build(nil) {
t.Fatalf("Expected index build to fail")
}
}