forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_test.go
278 lines (232 loc) · 6.11 KB
/
query_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
// Copyright 2020 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"
"reflect"
"testing"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/storage"
inmem "github.com/open-policy-agent/opa/storage/inmem/test"
)
func TestQueryTracerDontPlugLocalVars(t *testing.T) {
cases := []struct {
note string
tracerConfs []TraceConfig
expectLocals bool
}{
{
note: "plug locals single tracer",
tracerConfs: []TraceConfig{
{PlugLocalVars: true},
},
expectLocals: true,
},
{
note: "dont plug locals single tracer",
tracerConfs: []TraceConfig{
{PlugLocalVars: false},
},
expectLocals: false,
},
{
note: "plug locals multiple tracers",
tracerConfs: []TraceConfig{
{PlugLocalVars: true},
{PlugLocalVars: true},
{PlugLocalVars: true},
},
expectLocals: true,
},
{
note: "dont plug locals multiple tracers",
tracerConfs: []TraceConfig{
{PlugLocalVars: false},
{PlugLocalVars: false},
{PlugLocalVars: false},
},
expectLocals: false,
},
{
note: "plug locals multiple plugins mixed",
tracerConfs: []TraceConfig{
{PlugLocalVars: false},
{PlugLocalVars: true},
{PlugLocalVars: false},
},
expectLocals: true,
},
}
for _, tc := range cases {
t.Run(tc.note, func(t *testing.T) {
query := initTracerTestQuery()
var tracers []*testQueryTracer
for _, conf := range tc.tracerConfs {
tt := &testQueryTracer{
events: []*Event{},
conf: conf,
enabled: true,
t: t,
}
tracers = append(tracers, tt)
query = query.WithQueryTracer(tt)
}
_, err := query.Run(context.Background())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Even if the individual tracer didn't specify for local metadata
// they will _all_ either have it or not.
for _, tt := range tracers {
for _, e := range tt.events {
if !tc.expectLocals && e.LocalMetadata != nil {
t.Fatalf("Expected event LocalMetadata to nil")
}
if tc.expectLocals && e.LocalMetadata == nil {
t.Fatalf("Expected event LocalMetadata to be non-nil")
}
}
}
})
}
}
func TestLegacyTracerUpgrade(t *testing.T) {
query := initTracerTestQuery()
tracer := &testQueryTracer{
events: []*Event{},
conf: TraceConfig{PlugLocalVars: false},
enabled: true,
t: t,
}
// Call with older API, expect to be "upgraded" to QueryTracer
// If the deprecated Trace() API is called the test will fail.
query.WithTracer(tracer)
_, err := query.Run(context.Background())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}
func TestLegacyTracerBackwardsCompatibility(t *testing.T) {
t.Helper()
query := initTracerTestQuery()
// Using a tracer that does _not_ implement the newer
// QueryTracer interface, only the deprecated Tracer one.
tracer := &testLegacyTracer{
events: []*Event{},
}
query.WithTracer(tracer)
// For comparison use a buffer tracer and the new interface
bt := NewBufferTracer()
query.WithQueryTracer(bt)
_, err := query.Run(context.Background())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(*bt) != len(tracer.events) {
t.Fatalf("Expected %d events on the test tracer, got %d", len(*bt), len(tracer.events))
}
if !reflect.DeepEqual([]*Event(*bt), tracer.events) {
t.Fatalf("Expected same events on test tracer and BufferTracer")
}
}
func TestDisabledTracer(t *testing.T) {
query := initTracerTestQuery()
tracer := &testQueryTracer{
events: []*Event{},
conf: TraceConfig{PlugLocalVars: false},
enabled: false,
t: t,
}
// Both API's should ignore the disabled tracer
query.WithTracer(tracer)
query.WithQueryTracer(tracer)
_, err := query.Run(context.Background())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(tracer.events) > 0 {
t.Fatalf("Expected no events on test tracer, got %d", len(tracer.events))
}
}
func TestRegoMetadataBuiltinCall(t *testing.T) {
tests := []struct {
note string
query string
expectedError string
}{
{
note: "rego.metadata.chain() call",
query: "rego.metadata.chain()",
expectedError: "rego.metadata.chain(): eval_builtin_error: rego.metadata.chain: the rego.metadata.chain function must only be called within the scope of a rule",
},
{
note: "rego.metadata.rule() call",
query: "rego.metadata.rule()",
expectedError: "rego.metadata.rule(): eval_builtin_error: rego.metadata.rule: the rego.metadata.rule function must only be called within the scope of a rule",
},
}
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
c := ast.NewCompiler()
q := NewQuery(ast.MustParseBody(tc.query)).WithCompiler(c).
WithStrictBuiltinErrors(true)
_, err := q.Run(context.Background())
if err == nil {
t.Fatalf("expected error")
}
if tc.expectedError != err.Error() {
t.Fatalf("expected error:\n\n%s\n\ngot:\n\n%s", tc.expectedError, err.Error())
}
})
}
}
func initTracerTestQuery() *Query {
ctx := context.Background()
store := inmem.New()
inputTerm := &ast.Term{}
txn := storage.NewTransactionOrDie(ctx, store)
defer store.Abort(ctx, txn)
compiler := compileModules([]string{
`package x
p {
a := [1, 2, 3]
f(a[_])
}
f(x) {
x == 3
}
`})
return NewQuery(ast.MustParseBody("data.x.p")).
WithCompiler(compiler).
WithStore(store).
WithTransaction(txn).
WithInput(inputTerm)
}
type testQueryTracer struct {
events []*Event
conf TraceConfig
enabled bool
t *testing.T
}
func (n *testQueryTracer) Enabled() bool {
return n.enabled
}
func (n *testQueryTracer) Trace(e *Event) {
n.t.Errorf("Unexpected call to Trace() with event %v", e)
}
func (n *testQueryTracer) TraceEvent(e Event) {
n.events = append(n.events, &e)
}
func (n *testQueryTracer) Config() TraceConfig {
return n.conf
}
type testLegacyTracer struct {
events []*Event
}
func (n *testLegacyTracer) Enabled() bool {
return true
}
func (n *testLegacyTracer) Trace(e *Event) {
n.events = append(n.events, e)
}