my-homelab-configs/tests/jeannie/test_supply_chain_bootstrap.py

46 lines
2.0 KiB
Python

"""Narrow bootstrap admission recovery. Run on the Debian host."""
import copy
import importlib.util
from pathlib import Path
import unittest
SPEC = importlib.util.spec_from_file_location(
"supply_chain_bootstrap", Path(__file__).resolve().parents[2] / "scripts/supply-chain-bootstrap-reconcile.py"
)
MODULE = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(MODULE)
class SupplyChainBootstrapTests(unittest.TestCase):
def webhook(self):
return {"name": "ivpol.mutate.kyverno.svc-fail-finegrained-" + MODULE.POLICY,
"failurePolicy": "Fail", "clientConfig": {"service": {
"namespace": "kyverno", "path": "/ivpol/mutate/" + MODULE.POLICY}}}
def test_only_exact_policy_and_service_are_owned(self):
self.assertTrue(MODULE.owns_webhook(self.webhook()))
for field, value in (("namespace", "another-verifier"), ("path", "/ivpol/mutate/another-policy")):
webhook = self.webhook()
webhook["clientConfig"]["service"][field] = value
self.assertFalse(MODULE.owns_webhook(webhook))
webhook = self.webhook()
webhook["name"] += "-another-policy"
self.assertFalse(MODULE.owns_webhook(webhook))
def test_existing_scope_is_preserved_and_reconciliation_is_idempotent(self):
selector = {"matchLabels": {"team": "apps"}, "matchExpressions": [
{"key": "environment", "operator": "In", "values": ["homelab"]}]}
original = copy.deepcopy(selector)
desired = MODULE.selector_with_exclusion(selector)
self.assertEqual(selector, original)
self.assertEqual(desired["matchLabels"], selector["matchLabels"])
self.assertEqual(desired["matchExpressions"][0], selector["matchExpressions"][0])
self.assertEqual(desired["matchExpressions"][-1]["values"], ["container-registry", "kyverno"])
self.assertEqual(MODULE.selector_with_exclusion(desired), desired)
if __name__ == "__main__":
unittest.main()