infrastructure/tests/flatpak-cache.py

46 lines
2 KiB
Python

#!/usr/bin/env python3
"""A failed refresh retains a verified seed; corruption never becomes fallback."""
import hashlib
import json
import os
from pathlib import Path
import subprocess
import tempfile
script = Path(__file__).parents[1] / 'pipeline/flatpak-cache.sh'
for corrupt in (False, True):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
cache = root / 'cache'
seed = cache / 'exports/known-good'
seed.mkdir(parents=True)
(cache / 'current').symlink_to('exports/known-good')
policy = '{"applications": []}\n'
(seed / 'policy.json').write_text(policy)
(seed / 'SHA256SUMS').write_text(hashlib.sha256(policy.encode()).hexdigest() + ' policy.json\n')
if corrupt:
(seed / 'policy.json').write_text('corrupted')
source = root / 'src'
(source / 'purposes/proposals').mkdir(parents=True)
(source / 'purposes/proposals/gnome-flatpak-apps.json').write_text(policy)
tools = cache / 'tools/root.x86_64/etc'
(tools / 'pacman.d').mkdir(parents=True)
(tools / 'pacman.conf').write_text('[options]\n')
commands = root / 'bin'
commands.mkdir()
timeout = commands / 'timeout'
timeout.write_text('#!/bin/sh\nexit 124\n')
timeout.chmod(0o755)
result = subprocess.run(['bash', script, source, cache, root / 'bootstrap', root / 'packages', '1'],
env=dict(os.environ, PATH=str(commands) + ':' + os.environ['PATH']),
capture_output=True, text=True)
assert (cache / 'current').resolve() == seed
if corrupt:
assert result.returncode != 0
assert not (cache / 'status.json').exists()
else:
assert result.returncode == 0, result.stderr
status = json.loads((cache / 'status.json').read_text())
assert status['state'] == 'cached-after-refresh-failure'
assert status['refresh_exit_code'] == 124
print('FLATPAK_CACHE_VERIFIED_FALLBACK_PASS')