feat(deploy): add the recurring Device Core release domain and durable queue policy

This commit is contained in:
Codex
2026-09-29 18:41:21 +03:00
parent d87a0c48b4
commit d161d7fb91
5 changed files with 941 additions and 1 deletions
@@ -0,0 +1,138 @@
import tempfile
from pathlib import Path
import unittest
from deployment_queue import DeploymentQueue, QueueError, REPOSITORY, REPOSITORY_ID
def run(number, *, status="completed", conclusion="success", **updates):
return {"id": number + 100, "run_number": number, "run_attempt": 1,
"head_sha": format(number, "040x"), "event": "push", "head_branch": "main",
"path": "ci.yml@refs/heads/main", "status": status, "conclusion": conclusion, **updates}
class QueueTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.path = Path(self.temp.name) / "queue.db"
self.q = DeploymentQueue(self.path, first_run_number=1)
self.addCleanup(lambda: self.q.close())
def observe(self, *values):
self.q.observe(list(values), repository=REPOSITORY, repository_id=REPOSITORY_ID)
def reserve(self, number):
r = self.q.next_ready()
self.assertEqual(r["number"], number)
return self.q.reserve(r["id"], digest="a"*64,
artifact="nodedc-device-core-release-v1-r"+str(r["id"])+"-"+r["sha"]+".tgz")
def finish(self, row, outcome="deployed"):
self.q.reconcile(row["id"], digest=row["digest"], artifact=row["artifact"],
outcome=outcome, receipt="canonical-test-receipt-"+str(row["id"]))
def test_overlapping_pushes_wait_for_older_qualification(self):
self.observe(run(2), run(1, status="in_progress", conclusion=None))
self.assertIsNone(self.q.next_ready())
self.observe(run(1))
self.finish(self.reserve(1))
self.finish(self.reserve(2))
self.assertIsNone(self.q.next_ready())
def test_failed_tests_do_not_deploy_and_next_push_can_proceed(self):
self.observe(run(2), run(1, conclusion="failure"))
self.finish(self.reserve(2))
self.assertEqual(self.q.snapshot()[0]["phase"], "unqualified")
def test_duplicate_observations_and_receipts_are_idempotent(self):
self.observe(run(1), run(1))
row = self.reserve(1)
self.finish(row)
self.finish(row)
self.observe(run(1))
self.assertEqual(len(self.q.snapshot()), 1)
self.assertIsNone(self.q.next_ready())
def test_wrong_branch_event_and_workflow_never_deploy(self):
self.observe(run(1, head_branch="feature"), run(2, event="workflow_dispatch"), run(3, path="other.yml@refs/heads/main"))
self.assertIsNone(self.q.next_ready())
self.assertEqual({r["phase"] for r in self.q.snapshot()}, {"ignored"})
def test_wrong_repository_and_malformed_sha_rejected(self):
for repo, identity in [("other/Device", 38), (REPOSITORY, 39), (REPOSITORY, 38.0)]:
with self.assertRaises(QueueError): self.q.observe([run(1)], repository=repo, repository_id=identity)
with self.assertRaises(QueueError): self.observe(run(1, head_sha="main; execute"))
self.assertEqual(self.q.snapshot(), [])
def test_identity_conflict_rolls_back_entire_observation(self):
self.observe(run(1))
with self.assertRaises(QueueError): self.observe(run(2), run(1, head_sha="b"*40))
self.assertEqual(len(self.q.snapshot()), 1)
def test_restart_preserves_pending_and_requires_reconciliation_for_reserved(self):
self.observe(run(1), run(2))
row = self.reserve(1)
self.q.close()
self.q = DeploymentQueue(self.path, first_run_number=1)
with self.assertRaisesRegex(QueueError, "reconciliation"): self.q.next_ready()
self.finish(row)
self.assertEqual(self.q.next_ready()["number"], 2)
def test_ambiguous_or_failed_rollback_halts_following_deploys(self):
self.observe(run(1), run(2))
self.finish(self.reserve(1), "ambiguous")
with self.assertRaisesRegex(QueueError, "reconciliation"): self.q.next_ready()
def test_clean_rollback_never_retries_same_artifact_but_allows_forward_fix(self):
self.observe(run(1), run(2))
self.finish(self.reserve(1), "rolled-back")
self.observe(run(1, run_attempt=2))
self.assertEqual(self.q.next_ready()["number"], 2)
def test_late_old_run_and_same_source_cannot_downgrade_installed_revision(self):
self.observe(run(5))
self.finish(self.reserve(5))
self.observe(run(4), run(6, head_sha=run(5)["head_sha"]))
self.assertIsNone(self.q.next_ready())
self.assertEqual([r["phase"] for r in self.q.snapshot()], ["superseded", "deployed", "superseded"])
def test_second_controller_cannot_reserve_another_deploy(self):
self.observe(run(1), run(2))
second = DeploymentQueue(self.path, first_run_number=1)
self.addCleanup(second.close)
self.reserve(1)
with self.assertRaises(QueueError): second.next_ready()
def test_wrong_receipt_cannot_close_crashed_deployment(self):
self.observe(run(1))
row = self.reserve(1)
with self.assertRaisesRegex(QueueError, "artifact_mismatch"):
self.q.reconcile(row["id"], digest="b"*64, artifact=row["artifact"], outcome="deployed", receipt="wrong")
with self.assertRaises(QueueError): self.q.next_ready()
def test_activation_floor_is_persistent_and_old_history_is_not_deployed(self):
self.q.close()
self.path = Path(self.temp.name) / "second.db"
self.q = DeploymentQueue(self.path, first_run_number=3)
self.observe(run(1), run(2), run(3))
self.assertEqual(self.q.next_ready()["number"], 3)
with self.assertRaisesRegex(QueueError, "policy_changed"):
DeploymentQueue(self.path, first_run_number=1)
def test_terminal_api_outcome_cannot_change_without_a_new_attempt(self):
self.observe(run(1, conclusion="failure"))
with self.assertRaisesRegex(QueueError, "outcome_changed"): self.observe(run(1))
self.observe(run(1, run_attempt=2))
self.assertEqual(self.q.next_ready()["number"], 1)
def test_large_backlog_of_duplicate_sources_is_iterative(self):
self.observe(run(1))
self.finish(self.reserve(1))
self.observe(*(run(n, head_sha=run(1)["head_sha"]) for n in range(2, 1200)))
self.assertIsNone(self.q.next_ready())
self.assertEqual(len(self.q.snapshot()), 1199)
if __name__ == "__main__":
unittest.main()