forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_test.go
445 lines (416 loc) · 10.7 KB
/
eval_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
// Copyright 2018 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 topdown
import (
"context"
"testing"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/storage"
inmem "github.com/open-policy-agent/opa/storage/inmem/test"
)
func TestQueryIDFactory(t *testing.T) {
f := &queryIDFactory{}
for i := 0; i < 10; i++ {
if n := f.Next(); n != uint64(i) {
t.Errorf("expected %d, got %d", i, n)
}
}
}
func TestMergeNonOverlappingKeys(t *testing.T) {
realData := ast.MustParseTerm(`{"foo": "bar"}`).Value.(ast.Object)
mockData := ast.MustParseTerm(`{"baz": "blah"}`).Value.(ast.Object)
result, ok := merge(mockData, realData)
if !ok {
t.Fatal("Unexpected error occurred")
}
expected := ast.MustParseTerm(`{"foo": "bar", "baz": "blah"}`).Value
if expected.Compare(result) != 0 {
t.Fatalf("Expected %v but got %v", expected, result)
}
}
func TestMergeOverlappingKeys(t *testing.T) {
realData := ast.MustParseTerm(`{"foo": "bar"}`).Value.(ast.Object)
mockData := ast.MustParseTerm(`{"foo": "blah"}`).Value.(ast.Object)
result, ok := merge(mockData, realData)
if !ok {
t.Fatal("Unexpected error occurred")
}
expected := ast.MustParseTerm(`{"foo": "blah"}`).Value
if expected.Compare(result) != 0 {
t.Fatalf("Expected %v but got %v", expected, result)
}
realData = ast.MustParseTerm(`{"foo": {"foo1": {"foo11": [1,2,3], "foo12": "hello"}}, "bar": "baz"}`).Value.(ast.Object)
mockData = ast.MustParseTerm(`{"foo": {"foo1": [1,2,3], "foo12": "world", "foo13": 123}, "baz": "bar"}`).Value.(ast.Object)
result, ok = merge(mockData, realData)
if !ok {
t.Fatal("Unexpected error occurred")
}
expected = ast.MustParseTerm(`{"foo": {"foo1": [1,2,3], "foo12": "world", "foo13": 123}, "bar": "baz", "baz": "bar"}`).Value
if expected.Compare(result) != 0 {
t.Fatalf("Expected %v but got %v", expected, result)
}
}
func TestMergeWhenHittingNonObject(t *testing.T) {
cases := []struct {
note string
real, mock, exp *ast.Term
}{
{
note: "real object, mock string",
real: ast.MustParseTerm(`{"foo": "bar"}`),
mock: ast.StringTerm("foo"),
exp: ast.StringTerm("foo"),
},
{
note: "real string, mock object",
real: ast.StringTerm("foo"),
mock: ast.MustParseTerm(`{"foo": "bar"}`),
exp: ast.MustParseTerm(`{"foo": "bar"}`),
},
{
note: "real object with string value, where mock has object-value",
real: ast.MustParseTerm(`{"foo": ["bar"], "quz": false}`),
mock: ast.MustParseTerm(`{"foo": {"bar": 123}}`),
exp: ast.MustParseTerm(`{"foo": {"bar": 123}, "quz": false}`),
},
{
note: "real object with deeply-nested object value, where mock has number-value",
real: ast.MustParseTerm(`{"foo": {"bar": {"baz": "quz"}, "quz": true}}`),
mock: ast.MustParseTerm(`{"foo": {"bar": 10}}`),
exp: ast.MustParseTerm(`{"foo": {"bar": 10, "quz": true}}`),
},
{
note: "real object with deeply-nested string value, where mock has object-value",
real: ast.MustParseTerm(`{"foo": {"bar": {"baz": "quz"}, "quz": true}}`),
mock: ast.MustParseTerm(`{"foo": {"bar": {"baz": {"foo": "bar"}}}}`),
exp: ast.MustParseTerm(`{"foo": {"bar": {"baz": {"foo": "bar"}}, "quz": true}}`),
},
}
for _, tc := range cases {
t.Run(tc.note, func(t *testing.T) {
merged, ok := merge(tc.mock.Value, tc.real.Value)
if !ok {
t.Fatal("expected no error")
}
if tc.exp.Value.Compare(merged) != 0 {
t.Errorf("Expected %v but got %v", tc.exp, merged)
}
})
}
}
func TestRefContainsNonScalar(t *testing.T) {
cases := []struct {
note string
ref ast.Ref
expected bool
}{
{
note: "empty ref",
ref: ast.MustParseRef("data"),
expected: false,
},
{
note: "string ref",
ref: ast.MustParseRef(`data.foo["bar"]`),
expected: false,
},
{
note: "number ref",
ref: ast.MustParseRef("data.foo[1]"),
expected: false,
},
{
note: "set ref",
ref: ast.MustParseRef("data.foo[{0}]"),
expected: true,
},
{
note: "array ref",
ref: ast.MustParseRef(`data.foo[["bar"]]`),
expected: true,
},
{
note: "object ref",
ref: ast.MustParseRef(`data.foo[{"bar": 1}]`),
expected: true,
},
}
for _, tc := range cases {
t.Run(tc.note, func(t *testing.T) {
actual := refContainsNonScalar(tc.ref)
if actual != tc.expected {
t.Errorf("Expected %t for %s", tc.expected, tc.ref)
}
})
}
}
func TestContainsNestedRefOrCall(t *testing.T) {
tests := []struct {
note string
input string
want bool
}{
{
note: "single term - negative",
input: "p[x]",
want: false,
},
{
note: "single term - positive ref",
input: "p[q[x]]",
want: true,
},
{
note: "single term - positive composite ref",
input: "[q[x]]",
want: true,
},
{
note: "single term - positive composite call",
input: "[f(x)]",
want: true,
},
{
note: "call expr - negative",
input: "f(x)",
want: false,
},
{
note: "call expr - positive ref",
input: "f(p[x])",
want: true,
},
{
note: "call expr - positive call",
input: "f(g(x))",
want: true,
},
{
note: "call expr - positive composite",
input: "f([g(x)])",
want: true,
},
{
note: "unify expr - negative",
input: "p[x] = q[y]",
want: false,
},
{
note: "unify expr - positive ref",
input: "p[x] = q[r[y]]",
want: true,
},
{
note: "unify expr - positive call",
input: "f(x) = g(h(y))",
want: true,
},
}
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
vis := newNestedCheckVisitor()
expr := ast.MustParseExpr(tc.input)
result := containsNestedRefOrCall(vis, expr)
if result != tc.want {
t.Fatal("Expected", tc.want, "but got", result)
}
})
}
}
func TestTopdownVirtualCache(t *testing.T) {
ctx := context.Background()
store := inmem.New()
tests := []struct {
note string
module string
query string
hit, miss uint64
exp interface{} // if non-nil, check var `x`
}{
{
note: "different args",
module: `package p
f(0) = 1
f(x) = 12 { x > 0 }`,
query: `data.p.f(0); data.p.f(1)`,
hit: 0,
miss: 2,
},
{
note: "same args",
module: `package p
f(0) = 1
f(x) = 12 { x > 0 }`,
query: `data.p.f(1); data.p.f(1)`,
hit: 1,
miss: 1,
},
{
note: "captured output",
module: `package p
f(0) = 1
f(x) = 12 { x > 0 }`,
query: `data.p.f(0); data.p.f(0, x)`,
hit: 1,
miss: 1,
exp: 1,
},
{
note: "captured output, bool(false) result",
module: `package p
g(x) = true { x > 0 }
g(x) = false { x <= 0 }`,
query: `data.p.g(-1, x); data.p.g(-1, y)`,
hit: 1,
miss: 1,
exp: false,
},
{
note: "same args, iteration case",
module: `package p
f(0) = 1
f(x) = 12 { x > 0 }
q = y {
x := f(1)
y := f(1)
x == y
}`,
query: `x = data.p.q`,
hit: 1,
miss: 2, // one for q, one for f(1)
exp: 12,
},
{
note: "cache invalidation",
module: `package p
f(x) = y { x+input = y }`,
query: `data.p.f(1, z) with input as 7; data.p.f(1, z2) with input as 8`,
hit: 0,
miss: 2,
},
{
note: "partial object: simple",
module: `package p
s["foo"] = true { true }
s["bar"] = true { true }`,
query: `data.p.s["foo"]; data.p.s["foo"]`,
hit: 1,
miss: 1,
},
{
note: "partial set: simple",
module: `package p
s["foo"] { true }
s["bar"] { true }`,
query: `data.p.s["foo"]; data.p.s["foo"]`,
hit: 1,
miss: 1,
},
{
note: "partial set: object",
module: `package p
s[z] { z := {"foo": "bar"} }`,
query: `x = {"foo": "bar"}; data.p.s[x]; data.p.s[x]`,
hit: 1,
miss: 1,
},
{
note: "partial set: miss",
module: `package p
s[z] { z = true }`,
query: `data.p.s[true]; not data.p.s[false]`,
hit: 0,
miss: 2,
},
{
note: "partial set: full extent cached",
module: `package test
p[x] { x = 1 }
p[x] { x = 2 }
`,
query: "data.test.p = x; data.test.p = y",
hit: 1,
miss: 1,
},
{
note: "partial set: all rules + each rule (non-ground var) cached",
module: `package test
p = r { data.test.q = x; data.test.q[y] = z; data.test.q[a] = b; r := true }
q[x] { x = 1 }
q[x] { x = 2 }
`,
query: "data.test.p = true",
hit: 3, // 'data.test.q[y] = z' + 2x 'data.test.q[a] = b'
miss: 2, // 'data.test.p = true' + 'data.test.q = x'
},
{
note: "partial set: all rules + each rule (non-ground composite) cached",
module: `package test
p { data.test.q = x; data.test.q[[y, 1]] = z; data.test.q[[a, 2]] = b }
q[[x, x]] { x = 1 }
q[[x, x]] { x = 2 }
`,
query: "data.test.p = true",
hit: 2, // 'data.test.q[[y,1]] = z' + 'data.test.q[[a, 2]] = b'
miss: 2, // 'data.test.p = true' + 'data.test.q = x'
},
{
note: "partial set: each rule (non-ground var), full extent cached",
module: `package test
p = r { data.test.q[y] = z; data.test.q = x; r := true }
q[x] { x = 1 }
q[x] { x = 2 }
`,
query: "data.test.p = x",
hit: 2, // 2x 'data.test.q = x'
miss: 2, // 'data.test.p = true' + 'data.test.q[y] = z'
},
{
note: "partial set: each rule (non-ground composite), full extent cached",
module: `package test
p = y { data.test.q[[y, 1]] = z; data.test.q = x }
q[[x, x]] { x = 1 }
q[[x, x]] { x = 2 }
`,
query: "data.test.p = x",
hit: 0,
miss: 3, // 'data.test.p = true' + 'data.test.q[[y, 1]] = z' + 'data.test.q = x'
exp: 1,
},
}
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
compiler := compileModules([]string{tc.module})
txn := storage.NewTransactionOrDie(ctx, store)
defer store.Abort(ctx, txn)
m := metrics.New()
query := NewQuery(ast.MustParseBody(tc.query)).
WithCompiler(compiler).
WithStore(store).
WithTransaction(txn).
WithInstrumentation(NewInstrumentation(m))
qrs, err := query.Run(ctx)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if exp, act := 1, len(qrs); exp != act {
t.Fatalf("expected %d query result, got %d query results: %+v", exp, act, qrs)
}
if tc.exp != nil {
x := ast.Var("x")
if exp, act := ast.NewTerm(ast.MustInterfaceToValue(tc.exp)), qrs[0][x]; !exp.Equal(act) {
t.Errorf("unexpected query result: want = %v, got = %v", exp, act)
}
}
// check metrics
if exp, act := tc.hit, m.Counter(evalOpVirtualCacheHit).Value().(uint64); exp != act {
t.Errorf("expected %d cache hits, got %d", exp, act)
}
if exp, act := tc.miss, m.Counter(evalOpVirtualCacheMiss).Value().(uint64); exp != act {
t.Errorf("expected %d cache misses, got %d", exp, act)
}
})
}
}