"""Retry coverage for the HTTPS Linux bootstrap without installing on the test host.""" from contextlib import ExitStack from importlib.util import module_from_spec, spec_from_file_location import json from pathlib import Path import tempfile import unittest from unittest.mock import patch src = Path(__file__).resolve().parents[1] / 'crates/server/src/bootstrap/lifecycle.py' spec = spec_from_file_location('rmm_bootstrap_lifecycle', src) lifecycle = module_from_spec(spec) spec.loader.exec_module(lifecycle) class EnrollmentRetry(unittest.TestCase): def setUp(self): self.stack = ExitStack() self.addCleanup(self.stack.close) folder = Path(self.stack.enter_context(tempfile.TemporaryDirectory())) self.base = 'https://bootstrap.example' self.tenant = '11111111-1111-4111-8111-111111111111' root = folder / 'application' (root / 'current').mkdir(parents=True) (root / 'config.json').write_text(json.dumps({'bootstrap': self.base, 'tenant': self.tenant})) (root / 'current/manifest.json').write_text('{"version":"1.0"}') self.state = folder / 'device' self.state.mkdir() for field, value in [('ROOT',root),('STATE',self.state)]: self.stack.enter_context(patch.object(lifecycle,field,value)) self.enable = self.stack.enter_context(patch.object(lifecycle,'enable')) self.healthy = self.stack.enter_context(patch.object(lifecycle,'healthy',return_value=True)) self.save = self.stack.enter_context(patch.object(lifecycle,'save_token')) self.stack.enter_context(patch.object(lifecycle.pwd,'getpwnam')) self.token = folder / 'retry.token' self.token.write_text('0' * 64) def bootstrap_tenant(self, tenant): def answer(source, route, target): self.assertEqual(source, (self.base,'0' * 64)) target.write_text(json.dumps({'tenant_id': tenant})) return self.stack.enter_context(patch.object(lifecycle,'remote',side_effect=answer)) def test_completed_enrollment_keeps_identity_and_needs_no_token(self): identity = self.state / 'identity.json' identity.write_text('{"agent_id":"original"}') remote = self.bootstrap_tenant(self.tenant) lifecycle.install(self.base,None) self.assertEqual(identity.read_text(), '{"agent_id":"original"}') remote.assert_not_called() self.save.assert_not_called() self.enable.assert_called_once() def test_expired_initial_token_can_be_replaced_in_same_tenant(self): self.bootstrap_tenant(self.tenant) lifecycle.install(self.base,self.token) self.save.assert_called_once() self.healthy.assert_called_once_with('1.0',0) def test_token_for_another_tenant_cannot_replace_initial_token(self): self.bootstrap_tenant('22222222-2222-4222-8222-222222222222') with self.assertRaisesRegex(ValueError,'different enrollment tenant'): lifecycle.install(self.base,self.token) self.save.assert_not_called() self.enable.assert_not_called() if __name__ == '__main__': unittest.main()