#!/usr/bin/env python3 """Exercise publication, rejection and three-version GC against real OSTree.""" import importlib.util import json from pathlib import Path import tempfile import subprocess spec = importlib.util.spec_from_file_location('publisher', Path(__file__).parents[1] / 'pipeline/publish-hardware.py') m = importlib.util.module_from_spec(spec) spec.loader.exec_module(m) with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) repo = root / 'input' m.ostree(repo, 'init', '--mode=archive') payload = root / 'payload' payload.mkdir() public = root / 'public' first = [] for generation in range(4): records = [] for variant in ('core', 'gnome'): (payload / 'marker').write_text(variant + str(generation)) layer = m.ostree(repo, 'commit', '--orphan', '--branch=layer-' + variant, '--tree=dir=' + str(payload)) purpose = None if variant == 'gnome': (payload / 'marker').write_text('purpose-' + str(generation)) purpose = m.ostree(repo, 'commit', '--orphan', '--branch=purpose', '--tree=dir=' + str(payload)) commit = m.ostree(repo, 'commit', '--orphan', '--branch=' + variant, '--tree=dir=' + str(payload), '--add-metadata-string=archipelago.layer-core=' + layer, *(['--add-metadata-string=archipelago.layer-purpose=' + purpose] if purpose else [])) if generation == 0: first.extend([commit, layer]) iso = root / (variant + '.iso') iso.write_text('fixture ISO ' + commit) evidence = root / (variant + '.log') evidence.write_text('fixture acceptance ' + commit) r = dict(variant=variant, version=str(generation), repo=str(repo), iso=str(iso), sha256=m.digest(iso), image_commit=commit, layer_commit=layer, acceptance=str(root / (variant + '.json'))) if purpose: r['purpose_layer_commit'] = purpose Path(r['acceptance']).write_text(json.dumps(r | dict(scope='hardware-test', accepted=True, checks=[dict(path=str(evidence), sha256=m.digest(evidence))]))) records.append(r) m.publish(public, records) current = (public / 'current').resolve() assert m.ostree(current / 'ostree/layers', 'rev-parse', 'purpose/desktop/gnome') == records[1]['purpose_layer_commit'] purpose_refs = [r for r in m.ostree(current / 'ostree/layers', 'refs').splitlines() if r.startswith('history/purpose/gnome/')] assert len(purpose_refs) == min(generation + 1, 3) assert len(list((current / 'iso').glob('*.iso'))) == 2 api = json.loads((current / 'v1/releases.json').read_text()) assert len(api['releases']) == 2 * min(generation + 1, 3) assert sum(r['current'] for r in api['releases']) == 2 for release in api['releases']: artifact = current / release['url'].removeprefix('/') assert artifact.is_file() and m.digest(artifact) == release['sha256'] detail = current / 'v1/releases' / release['variant'] / (release['version'] + '.json') assert json.loads(detail.read_text())['release'] == release if generation == 3: assert not (current / 'iso/core/0').exists() assert len(list((public / '.generations').iterdir())) == 1 history = json.loads((current / 'history.json').read_text()) assert all(len(entries) == min(generation + 1, 3) for entries in history.values()) for kind in ('images', 'layers'): target = current / 'ostree' / kind assert m.ostree(target, 'rev-parse', 'hardware-test/core') == records[0]['image_commit' if kind == 'images' else 'layer_commit'] before = (public / 'current').readlink() wrong_purpose = [dict(r) for r in records] wrong_purpose[1]['purpose_layer_commit'] = records[0]['layer_commit'] try: m.publish(public, wrong_purpose) except ValueError: pass else: raise AssertionError('Mismatched Purpose acceptance was published') assert (public / 'current').readlink() == before real_ostree = m.ostree def fail_summary(repo, *args): if args[0] == 'summary': raise RuntimeError('injected summary failure after staging') return real_ostree(repo, *args) m.ostree = fail_summary try: m.publish(public, records) except RuntimeError: pass else: raise AssertionError('Injected failure was ignored') finally: m.ostree = real_ostree assert (public / 'current').readlink() == before assert len(list((public / '.generations').iterdir())) == 1 Path(records[0]['iso']).write_text('corrupt') try: m.publish(public, records) except ValueError: pass else: raise AssertionError('Corrupt ISO was published') assert (public / 'current').readlink() == before for kind in ('images', 'layers'): for commit in first: assert not (current / 'ostree' / kind / 'objects' / commit[:2] / (commit[2:] + '.commit')).exists() print('PUBLICATION_RETENTION_FAILURE_PRESERVATION_PASS') home = root / 'gpg' home.mkdir(mode=0o700) subprocess.run(['gpg', '--homedir', str(home), '--batch', '--pinentry-mode', 'loopback', '--passphrase', '', '--quick-generate-key', 'Archipelago disposable test', 'ed25519', 'sign', '1d'], check=True, capture_output=True) keys = subprocess.check_output(['gpg', '--homedir', str(home), '--with-colons', '--list-keys'], text=True) key = next(line.split(':')[9] for line in keys.splitlines() if line.startswith('fpr:')) Path(records[0]['iso']).write_text('fixture ISO ' + records[0]['image_commit']) m.publish(public, records, home, key) m.publish(public, records, home, key, url_prefix='/offline/os', refs={'core': 'lab/core', 'gnome': 'lab/desktop'}) current = (public / 'current').resolve() catalog = json.loads((current / 'v1/releases.json').read_text()) assert all(r['url'].startswith('/offline/os/iso/') for r in catalog['releases']) for kind in ('images', 'layers'): refs = m.ostree(current / 'ostree' / kind, 'refs').splitlines() assert 'lab/core' in refs and 'lab/desktop' in refs assert not any(ref.startswith('hardware-test/') for ref in refs) assert (current / 'ostree/images/summary.sig').is_file() public_key = root / 'test.asc' public_key.write_bytes(subprocess.check_output(['gpg', '--homedir', str(home), '--export', key])) client = root / 'client' m.ostree(client, 'init', '--mode=archive') m.ostree(client, 'remote', 'add', '--gpg-import=' + str(public_key), '--set=gpg-verify-summary=true', 'test', (current / 'ostree/images').as_uri()) refs = m.ostree(client, 'remote', 'summary', 'test') print(refs) m.ostree(client, 'pull', 'test', records[0]['image_commit']) (current / 'ostree/images/summary.sig').write_bytes(b'invalid signature') try: m.ostree(client, 'remote', 'summary', 'test') except subprocess.CalledProcessError: pass else: raise AssertionError('Invalid summary signature accepted') print('SIGNED_PUBLICATION_PULL_PASS') previous = (public / 'current').resolve() try: m.publish(public, records, home, key, channel='stable') except ValueError: pass else: raise AssertionError('Unapproved stable publication accepted') assert (public / 'current').resolve() == previous exception = dict(channel='stable', decision='fixture', reason='Explicit test exception', limitations=['No production evidence'], candidates=[{k:r[k] for k in ('variant', 'version', 'sha256', 'image_commit', 'layer_commit')} for r in records]) m.publish(public, records, home, key, channel='stable', exception=exception, refs={'core':'stable/core','gnome':'stable/gnome'}, compatibility={'core':['hardware-test/core'], 'gnome':['hardware-test/gnome']}) current = (public / 'current').resolve() catalog = json.loads((current / 'v1/releases.json').read_text()) assert all(r['channel'] == 'stable' and r['release_exception']['decision'] == 'fixture' for r in catalog['releases'] if r['current']) assert all(r['channel'] == 'hardware-test' for r in catalog['releases'] if not r['current']) for kind in ('images', 'layers'): repo = current / 'ostree' / kind for variant in ('core', 'gnome'): assert m.ostree(repo, 'rev-parse', 'stable/' + variant) == m.ostree(repo, 'rev-parse', 'hardware-test/' + variant) exception['candidates'][0]['sha256'] = '0' * 64 try: m.publish(public, records, home, key, channel='stable', exception=exception) except ValueError: pass else: raise AssertionError('Exception accepted for different bytes') assert (public / 'current').resolve() == current print('SCOPED_STABLE_PROMOTION_COMPATIBILITY_PASS')