forked from proofpoint/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanner_test.go
170 lines (163 loc) · 3.54 KB
/
planner_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
// 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 planner
import (
"os"
"testing"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/internal/ir"
)
func TestPlannerHelloWorld(t *testing.T) {
// NOTE(tsandall): These tests are not meant to give comprehensive coverage
// of the planner. Currently we have a suite of end-to-end tests in the
// test/wasm/ directory that are specified in YAML, compiled into Wasm, and
// executed inside of a node program. For the time being, the planner is
// simple enough that exhaustive unit testing is not as valuable as
// end-to-end testing. These tests provide a quick sanity check that the
// planner is not failing on simple inputs.
tests := []struct {
note string
queries []string
modules []string
exp ir.Policy
}{
{
note: "empty",
queries: []string{},
},
{
note: "hello world",
queries: []string{"input.a = 1"},
},
{
note: "conjunction",
queries: []string{"1 = 1; 2 = 2"},
},
{
note: "disjunction",
queries: []string{"input.a = 1", "input.b = 2"},
},
{
note: "iteration",
queries: []string{"input.a[i] = 1; input.b = 2"},
},
{
note: "iteration: compare key",
queries: []string{"input.a[i] = 1; input.b = i"},
},
{
note: "iteration: nested",
queries: []string{"input.a[i] = 1; input.b[j] = 2"},
},
{
note: "iteration: chained",
queries: []string{"input.a[i][j] = 1"},
},
{
note: "negation",
queries: []string{"not input.x.y = 1"},
},
{
note: "array ref pattern match",
queries: []string{"input.x = [1, [y]]"},
},
{
note: "arrays pattern match",
queries: []string{"[x, 3, [2]] = [1, 3, [y]]"},
},
{
note: "sets",
queries: []string{"x = {1,2,3}; x[y]"},
},
{
note: "vars",
queries: []string{"x = 1"},
},
{
note: "complete rules",
queries: []string{"true"},
modules: []string{`
package test
p = x { x = 1 }
p = y { y = 2 }
`},
},
{
note: "complete rule reference",
queries: []string{"data.test.p = 10"},
modules: []string{`
package test
p = x { x = 10 }
`},
},
{
note: "functions",
queries: []string{"data.test.f([1,x])"},
modules: []string{`
package test
f([a, b]) {
a = b
}
`},
},
{
note: "else",
queries: []string{"data.test.p = 1"},
modules: []string{`
package test
p = 0 {
false
} else = 1 {
true
}
`},
},
{
note: "partial set",
queries: []string{"data.test.p = {1,2}"},
modules: []string{`
package test
p[1]
p[2]
`},
},
{
note: "partial object",
queries: []string{`data.test.p = {"a": 1, "b": 2}`},
modules: []string{`
package test
p["a"] = 1
p["b"] = 2
`},
},
{
note: "virtual extent",
queries: []string{`data`},
modules: []string{`
package test
p = 1
q = 2 { false }
`},
},
}
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
queries := make([]ast.Body, len(tc.queries))
for i := range queries {
queries[i] = ast.MustParseBody(tc.queries[i])
}
modules := make([]*ast.Module, len(tc.modules))
for i := range modules {
modules[i] = ast.MustParseModule(tc.modules[i])
}
planner := New().WithQueries(queries).WithModules(modules)
policy, err := planner.Plan()
if err != nil {
t.Fatal(err)
}
_ = policy
ir.Pretty(os.Stderr, policy)
})
}
}