Files
canina/scripts/test-recovery-20261026.py
T
Canina Incident Recovery 07bc25ca9f
Incident restore production backend 20261026 / recover (push) Failing after 41s
ops: add reviewed backend-only incident rollback CI with read-only preflight
2026-10-26 05:42:15 +00:00

42 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Side-effect-free incident boundary fixtures; never executes main."""
import ast, copy, importlib.util, pathlib, unittest
from unittest.mock import patch
P=pathlib.Path(__file__).with_name('recover-production-20261026.py')
spec=importlib.util.spec_from_file_location('recovery',P)
r=importlib.util.module_from_spec(spec);spec.loader.exec_module(r)
class RecoveryTests(unittest.TestCase):
def setUp(self):
self.c={'services':{'backend_prod':{'image':r.IMAGE,'container_name':'canino_backend_prod','labels':{'ir.canina.release.sha':r.BASE},'command':['node','dist/main.js'],'user':'node','networks':['traefik_public'],'volumes':[{'source':'prod_canina_prod_uploads'},{'source':'/opt/services/proxy/certs/canina'}]},'frontend_prod':{'image':r.FRONT}},'volumes':{'prod_canina_prod_uploads':{'external':True}}}
def test_identity_accepts_exact(self): self.assertIs(r.validate(self.c),self.c['services']['backend_prod'])
def test_wrong_image(self):
self.c['services']['backend_prod']['image']='evil';self.assertRaises(AssertionError,r.validate,self.c)
def test_wrong_frontend(self):
self.c['services']['frontend_prod']['image']='evil';self.assertRaises(AssertionError,r.validate,self.c)
def test_wrong_network(self):
self.c['services']['backend_prod']['networks']=['stage'];self.assertRaises(AssertionError,r.validate,self.c)
def test_wrong_volume(self):
self.c['volumes']['prod_canina_prod_uploads']['external']=False;self.assertRaises(AssertionError,r.validate,self.c)
def test_command_injection_rejected(self):
self.c['services']['backend_prod']['command']=['sh','-c','touch /tmp/pwn'];self.assertRaises(AssertionError,r.validate,self.c)
def test_arguments_not_shell(self):
value='x; touch /tmp/pwn'
with patch.object(r.subprocess,'check_output',return_value=b'ok') as call:
self.assertEqual(r.run(['docker','inspect',value]),b'ok')
call.assert_called_once_with(['docker','inspect',value])
def test_no_shell_calls(self):
for n in ast.walk(ast.parse(P.read_text())):
if isinstance(n,ast.Call):
self.assertFalse(any(k.arg=='shell' for k in n.keywords))
def test_no_database_mutation_commands(self):
text='\n'.join(line for line in P.read_text().splitlines() if not line.lstrip().startswith('#'))
for forbidden in ['migrate deploy','db push','TRUNCATE ','DROP TABLE','INSERT INTO','UPDATE public','seed','compose down','--remove-orphans']:
self.assertNotIn(forbidden,text)
self.assertIn('SET TRANSACTION READ ONLY',text)
self.assertIn("'--exit-code'",text)
self.assertIn("['up','-d','--no-deps','backend_prod']",text)
def test_non_ci_rejected_before_any_docker(self):
with patch.dict(r.os.environ,{'GITHUB_ACTIONS':'false'}),patch.object(r,'run') as call:
self.assertRaises(AssertionError,r.main);call.assert_not_called()
if __name__=='__main__': unittest.main()