Files
parsaaghayi-backend/tests/deploy_fixture.py
T
2026-11-18 13:45:16 +00:00

52 lines
2.3 KiB
Python

"""Disposable deployment boundary tests. Never contacts Docker or production."""
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
class DeployBoundary(unittest.TestCase):
def run_fixture(self, broken=False):
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp)
(p/'bin').mkdir()
(p/'repo/ops').mkdir(parents=True)
(p/'repo/ops/backend.compose.yml').write_text('services: {}\n')
(p/'base.yml').write_text('services: {}\n')
log = p/'calls'
for name, body in {
'git': 'if [ "$1" = rev-parse ]; then printf "%040d\\n" 1; fi',
'docker': 'printf "%s\\n" "$*" >> "$CALLS"; if [ "$1" = run ] && [ "$BROKEN" = 1 ]; then exit 17; fi',
'curl': 'printf 200',
'sleep': ':',
}.items():
f = p/'bin'/name
f.write_text('#!/bin/sh\n'+body+'\n')
f.chmod(0o755)
env = dict(os.environ, PATH=str(p/'bin')+':'+os.environ['PATH'],
APP_DIR=str(p/'repo'), BASE_COMPOSE=str(p/'base.yml'),
CALLS=str(log), BROKEN=str(int(broken)))
result = subprocess.run(['bash', str(ROOT/'ops/deploy-backend.sh'), '0'*39+'1'], env=env, capture_output=True, text=True)
return result, log.read_text() if log.exists() else ''
def test_missing_vendor_prevents_container_swap(self):
r, calls = self.run_fixture(True)
self.assertNotEqual(r.returncode, 0)
self.assertIn('run --rm --network none --entrypoint php', calls)
self.assertNotIn(' up ', calls)
def test_build_assertion_precedes_backend_only_swap(self):
r, calls = self.run_fixture()
self.assertEqual(r.returncode, 0, r.stderr)
self.assertLess(calls.index(' build backend'), calls.index('run --rm'))
self.assertLess(calls.index('run --rm'), calls.index(' up '))
self.assertIn('up -d --no-deps --no-build backend', calls)
self.assertIn('migrate --force --path=database/migrations/2026_11_18_000001_create_activity_credentials_table.php', calls)
self.assertNotIn('db:seed', calls)
self.assertNotIn('prune', calls)
if __name__ == '__main__':
unittest.main()