import unittest from pathlib import Path from tempfile import TemporaryDirectory from unittest.mock import call, patch import control class ReleaseTest(unittest.TestCase): def test_forecast_mount_exists_before_switching_a_readonly_website(self): entry = next(service for service in control.SERVICES if service[1] == 'website-ukmesh') with TemporaryDirectory() as tmp, patch.object(control, 'ARTIFACTS', Path(tmp)): release = Path(tmp) / 'release' site = release / 'website' site.mkdir(parents=True) (site / 'index.html').write_text('') digest = control.fingerprint(site) mount = site / 'tropo' / 'data' def stop_before_compose(*args, **kwargs): self.assertTrue(mount.is_dir()) raise RuntimeError('checked mount') with patch.object(control.subprocess, 'run', side_effect=stop_before_compose): for _ in range(2): with self.assertRaisesRegex(RuntimeError, 'checked mount'): control.switch('release', entry) self.assertEqual(control.fingerprint(site), digest) mount.rmdir() (site / 'tropo').rmdir() (site / 'tropo').symlink_to(Path(tmp)) with self.assertRaisesRegex(ValueError, 'forecast mountpoint'): control.switch('release', entry) self.assertFalse((Path(tmp) / 'data').exists()) @patch.object(control, 'write_json') @patch.object(control, 'switch') @patch.object(control, 'require_originals') @patch.object(control, 'verify') def test_publishes_pointer_after_switching_all_services(self, verify, require_originals, switch, save): control.apply('redesign', 'classic') verify.assert_has_calls([call('redesign'), call('classic')]) require_originals.assert_called_once() self.assertEqual(switch.call_args_list, [call('redesign', entry) for entry in control.SERVICES]) save.assert_called_once_with(control.ACTIVE, {'current': 'redesign', 'previous': 'classic'}) @patch.object(control, 'write_json') @patch.object(control, 'switch') @patch.object(control, 'require_originals') @patch.object(control, 'verify') def test_partial_switch_restores_services_in_reverse_order(self, verify, require_originals, switch, save): first, second = control.SERVICES[:2] switch.side_effect = [None, ValueError('failed health check'), None, None] with self.assertRaisesRegex(RuntimeError, 'prior release restored'): control.apply('redesign', 'classic') self.assertEqual(switch.call_args_list, [ call('redesign', first), call('redesign', second), call('classic', second), call('classic', first), ]) save.assert_not_called() if __name__ == '__main__': unittest.main()