"""Offline guard tests for the isolated MariaDB lane.""" import importlib.util from pathlib import Path import unittest from unittest import mock import tempfile import json spec = importlib.util.spec_from_file_location('mariadb_integration', Path(__file__).with_name('mariadb-integration.py')) runner = importlib.util.module_from_spec(spec) spec.loader.exec_module(runner) class Guards(unittest.TestCase): def test_compose_project_is_generated_and_test_only(self): command = runner.compose_command('jobtracker-it-abc123') self.assertIn('jobtracker-it-abc123', command) self.assertIn('docker-compose.database.yml', ' '.join(command)) self.assertNotIn('docker-compose.yml', command) for name in ['production', 'jobtracker-local', '', 'jobtracker-it-x;whoami']: with self.assertRaises(ValueError): runner.compose_command(name) def test_credentials_and_address_are_local_test_only(self): self.assertEqual(runner.test_connection(33307), 'Server=127.0.0.1;Port=33307;User ID=root;Password=jt-integration-only;SslMode=None') for port in [0, 65536, -1]: with self.assertRaises(ValueError): runner.test_connection(port) def test_existing_server_requires_explicit_environment(self): with self.assertRaises(ValueError): runner.existing_connection({}) with self.assertRaises(ValueError): runner.existing_connection({'JOBTRACKER_MARIADB_TEST_ADMIN':' '}) self.assertEqual(runner.existing_connection({'JOBTRACKER_MARIADB_TEST_ADMIN':'local-test'}), 'local-test') def test_cleanup_failure_cannot_turn_into_a_green_ci_run(self): with tempfile.TemporaryDirectory() as folder, mock.patch('sys.argv', ['runner','--output',folder]), \ mock.patch.dict(runner.os.environ, {'DOTNET_HOST_PATH':'test-dotnet'}, clear=True), \ mock.patch.object(runner.subprocess, 'run') as execute: execute.side_effect = [mock.Mock(returncode=n) for n in [0,0,0,0,17]] self.assertEqual(runner.main(), 17) record=json.loads((Path(folder)/'run.json').read_text()) self.assertEqual(record['exitCode'],17) self.assertEqual(record['commands'][-1]['command'][-4:], ['rm','--stop','--force','mariadb-test']) self.assertNotIn('down',record['commands'][-1]['command']) def test_setup_exception_is_recorded_as_failure(self): with tempfile.TemporaryDirectory() as folder, mock.patch('sys.argv',['runner','--output',folder]), \ mock.patch.dict(runner.os.environ,{},clear=True), mock.patch.object(runner.shutil,'which',return_value=None), \ mock.patch.object(runner.subprocess,'run',return_value=mock.Mock(returncode=0)): with self.assertRaises(ValueError): runner.main() self.assertEqual(json.loads((Path(folder)/'run.json').read_text())['exitCode'],1) if __name__ == '__main__': unittest.main()