|
| 1 | +"""Check local test isolation and cleanup without running databases or Cargo.""" |
| 2 | +import json |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | + |
| 10 | + |
| 11 | +class LocalRunnerTests(unittest.TestCase): |
| 12 | + def run_backend(self, backend, cargo_status=0, docker_status=0): |
| 13 | + with tempfile.TemporaryDirectory() as directory: |
| 14 | + root = Path(directory) |
| 15 | + log = root / "commands.jsonl" |
| 16 | + program = f"#!{sys.executable}\n" + ''' |
| 17 | +import json |
| 18 | +import os |
| 19 | +from pathlib import Path |
| 20 | +import sys |
| 21 | +
|
| 22 | +name = Path(sys.argv[0]).name |
| 23 | +with open(os.environ["COMMAND_LOG"], "a") as log: |
| 24 | + log.write(json.dumps([name, sys.argv[1:], os.environ.get("DATABASE_URL")]) + "\\n") |
| 25 | +if name == "cargo": |
| 26 | + sys.exit(int(os.environ["CARGO_STATUS"])) |
| 27 | +if sys.argv[1] == "compose": |
| 28 | + if int(os.environ["DOCKER_STATUS"]): |
| 29 | + sys.exit(int(os.environ["DOCKER_STATUS"])) |
| 30 | + print("owned-container") |
| 31 | +elif sys.argv[1] == "port": |
| 32 | + print("127.0.0.1:49123") |
| 33 | +elif sys.argv[1] == "inspect": |
| 34 | + print("true") |
| 35 | +''' |
| 36 | + for name in ("docker", "cargo"): |
| 37 | + tool = root / name |
| 38 | + tool.write_text(program) |
| 39 | + tool.chmod(0o755) |
| 40 | + # A user's saved DSN must neither be overwritten nor selected. |
| 41 | + config = root / ".odbc.ini" |
| 42 | + config.write_text("[SNOWFLAKE]\nDatabase=production\n") |
| 43 | + env = dict(os.environ, PATH=f"{root}{os.pathsep}{os.environ['PATH']}", |
| 44 | + COMMAND_LOG=str(log), CARGO_STATUS=str(cargo_status), |
| 45 | + DOCKER_STATUS=str(docker_status), ODBCINI=str(config), |
| 46 | + DATABASE_URL="DSN=SNOWFLAKE") |
| 47 | + result = subprocess.run( |
| 48 | + ["bash", str(Path(__file__).resolve().parent.parent / "test.sh"), backend], |
| 49 | + cwd=root, env=env, text=True, capture_output=True, timeout=10, |
| 50 | + ) |
| 51 | + self.assertEqual(config.read_text(), "[SNOWFLAKE]\nDatabase=production\n") |
| 52 | + commands = [json.loads(line) for line in log.read_text().splitlines()] if log.exists() else [] |
| 53 | + return result, commands |
| 54 | + |
| 55 | + def test_database_backends_use_owned_containers_and_random_loopback_ports(self): |
| 56 | + for backend in ("postgres", "mysql", "mssql", "odbc"): |
| 57 | + with self.subTest(backend=backend): |
| 58 | + result, commands = self.run_backend(backend) |
| 59 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 60 | + compose = next(args for name, args, _ in commands if name == "docker" and args[0] == "compose") |
| 61 | + self.assertIn("-d", compose) |
| 62 | + self.assertIn("--rm", compose) |
| 63 | + self.assertTrue(compose[compose.index("-p", compose.index("run")) + 1].startswith("127.0.0.1::")) |
| 64 | + cargo = next(command for command in commands if command[0] == "cargo") |
| 65 | + self.assertIn("--locked", cargo[1]) |
| 66 | + self.assertIn("49123", cargo[2]) |
| 67 | + self.assertNotIn("DSN=", cargo[2]) |
| 68 | + self.assertEqual(commands[-1][1][-2:], ["down", "--remove-orphans"]) |
| 69 | + |
| 70 | + def test_test_failure_is_preserved_after_cleanup(self): |
| 71 | + result, commands = self.run_backend("odbc", cargo_status=17) |
| 72 | + self.assertEqual(result.returncode, 17) |
| 73 | + self.assertEqual(commands[-1][1][-2:], ["down", "--remove-orphans"]) |
| 74 | + |
| 75 | + def test_failed_container_start_never_runs_tests(self): |
| 76 | + result, commands = self.run_backend("postgres", docker_status=19) |
| 77 | + self.assertEqual(result.returncode, 19) |
| 78 | + self.assertFalse(any(name == "cargo" for name, _, _ in commands)) |
| 79 | + |
| 80 | + def test_sqlite_does_not_start_docker(self): |
| 81 | + result, commands = self.run_backend("sqlite") |
| 82 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 83 | + self.assertEqual([name for name, _, _ in commands], ["cargo"]) |
| 84 | + self.assertTrue(commands[0][2].startswith("sqlite:")) |
| 85 | + self.assertFalse(Path(commands[0][2].removeprefix("sqlite://")).exists()) |
| 86 | + |
| 87 | + def test_invalid_backend_does_not_start_anything(self): |
| 88 | + result, commands = self.run_backend("production") |
| 89 | + self.assertEqual(result.returncode, 2) |
| 90 | + self.assertEqual(commands, []) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + unittest.main() |
0 commit comments