Files
canina/scripts/test_production_release.py
T
devops b4420e54b7
Deploy Canina / deploy (push) Failing after 9h30m37s
fix(release): admit exact reviewed publisher SEO repair paths
2026-10-25 18:34:09 +00:00

162 lines
9.6 KiB
Python

import importlib.util
import tempfile
import unittest
from pathlib import Path
SPEC = importlib.util.spec_from_file_location('production', Path(__file__).with_name('production-release.py'))
m = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(m)
class ProductionGuards(unittest.TestCase):
def receipt(self):
return dict(sha='a'*40, baseline='b'*40, stage_run_url='https://git.parsaaghayi.ir/parsa/canina/actions/runs/123', reviewer='CEO delegated acceptance', accepted=True, scope='reliability-only', paths=['nginx.conf'], typed_catalog_acceptance=dict(accepted=True, sha='a'*40, scope='native-types-and-constraints', reviewer='Independent schema reviewer', evidence_sha256='c'*64))
def test_exact_auth_schema_paths_from_real_git_diff_allowed_only(self):
import subprocess
paths = subprocess.check_output([
'git', 'diff', '--name-only', '688a0e4^', '688a0e4', '--',
'.gitea/workflows/auth-schema-reconcile.yml',
'docs/auth-schema-reconciliation.md', 'docs/auth-schema-rehearsal.json',
'scripts/auth-schema-reconcile.sql', 'scripts/auth-schema-verify.sql',
'scripts/ci-auth-schema-reconcile.sh', 'scripts/test_auth_schema_reconcile.py',
], cwd=Path(__file__).parents[1], text=True).splitlines()
self.assertEqual(len(paths), 7, paths)
r = self.receipt(); r['paths'] = paths
m.validate_receipt(r, 'a'*40, 'b'*40, paths)
for path in ['scripts/auth-schema-other.sql', 'docs/auth-schema-other.md',
'.gitea/workflows/other.yml', 'scripts/test_auth_schema_other.py']:
r['paths'] = [path]
with self.assertRaises(ValueError):
m.validate_receipt(r, 'a'*40, 'b'*40, [path])
self.assertFalse(any('*' in path for path in m.ALLOWED))
def test_exact_six_baseline_gate_repairs_allowed_not_frontend_wildcard(self):
files = {
'frontend/admin-panel/src/pages/OrchestrationCommandCenter.tsx',
'frontend/admin-panel/src/pages/selectionSafety.test.tsx',
'frontend/admin-panel/src/pages/workflowSafety.test.tsx',
'frontend/admin-panel/src/test/setup.ts',
'frontend/admin-panel/src/test/storage.test.ts',
'frontend/application/components/AnalyticsDeferred.tsx',
'frontend/application/components/BlogPage.tsx',
'frontend/application/components/__tests__/BlogPage.sorting.test.tsx',
'frontend/application/components/BlogPostClient.tsx',
'frontend/application/components/__tests__/BlogPostClient.test.tsx',
'frontend/application/lib/extractFaqs.ts',
'frontend/application/lib/extractFaqs.test.ts',
}
r=self.receipt();r['paths']=sorted(files)
m.validate_receipt(r,'a'*40,'b'*40,sorted(files))
for path in ['frontend/admin-panel/src/pages/Other.tsx', 'frontend/application/components/Other.tsx']:
r['paths']=[path]
with self.assertRaises(ValueError): m.validate_receipt(r,'a'*40,'b'*40,[path])
def test_exact_editorial_installer_repair_paths_allowed_only(self):
paths = ['infra/hermes-editorial/installer.py',
'infra/hermes-editorial/tests/test_installer.py',
'scripts/tests/test_secure_build_install.py']
r = self.receipt(); r['paths'] = paths
m.validate_receipt(r, 'a'*40, 'b'*40, paths)
for path in ['infra/hermes-editorial/model-policy.json',
'infra/hermes-editorial/payload/editorial-v4/status.json',
'infra/hermes-editorial/other.py']:
r['paths'] = [path]
with self.assertRaises(ValueError):
m.validate_receipt(r, 'a'*40, 'b'*40, [path])
def test_exact_publisher_seo_repair_paths_allowed_only(self):
paths = ['frontend/application/app/blog/[slug]/page.tsx',
'frontend/application/app/blog/[slug]/publisher-seo.test.tsx',
'frontend/application/lib/blogAuthor.ts',
'frontend/application/lib/blogAuthor.test.ts']
r = self.receipt(); r['paths'] = paths
m.validate_receipt(r, 'a'*40, 'b'*40, paths)
for path in ['frontend/application/app/blog/[slug]/other.tsx',
'frontend/application/lib/other.ts',
'frontend/application/app/blog/page.tsx']:
r['paths'] = [path]
with self.assertRaises(ValueError):
m.validate_receipt(r, 'a'*40, 'b'*40, [path])
self.assertFalse(any('*' in path for path in m.ALLOWED))
def test_independent_typed_catalog_acceptance_required(self):
r = self.receipt()
r.pop('typed_catalog_acceptance', None)
with self.assertRaisesRegex(ValueError, 'typed catalog'):
m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf'])
for acceptance in [
{'accepted': False, 'sha': 'a'*40, 'evidence_sha256': 'c'*64},
{'accepted': True, 'sha': 'd'*40, 'evidence_sha256': 'c'*64},
{'accepted': True, 'sha': 'a'*40, 'evidence_sha256': ''},
]:
r['typed_catalog_acceptance'] = acceptance
with self.assertRaisesRegex(ValueError, 'typed catalog'):
m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf'])
def test_exact_stage_sha(self):
r=self.receipt()
m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf'])
with self.assertRaises(ValueError): m.validate_receipt(r, 'c'*40, 'b'*40, ['nginx.conf'])
def test_scope_and_baseline_fail_closed(self):
r=self.receipt()
for baseline, paths in [('c'*40, ['nginx.conf']), ('b'*40, ['backend/prisma/schema.prisma']), ('b'*40, ['nginx.conf','backend/src/other.ts'])]:
with self.assertRaises(ValueError): m.validate_receipt(r, 'a'*40, baseline, paths)
r['accepted']=False
with self.assertRaises(ValueError): m.validate_receipt(r,'a'*40,'b'*40,['nginx.conf'])
def test_receipt_rejects_non_gitea_evidence(self):
r=self.receipt();r['stage_run_url']='https://evil.test/run/1'
with self.assertRaises(ValueError): m.validate_receipt(r,'a'*40,'b'*40,['nginx.conf'])
def test_snapshot_preserves_runtime_and_immutable_image(self):
x={'Name':'/canino_backend_prod','Image':'sha256:'+'a'*64,'Config':{'Env':['DATABASE_URL=private'], 'Labels':{'com.docker.compose.project':'prod','com.docker.compose.service':'backend_prod','traefik.enable':'true'},'User':'node','WorkingDir':'/app','Entrypoint':['node'],'Cmd':['dist/main']},'Mounts':[{'Type':'volume','Name':'prod_canina_prod_uploads','Destination':'/app/uploads','RW':True},{'Type':'bind','Source':'/opt/services/proxy/certs/canina','Destination':'/app/ssl_certs','RW':True}],'NetworkSettings':{'Networks':{'traefik_public':{}}},'HostConfig':{'RestartPolicy':{'Name':'always'},'PortBindings':{},'Privileged':False}}
c=m.snapshot([x]); s=c['services']['backend_prod']
self.assertEqual(s['image'],x['Image'])
self.assertEqual(s['environment'],['DATABASE_URL=private'])
self.assertTrue(c['volumes']['prod_canina_prod_uploads']['external'])
self.assertNotIn('com.docker.compose.project',s['labels'])
x['Mounts'][0]['Name']='stage_canina_stage_uploads'
with self.assertRaises(ValueError): m.snapshot([x])
def test_production_snapshot_forces_direct_next_url(self):
import os, json
from unittest.mock import patch
c={'services':{n:{'environment':['FRONTEND_INTERNAL_URL=http://legacy:8080','REVALIDATION_SECRET=old'], 'labels':{}} for n in ['backend_prod','frontend_prod']}}
with tempfile.TemporaryDirectory() as d:
old=os.getcwd()
try:
os.chdir(d)
with patch.object(m.sys,'argv',['production-release.py','snapshot']), patch.dict(os.environ,DEPLOY_SHA='a'*40), patch.object(m.subprocess,'check_output',return_value=b'[]'), patch.object(m,'snapshot',return_value=c):
m.main()
services=json.loads(Path('.prod-compose.json').read_text())['services']
self.assertIn('FRONTEND_INTERNAL_URL=http://frontend_prod:3000',services['backend_prod']['environment'])
for s in services.values():
self.assertFalse(any(v.startswith('REVALIDATION_SECRET=') for v in s['environment']))
self.assertTrue(s['env_file'][0]['required'])
finally: os.chdir(old)
def test_private_receipt_permissions(self):
import os
with tempfile.TemporaryDirectory() as d:
p=Path(d)/'receipt.json';p.write_text('{}');p.chmod(0o644)
with self.assertRaises(ValueError): m.private_json(p)
p.unlink();p.symlink_to('/dev/null')
with self.assertRaises(ValueError): m.private_json(p)
def test_no_schema_or_orphan_mutations_in_production(self):
text=Path(__file__).with_name('rollout-production.sh').read_text()
for dangerous in ['migrate deploy','db push','--remove-orphans','docker compose down','git reset']:
self.assertNotIn(dangerous,text)
self.assertIn('trap rollback EXIT',text)
self.assertIn('x-revalidate-secret',text)
self.assertIn('prod_canina_prod_db prod_canina_prod_uploads',text)
def test_ci_and_nonsha_refused_before_effects(self):
import subprocess, os
for script,args in [('deploy.sh',['main']),('ci-host-deploy.sh',[]) ,('rollout-production.sh',[])]:
p=subprocess.run(['bash',str(Path(__file__).with_name(script)),*args],env={'PATH':os.environ['PATH']},capture_output=True)
self.assertEqual(p.returncode,2, p.stderr)
if __name__=='__main__': unittest.main()