1Open(Policy) Open(Policy)
2
3
4
6 opa-test - Execute Rego test cases
7
8
9
11 opa test [path [...]] [flags]
12
13
14
16 Execute Rego test cases.
17
18
19 The 'test' command takes a file or directory path as input and executes
20 all test cases discovered in matching files. Test cases are rules whose
21 names have the prefix "test_".
22
23
24 If the '--bundle' option is specified the paths will be treated as pol‐
25 icy bundles and loaded following standard bundle conventions. The path
26 can be a compressed archive file or a directory which will be treated
27 as a bundle. Without the '--bundle' flag OPA will recursively load ALL
28 *.rego, *.json, and *.yaml files for evaluating the test cases.
29
30
31 Example policy (example/authz.rego):
32
33
34 package authz
35
36 allow {
37 input.path = ["users"]
38 input.method = "POST"
39 }
40
41 allow {
42 input.path = ["users", profile_id]
43 input.method = "GET"
44 profile_id = input.user_id
45 }
46
47
48
49 Example test (example/authz_test.rego):
50
51
52 package authz
53
54 test_post_allowed {
55 allow with input as {"path": ["users"], "method": "POST"}
56 }
57
58 test_get_denied {
59 not allow with input as {"path": ["users"], "method": "GET"}
60 }
61
62 test_get_user_allowed {
63 allow with input as {"path": ["users", "bob"], "method": "GET", "user_id": "bob"}
64 }
65
66 test_get_another_user_denied {
67 not allow with input as {"path": ["users", "bob"], "method": "GET", "user_id": "alice"}
68 }
69
70
71
72 Example test run:
73
74
75 $ opa test ./example/
76
77
78
79 If used with the '--bench' option then tests will be benchmarked.
80
81
82 Example benchmark run:
83
84
85 $ opa test --bench ./example/
86
87
88
89 The optional "gobench" output format conforms to the Go Benchmark Data
90 Format.
91
92
93
95 --bench[=false] benchmark the unit tests
96
97
98 --benchmem[=true] report memory allocations with benchmark results
99
100
101 -b, --bundle[=false] load paths as bundle files or root directo‐
102 ries
103
104
105 --count=1 number of times to repeat each test (default 1)
106
107
108 -c, --coverage[=false] report coverage (overrides debug tracing)
109
110
111 --explain=fails enable query explanations
112
113
114 -f, --format=pretty set output format
115
116
117 -h, --help[=false] help for test
118
119
120 --ignore=[] set file and directory names to ignore during loading
121 (e.g., '.*' excludes hidden files)
122
123
124 -m, --max-errors=10 set the number of errors to allow before com‐
125 pilation fails early
126
127
128 -r, --run="" run only test cases matching the regular expression.
129
130
131 --threshold=0 set coverage threshold and exit with non-zero status
132 if coverage is less than threshold %
133
134
135 -t, --timeout=5s set test timeout
136
137
138 -v, --verbose[=false] set verbose reporting mode
139
140
141
143 opa(1)
144
145
146
1472020 Agent(1)Oct Open(Policy)