117 lines
4.1 KiB
Python
117 lines
4.1 KiB
Python
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location('quality_macos_service', Path(__file__).with_name('service.py'))
|
|
service = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(service)
|
|
|
|
|
|
class Fixture(service.Guardian):
|
|
def command(self, role):
|
|
return ['/bin/sleep', '90' if role == 'mysql' else '91']
|
|
|
|
def occupied(self, role):
|
|
return False
|
|
|
|
def environment(self):
|
|
return os.environ.copy()
|
|
|
|
def db_ready(self):
|
|
return self.ready
|
|
|
|
def backend_ready(self):
|
|
return service.matches(self.records.get('backend')) is True
|
|
|
|
|
|
class GuardianTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp = tempfile.TemporaryDirectory()
|
|
self.root = Path(self.temp.name)
|
|
(self.root / '.runtime').mkdir()
|
|
self.g = Fixture({'root': str(self.root)}, self.root / 'guardian')
|
|
self.g.value = {'mysqlPort': 59998}
|
|
self.g.ready = False
|
|
self.external = []
|
|
|
|
def tearDown(self):
|
|
for p in list(self.g.children.values()) + self.external:
|
|
if p.poll() is None:
|
|
p.terminate()
|
|
p.wait(timeout=10)
|
|
self.temp.cleanup()
|
|
|
|
def test_database_must_be_ready_before_backend_starts(self):
|
|
self.g.tick()
|
|
self.assertIn('mysql', self.g.records)
|
|
self.assertNotIn('backend', self.g.records)
|
|
self.g.ready = True
|
|
self.g.tick()
|
|
self.assertTrue(service.matches(self.g.records['backend']))
|
|
|
|
def test_abnormal_child_exit_is_restarted_without_duplicate_mysql(self):
|
|
self.g.ready = True
|
|
self.g.tick()
|
|
mysql = self.g.records['mysql']['pid']
|
|
old = self.g.children['backend']
|
|
old.kill()
|
|
old.wait()
|
|
self.g.tick()
|
|
self.assertNotIn('backend', self.g.records)
|
|
self.assertEqual(self.g.records['mysql']['pid'], mysql)
|
|
self.g.next_start['backend'] = 0
|
|
self.g.tick()
|
|
self.assertNotEqual(self.g.records['backend']['pid'], old.pid)
|
|
self.assertEqual(self.g.restarts, {'mysql': 1, 'backend': 2})
|
|
|
|
def test_database_exit_is_restarted_before_missing_backend(self):
|
|
self.g.tick()
|
|
old = self.g.children['mysql']
|
|
old.kill()
|
|
old.wait()
|
|
self.g.tick()
|
|
self.assertNotIn('mysql', self.g.records)
|
|
self.g.next_start['mysql'] = 0
|
|
self.g.tick()
|
|
self.assertNotEqual(self.g.records['mysql']['pid'], old.pid)
|
|
self.assertNotIn('backend', self.g.records)
|
|
|
|
def test_existing_process_is_adopted_and_unknown_process_is_not_replaced(self):
|
|
command = self.g.command('mysql')
|
|
p = subprocess.Popen(command, start_new_session=True)
|
|
self.external.append(p)
|
|
info = service.record(p.pid, command)
|
|
service.atomic(self.root / '.runtime/mysql.process.json', {'pid': p.pid, 'started': info['started']})
|
|
self.assertTrue(self.g.ensure('mysql'))
|
|
self.assertEqual(self.g.restarts['mysql'], 0)
|
|
self.assertIsNone(service.matches(info, self.g.command('backend')))
|
|
self.assertIs(p.poll(), None)
|
|
|
|
def test_missing_data_marker_never_initializes_database(self):
|
|
service.atomic(self.root / '.runtime/local-config.json', {'databaseReady': True})
|
|
real = service.Guardian({'root': str(self.root), 'jarSha256': '0'*64}, self.root / 'guardian')
|
|
with self.assertRaises(RuntimeError):
|
|
real.preflight()
|
|
self.assertFalse((self.root / '.runtime/mysql-data').exists())
|
|
|
|
def test_pinned_jar_mismatch_is_rejected(self):
|
|
r = self.root / '.runtime'
|
|
(r / 'mysql-data').mkdir()
|
|
(r / 'mysql-data/auto.cnf').write_text('existing marker')
|
|
(r / 'backend-builds').mkdir()
|
|
(r / 'backend-builds' / ('0'*64+'.jar')).write_bytes(b'unaccepted')
|
|
service.atomic(r / 'local-config.json', {'databaseReady': True})
|
|
real = service.Guardian({'root': str(self.root), 'jarSha256': '0'*64}, self.root / 'guardian')
|
|
with self.assertRaises(RuntimeError):
|
|
real.preflight()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|