Rego is the declarative policy language used by Open Policy Agent (OPA). This tile covers writing and testing Rego policies for Kubernetes admission control, Terraform and infrastructure-as-code plan validation, Docker container authorization, HTTP API authorization, RBAC and role-based access control, data filtering, metadata annotations with opa inspect, and OPA policy testing with opa test.
99
Quality
Pending
Does it follow best practices?
Impact
99%
1.19xAverage score across 31 eval scenarios
Pending
The risk profile of this skill
This document covers Regal rules related to how tests are written and organized.
Relevant Regal rules:
file-missing-test-suffix — test files must have a _test.rego filename suffixtest-outside-test-package — test rules must live in packages ending in _testidentically-named-tests — every test_ rule in a package must have a unique nametodo-test — do not commit todo_test_ prefixed rules; write the test or remove itTest files must use a _test.rego suffix alongside their policy files.
# CORRECT file layout:
policy.rego # the policy
policy_test.rego # the tests
# WRONG — not recognized as a test file:
policy.test.rego # (file-missing-test-suffix)
test_policy.rego # (file-missing-test-suffix)Test packages must mirror the policy package with a _test suffix.
# policy.rego
package myapp.authz
import rego.v1
# policy_test.rego — CORRECT
package myapp.authz_test
import rego.v1
import data.myapp.authz # import the policy to test it# WRONG: test rules inside the policy package (test-outside-test-package)
package myapp.authz
test_allow if { ... } # violation — tests must be in *_test packagesBecause the test package is different from the policy package, import the policy and use its alias.
package myapp.authz_test
import rego.v1
import data.myapp.authz # gives the alias "authz"
test_admin_allowed if {
authz.allow with input as {"role": "admin"}
}
test_viewer_denied if {
not authz.allow with input as {"role": "viewer"}
}Every test_ rule in a package must have a unique name.
# WRONG: duplicate name (identically-named-tests violation)
test_allow if { authz.allow with input as {"role": "admin"} }
test_allow if { authz.allow with input as {"role": "superuser"} } # duplicate!
# CORRECT: descriptive unique names
test_allow_admin if { authz.allow with input as {"role": "admin"} }
test_allow_superuser if { authz.allow with input as {"role": "superuser"} }Do not leave placeholder todo_test_ rules in committed code. Either write the test or remove it.
# WRONG: todo test stub (todo-test violation)
todo_test_rate_limiting if { true }
# CORRECT: write the actual test
test_rate_limit_premium if {
limits.max_requests == 1000
with input as {"user": "alice"}
with data.tiers as {"alice": "premium"}
}authz.rego:
package api.authz
import rego.v1
default allow := false
allow if input.role == "admin"
allow if {
input.role == "viewer"
input.method == "GET"
}authz_test.rego:
package api.authz_test
import rego.v1
import data.api.authz
test_allow_admin if {
authz.allow with input as {"role": "admin", "method": "POST"}
}
test_allow_viewer_get if {
authz.allow with input as {"role": "viewer", "method": "GET"}
}
test_deny_viewer_post if {
not authz.allow with input as {"role": "viewer", "method": "POST"}
}
test_deny_unknown_role if {
not authz.allow with input as {"role": "guest", "method": "GET"}
}Run with: opa test . -v
docs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10
scenario-11
scenario-12
scenario-13
scenario-14
scenario-15
scenario-16
scenario-17
scenario-18
scenario-19
scenario-20
scenario-21
scenario-22
scenario-23
scenario-24
scenario-25
scenario-26
scenario-27
scenario-28
scenario-29
scenario-30
scenario-31