Files
rust-discovery/tests/linux_release_bootstrap.py
T

97 lines
4.6 KiB
Python

"""Retry coverage for the HTTPS Linux bootstrap without installing on the test host."""
from contextlib import ExitStack, redirect_stdout
from importlib.util import module_from_spec, spec_from_file_location
import io
import json
import sqlite3
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.service = self.stack.enter_context(patch.object(lifecycle,'service'))
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 test_unresolved_effects_delay_release_without_blocking_closed_jobs(self):
with sqlite3.connect(self.state / 'journal.sqlite') as db:
db.execute('CREATE TABLE job_attempts (state TEXT)')
for state, expected in [('running','busy'), ('unknown','busy'), ('awaiting_reboot','busy'),
('closed_unknown','idle'), ('accepted','idle'), ('succeeded','idle')]:
with self.subTest(state=state):
db.execute('DELETE FROM job_attempts')
db.execute('INSERT INTO job_attempts VALUES(?)', (state,))
db.commit()
with redirect_stdout(io.StringIO()) as output:
lifecycle.device_idle()
self.assertEqual(output.getvalue().strip(), expected)
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_timer_starts_only_after_confirmed_first_connection(self):
self.bootstrap_tenant(self.tenant)
self.healthy.return_value = False
with self.assertRaisesRegex(ValueError, 'awaiting authenticated device heartbeat'):
lifecycle.install(self.base,self.token)
self.enable.assert_called_once()
self.service.assert_not_called()
self.healthy.return_value = True
lifecycle.install(self.base,self.token)
self.service.assert_called_once_with('enable','--now',lifecycle.TIMER)
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()