web/website/ops/test_shared_design_route.py

46 lines
2.1 KiB
Python

import importlib.util
from pathlib import Path
import subprocess
import tempfile
import unittest
spec = importlib.util.spec_from_file_location('route', Path(__file__).with_name('publish-shared-design-route.py'))
route = importlib.util.module_from_spec(spec)
spec.loader.exec_module(route)
class SharedDesignRouteTests(unittest.TestCase):
def test_preserves_cookbook_routes_and_is_idempotent(self):
other = 'location = /cookbook.html { alias /var/www/old/cookbook.html; }\n'
old = other + 'location = /realm-design.css { alias ' + route.LEGACY + '; default_type text/css; }\n'
new = route.route_content(old)
self.assertTrue(new.startswith(other))
self.assertIn('current/dist/shared-chrome', new)
self.assertIn('try_files $uri @dunerealms_shared_design_legacy;', new)
self.assertEqual(route.route_content(new), new)
def test_refuses_unknown_or_duplicate_alias(self):
for old in ['', 'location = /realm-design.css { alias /unexpected.css; }',
('location = /realm-design.css { alias ' + route.LEGACY + '; }') * 2]:
with self.assertRaises(ValueError):
route.route_content(old)
def test_failed_validation_or_reload_restores_exact_config_and_backup(self):
for failure in (1, 2):
with self.subTest(failure=failure), tempfile.TemporaryDirectory() as tmp:
target = Path(tmp) / 'nginx.conf'
target.write_bytes(b'original\n')
backup = Path(tmp) / 'backup' / 'nginx.conf'
calls = []
def run(argv, **kwargs):
calls.append(argv)
if len(calls) == failure:
raise subprocess.CalledProcessError(1, argv)
with self.assertRaises(subprocess.CalledProcessError):
route.replace_config(target, 'changed\n', backup, run)
self.assertEqual(target.read_bytes(), b'original\n')
self.assertEqual(backup.read_bytes(), b'original\n')
if __name__ == '__main__':
unittest.main()