Skip to content

Commit ad09318

Browse files
authored
Added conftest tests to resources (#149)
* Added conftest tests to resources * Added testing doc
1 parent 9c7cd05 commit ad09318

5 files changed

Lines changed: 156 additions & 1 deletion

File tree

.github/workflows/conftest.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Validate
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
conftest:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v2
11+
12+
- name: Conftest
13+
uses: redhat-cop/github-actions/confbatstest@master
14+
with:
15+
tests: _test/conftest.sh

.gitignore

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,33 @@ dmypy.json
113113

114114
# Ignore vscode meta
115115
*.code-workspace
116-
.vscode/
116+
.vscode/
117+
118+
### JetBrains template
119+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
120+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
121+
122+
# User-specific stuff:
123+
.idea/
124+
.idea/**/workspace.xml
125+
.idea/**/tasks.xml
126+
.idea/dictionaries
127+
128+
# Sensitive or high-churn files:
129+
.idea/**/dataSources/
130+
.idea/**/dataSources.ids
131+
.idea/**/dataSources.xml
132+
.idea/**/dataSources.local.xml
133+
.idea/**/sqlDataSources.xml
134+
.idea/**/dynamic.xml
135+
.idea/**/uiDesigner.xml
136+
137+
## File-based project format:
138+
*.iws
139+
*.iml
140+
141+
# Rego
142+
policy/
143+
144+
# BATS
145+
_test/test_helper/

_test/TESTING.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Testing
2+
The OCP resources should be tested via [conftest](https://github.com/open-policy-agent/conftest).
3+
The tests use [BATS](https://github.com/bats-core/bats-core) as a test framework.
4+
5+
## Executing Locally
6+
```bash
7+
bats _test/conftest.sh
8+
```
9+
10+
## Policies which already exist
11+
There are two policies repos which are currently pulled via the CI:
12+
- https://github.com/redhat-cop
13+
- https://github.com/swade1987
14+
15+
Policies can also be local to this repo in the policy dir.
16+
17+
## Including a new Policy
18+
Conftest activates policies via the `--namespace` flag.
19+
20+
By default, we use a regex selector. In the example below, we only activate all the `deprecated` policies:
21+
```bash
22+
@test "charts/deploy" {
23+
tmp=$(helm_template "charts/deploy")
24+
25+
namespaces=$(get_rego_namespaces "ocp\.deprecated\.*")
26+
cmd="conftest test ${tmp} --output tap ${namespaces}"
27+
run ${cmd}
28+
29+
print_info "${status}" "${output}" "${cmd}" "${tmp}"
30+
[ "$status" -eq 0 ]
31+
}
32+
```
33+
34+
As the selector is regex, we can use groups. In the example below, we only activate `deprecated` policies for `4.1` and `4.3`:
35+
```bash
36+
@test "charts/deploy" {
37+
tmp=$(helm_template "charts/deploy")
38+
39+
namespaces=$(get_rego_namespaces "(ocp\.deprecated\.ocp4_1.*|ocp\.deprecated\.ocp4_3.*)")
40+
cmd="conftest test ${tmp} --output tap ${namespaces}"
41+
run ${cmd}
42+
43+
print_info "${status}" "${output}" "${cmd}" "${tmp}"
44+
[ "$status" -eq 0 ]
45+
}
46+
```
47+
48+
It is also possible to active all namespaces via:
49+
```bash
50+
@test "charts/deploy" {
51+
tmp=$(helm_template "charts/deploy")
52+
53+
cmd="conftest test ${tmp} --output tap --all-namespaces"
54+
run ${cmd}
55+
56+
print_info "${status}" "${output}" "${cmd}" "${tmp}"
57+
[ "$status" -eq 0 ]
58+
}
59+
```

_test/bats-support-clone.bash

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if [[ ! -d "_test/test_helper/bats-support" ]]; then
2+
# Download bats-support dynamically so it doesnt need to be added into source
3+
git clone https://github.com/ztombol/bats-support _test/test_helper/bats-support --depth 1
4+
fi
5+
6+
if [[ ! -d "_test/test_helper/redhatcop-bats-library" ]]; then
7+
# Download redhat-cop/bats-library dynamically so it doesnt need to be added into source
8+
git clone https://github.com/redhat-cop/bats-library _test/test_helper/redhatcop-bats-library --depth 1
9+
fi

_test/conftest.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bats
2+
3+
load bats-support-clone
4+
load test_helper/bats-support/load
5+
load test_helper/redhatcop-bats-library/load
6+
7+
setup_file() {
8+
rm -rf /tmp/rhcop
9+
conftest_pull
10+
}
11+
12+
@test "charts/deploy" {
13+
tmp=$(helm_template "charts/deploy")
14+
15+
namespaces=$(get_rego_namespaces "ocp\.deprecated\.*")
16+
cmd="conftest test ${tmp} --output tap ${namespaces}"
17+
run ${cmd}
18+
19+
print_info "${status}" "${output}" "${cmd}" "${tmp}"
20+
[ "$status" -eq 0 ]
21+
}
22+
23+
@test "charts/exporter" {
24+
tmp=$(helm_template "charts/exporter")
25+
26+
namespaces=$(get_rego_namespaces "ocp\.deprecated\.*")
27+
cmd="conftest test ${tmp} --output tap ${namespaces}"
28+
run ${cmd}
29+
30+
print_info "${status}" "${output}" "${cmd}" "${tmp}"
31+
[ "$status" -eq 0 ]
32+
}
33+
34+
@test "storage/minio-scc.yaml" {
35+
tmp=$(split_files "storage/minio-scc.yaml")
36+
37+
namespaces=$(get_rego_namespaces "ocp\.deprecated\.*")
38+
cmd="conftest test ${tmp} --output tap ${namespaces}"
39+
run ${cmd}
40+
41+
print_info "${status}" "${output}" "${cmd}" "${tmp}"
42+
[ "$status" -eq 0 ]
43+
}

0 commit comments

Comments
 (0)