Files
canina/scripts/tests/test_release_schema_guard.py

71 lines
4.1 KiB
Python

"""Generated-client drift must block a release even with unchanged Git schema."""
import importlib.util
from pathlib import Path
import unittest
SPEC = importlib.util.spec_from_file_location('release', Path(__file__).parents[1]/'production-release.py')
m = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(m)
class SchemaGate(unittest.TestCase):
def test_unchanged_source_baseline_new_client_drift(self):
# Both Git SHAs already contain these fields; the old runtime client did not.
models = [{'name':'User','dbName':'users','fields':[
{'name':n,'dbName':n,'kind':'scalar','type':t,'isList':False}
for n,t in [('id','String'),('auth_expires_at','DateTime'),
('stage_test_session','String'),('stage_test_generation','String')]]},
{'name':'PrepareWorkflowOutbox','dbName':'prepare_workflow_outbox','fields':[
{'name':'id','kind':'scalar','type':'String','isList':False}]}]
old_catalog = [{'table':'users','column':'id','type':'uuid'}]
with self.assertRaisesRegex(ValueError, 'auth_expires_at.*prepare_workflow_outbox'):
m.validate_catalog(models, old_catalog)
def test_gate_pins_built_image_and_reads_live_catalog_before_replacement(self):
import json
from unittest.mock import patch
image = 'sha256:'+'c'*64
runtime = {'Image':'sha256:'+'d'*64,'Id':'running-id','State':{'Running':True}}
models = [{'name':'User','dbName':'users','fields':[{'name':'id','kind':'scalar'}]}]
responses = [json.dumps([runtime]), json.dumps([{'Id':image}]),
json.dumps(models), json.dumps([{'table':'users','column':'id','type':'uuid'}]),
json.dumps([runtime])]
with patch.object(m.subprocess,'check_output',side_effect=responses) as run:
result = m.schema_gate('canina-production-backend_prod:'+'a'*40)
self.assertEqual(result['target_image'], image)
self.assertEqual(result['scope'], 'missing-columns-only')
self.assertFalse(result['native_types_checked'])
self.assertFalse(result['constraints_checked'])
calls = [c.args[0] for c in run.call_args_list]
self.assertIn(image, calls[2])
self.assertIn('--network', calls[2])
self.assertIn('running-id', calls[3])
self.assertIn('READ ONLY', calls[3][-1])
script = Path(__file__).parents[1].joinpath('rollout-production.sh').read_text()
self.assertLess(script.index('production-release.py schema-check'), script.index('started=true'))
def test_wrong_native_type_is_outside_missing_column_guard(self):
# Deliberately document the boundary: this must not imply compatibility.
models = [{'name': 'User', 'dbName': 'users', 'fields': [
{'name': 'id', 'kind': 'scalar', 'type': 'String', 'isList': False}]}]
m.validate_catalog(models, [{'table': 'users', 'column': 'id', 'type': 'int4'}])
with self.assertRaisesRegex(ValueError, 'typed catalog'):
m.validate_receipt(dict(accepted=True, scope='reliability-only',
sha='a'*40, baseline='b'*40, reviewer='reviewer',
stage_run_url='https://git.parsaaghayi.ir/parsa/canina/actions/runs/123',
paths=['nginx.conf']), 'a'*40, 'b'*40, ['nginx.conf'])
def test_catalog_failure_never_falls_back_to_source_diff(self):
from unittest.mock import patch
import subprocess
with patch.object(m.subprocess,'check_output',side_effect=subprocess.CalledProcessError(1,['docker'])):
with self.assertRaises(subprocess.CalledProcessError):
m.schema_gate('target')
def test_exact_guard_paths_allowed_and_receipt_not_bound_to_checkout(self):
self.assertIn('scripts/tests/test_release_schema_guard.py', m.ALLOWED)
source = Path(__file__).parents[1].joinpath('production-release.py').read_text()
self.assertNotIn("['git','-C','/opt/services/canina/prod','rev-parse','HEAD']", source)
self.assertIn("r.get('production_images')", source)
if __name__ == '__main__': unittest.main()