the-custodian/docs/changes/2026-11-01-scanner-json-digest/native-r4/test_controller.py

40 lines
1.9 KiB
Python

"""Existing immutable ConfigMap and unchanged source preimages; no cluster access."""
from pathlib import Path
import copy
import importlib.util
import json
import unittest
HERE=Path(__file__).resolve().parent
s=importlib.util.spec_from_file_location('r4',HERE/'rollout.py');r=importlib.util.module_from_spec(s);s.loader.exec_module(r)
m=r.support();C=json.loads((r.ORIGINAL/'inputs.json').read_bytes())
class Tests(unittest.TestCase):
def runner(self,cm):
class Fake(r.make_runner(m)):
def k(self,*args,**kw):
self.last_command=args
return json.dumps(cm).encode()
return Fake(C)
def cm(self):
cm=json.loads((r.ORIGINAL/'configmap.json').read_bytes());cm['metadata']['uid']='known-public-cm-uid';return cm
def test_identical_existing_immutable_cm_is_reused_with_uid_pin(self):
cm=self.cm();runner=self.runner(cm);runner.existing_cm();runner.existing_cm()
self.assertEqual(runner.cm_uid,'known-public-cm-uid')
self.assertIn('get',runner.last_command);self.assertNotIn('create',runner.last_command)
cm['metadata']['uid']='recreated'
with self.assertRaisesRegex(RuntimeError,'identity-drift'):runner.existing_cm()
def test_mutable_or_changed_cm_data_refused(self):
for key in ('mutable','changed-data','extra-data'):
cm=self.cm()
if key=='mutable':cm['immutable']=False
elif key=='changed-data':cm['data']['hook']='changed'
else:cm['data']['unexpected']='public-sentinel'
with self.subTest(key=key),self.assertRaisesRegex(RuntimeError,'configmap-drift'):self.runner(cm).existing_cm()
def test_same_six_source_candidates_still_match(self):
s=importlib.util.spec_from_file_location('follow',HERE/'source_followthrough.py');f=importlib.util.module_from_spec(s);s.loader.exec_module(f)
self.assertEqual(len(f.candidates()),6)
if __name__=='__main__':unittest.main()