Document how to mock Gatekeeper external_data in OPA tests#4563
Document how to mock Gatekeeper external_data in OPA tests#4563JaydipGabani with Copilot wants to merge 3 commits into
Conversation
Agent-Logs-Url: https://github.com/open-policy-agent/gatekeeper/sessions/31654b2c-2584-49bd-95ca-e274a28cf6ea Co-authored-by: JaydipGabani <20255485+JaydipGabani@users.noreply.github.com>
JaydipGabani
left a comment
There was a problem hiding this comment.
@copilot address changes
| ```rego | ||
| package k8sexternaldata | ||
|
|
||
| mock_external_data(request) = response { |
There was a problem hiding this comment.
instead of policy code including mock function it should belong in test file -
something like this -
test_input_not_allow {
input := xxx
results := violation with input as input
count(results) == 1
}
external_data(object) = out {
array_contains(object.keys, "system_error_image")
out = {
"status_code": 504,
"system_error": "provider not responsing",
}
}
external_data(object) = out {
not array_contains(object.keys, "system_error_image")
out = {
"errors": {x| x:=object.keys[i];x=="error_image"},
"responses": {x | x:=verifier_report(object.keys[i]); object.keys[i] != "error_image"},
"status_code": 200,
"system_error": ""
}
}
thorogly verify this and then update all docs accordingly
There was a problem hiding this comment.
Updated the docs to show external_data(request) = response defined in the *_test.rego file instead of embedding a separate mock function in policy code. Verified the example with opa test --v0-compatible, rebuilt the website, and confirmed the rendered docs. Addressed in 4d56b19. Screenshot: https://github.com/user-attachments/assets/70756e8a-5f15-44c3-a7d1-4a2180a0f6f1
Agent-Logs-Url: https://github.com/open-policy-agent/gatekeeper/sessions/983a1687-b163-4bd7-a899-b7d32adf2775 Co-authored-by: JaydipGabani <20255485+JaydipGabani@users.noreply.github.com>
Addressed the review feedback in 4d56b19 by moving the primary mock example into the test-file pattern and validating it with OPA plus the website build. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Users testing Rego policies with
external_datacan define a test-onlyexternal_datafunction in their*_test.regofiles. The external data docs now describe this pattern and show how to return mocked provider responses without adding mock code to the deployed policy.External data testing guidance
external_data” section.external_datafunction in the same package as the policy under test.Mocking example
external_data(request) = responserules in the test file.with+ capabilities-file approach as an alternative.v3.22.xandv3.21.x.