Isolate commit-pinned Stage releases from dirty operational checkout
This commit is contained in:
1 parent
cc9ec25388
commit
e40398a843
2 files changed
+164
-122
No files matched your search
@@ -14,8 +14,9 @@ class RolloutTests(unittest.TestCase):
|
||||
workflow=(ROOT/'.gitea/workflows/deploy.yml').read_text()
|
||||
self.assertIn('DEPLOY_SHA: ${{ github.sha }}',workflow)
|
||||
self.assertIn('/raw/commit/${DEPLOY_SHA}/scripts/deploy.sh',workflow)
|
||||
self.assertIn('git reset --hard "$DEPLOY_SHA"',script)
|
||||
self.assertNotIn('git reset --hard "origin/$BRANCH"',script)
|
||||
self.assertNotIn('git reset',script)
|
||||
self.assertIn('git checkout --detach "$DEPLOY_SHA"',script)
|
||||
self.assertIn('/opt/services/canina/releases',script)
|
||||
|
||||
def test_commit_guard_rejects_missing_and_malformed_sha_before_side_effects(self):
|
||||
guard=(ROOT/'scripts/deploy.sh').read_text().split('# END ROLLOUT GUARD')[0]
|
||||
@@ -35,6 +36,82 @@ class RolloutTests(unittest.TestCase):
|
||||
self.assertEqual(run.strip().splitlines()[0].strip(),'set -euo pipefail')
|
||||
self.assertIn('./deploy.sh "${BRANCH}"',run)
|
||||
|
||||
def test_isolated_deploy_preserves_dirty_stage_and_stable_storage(self):
|
||||
import json, shutil
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
base = pathlib.Path(directory)
|
||||
source, stage, releases, tools = [base / n for n in ('source', 'stage', 'releases', 'tools')]
|
||||
for p in (source, stage, tools): p.mkdir()
|
||||
def git(*args, cwd=source):
|
||||
return subprocess.check_output(['git', *args], cwd=cwd, text=True).strip()
|
||||
git('init', '-b', 'develop')
|
||||
git('config', 'user.email', 'test@example.invalid')
|
||||
git('config', 'user.name', 'Test')
|
||||
shutil.copytree(ROOT/'scripts', source/'scripts')
|
||||
shutil.copyfile(ROOT/'.dockerignore', source/'.dockerignore')
|
||||
(source/'nginx.conf').write_text('__BACKEND_HOST__')
|
||||
git('add', '.')
|
||||
git('commit', '-qm', 'fixture')
|
||||
sha = git('rev-parse', 'HEAD')
|
||||
git('init', '-b', 'develop', cwd=stage)
|
||||
git('config', 'user.email', 'test@example.invalid', cwd=stage)
|
||||
git('config', 'user.name', 'Test', cwd=stage)
|
||||
(stage/'dirty').write_text('original')
|
||||
git('add', '.', cwd=stage)
|
||||
git('commit', '-qm', 'stage', cwd=stage)
|
||||
(stage/'dirty').write_text('staged')
|
||||
git('add', '.', cwd=stage)
|
||||
(stage/'dirty').write_text('unstaged')
|
||||
for n in ('.support-chat.env', '.revalidation.env'):
|
||||
(stage/n).write_text('TEST_SECRET=never-log-this\n')
|
||||
before = git('status', '--porcelain', cwd=stage), git('diff', '--binary', 'HEAD', cwd=stage), (stage/'.git/index').read_bytes()
|
||||
config = {'services': {
|
||||
'db_stage': {'volumes': [{'type': 'volume', 'source': 'canina_stage_db', 'target': '/var/lib/postgresql/data'}]},
|
||||
'redis_stage': {},
|
||||
'backend_stage': {'container_name': 'canino_backend_stage', 'volumes': [
|
||||
{'type': 'volume', 'source': 'canina_stage_uploads', 'target': '/app/uploads'},
|
||||
{'type': 'bind', 'source': '/opt/services/proxy/certs/canina', 'target': '/app/ssl_certs'}]},
|
||||
'frontend_stage': {'container_name': 'canino_frontend_stage'}},
|
||||
'volumes': {n: {'name': 'stage_'+n} for n in ('canina_stage_db','canina_stage_uploads')},
|
||||
'networks': {'traefik_public': {'name': 'traefik_public', 'external': True}}}
|
||||
(base/'config.json').write_text(json.dumps(config))
|
||||
(tools/'docker').write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$TRACE"\nif [[ "$*" == *"config --format json"* ]]; then /bin/cat "$FIXTURE"; fi\nif [[ "$*" == *"run --rm"* && "${FAIL_MIGRATION:-}" == 1 ]]; then exit 42; fi\n')
|
||||
(tools/'docker').chmod(0o755)
|
||||
script = (ROOT/'scripts/deploy.sh').read_text()
|
||||
script = script.replace('/opt/services/canina/stage-backend-deploy.lock', str(base/'lock')).replace('/opt/services/canina/stage', str(stage)).replace('/opt/services/canina/releases', str(releases)).replace('https://git.parsaaghayi.ir/parsa/canina.git', str(source)).replace('[ -d /opt/services/proxy/certs/canina ]', 'true')
|
||||
deploy = base/'deploy.sh'
|
||||
deploy.write_text(script)
|
||||
env = dict(os.environ, GITHUB_ACTIONS='true', DEPLOY_SHA=sha, PATH=str(tools)+':'+os.environ['PATH'], TRACE=str(base/'trace'), FIXTURE=str(base/'config.json'))
|
||||
for fail in ('1', '0'):
|
||||
if releases.exists(): shutil.rmtree(releases)
|
||||
(base/'trace').write_text('')
|
||||
result = subprocess.run(['bash', str(deploy), 'develop'], env=dict(env, FAIL_MIGRATION=fail), capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, 42 if fail == '1' else 0, result.stderr)
|
||||
trace = (base/'trace').read_text()
|
||||
compose = [line for line in trace.splitlines() if line.startswith('compose ')]
|
||||
self.assertTrue(compose)
|
||||
self.assertTrue(all(line.startswith('compose -p stage --env-file /dev/null --project-directory '+str(releases/sha)) for line in compose))
|
||||
self.assertNotIn('canina-bot', trace)
|
||||
self.assertNotIn('remove-orphans', trace)
|
||||
self.assertNotIn('up -d --no-deps db_stage', trace)
|
||||
self.assertNotIn('never-log-this', result.stdout+result.stderr)
|
||||
if fail == '1': self.assertNotIn('up -d', trace)
|
||||
else:
|
||||
self.assertLess(trace.index('migrate deploy'), trace.index('up -d --no-deps backend_stage'))
|
||||
self.assertLess(trace.index('exec -T backend_stage'), trace.index('up -d --no-deps frontend_stage'))
|
||||
release = releases/sha
|
||||
self.assertEqual(git('rev-parse', 'HEAD', cwd=release), sha)
|
||||
self.assertEqual((release/'.support-chat.env').stat().st_mode & 0o777, 0o600)
|
||||
self.assertIn('.stage-*.json', (release/'.dockerignore').read_text())
|
||||
self.assertIn('.support-chat.env', (release/'.dockerignore').read_text())
|
||||
self.assertIn('.revalidation.env', (release/'.dockerignore').read_text())
|
||||
self.assertEqual((release/'.stage-compose.json').stat().st_mode & 0o777, 0o600)
|
||||
effective = json.loads((release/'.stage-compose.json').read_text())
|
||||
self.assertEqual(effective['volumes'], {n: {'name': 'stage_'+n, 'external': True} for n in ('canina_stage_db','canina_stage_uploads')})
|
||||
self.assertEqual(before, (git('status', '--porcelain', cwd=stage), git('diff', '--binary', 'HEAD', cwd=stage), (stage/'.git/index').read_bytes()))
|
||||
retry = subprocess.run(['bash', str(deploy), 'develop'], env=env, capture_output=True, text=True)
|
||||
self.assertNotEqual(retry.returncode, 0)
|
||||
|
||||
def test_migration_failure_never_starts_apps(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
docker = pathlib.Path(d)/'docker'
|
||||
@@ -60,4 +137,3 @@ class RolloutTests(unittest.TestCase):
|
||||
self.assertIn('exec -T backend_stage node',trace)
|
||||
self.assertNotIn('up -d --no-deps frontend_stage',trace)
|
||||
self.assertNotIn('SUCCESS',result.stdout)
|
||||
if __name__=='__main__': unittest.main()
|
||||
Reference in new issue
Block a user