import hashlib from pathlib import Path import tempfile import unittest from unittest import mock from deployment_queue import DeploymentQueue, QueueError from controller import Controller from test_gitea_source import run,A,B,C class ControllerTests(unittest.TestCase): def setUp(self): self.tmp=tempfile.TemporaryDirectory();self.addCleanup(self.tmp.cleanup) self.path=Path(self.tmp.name)/'queue.db' self.q=DeploymentQueue(self.path,first_run_number=8);self.addCleanup(self.q.close) self.s=mock.Mock();self.i=mock.Mock() self.i.installed_source.return_value=A self.r=run();self.s.discover.return_value=[self.r];self.s.successful_run.return_value=self.r self.data=b'archive';self.artifact='nodedc-device-core-release-v1-r8-'+B+'.tgz' self.receipt={'artifact':self.artifact,'sha256':hashlib.sha256(self.data).hexdigest()} self.s.artifact.return_value=(self.receipt,self.data) self.i.receipt.return_value=('deployed','canonical:line1') self.c=Controller(self.q,self.s,self.i) def test_success_is_reserved_before_apply_and_never_repeated(self): def apply(a):self.assertEqual(self.q.snapshot()[0]['phase'],'deploying') self.i.apply.side_effect=apply self.assertEqual(self.c.tick()['status'],'deployed') self.assertEqual(self.c.tick()['status'],'waiting') self.i.apply.assert_called_once_with(self.artifact) def test_earlier_pending_blocks_newer_ready_but_failed_ci_does_not(self): pending=run();pending.update(status='in_progress',conclusion=None) self.s.discover.return_value=[run(9,C),pending] self.assertEqual(self.c.tick()['status'],'waiting');self.i.apply.assert_not_called() pending.update(status='completed',conclusion='failure');self.c.observe() self.assertEqual(self.q.next_ready()['id'],9) def test_failure_with_clean_rollback_is_terminal(self): self.i.apply.side_effect=RuntimeError('failure');self.i.receipt.return_value=('rolled-back','failed:line1') with self.assertRaises(RuntimeError):self.c.tick() self.assertEqual(self.q.snapshot()[0]['phase'],'failed') self.assertEqual(self.c.tick()['status'],'waiting');self.assertEqual(self.i.apply.call_count,1) def test_missing_receipt_blocks_restart(self): self.i.apply.side_effect=RuntimeError('crash');self.i.receipt.return_value=('ambiguous','missing-receipt') with self.assertRaises(RuntimeError):self.c.tick() with self.assertRaises(QueueError):self.c.tick() self.assertEqual(self.i.apply.call_count,1) def test_restart_reconciles_success_without_second_apply(self): self.c.observe();self.q.reserve(8,digest=self.receipt['sha256'],artifact=self.artifact) self.assertEqual(self.c.tick()['status'],'waiting') self.i.apply.assert_not_called();self.assertEqual(self.q.snapshot()[0]['phase'],'deployed') def test_network_or_plan_error_before_reservation_preserves_ready_queue(self): self.i.plan.side_effect=RuntimeError('network') with self.assertRaises(RuntimeError):self.c.tick() self.assertEqual(self.q.snapshot()[0]['phase'],'ready');self.i.apply.assert_not_called() def test_new_rerun_before_reservation_prevents_apply(self): pending=run();pending.update(run_attempt=2,status='in_progress',conclusion=None) self.s.discover.side_effect=[[run()],[pending]] with self.assertRaises(QueueError):self.c.tick() self.i.apply.assert_not_called() def test_disappeared_pending_run_never_skipped(self): pending=run();pending.update(status='in_progress',conclusion=None) self.s.discover.return_value=[pending];self.c.observe() self.s.discover.return_value=[run(9,C)] with self.assertRaisesRegex(QueueError,'disappeared'):self.c.tick() self.i.apply.assert_not_called() if __name__=='__main__':unittest.main()