"""Bound HOST-before/after logic; no native package, namespace or reboot calls.""" import copy from pathlib import Path import tempfile from unittest import TestCase from unittest.mock import patch from test_guard import apt import test_native_acceptance as fixture class AmbientRebootTests(TestCase): def scenario(self, before, after, private=False, missing=False, changed_boot=False): ctx=fixture.context() with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp: base=Path(temp);active=fixture.CompletionTests().setup_files(base,ctx) record=fixture.host_before(ctx,before) (active/'host-reboot-before.json').write_bytes(apt.canonical(record)) (active/'native-prechecks.json').write_bytes(apt.canonical(fixture.prechecks(ctx,record))) (active/'sandbox-result.json').write_bytes(apt.canonical({'transaction_sha256':ctx.transaction_sha256,'apt_exit_code':0,'reboot_required':private})) if missing:(active/'host-reboot-before.json').unlink() original_exists=Path.exists with patch.object(apt,'BASE',base),patch.object(apt,'ACTIVE',active),patch.object(apt,'secure_path'), \ patch.object(apt,'sealed_read',side_effect=lambda path,*a,**k:path.read_bytes()), \ patch.object(apt,'runtime_identity',return_value={}), \ patch.object(Path,'exists',lambda path:after if str(path)=='/run/reboot-required' else original_exists(path)), \ patch.object(Path,'read_text',return_value='other-boot' if changed_boot else ctx.boot_id), \ patch.object(apt.subprocess,'Popen',side_effect=AssertionError('No effects in acceptance publication')): obj=fixture.fixture_transaction();tx=apt.ResolvedAptTransaction.parse(obj) evidence={'reboot_required':after} if before!=after or private or missing or changed_boot: with self.assertRaises((apt.Veto,OSError)): apt.complete_acceptance(tx,ctx,fixture.action_manifest(ctx,obj,{}),{},evidence) self.assertFalse(apt.record_path(tx).exists()) else: apt.complete_acceptance(tx,ctx,fixture.action_manifest(ctx,obj,{}),{},evidence) self.assertTrue(apt.record_path(tx).exists()) self.assertIs(evidence['reboot_required'],before) self.assertIs(evidence['host_reboot_required_before'],before) self.assertIs(evidence['host_reboot_required_after'],after) self.assertFalse(evidence['reboot_required_introduced']) self.assertEqual(evidence['host_reboot_before_sha256'],apt.digest(apt.canonical(record))) proof=apt.json_bytes(apt.record_path(tx).read_bytes()) self.assertEqual(proof['schema_version'],3) self.assertEqual(proof['native_prechecks_schema_version'],2) apt.validate_acceptance_record(proof,apt.fixture_scope(tx),{}) for key,value in (('schema_version',2),('native_prechecks_schema_version',1), ('host_reboot_required_after',not before),('host_reboot_required_before',int(before)), ('reboot_required_introduced',True)): bad=copy.deepcopy(proof);bad[key]=value with self.subTest(field=key),self.assertRaises(apt.Veto):apt.validate_acceptance_record(bad,apt.fixture_scope(tx),{}) def test_ambient_false_false_and_true_true_are_honest_success(self): for pending in (False,True): with self.subTest(pending=pending):self.scenario(pending,pending) def test_host_changes_private_reboot_missing_before_and_boot_change_never_accept(self): for before,after in ((False,True),(True,False)): with self.subTest(before=before,after=after):self.scenario(before,after) self.scenario(True,True,private=True) self.scenario(False,False,missing=True) self.scenario(False,False,changed_boot=True) def test_parent_capture_is_once_and_child_prechecks_only_bind_that_sample(self): ctx=fixture.context() with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp: active=Path(temp) with patch.object(apt,'ACTIVE',active),patch.object(apt,'secure_path'), \ patch.object(apt,'sealed_read',side_effect=lambda path,*a,**k:path.read_bytes()), \ patch.object(apt.VerifiedHelperContext,'check_deadline'),patch.object(apt.time,'time_ns',return_value=2000000000), \ patch.object(Path,'exists',return_value=True): apt.capture_host_reboot_before(ctx) before,sha=apt.load_host_reboot_before(ctx) self.assertTrue(before['host_reboot_required']) with self.assertRaisesRegex(apt.Veto,'ALREADY_HANDED_OFF'):apt.capture_host_reboot_before(ctx) with patch.object(Path,'exists',return_value=False) as child_private_flag, \ patch.object(apt,'sandbox_checks',return_value=fixture.prechecks(ctx)['checks']): checks=apt.native_prechecks(ctx) self.assertTrue(checks['host_reboot_required_before']) self.assertEqual(checks['host_reboot_before_sha256'],sha) child_private_flag.assert_not_called()