forked from open-policy-agent/conftest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptance.bats
executable file
·291 lines (238 loc) · 9.81 KB
/
acceptance.bats
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
#!/usr/bin/env bats
@test "Not fail when testing a service with a warning" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/service.yaml
[ "$status" -eq 0 ]
}
@test "Not fail when passed an explicit blank filename" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/service.yaml ""
[ "$status" -eq 0 ]
}
@test "Fail when testing a deployment with root containers" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/deployment.yaml
[ "$status" -eq 1 ]
}
@test "Fail when testing a service with warnings" {
run ./conftest test --fail-on-warn -p examples/kubernetes/policy examples/kubernetes/service.yaml
[ "$status" -eq 1 ]
}
@test "Fail when testing with no policies path" {
run ./conftest test -p internal/ examples/kubernetes/deployment.yaml
[ "$status" -eq 1 ]
}
@test "Pass when testing a blank namespace" {
run ./conftest test --namespace notpresent -p examples/kubernetes/policy examples/kubernetes/deployment.yaml
[ "$status" -eq 0 ]
}
@test "when testing a YAML document via stdin, default parser should be yaml if no parser flag is passed" {
run ./conftest test -p examples/kubernetes/policy - < examples/kubernetes/service.yaml
[ "$status" -eq 0 ]
}
@test "Pass when testing a YAML document via stdin" {
run ./conftest test --parser yaml -p examples/kubernetes/policy - < examples/kubernetes/service.yaml
[ "$status" -eq 0 ]
}
@test "Fail due to picking up settings from configuration file" {
cd examples/configfile
run ../../conftest test deployment.yaml
[ "$status" -eq 1 ]
[[ "$output" =~ "Containers must not run as root" ]]
}
@test "Has version flag" {
run ./conftest --version
[ "$status" -eq 0 ]
}
@test "Test command with multiple input type" {
run ./conftest test examples/traefik/traefik.toml examples/kubernetes/service.yaml -p examples/kubernetes/policy
[ "$status" -eq 0 ]
[[ "$output" =~ "Found service hello-kubernetes but services are not allowed" ]]
}
@test "Test command has trace flag" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/service.yaml --trace
[ "$status" -eq 0 ]
[[ "$output" =~ "data.kubernetes.is_service" ]]
}
@test "Test command with all namespaces flag" {
run ./conftest test -p examples/docker/policy examples/docker/Dockerfile --all-namespaces
[ "$status" -eq 1 ]
[[ "$output" =~ "unallowed image found [\"openjdk:8-jdk-alpine\"]" ]]
[[ "$output" =~ "unallowed commands found [\"apk add --no-cache python3 python3-dev build-base && pip3 install awscli==1.18.1\"]" ]]
}
@test "Test command works with nested namespaces" {
run ./conftest test --namespace main.gke -p examples/hcl1/policy/ examples/hcl1/gke.tf --no-color
[ "$status" -eq 1 ]
[ "${lines[1]}" = "1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions" ]
}
@test "Verify command has trace flag" {
run ./conftest verify --policy ./examples/kubernetes/policy --trace
[ "$status" -eq 0 ]
[[ "$output" =~ "data.kubernetes.is_service" ]]
}
@test "Fail when verifying with no policies path" {
run ./conftest verify -p internal/
[ "$status" -eq 1 ]
}
@test "Has help flag" {
run ./conftest --help
[ "$status" -eq 0 ]
}
@test "Allow .rego files in the policy flag" {
run ./conftest test -p examples/hcl1/policy/base.rego examples/hcl1/gke-show.json
[ "$status" -eq 1 ]
[[ "$output" =~ "Terraform plan will change prohibited resources in the following namespaces: google_iam, google_container" ]]
}
@test "Can parse hcl1 files" {
run ./conftest test -p examples/hcl1/policy/gke.rego examples/hcl1/gke.tf
[ "$status" -eq 0 ]
}
@test "Can parse toml files" {
run ./conftest test -p examples/traefik/policy examples/traefik/traefik.toml
[ "$status" -eq 1 ]
}
@test "Can parse edn files" {
run ./conftest test -p examples/edn/policy examples/edn/sample_config.edn
[ "$status" -eq 1 ]
}
@test "Can parse xml files" {
run ./conftest test -p examples/xml/policy examples/xml/pom.xml
[ "$status" -eq 1 ]
[[ "$output" =~ "--- maven-plugin must have the version: 3.6.1" ]]
}
@test "Can parse hocon files" {
run ./conftest test -p examples/hocon/policy examples/hocon/hocon.conf --parser hocon
[ "$status" -eq 1 ]
[[ "$output" =~ "Play http server port should be 9000" ]]
}
@test "Can parse vcl files" {
run ./conftest test -p examples/vcl/policy examples/vcl/varnish.vcl
[ "$status" -eq 1 ]
[[ "$output" =~ "default backend port should be 8080" ]]
}
@test "Can parse jsonnet files" {
run ./conftest test -p examples/jsonnet/policy examples/jsonnet/arith.jsonnet
[ "$status" -eq 1 ]
[[ "$output" =~ "Concat array should be less than 3" ]]
}
@test "Can parse .dockerignore files" {
run ./conftest test -p examples/ignore/dockerignore/policy examples/ignore/dockerignore/.dockerignore
[ "$status" -eq 1 ]
[[ "$output" =~ ".git directories should be ignored" ]]
}
@test "Can parse .gitignore files" {
run ./conftest test -p examples/ignore/gitignore/policy examples/ignore/gitignore/.gitignore
[ "$status" -eq 1 ]
[[ "$output" =~ "id_rsa files should be ignored" ]]
}
@test "Can parse cue files" {
run ./conftest test -p examples/cue/policy examples/cue/deployment.cue
[ "$status" -eq 1 ]
[[ "$output" =~ "The image port should be 8080 in deployment.cue. you got : 8081" ]]
}
@test "Can parse ini files" {
run ./conftest test -p examples/ini/policy examples/ini/grafana.ini
[ "$status" -eq 1 ]
[[ "$output" =~ "Users should verify their e-mail address" ]]
}
@test "Can parse hcl files" {
run ./conftest test -p examples/hcl2/policy examples/hcl2/terraform.tf
[ "$status" -eq 1 ]
[[ "$output" =~ "ALB \`my-alb-listener\` is using HTTP rather than HTTP" ]]
}
@test "Can parse stdin with parser flag" {
run bash -c "cat examples/ini/grafana.ini | ./conftest test -p examples/ini/policy --parser ini -"
[ "$status" -eq 1 ]
[[ "$output" =~ "Users should verify their e-mail address" ]]
[[ "$output" != *"Basic auth should be enabled"* ]]
}
@test "Using --parser should force the chosen parser and fail the rego policy" {
run ./conftest test -p examples/terraform/policy/gke.rego examples/terraform/gke.tf --parser ini
[ "$status" -eq 1 ]
}
@test "Can combine configs and reference by file" {
run ./conftest test -p examples/hcl1/policy/gke_combine.rego examples/hcl1/gke.tf --combine --parser hcl1 --all-namespaces
[ "$status" -eq 0 ]
}
@test "Can parse docker files" {
run ./conftest test -p examples/docker/policy examples/docker/Dockerfile
[ "$status" -eq 1 ]
[[ "$output" =~ "unallowed image found [\"openjdk:8-jdk-alpine\"]" ]]
}
@test "Can disable color" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/service.yaml --no-color
[ "$status" -eq 0 ]
[[ "$output" != *"[33m"* ]]
}
@test "Output results only once" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/deployment.yaml
count="${#lines[@]}"
[ "$count" -eq 5 ]
}
@test "Can verify rego tests" {
run ./conftest verify --policy ./examples/kubernetes/policy
[ "$status" -eq 0 ]
[[ "$output" =~ "4 tests, 4 passed" ]]
}
@test "Can parse inputs with 'conftest parse'" {
run ./conftest parse examples/docker/Dockerfile
[ "$status" -eq 0 ]
[[ "$output" =~ "\"Cmd\": \"from\"" ]]
}
@test "Can output tap format in test command" {
run ./conftest test -p examples/kubernetes/policy/ -o tap examples/kubernetes/deployment.yaml
[[ "$output" =~ "not ok" ]]
}
@test "Can output tap format in verify command" {
run ./conftest verify -p examples/kubernetes/policy/ -o tap
[[ "$output" =~ "ok" ]]
}
@test "Can output table format in test command" {
run ./conftest test -p examples/kubernetes/policy/ -o table examples/kubernetes/deployment.yaml
[[ "$output" =~ "failure" ]]
}
@test "Can output table format in verify command" {
run ./conftest verify -p examples/kubernetes/policy/ -o table
[[ "$output" =~ "success" ]]
}
@test "Multi-file tests correctly fail when last file is fine" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/deployment.yaml examples/kubernetes/service.yaml
[ "$status" -eq 1 ]
}
@test "Fail when unit test rego" {
run ./conftest verify -p examples/traefik/policy
[ "$status" -eq 1 ]
}
@test "Can load data along with rego policies" {
run ./conftest test -p examples/data/policy -d examples/data/exclusions examples/data/service.yaml
[ "$status" -eq 1 ]
[[ "$output" =~ "Cannot expose port" ]]
}
@test "Can load data in unit tests" {
run ./conftest verify -p examples/data/policy -d examples/data/exclusions examples/data/service.yaml
[ "$status" -eq 0 ]
[[ "$output" =~ "1 test, 1 passed, 0 warnings, 0 failures" ]]
}
@test "Can update policies in test command" {
run ./conftest test --update https://raw.githubusercontent.com/open-policy-agent/conftest/master/examples/compose/policy/deny.rego examples/compose/docker-compose.yml
rm -rf policy/deny.rego
[ "$status" -eq 1 ]
[[ "$output" =~ "No images tagged latest" ]]
}
@test "The number of tests run is accurate" {
run ./conftest test -p examples/kubernetes/policy examples/kubernetes/service.yaml --no-color
[ "$status" -eq 0 ]
[ "${lines[1]}" = "5 tests, 4 passed, 1 warning, 0 failures, 0 exceptions" ]
}
@test "Exceptions reported correctly" {
run ./conftest test -p examples/exceptions/policy examples/exceptions/deployments.yaml --no-color
[ "$status" -eq 1 ]
[ "${lines[2]}" = "2 tests, 0 passed, 0 warnings, 1 failure, 1 exception" ]
}
@test "Can combine yaml files" {
run ./conftest test -p examples/combine/policy examples/combine/team.yaml examples/combine/user1.yaml examples/combine/user2.yaml --combine
[ "$status" -eq 1 ]
[[ "$output" =~ "2 tests, 1 passed, 0 warnings, 1 failure" ]]
}
@test "Combining multi-document yaml file has same result" {
run ./conftest test -p examples/combine/policy examples/combine/team.yaml examples/combine/users.yaml --combine
[ "$status" -eq 1 ]
[[ "$output" =~ "2 tests, 1 passed, 0 warnings, 1 failure" ]]
}