"""Tests for the release backfill scripts in .github/scripts, run against a stub gh on PATH. The stub serves a release whose assets are described by RELEASE_ASSETS (a JSON object of name to content) and fails every download when GH_DOWNLOAD_FAILS is set, so a test can tell a release with nothing attached from one whose assets could not be fetched. A stub cosign accepts every bundle unless COSIGN_REJECTS is set, and logs its arguments. """ import hashlib import json import os import pathlib import subprocess import tempfile import textwrap import unittest SCRIPTS = pathlib.Path(__file__).resolve().parents[1] / 'scripts' EXISTING = SCRIPTS / 'release-binaries-existing.sh' SUMS = SCRIPTS / 'release-binaries-sums.sh' STUB_GH = textwrap.dedent('''\ #!/usr/bin/env python3 import json, os, pathlib, sys assets = json.loads(os.environ.get('RELEASE_ASSETS', '{}')) args = sys.argv[1:] with open(os.environ['GH_CALLS'], 'a') as log: log.write(' '.join(args) + '\\n') if args[:2] == ['release', 'view']: print('\\n'.join(assets)) sys.exit(0) if args[:2] == ['release', 'download']: if os.environ.get('GH_DOWNLOAD_FAILS'): print('HTTP 502: Bad Gateway', file=sys.stderr) sys.exit(1) target = pathlib.Path(args[args.index('-D') + 1]) target.mkdir(parents=True, exist_ok=True) name = args[args.index('-p') + 1] if name not in assets: print('no assets match the file pattern', file=sys.stderr) sys.exit(1) (target / name).write_text(assets[name]) sys.exit(0) sys.exit(2) ''') STUB_COSIGN = textwrap.dedent('''\ #!/usr/bin/env python3 import os, sys with open(os.environ['COSIGN_CALLS'], 'a') as log: log.write(' '.join(sys.argv[1:]) + '\\n') sys.exit(1 if os.environ.get('COSIGN_REJECTS') else 0) ''') def sha256(text): return hashlib.sha256(text.encode()).hexdigest() class ReleaseScriptsTest(unittest.TestCase): def setUp(self): self.tmp = pathlib.Path(tempfile.mkdtemp()) bin_dir = self.tmp / 'bin' bin_dir.mkdir() gh = bin_dir / 'gh' gh.write_text(STUB_GH) gh.chmod(0o755) cosign = bin_dir / 'cosign' cosign.write_text(STUB_COSIGN) cosign.chmod(0o755) self.calls = self.tmp / 'calls.log' self.calls.touch() self.cosign_calls = self.tmp / 'cosign.log' self.cosign_calls.touch() self.env = dict(os.environ, PATH=f'{bin_dir}:{os.environ["PATH"]}', GH_CALLS=str(self.calls), COSIGN_CALLS=str(self.cosign_calls)) def existing(self, assets, download_fails=False): env = dict(self.env, RELEASE_ASSETS=json.dumps(assets)) if download_fails: env['GH_DOWNLOAD_FAILS'] = '1' target = self.tmp / 'existing' result = subprocess.run([str(EXISTING), '2.5.0', str(target)], env=env, capture_output=True, text=True) return result, target def test_a_failed_download_stops_the_backfill(self): result, _ = self.existing({'floci-aws-linux-amd64': 'bin', 'sha256sums.txt': 'sums'}, download_fails=True) self.assertNotEqual(0, result.returncode, result.stdout + result.stderr) def test_a_release_with_nothing_attached_downloads_nothing(self): result, target = self.existing({}) self.assertEqual(0, result.returncode, result.stderr) self.assertEqual([], sorted(p.name for p in target.iterdir())) self.assertNotIn('release download', self.calls.read_text()) def test_every_attached_binary_the_sums_file_and_its_bundle_are_downloaded_but_not_binary_bundles(self): assets = {'floci-aws-linux-amd64': 'a', 'floci-aws-linux-amd64.sigstore.json': 'bundle', 'sha256sums.txt': 'sums', 'sha256sums.txt.sigstore.json': 'bundle', 'notes.txt': 'x'} result, target = self.existing(assets) self.assertEqual(0, result.returncode, result.stderr) self.assertEqual(['floci-aws-linux-amd64', 'sha256sums.txt', 'sha256sums.txt.sigstore.json'], sorted(p.name for p in target.iterdir())) def sums(self, existing, dist, cosign_rejects=False): existing_dir, dist_dir = self.tmp / 'e', self.tmp / 'd' for directory, files in ((existing_dir, existing), (dist_dir, dist)): directory.mkdir() for name, content in files.items(): (directory / name).write_text(content) env = dict(self.env, COSIGN_REJECTS='1') if cosign_rejects else self.env result = subprocess.run([str(SUMS), str(existing_dir), str(dist_dir)], env=env, capture_output=True, text=True) return result, dist_dir / 'sha256sums.txt' def test_the_sums_keep_the_lines_of_attached_binaries_and_add_the_new_ones(self): listed = f'{sha256("old")} floci-aws-linux-amd64\n' result, out = self.sums({'floci-aws-linux-amd64': 'old', 'sha256sums.txt': listed, 'sha256sums.txt.sigstore.json': 'bundle'}, {'floci-aws-darwin-amd64': 'new'}) self.assertEqual(0, result.returncode, result.stderr) self.assertIn("--certificate-identity-regexp ^https://github.com/floci-io/floci/.github/workflows/release.yml@", self.cosign_calls.read_text()) self.assertEqual(f'{sha256("new")} floci-aws-darwin-amd64\n{sha256("old")} floci-aws-linux-amd64\n', out.read_text()) def test_an_attached_binary_that_does_not_match_its_line_stops_the_publish(self): listed = f'{sha256("old")} floci-aws-linux-amd64\n' result, _ = self.sums({'floci-aws-linux-amd64': 'tampered', 'sha256sums.txt': listed, 'sha256sums.txt.sigstore.json': 'bundle'}, {'floci-aws-darwin-amd64': 'new'}) self.assertNotEqual(0, result.returncode) def test_sums_whose_signature_does_not_verify_stop_the_publish(self): listed = f'{sha256("old")} floci-aws-linux-amd64\n' result, _ = self.sums({'floci-aws-linux-amd64': 'old', 'sha256sums.txt': listed, 'sha256sums.txt.sigstore.json': 'bundle'}, {'floci-aws-darwin-amd64': 'new'}, cosign_rejects=True) self.assertNotEqual(0, result.returncode) def test_sums_without_their_bundle_stop_the_publish(self): listed = f'{sha256("old")} floci-aws-linux-amd64\n' result, _ = self.sums({'floci-aws-linux-amd64': 'old', 'sha256sums.txt': listed}, {'floci-aws-darwin-amd64': 'new'}) self.assertNotEqual(0, result.returncode) if __name__ == '__main__': unittest.main()