Files
floci/.github/ci/test_release_images.py
T

161 lines
7.1 KiB
Python

"""Tests for .github/scripts/release-images-mirror.sh and the image-free plan, run against stubs on PATH.
The stub docker answers `buildx imagetools inspect` from IMAGES (a JSON object of reference to
digest; a reference it lacks is not found) and logs every `buildx imagetools create`. It fails the
first COPY_FAILS creates, so a test can tell a retried copy from one that never succeeds.
"""
import json
import os
import pathlib
import subprocess
import tempfile
import textwrap
import unittest
SCRIPTS = pathlib.Path(__file__).resolve().parents[1] / 'scripts'
MIRROR = SCRIPTS / 'release-images-mirror.sh'
PLAN = SCRIPTS / 'release-binaries-plan.sh'
STUB_DOCKER = textwrap.dedent('''\
#!/usr/bin/env python3
import json, os, pathlib, sys
args = sys.argv[1:]
if args[:3] == ['buildx', 'imagetools', 'inspect']:
images = json.loads(os.environ.get('IMAGES', '{}'))
if args[3] not in images:
print('not found', file=sys.stderr)
sys.exit(1)
print(images[args[3]])
sys.exit(0)
if args[:3] == ['buildx', 'imagetools', 'create']:
with open(os.environ['DOCKER_CALLS'], 'a') as log:
log.write(' '.join(args[3:]) + '\\n')
counter = pathlib.Path(os.environ['DOCKER_CALLS'] + '.failed')
failed = int(counter.read_text()) if counter.exists() else 0
if failed < int(os.environ.get('COPY_FAILS', '0')):
counter.write_text(str(failed + 1))
print('unknown: The upload with id does not exist', file=sys.stderr)
sys.exit(1)
sys.exit(0)
sys.exit(2)
''')
STUB_GH = textwrap.dedent('''\
#!/usr/bin/env python3
import json, os, sys
if sys.argv[1:3] == ['release', 'view']:
print('\\n'.join(json.loads(os.environ.get('RELEASE_ASSETS', '[]'))))
sys.exit(0)
sys.exit(2)
''')
D = {v: 'sha256:' + (c * 64) for v, c in (('native', 'a'), ('baseline', 'b'), ('compat', 'c'))}
RELEASE = {
'floci/floci:2.6.0': D['native'],
'floci/floci:2.6.0-baseline': D['baseline'],
'floci/floci:2.6.0-compat': D['compat'],
}
class ReleaseImagesTest(unittest.TestCase):
def setUp(self):
self.tmp = pathlib.Path(tempfile.mkdtemp())
bin_dir = self.tmp / 'bin'
bin_dir.mkdir()
for name, body in (('docker', STUB_DOCKER), ('gh', STUB_GH)):
stub = bin_dir / name
stub.write_text(body)
stub.chmod(0o755)
self.calls = self.tmp / 'docker.log'
self.calls.touch()
self.out = self.tmp / 'out'
self.out.touch()
self.env = dict(os.environ, PATH=f'{bin_dir}:{os.environ["PATH"]}', DOCKER_CALLS=str(self.calls),
GITHUB_OUTPUT=str(self.out), RETRY_DELAYS='0 0')
def run_mirror(self, mode, images, digests=None, copy_fails=0, version='2.6.0'):
digests = dict(D, **(digests or {}))
env = dict(self.env, IMAGES=json.dumps(images), COPY_FAILS=str(copy_fails), VERSION=version,
NATIVE_DIGEST=digests['native'], BASELINE_DIGEST=digests['baseline'],
COMPAT_DIGEST=digests['compat'])
return subprocess.run([str(MIRROR), mode], env=env, capture_output=True, text=True)
def copies(self):
return self.calls.read_text().splitlines()
def outputs(self):
return dict(line.split('=', 1) for line in self.out.read_text().splitlines())
def test_verify_passes_when_docker_hub_serves_the_digests_the_release_run_built(self):
result = self.run_mirror('verify', RELEASE)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(self.outputs(), {'verified': 'true'})
self.assertEqual(self.copies(), [])
def test_verify_refuses_a_docker_hub_tag_moved_to_another_image(self):
images = dict(RELEASE, **{'floci/floci:2.6.0': 'sha256:' + 'f' * 64})
result = self.run_mirror('verify', images)
self.assertNotEqual(result.returncode, 0)
self.assertIn('refusing to copy or sign it', result.stderr)
self.assertNotIn('verified', self.outputs())
def test_verify_refuses_a_variant_missing_from_docker_hub(self):
images = {k: v for k, v in RELEASE.items() if not k.endswith('-compat')}
result = self.run_mirror('verify', images)
self.assertNotEqual(result.returncode, 0)
self.assertIn('2.6.0-compat is not on Docker Hub', result.stderr)
def test_a_malformed_or_missing_digest_is_refused_before_anything_is_read(self):
for bad in ('', 'sha256:short', 'latest'):
with self.subTest(digest=bad):
result = self.run_mirror('copy', RELEASE, digests={'compat': bad})
self.assertNotEqual(result.returncode, 0)
self.assertIn('is not a sha256 digest', result.stderr)
self.assertEqual(self.copies(), [])
def test_copy_points_each_version_tag_at_the_given_digest(self):
result = self.run_mirror('copy', RELEASE)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(self.copies(), [
f'--tag public.ecr.aws/floci/floci:2.6.0 floci/floci@{D["native"]}',
f'--tag public.ecr.aws/floci/floci:2.6.0-baseline floci/floci@{D["baseline"]}',
f'--tag public.ecr.aws/floci/floci:2.6.0-compat floci/floci@{D["compat"]}',
])
def test_latest_moves_only_where_docker_hub_latest_is_this_release_and_after_every_version_tag(self):
images = dict(RELEASE, **{'floci/floci:latest': D['native'], 'floci/floci:latest-compat': D['compat'],
'floci/floci:latest-baseline': 'sha256:' + 'e' * 64})
result = self.run_mirror('copy', images)
self.assertEqual(result.returncode, 0, result.stderr)
tags = [line.split()[1].rsplit(':', 1)[1] for line in self.copies()]
self.assertEqual(tags, ['2.6.0', '2.6.0-baseline', '2.6.0-compat', 'latest', 'latest-compat'])
def test_a_dropped_upload_is_retried(self):
result = self.run_mirror('copy', RELEASE, copy_fails=2)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(len(self.copies()), 5)
self.assertIn('retrying', result.stderr)
def test_a_copy_that_keeps_failing_fails_the_run(self):
result = self.run_mirror('copy', RELEASE, copy_fails=100)
self.assertNotEqual(result.returncode, 0)
self.assertIn('after 3 attempts', result.stderr)
def test_a_version_that_is_not_a_release_is_refused(self):
result = self.run_mirror('verify', RELEASE, version='nightly')
self.assertNotEqual(result.returncode, 0)
self.assertEqual(self.copies(), [])
def test_the_plan_builds_no_binaries_for_an_images_only_dispatch(self):
env = dict(self.env, EVENT='workflow_dispatch', TAG='2.6.0', PLATFORMS='none', REF='refs/heads/main',
RELEASE_ASSETS=json.dumps(['floci-aws-linux-amd64']))
result = subprocess.run([str(PLAN)], env=env, capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(self.outputs()['any'], 'false')
self.assertEqual(self.outputs()['standalone'], '')
if __name__ == '__main__':
unittest.main()