Files
homelab/k8s-ng/apps/openartifact/tests/test_apply_sso.py
T
ab 01b598a285 Make OpenArtifact policy application extensible
Move publisher manifests into a dedicated policies directory and mount them
through a separate Kustomize-generated ConfigMap. Apply all mounted YAML files
in filename order instead of hard-coding the WinGet manifest and seven objects.
Require a nonempty successful response for each manifest and fail the PostSync
hook on partial or malformed results. Keep SSO secret substitution separate.

Document registration, ordering and explicit revocation. New publishers require
only a policy file and a ConfigMap files entry, without Python or Job changes.

Validation: four Python unit tests passed, including arbitrary object counts,
manifest discovery and error handling. Rendered ConfigMaps and the PostSync Job
with kubectl kustomize; full Helm chart rendering and live application were not
performed.
2026-12-02 03:59:15 +02:00

62 lines
3.1 KiB
Python

import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import Mock
spec = importlib.util.spec_from_file_location("apply_sso", Path(__file__).resolve().parents[1] / "apply-sso.py")
apply = importlib.util.module_from_spec(spec)
spec.loader.exec_module(apply)
class ApplyPolicyTests(unittest.TestCase):
def test_discovers_added_manifests_and_accepts_any_positive_object_count(self):
with tempfile.TemporaryDirectory() as temp:
directory = Path(temp)
(directory / "z-new.yaml").write_text("new policy", encoding="utf-8")
(directory / "a-existing.yaml").write_text("existing policies", encoding="utf-8")
(directory / "ignored.txt").write_text("not a policy", encoding="utf-8")
post = Mock(side_effect=[
json.dumps({"items": [{"name": str(i), "result": "applied"} for i in range(9)]}),
json.dumps({"items": [{"name": "new", "result": "applied"}]}),
])
apply.apply_policies(post, directory)
self.assertEqual([call.args for call in post.call_args_list], [
("admin/declarative/apply", {"manifest": "existing policies"}),
("admin/declarative/apply", {"manifest": "new policy"}),
])
def test_failed_or_malformed_response_stops_later_manifests(self):
for response in ({}, {"items": []}, {"items": {}}, {"items": [None]},
{"items": [{"name": "bad", "result": "failed"}]},
{"items": [{"name": "ok", "result": "applied"}, {"name": "bad", "result": "failed"}]}):
with self.subTest(response=response), tempfile.TemporaryDirectory() as temp:
directory = Path(temp)
(directory / "a.yaml").write_text("first", encoding="utf-8")
(directory / "b.yaml").write_text("second", encoding="utf-8")
post = Mock(return_value=json.dumps(response))
with self.assertRaisesRegex(RuntimeError, "a.yaml"):
apply.apply_policies(post, directory)
post.assert_called_once()
def test_empty_directory_is_an_error(self):
with tempfile.TemporaryDirectory() as temp:
post = Mock()
with self.assertRaisesRegex(RuntimeError, "No policy manifests"):
apply.apply_policies(post, Path(temp))
post.assert_not_called()
def test_sso_secret_options_are_only_sent_for_sso(self):
with tempfile.TemporaryDirectory() as temp:
path = Path(temp) / "sso.yaml"
path.write_text("provider", encoding="utf-8")
post = Mock(return_value=json.dumps({"items": [{"name": "keycloak", "result": "applied"}]}))
apply.apply_manifest(post, path, force_secrets=True, secrets={"OIDC_CLIENT_SECRET": "fixture"})
self.assertEqual(post.call_args.args[1], {"manifest": "provider", "force_secrets": True,
"secrets": {"OIDC_CLIENT_SECRET": "fixture"}})
if __name__ == "__main__":
unittest.main()