Files
canina/scripts/test_editorial_ownership.py
2026-10-24 10:28:30 +00:00

53 lines
2.9 KiB
Python

import importlib.util
import os
from pathlib import Path
import tempfile
import unittest
spec=importlib.util.spec_from_file_location('migration',Path(__file__).with_name('editorial-ownership.py'))
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
@unittest.skipUnless(os.getuid()==0,'root fixture required')
class OwnershipTests(unittest.TestCase):
def test_migrate_twice_preserves_private_modes_and_rollback(self):
with tempfile.TemporaryDirectory() as t:
h=Path(t); d=h/'editorial-v4/credentials';d.mkdir(parents=True);d.chmod(0o700)
secret=d/'editorial-pmo-token';secret.write_text('fixture');secret.chmod(0o400)
c=h/'profiles/canina-competitor-worker/cache/editorial-runner';c.mkdir(parents=True);c.chmod(0o700)
outside=h/'config.yaml';outside.write_text('sentinel')
before={str(p.relative_to(h)):(p.stat().st_uid,p.stat().st_gid,p.stat().st_mode&0o777) for p in [d,d.parent,secret,c]}
journal=h/'ownership.json'
m.migrate(h,10000,10000,journal)
m.migrate(h,10000,10000,journal)
for p in [d,d.parent,secret,c]:self.assertEqual(p.stat().st_uid,10000)
self.assertEqual(secret.stat().st_mode&0o777,0o400)
self.assertEqual(d.stat().st_mode&0o777,0o700)
self.assertEqual(outside.stat().st_uid,0)
m.rollback(h,journal)
for p,expected in before.items():
s=(h/p).stat();self.assertEqual((s.st_uid,s.st_gid,s.st_mode&0o777),expected)
def test_preflight_refuses_unexpected_symlinks_world_readable_secret_without_changes(self):
for bad in ['symlink','public','unknown']:
with tempfile.TemporaryDirectory() as t:
h=Path(t);d=h/'editorial-v4/credentials';d.mkdir(parents=True);d.chmod(0o700)
p=d/'editorial-pmo-token';p.write_text('fixture');p.chmod(0o400)
if bad=='symlink':p.unlink();p.symlink_to(h/'elsewhere')
if bad=='public':p.chmod(0o644)
if bad=='unknown':(d/'other-token').write_text('fixture')
with self.assertRaises(ValueError):m.migrate(h,10000,10000,h/'journal')
self.assertEqual(d.stat().st_uid,0)
def test_partial_failure_rolls_back(self):
with tempfile.TemporaryDirectory() as t:
h=Path(t);d=h/'editorial-v4';d.mkdir();p=d/'deployment.json';p.write_text('{}');p.chmod(0o400)
from unittest.mock import patch
real=os.chown;calls=[]
def fail(path,uid,gid,**kw):
calls.append(path)
if len(calls)==2:raise OSError('fixture interruption')
return real(path,uid,gid,**kw)
with patch.object(m.os,'chown',side_effect=fail):
with self.assertRaises(OSError):m.migrate(h,10000,10000,h/'journal')
self.assertEqual(d.stat().st_uid,0);self.assertEqual(p.stat().st_uid,0)