Files
rust-discovery/deploy/windows/test_system_pipe_upgrade.py
2026-11-14 14:16:56 +01:00

71 lines
4.5 KiB
Python

"""Four local orchestration guards; no credentials/VM/Windows API calls."""
import contextlib
import copy
import io
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import Mock, patch
import system_pipe_upgrade as p
def settings():
return dict(schema_version=1, binding={'public_fixture': True}, agent=dict(binary_sha256=p.OLD_AGENT), helper=dict(binary_sha256=p.OLD_HELPER), allowed_services=[], max_runtime_seconds=30, enable_effects=False, allow_reboot=False)
class PipeUpgradeGuards(unittest.TestCase):
def test_closed_reciprocal_settings_only_change_two_image_pins(self):
original = settings(); preserved = copy.deepcopy(original)
raw, pin, policy = p.passive_settings(original, 'a' * 64, 'b' * 64)
changed = json.loads(raw); changed['agent']['binary_sha256'] = p.OLD_AGENT; changed['helper']['binary_sha256'] = p.OLD_HELPER
self.assertEqual(changed, preserved); self.assertEqual(original, preserved)
self.assertEqual(p.raw_sha(raw), policy); self.assertEqual(json.loads(pin)['policy_sha256'], policy)
for field, value in [('enable_effects', True), ('allow_reboot', True), ('allowed_services', ['foreign']), ('max_runtime_seconds', 120), ('schema_version', True)]:
bad = settings(); bad[field] = value
with self.subTest(field=field), self.assertRaises(RuntimeError): p.passive_settings(bad, 'a' * 64, 'b' * 64)
def test_historical_mutation_helpers_excluded_and_explicit_backups_ordered(self):
p.guards()
self.assertNotIn('function PinProcess', p.PREFIX)
self.assertNotIn('[IO.File]::Replace($candidate,$destination,$null)', p.SCRIPT)
self.assertIn('[RmmPipeNative]::Open([int]$before.service.pid)', p.SCRIPT)
self.assertNotIn('$expected=5140', p.SCRIPT)
self.assertIn("'helper-already-stopped.json'", p.SCRIPT)
self.assertIn('stop_called=$false;start_called=$false', p.SCRIPT)
self.assertIn("@('config','ca','identity','certificate','private_key')", p.SCRIPT)
self.assertEqual(p.SCRIPT.count('PreserveImmutableFiles '), 5)
readback = p.PHASES.split("if($p.action-ceq'readback'){", 1)[1].split("if($p.action-ceq'finish'){", 1)[0]
self.assertIn("(ReadbackService 'agent')", readback)
self.assertNotIn('(Service ', readback)
self.assertIn('process_identity_verified=$false', p.READBACK_SERVICE)
self.assertIn('pack.verified(manifest_path)', p.VERIFY_PACK)
self.assertIn("names==report['names']==pack.TESTS[kind]", p.VERIFY_PACK)
with patch.object(p.subprocess, 'run', return_value=Mock(returncode=0, stdout=b'{"type":"rmm.windows.system-delegation-build"}')):
with self.assertRaises(RuntimeError): p.qualified(Path('unused-fixture'), Path('unused-proof'))
def test_lost_enter_reply_cannot_repeat_target_mutation(self):
with tempfile.TemporaryDirectory() as temporary:
folder = Path(temporary); p.write_bytes(folder / 'baseline.json', b'{}')
digest = p.sha(folder / 'baseline.json'); indices = {'unit_fixture': True}
intent = dict(payload=dict(baseline_sha256=digest), original_indices=indices)
p.save(folder / 'server-before.json', {'unit_counts': 0})
access = Mock(); access.server_state.return_value = {'unit_counts': 0}
with patch.object(p, 'load', return_value=(folder, intent)), patch.object(p, 'prior', return_value=(indices, {}, {})), patch.object(p, 'remote', side_effect=TimeoutError('unit fixture lost reply')) as remote:
with self.assertRaises(TimeoutError): p.enter(access, 'fixture', digest)
self.assertTrue((folder / 'enter-attempt.json').exists())
with self.assertRaises(FileExistsError): p.enter(access, 'fixture', digest)
self.assertEqual(remote.call_count, 1)
def test_readback_never_queries_server_starts_or_publishes_main_indices(self):
with tempfile.TemporaryDirectory() as temporary:
access = Mock(); access.server_state.side_effect = AssertionError('server unavailable in readback')
with patch.object(p, 'load', return_value=(Path(temporary), {})), patch.object(p, 'remote', return_value=dict(read_only=True, id='fixture', phase_receipts={})) as remote, contextlib.redirect_stdout(io.StringIO()):
p.finish(access, 'fixture', read_only=True)
remote.assert_called_once_with(access, {}, 'readback')
self.assertFalse(access.server_state.called)
if __name__ == '__main__': unittest.main()