Testing OPA Policies
π Introduction
π§ͺ OPA Test Basics
package k8snolatesttag_test
import rego.v1
import data.k8snolatesttag
# --- Test data ---
pod_with_latest := {
"review": {
"object": {
"spec": {
"containers": [{"name": "app", "image": "nginx:latest"}]
}
}
}
}
pod_with_pinned := {
"review": {
"object": {
"spec": {
"containers": [{"name": "app", "image": "nginx:1.25.3"}]
}
}
}
}
# --- Tests ---
test_deny_latest_tag if {
count(k8snolatesttag.violation) > 0 with input as pod_with_latest
}
test_allow_pinned_tag if {
count(k8snolatesttag.violation) == 0 with input as pod_with_pinned
}π Running Tests
ποΈ Test Structure Conventions
π Writing Comprehensive Tests
π€ Testing with with for Data
with for Dataπ Code Coverage
π§° Conftest: Policy Testing for YAML and JSON Files
Directory Structure for Conftest
Writing Conftest-compatible Policies
Running Conftest
π Integration with opa test in a Makefile
opa test in a Makefileπ§ What's Next
π References
Last updated