forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
366 lines (301 loc) · 9.75 KB
/
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
// 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 cmd
import (
"context"
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/compile"
"github.com/open-policy-agent/opa/cover"
"github.com/open-policy-agent/opa/internal/runtime"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/open-policy-agent/opa/tester"
"github.com/open-policy-agent/opa/topdown"
"github.com/open-policy-agent/opa/topdown/lineage"
"github.com/open-policy-agent/opa/util"
)
const (
testPrettyOutput = "pretty"
testJSONOutput = "json"
)
type testCommandParams struct {
verbose bool
explain *util.EnumFlag
errLimit int
outputFormat *util.EnumFlag
coverage bool
threshold float64
timeout time.Duration
ignore []string
bundleMode bool
benchmark bool
benchMem bool
runRegex string
count int
target *util.EnumFlag
skipExitZero bool
}
func newTestCommandParams() *testCommandParams {
return &testCommandParams{
outputFormat: util.NewEnumFlag(testPrettyOutput, []string{testPrettyOutput, testJSONOutput, benchmarkGoBenchOutput}),
explain: newExplainFlag([]string{explainModeFails, explainModeFull, explainModeNotes}),
target: util.NewEnumFlag(compile.TargetRego, []string{compile.TargetRego, compile.TargetWasm}),
}
}
var testParams = newTestCommandParams()
var testCommand = &cobra.Command{
Use: "test <path> [path [...]]",
Short: "Execute Rego test cases",
Long: `Execute Rego test cases.
The 'test' command takes a file or directory path as input and executes all
test cases discovered in matching files. Test cases are rules whose names have the prefix "test_".
If the '--bundle' option is specified the paths will be treated as policy bundles
and loaded following standard bundle conventions. The path can be a compressed archive
file or a directory which will be treated as a bundle. Without the '--bundle' flag OPA
will recursively load ALL *.rego, *.json, and *.yaml files for evaluating the test cases.
Test cases under development may be prefixed "todo_" in order to skip their execution,
while still getting marked as skipped in the test results.
Example policy (example/authz.rego):
package authz
allow {
input.path = ["users"]
input.method = "POST"
}
allow {
input.path = ["users", profile_id]
input.method = "GET"
profile_id = input.user_id
}
Example test (example/authz_test.rego):
package authz
test_post_allowed {
allow with input as {"path": ["users"], "method": "POST"}
}
test_get_denied {
not allow with input as {"path": ["users"], "method": "GET"}
}
test_get_user_allowed {
allow with input as {"path": ["users", "bob"], "method": "GET", "user_id": "bob"}
}
test_get_another_user_denied {
not allow with input as {"path": ["users", "bob"], "method": "GET", "user_id": "alice"}
}
todo_test_user_allowed_http_client_data {
false # Remember to test this later!
}
Example test run:
$ opa test ./example/
If used with the '--bench' option then tests will be benchmarked.
Example benchmark run:
$ opa test --bench ./example/
The optional "gobench" output format conforms to the Go Benchmark Data Format.
`,
PreRunE: func(Cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("specify at least one file")
}
// If an --explain flag was set, turn on verbose output
if testParams.explain.IsSet() {
testParams.verbose = true
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
os.Exit(opaTest(args))
},
}
func opaTest(args []string) int {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if testParams.outputFormat.String() == benchmarkGoBenchOutput && !testParams.benchmark {
fmt.Fprintf(os.Stderr, "cannot use output format %s without running benchmarks (--bench)\n", benchmarkGoBenchOutput)
return 0
}
filter := loaderFilter{
Ignore: testParams.ignore,
}
var modules map[string]*ast.Module
var bundles map[string]*bundle.Bundle
var store storage.Store
var err error
if testParams.bundleMode {
bundles, err = tester.LoadBundles(args, filter.Apply)
store = inmem.New()
} else {
modules, store, err = tester.Load(args, filter.Apply)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
txn, err := store.NewTransaction(ctx, storage.WriteParams)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
defer store.Abort(ctx, txn)
compiler := ast.NewCompiler().
SetErrorLimit(testParams.errLimit).
WithPathConflictsCheck(storage.NonEmpty(ctx, store, txn)).
WithEnablePrintStatements(!testParams.benchmark)
info, err := runtime.Term(runtime.Params{})
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
if testParams.threshold > 0 && !testParams.coverage {
testParams.coverage = true
}
var cov *cover.Cover
var coverTracer topdown.QueryTracer
if testParams.coverage {
if testParams.benchmark {
fmt.Fprintln(os.Stderr, "coverage reporting is not supported when benchmarking tests")
return 1
}
cov = cover.New()
coverTracer = cov
}
timeout := testParams.timeout
if timeout == 0 { // unset
timeout = time.Duration(5 * time.Second)
if testParams.benchmark {
timeout = time.Duration(30 * time.Second)
}
}
runner := tester.NewRunner().
SetCompiler(compiler).
SetStore(store).
CapturePrintOutput(true).
EnableTracing(testParams.verbose).
SetCoverageQueryTracer(coverTracer).
SetRuntime(info).
SetModules(modules).
SetBundles(bundles).
SetTimeout(timeout).
Filter(testParams.runRegex).
Target(testParams.target.String())
var reporter tester.Reporter
goBench := false
if !testParams.coverage {
switch testParams.outputFormat.String() {
case testJSONOutput:
reporter = tester.JSONReporter{
Output: os.Stdout,
}
case benchmarkGoBenchOutput:
goBench = true
fallthrough
default:
reporter = tester.PrettyReporter{
Verbose: testParams.verbose,
Output: os.Stdout,
BenchmarkResults: testParams.benchmark,
BenchMarkShowAllocations: testParams.benchMem,
BenchMarkGoBenchFormat: goBench,
}
}
} else {
reporter = tester.JSONCoverageReporter{
Cover: cov,
Modules: modules,
Output: os.Stdout,
Threshold: testParams.threshold,
}
}
for i := 0; i < testParams.count; i++ {
exitCode := runTests(ctx, txn, runner, reporter)
if exitCode != 0 {
return exitCode
}
}
return 0
}
func runTests(ctx context.Context, txn storage.Transaction, runner *tester.Runner, reporter tester.Reporter) int {
var err error
var ch chan *tester.Result
if testParams.benchmark {
benchOpts := tester.BenchmarkOptions{
ReportAllocations: testParams.benchMem,
}
ch, err = runner.RunBenchmarks(ctx, txn, benchOpts)
} else {
ch, err = runner.RunTests(ctx, txn)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
exitCode := 0
dup := make(chan *tester.Result)
go func() {
defer close(dup)
for tr := range ch {
if !tr.Pass() && !testParams.skipExitZero {
exitCode = 2
}
if tr.Skip && exitCode == 0 && testParams.skipExitZero {
// there is a skipped test, adding the flag -z exits 0 if there are no failures
exitCode = 0
}
tr.Trace = filterTrace(testParams, tr.Trace)
dup <- tr
}
}()
if err := reporter.Report(dup); err != nil {
fmt.Fprintln(os.Stderr, err)
if !testParams.benchmark {
if _, ok := err.(*cover.CoverageThresholdError); ok {
return 2
}
}
return 1
}
return exitCode
}
func filterTrace(params *testCommandParams, trace []*topdown.Event) []*topdown.Event {
ops := map[topdown.Op]struct{}{}
mode := params.explain.String()
if mode == explainModeFull {
// Don't bother filtering anything
return trace
}
// If an explain mode was specified, filter based
// on the mode. If no explain mode was specified,
// default to show both notes and fail events
showDefault := !params.explain.IsSet() && params.verbose
if mode == explainModeNotes || showDefault {
ops[topdown.NoteOp] = struct{}{}
}
if mode == explainModeFails || showDefault {
ops[topdown.FailOp] = struct{}{}
}
return lineage.Filter(trace, func(event *topdown.Event) bool {
_, relevant := ops[event.Op]
return relevant
})
}
func init() {
testCommand.Flags().BoolVarP(&testParams.skipExitZero, "exit-zero-on-skipped", "z", false, "skipped tests return status 0")
testCommand.Flags().BoolVarP(&testParams.verbose, "verbose", "v", false, "set verbose reporting mode")
testCommand.Flags().DurationVar(&testParams.timeout, "timeout", 0, "set test timeout (default 5s, 30s when benchmarking)")
testCommand.Flags().VarP(testParams.outputFormat, "format", "f", "set output format")
testCommand.Flags().BoolVarP(&testParams.coverage, "coverage", "c", false, "report coverage (overrides debug tracing)")
testCommand.Flags().Float64VarP(&testParams.threshold, "threshold", "", 0, "set coverage threshold and exit with non-zero status if coverage is less than threshold %")
testCommand.Flags().BoolVar(&testParams.benchmark, "bench", false, "benchmark the unit tests")
testCommand.Flags().StringVarP(&testParams.runRegex, "run", "r", "", "run only test cases matching the regular expression.")
addBundleModeFlag(testCommand.Flags(), &testParams.bundleMode, false)
addBenchmemFlag(testCommand.Flags(), &testParams.benchMem, true)
addCountFlag(testCommand.Flags(), &testParams.count, "test")
addMaxErrorsFlag(testCommand.Flags(), &testParams.errLimit)
addIgnoreFlag(testCommand.Flags(), &testParams.ignore)
setExplainFlag(testCommand.Flags(), testParams.explain)
addTargetFlag(testCommand.Flags(), testParams.target)
RootCommand.AddCommand(testCommand)
}