222 lines
11 KiB
Python
222 lines
11 KiB
Python
"""Review the nondeploying release verifier using only temporary fixtures.
|
|
|
|
Git source reads are stubbed with fixture blobs. No Git, Docker, registry,
|
|
production or real export mutation occurs. Failing negative tests identify a
|
|
verifier gap; they do not authorize bypassing an approval gate.
|
|
"""
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
import copy
|
|
import hashlib
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import tarfile
|
|
import tempfile
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location('release_artifact_subject', Path(__file__).resolve().parents[2] / 'deploy/release-artifact.py')
|
|
subject = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(subject)
|
|
|
|
|
|
class ReleaseVerifier(unittest.TestCase):
|
|
def setUp(self):
|
|
self.directory = tempfile.TemporaryDirectory(prefix='videoreg-release-review-fixture-')
|
|
self.root = Path(self.directory.name)
|
|
self.root_patch = patch.object(subject, 'ROOT', self.root)
|
|
self.root_patch.start()
|
|
self.source_revision = '1' * 40
|
|
self.delivery_revision = '2' * 40
|
|
self.blobs = {}
|
|
self.app_rows = []
|
|
app_path = 'web-front/src/app/page.tsx'
|
|
self.write(app_path, b'fixture source')
|
|
self.blobs[(self.source_revision, app_path)] = b'fixture source'
|
|
self.app_rows.append(dict(path=app_path, sha256=subject.sha(b'fixture source')))
|
|
self.deploy_rows = []
|
|
for path in subject.DEPLOY_FILES:
|
|
body = ('fixture deployment ' + path).encode()
|
|
self.write(path, body)
|
|
self.blobs[(self.delivery_revision, path)] = body
|
|
self.deploy_rows.append(dict(path=path, sha256=subject.sha(body)))
|
|
self.git_patch = patch.object(subject.subprocess, 'run', self.git_read)
|
|
self.git_patch.start()
|
|
self.payload = {'index.html': b'<main>reviewed fixture</main>', 'nested/a.txt': b'nested fixture'}
|
|
self.rows = [dict(path=path, sha256=subject.sha(data), bytes=len(data)) for path, data in sorted(self.payload.items(), key=lambda item: Path(item[0]))]
|
|
self.manifest = dict(schemaVersion=1, scope='reviewed-corpus-only', historicalRestorationComplete=False,
|
|
publicationDecision='ALLOW_LIMITED_PUBLICATION', sourceCommit=self.source_revision,
|
|
appSourceManifestSha256=subject.hash_manifest(self.app_rows), appSourceFiles=self.app_rows,
|
|
deploymentSourceCommit=self.delivery_revision, deploymentFiles=self.deploy_rows,
|
|
exportManifestSha256=subject.hash_manifest(self.rows), files=self.rows,
|
|
archivePath='release/export.tar', archiveSha256='', roleApprovals=[])
|
|
self.write('release/export.tar', self.archive_bytes())
|
|
self.manifest['archiveSha256'] = subject.sha((self.root / 'release/export.tar').read_bytes())
|
|
for role in sorted(subject.ROLES):
|
|
report_path = 'reviews/' + role + '.md'
|
|
self.write(report_path, ('fixture report: ' + role).encode())
|
|
approval = dict(role=role, agent='/fixture/' + role, decision='ALLOW_LIMITED_PUBLICATION',
|
|
fullHistoricalRestorationAccepted=False, candidateSourceCommit=self.source_revision,
|
|
appSourceManifestSha256=self.manifest['appSourceManifestSha256'],
|
|
exportManifestSha256=self.manifest['exportManifestSha256'],
|
|
reportPath=report_path, reportSha256=subject.sha((self.root/report_path).read_bytes()))
|
|
path = 'approvals/' + role + '.json'
|
|
self.write(path, json.dumps(approval).encode())
|
|
self.manifest['roleApprovals'].append(dict(path=path, sha256=subject.sha((self.root/path).read_bytes())))
|
|
self.save()
|
|
|
|
def tearDown(self):
|
|
self.git_patch.stop()
|
|
self.root_patch.stop()
|
|
self.directory.cleanup()
|
|
|
|
def write(self, relative, data):
|
|
path = self.root / relative
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(data)
|
|
|
|
def git_read(self, args, **kwargs):
|
|
assert args[:2] == ['git', 'show'], args
|
|
revision, path = args[2].split(':', 1)
|
|
body = self.blobs.get((revision, path))
|
|
return SimpleNamespace(returncode=0 if body is not None else 1, stdout=body or b'', stderr=b'fixture git source unavailable' if body is None else b'')
|
|
|
|
def save(self):
|
|
self.write('release/manifest.json', json.dumps(self.manifest).encode())
|
|
|
|
def edit_approval(self, index, **changes):
|
|
ref = self.manifest['roleApprovals'][index]
|
|
path = self.root / ref['path']
|
|
approval = json.loads(path.read_text())
|
|
approval.update(changes)
|
|
path.write_text(json.dumps(approval))
|
|
ref['sha256'] = subject.sha(path.read_bytes())
|
|
self.save()
|
|
|
|
def checked(self, approvals=True):
|
|
self.save()
|
|
return subject.checked_manifest('release/manifest.json', approvals)
|
|
|
|
def archive_bytes(self, members=None):
|
|
data = io.BytesIO()
|
|
with tarfile.open(fileobj=data, mode='w') as archive:
|
|
for name, body, kind in members or [(path, value, tarfile.REGTYPE) for path, value in self.payload.items()]:
|
|
info = tarfile.TarInfo(name)
|
|
info.type = kind
|
|
if kind == tarfile.REGTYPE:
|
|
info.size = len(body)
|
|
archive.addfile(info, io.BytesIO(body))
|
|
else:
|
|
if kind in (tarfile.SYMTYPE, tarfile.LNKTYPE):
|
|
info.linkname = '../fixture-outside'
|
|
archive.addfile(info)
|
|
return data.getvalue()
|
|
|
|
def tar(self, data, expected=None, extract=None):
|
|
return subject.tar_rows(io.BytesIO(data), expected or {row['path']: row for row in self.rows}, extract)
|
|
|
|
def test_valid_five_roles_and_separate_delivery_revision(self):
|
|
self.assertEqual(self.checked()['deploymentSourceCommit'], self.delivery_revision)
|
|
|
|
def test_pending_five_decisions_rejected_for_publication(self):
|
|
self.manifest['publicationDecision'] = 'PENDING_FIVE_SCOPED_DECISIONS'
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_prepare_only_does_not_invent_publication_approval(self):
|
|
self.manifest['publicationDecision'] = 'PENDING_FIVE_SCOPED_DECISIONS'
|
|
self.manifest['roleApprovals'] = []
|
|
self.assertEqual(self.checked(False)['publicationDecision'], 'PENDING_FIVE_SCOPED_DECISIONS')
|
|
|
|
def test_missing_role_rejected(self):
|
|
self.manifest['roleApprovals'].pop()
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_duplicate_agent_rejected(self):
|
|
self.edit_approval(0, agent='/fixture/shared')
|
|
self.edit_approval(1, agent='/fixture/shared')
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_empty_agent_rejected(self):
|
|
self.edit_approval(0, agent='')
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_veto_rejected(self):
|
|
self.edit_approval(0, decision='BLOCK')
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_full_history_claim_rejected(self):
|
|
self.edit_approval(0, fullHistoricalRestorationAccepted=True)
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_wrong_candidate_revision_rejected(self):
|
|
self.edit_approval(0, candidateSourceCommit='3'*40)
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_wrong_candidate_export_rejected(self):
|
|
self.edit_approval(0, exportManifestSha256='3'*64)
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_wrong_candidate_app_manifest_rejected(self):
|
|
self.edit_approval(0, appSourceManifestSha256='3'*64)
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_changed_report_rejected(self):
|
|
self.write('reviews/designer.md', b'changed fixture report')
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_changed_working_source_rejected(self):
|
|
self.write(self.app_rows[0]['path'], b'changed fixture source')
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_empty_app_source_set_rejected(self):
|
|
self.manifest['appSourceFiles'] = []
|
|
self.manifest['appSourceManifestSha256'] = subject.hash_manifest([])
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_empty_deployment_set_rejected(self):
|
|
self.manifest['deploymentFiles'] = []
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_omitted_deployment_file_rejected(self):
|
|
self.manifest['deploymentFiles'].pop()
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_duplicate_deployment_file_rejected(self):
|
|
self.manifest['deploymentFiles'].append(copy.deepcopy(self.manifest['deploymentFiles'][0]))
|
|
with self.assertRaises(ValueError): self.checked()
|
|
|
|
def test_valid_extract_into_fresh_temporary_directory(self):
|
|
output = self.root / 'fixture-extraction'
|
|
output.mkdir()
|
|
self.assertEqual(self.tar(self.archive_bytes(), extract=output), self.rows)
|
|
self.assertEqual((output/'index.html').read_bytes(), self.payload['index.html'])
|
|
|
|
def test_changed_archive_file_rejected(self):
|
|
members = [('index.html', b'changed', tarfile.REGTYPE), ('nested/a.txt', self.payload['nested/a.txt'], tarfile.REGTYPE)]
|
|
with self.assertRaises(ValueError): self.tar(self.archive_bytes(members))
|
|
|
|
def test_missing_archive_file_rejected(self):
|
|
with self.assertRaises(ValueError): self.tar(self.archive_bytes([('index.html', self.payload['index.html'], tarfile.REGTYPE)]))
|
|
|
|
def test_unexpected_archive_file_rejected(self):
|
|
with self.assertRaises(ValueError): self.tar(self.archive_bytes([('extra.txt', b'fixture', tarfile.REGTYPE)]))
|
|
|
|
def test_duplicate_archive_file_rejected(self):
|
|
members = [('index.html', self.payload['index.html'], tarfile.REGTYPE)]*2
|
|
with self.assertRaises(ValueError): self.tar(self.archive_bytes(members))
|
|
|
|
def test_unsafe_tar_member_types_and_names_rejected(self):
|
|
for name, kind in [('../fixture.txt', tarfile.REGTYPE), ('/fixture.txt', tarfile.REGTYPE), ('fixture-link', tarfile.SYMTYPE), ('fixture-hardlink', tarfile.LNKTYPE), ('fixture-pipe', tarfile.FIFOTYPE)]:
|
|
with self.subTest(name=name, kind=kind):
|
|
with self.assertRaises(ValueError): self.tar(self.archive_bytes([(name, b'fixture', kind)]))
|
|
|
|
def test_oversized_archive_member_rejected_before_read(self):
|
|
info = tarfile.TarInfo('big.bin'); info.size = 26*1024*1024
|
|
stream = info.tobuf() + b'\0'*1024
|
|
with self.assertRaises(ValueError): self.tar(stream, {'big.bin':dict(path='big.bin',bytes=info.size,sha256=subject.sha(b''))})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main(verbosity=2)
|