Skip to content

Document how to mock Gatekeeper external_data in OPA tests#4563

Draft
JaydipGabani with Copilot wants to merge 3 commits into
masterfrom
copilot/update-docs-mock-external-data
Draft

Document how to mock Gatekeeper external_data in OPA tests#4563
JaydipGabani with Copilot wants to merge 3 commits into
masterfrom
copilot/update-docs-mock-external-data

Conversation

Copilot AI commented May 14, 2026

Copy link
Copy Markdown
Contributor

Users testing Rego policies with external_data can define a test-only external_data function in their *_test.rego files. 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

    • Added a “Testing policies that use external_data” section.
    • Explains defining a test-only external_data function in the same package as the policy under test.
    • Notes that OPA compiles policy and test files together, so the mock is used only during tests and is not included in the deployed ConstraintTemplate.
  • Mocking example

    • Demonstrates putting external_data(request) = response rules in the test file.
    • Shows mocked provider responses for normal provider errors and system errors.
    • Mentions the with + capabilities-file approach as an alternative.
external_data(request) = response {
  request.provider == "dummy-provider"
  request.keys != ["system_error_image"]
  response := {
    "errors": [[key, "not allowed"] | key := request.keys[_]; key == "error_image"],
    "responses": [[key, "allowed"] | key := request.keys[_]; key != "error_image"],
    "status_code": 200,
    "system_error": "",
  }
}
  • Docs coverage
    • Updated current docs and the last two versioned docs: v3.22.x and v3.21.x.

Copilot AI self-assigned this May 14, 2026
Copilot AI review requested due to automatic review settings May 14, 2026 17:18
Copilot AI removed the request for review from Copilot May 14, 2026 17:18
Copilot AI linked an issue May 14, 2026 that may be closed by this pull request
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>
Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 17:27
Copilot AI changed the title [WIP] Update documentation to include guidance on mocking external data Document how to mock Gatekeeper external_data in OPA tests May 14, 2026
Copilot AI requested a review from JaydipGabani May 14, 2026 17:28

@JaydipGabani JaydipGabani left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot address changes

Comment thread website/docs/externaldata.md Outdated
```rego
package k8sexternaldata

mock_external_data(request) = response {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copilot AI requested review from Copilot and removed request for Copilot May 16, 2026 02:38

Copilot AI commented May 16, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address changes

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:

  • accounts.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --use-angle=swiftshader-webgl --crashpad-handler-pid=8229 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/org.chromium.Chromium.scoped_dir.oklJDb --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,18281695867956433279,9597895196197050962,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,10508175649249628418,3271248600914034562,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /usr/bin/chromium-browser chromium-browser --headless=new --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --window-size=1280,900 --screenshot=/tmp/externaldata-test-file-guidance.png REDACTED (dns block)
  • clients2.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --use-angle=swiftshader-webgl --crashpad-handler-pid=8229 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/org.chromium.Chromium.scoped_dir.oklJDb --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,18281695867956433279,9597895196197050962,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,10508175649249628418,3271248600914034562,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /usr/bin/chromium-browser chromium-browser --headless=new --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --window-size=1280,900 --screenshot=/tmp/externaldata-test-file-guidance.png REDACTED (dns block)
  • docs.google.com
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • firefox.settings.services.mozilla.com
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • kubernetes.io
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • location.services.mozilla.com
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • open-policy-agent.github.io
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • pt2ix43zfm-dsn.algolia.net
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --use-angle=swiftshader-webgl --crashpad-handler-pid=8229 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/org.chromium.Chromium.scoped_dir.oklJDb --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,18281695867956433279,9597895196197050962,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,10508175649249628418,3271248600914034562,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /usr/bin/chromium-browser chromium-browser --headless=new --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --window-size=1280,900 --screenshot=/tmp/externaldata-test-file-guidance.png REDACTED (dns block)
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • redirector.gvt1.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --use-angle=swiftshader-webgl --crashpad-handler-pid=8229 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/org.chromium.Chromium.scoped_dir.oklJDb --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,18281695867956433279,9597895196197050962,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,10508175649249628418,3271248600914034562,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /usr/bin/chromium-browser chromium-browser --headless=new --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --window-size=1280,900 --screenshot=/tmp/externaldata-test-file-guidance.png REDACTED (dns block)
  • slack.openpolicyagent.org
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • www.google-analytics.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --use-angle=swiftshader-webgl --crashpad-handler-pid=8229 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/org.chromium.Chromium.scoped_dir.oklJDb --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,18281695867956433279,9597895196197050962,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,10508175649249628418,3271248600914034562,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /usr/bin/chromium-browser chromium-browser --headless=new --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --window-size=1280,900 --screenshot=/tmp/externaldata-test-file-guidance.png REDACTED (dns block)
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • www.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --use-angle=swiftshader-webgl --crashpad-handler-pid=8229 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/org.chromium.Chromium.scoped_dir.oklJDb --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,18281695867956433279,9597895196197050962,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,10508175649249628418,3271248600914034562,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /usr/bin/chromium-browser chromium-browser --headless=new --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --window-size=1280,900 --screenshot=/tmp/externaldata-test-file-guidance.png REDACTED (dns block)
  • www.googletagmanager.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --use-angle=swiftshader-webgl --crashpad-handler-pid=8229 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/org.chromium.Chromium.scoped_dir.oklJDb --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,18281695867956433279,9597895196197050962,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,10508175649249628418,3271248600914034562,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /usr/bin/chromium-browser chromium-browser --headless=new --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --window-size=1280,900 --screenshot=/tmp/externaldata-test-file-guidance.png REDACTED (dns block)
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)
  • www.openpolicyagent.org
    • Triggering command: /usr/lib/firefox/firefox /usr/lib/firefox/firefox --headless --screenshot /tmp/externaldata-firefox.png --window-size 1280,900 REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from JaydipGabani May 16, 2026 02:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to mock external_data

2 participants