Wait for the device certificate before probing Linux enrollment

This commit is contained in:
2026-11-18 18:09:52 +01:00
parent bfdc3da8af
commit 4b3d0136e9
2 changed files with 40 additions and 0 deletions
+5
View File
@@ -194,7 +194,12 @@ def installed():
def healthy(version, baseline, duration=90, pin=None): def healthy(version, baseline, duration=90, pin=None):
until = time.monotonic() + duration until = time.monotonic() + duration
started = time.time() started = time.time()
certificate_pointer = STATE / 'certificates/current.json'
while time.monotonic() < until: while time.monotonic() < until:
# The agent writes this pointer after obtaining its first device certificate.
if not certificate_pointer.is_file():
time.sleep(2)
continue
try: try:
record = installed() record = installed()
contact = record.get('last_contact_at') contact = record.get('last_contact_at')
+35
View File
@@ -1,5 +1,6 @@
"""Retry coverage for the HTTPS Linux bootstrap without installing on the test host.""" """Retry coverage for the HTTPS Linux bootstrap without installing on the test host."""
from contextlib import ExitStack, redirect_stdout from contextlib import ExitStack, redirect_stdout
from datetime import datetime, timezone
from importlib.util import module_from_spec, spec_from_file_location from importlib.util import module_from_spec, spec_from_file_location
import io import io
import json import json
@@ -17,6 +18,40 @@ lifecycle = module_from_spec(spec)
spec.loader.exec_module(lifecycle) spec.loader.exec_module(lifecycle)
class EnrollmentHeartbeat(unittest.TestCase):
def test_health_probe_waits_for_the_first_certificate_before_fetching(self):
with tempfile.TemporaryDirectory() as folder:
state = Path(folder)
certificate = state / 'certificates/current.json'
certificate.parent.mkdir()
now = datetime(2026, 11, 18, 17, tzinfo=timezone.utc)
record = {'last_contact_at': now.isoformat(),
'agent_version': '0.1.0-arm64.1', 'connection_generation': 1}
def finish_enrollment(_seconds):
probe.assert_not_called()
certificate.touch()
with patch.object(lifecycle, 'STATE', state), \
patch.object(lifecycle.time, 'time', return_value=now.timestamp()), \
patch.object(lifecycle.time, 'monotonic', side_effect=[0, 0, 2]), \
patch.object(lifecycle.time, 'sleep', side_effect=finish_enrollment) as wait, \
patch.object(lifecycle, 'installed', return_value=record) as probe, \
patch.object(lifecycle.subprocess, 'run', return_value=SimpleNamespace(returncode=0)):
self.assertTrue(lifecycle.healthy('0.1.0-arm64.1', 0))
wait.assert_called_once_with(2)
probe.assert_called_once_with()
def test_missing_certificate_still_times_out(self):
with tempfile.TemporaryDirectory() as folder, \
patch.object(lifecycle, 'STATE', Path(folder)), \
patch.object(lifecycle.time, 'monotonic', side_effect=[0, 0, 2]), \
patch.object(lifecycle.time, 'sleep'), \
patch.object(lifecycle, 'installed', return_value={}) as probe:
self.assertFalse(lifecycle.healthy('0.1.0-arm64.1', 0, duration=1))
probe.assert_not_called()
class EnrollmentRetry(unittest.TestCase): class EnrollmentRetry(unittest.TestCase):
def setUp(self): def setUp(self):
self.stack = ExitStack() self.stack = ExitStack()