gatekeeper icon indicating copy to clipboard operation
gatekeeper copied to clipboard

How to mock external_data

Open NitroCao opened this issue 2 years ago • 6 comments
trafficstars

I write some rules which use external_data keyword and want to write unit tests for them, but have no idea cause the original opa command doesn't support external_data. Is there any solution or workaround for this? Gatekeeper version: 3.11.0

NitroCao avatar Jun 12 '23 08:06 NitroCao

my own workaround is that, in unit test file, I directly wrote the multiple external data source functions that output different results for different object.keys inputs

My example: (my input is a list of container images)

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": ""
	}
}

fseldow avatar Jun 13 '23 00:06 fseldow

We should add this to https://open-policy-agent.github.io/gatekeeper/website/docs/externaldata

sozercan avatar Jun 13 '23 01:06 sozercan

@fseldow Thanks for your solution, it's a good idea!🤩 @sozercan Is it better to close this issue with a PR to avoid missing this task?😃

NitroCao avatar Jun 13 '23 07:06 NitroCao

Similar solution: https://github.com/open-policy-agent/opa/issues/5956

fardin01 avatar Jun 14 '23 09:06 fardin01

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 15 '23 23:08 stale[bot]

not stale

ritazh avatar Aug 16 '23 14:08 ritazh