99 lines
4.3 KiB
Python
99 lines
4.3 KiB
Python
import importlib.util
|
|
import io
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
import unittest
|
|
import urllib.parse
|
|
import zipfile
|
|
|
|
from test_release_artifacts import fixture
|
|
|
|
spec = importlib.util.spec_from_file_location('promote_release', Path(__file__).with_name('promote-release.py'))
|
|
promote = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(promote)
|
|
|
|
|
|
class PromotionTests(unittest.TestCase):
|
|
def scenario(self, *, extra=False, corrupt_remote=False, corrupt_artifact=False, resume=False, wrong_commit=False):
|
|
contents = fixture()
|
|
if corrupt_artifact:
|
|
contents['Cutdex-win-x64-Setup.exe'] += b'corrupt'
|
|
archive = io.BytesIO()
|
|
with zipfile.ZipFile(archive, 'w') as bundle:
|
|
for name, body in contents.items():
|
|
bundle.writestr(name, body)
|
|
stored = dict(contents) if resume else {}
|
|
if extra:
|
|
stored['stale.zip'] = b'stale'
|
|
calls = []
|
|
|
|
def assets():
|
|
return [{'name': name} for name in stored]
|
|
|
|
def request(url, data=None, method=None, content_type=None):
|
|
calls.append((method, url))
|
|
if url.endswith('/actions/runs?limit=100'):
|
|
return {'workflow_runs': [{'head_sha': 'other' if wrong_commit else 'commit',
|
|
'path': '.gitea/workflows/ci.yml@refs/heads/main', 'conclusion': 'success', 'id': 1}]}
|
|
if url.endswith('/actions/runs/1/artifacts'):
|
|
return {'artifacts': [{'name': 'cutdex-release-packages', 'id': 2}]}
|
|
if url.endswith('/actions/artifacts/2/zip'):
|
|
return archive.getvalue()
|
|
if url.endswith('/releases?limit=100'):
|
|
return [{'id': 3, 'tag_name': 'v0.1.0', 'draft': True}] if resume else []
|
|
if url.endswith('/releases') and method == 'POST':
|
|
self.assertTrue(data['draft'])
|
|
return {'id': 3}
|
|
if url.endswith('/releases/3/assets'):
|
|
return assets()
|
|
if '/assets?name=' in url and method == 'POST':
|
|
name = urllib.parse.parse_qs(urllib.parse.urlparse(url).query)['name'][0]
|
|
boundary = content_type.split('boundary=')[1].encode()
|
|
stored[name] = data.split(b'\r\n\r\n', 1)[1].rsplit(b'\r\n--' + boundary, 1)[0]
|
|
return {'name': name}
|
|
if url.endswith('/releases/3') and method == 'PATCH':
|
|
self.assertEqual(data, {'draft': False, 'prerelease': False})
|
|
return {}
|
|
raise AssertionError('Unexpected request: ' + url)
|
|
|
|
def download(asset):
|
|
body = stored[asset['name']]
|
|
return body + b'corrupt' if corrupt_remote else body
|
|
|
|
environment = {'GITEA_REPOSITORY': 'owner/cutdex', 'GITEA_SHA': 'commit', 'RELEASE_TAG': 'v0.1.0'}
|
|
with patch.dict('os.environ', environment), patch.object(promote, 'request', side_effect=request), \
|
|
patch.object(promote, 'download', side_effect=download), \
|
|
patch.object(promote, 'app_versions', return_value=('0.1.0', '0.1.0-build.2')):
|
|
if extra or corrupt_remote or corrupt_artifact or wrong_commit:
|
|
with self.assertRaises(ValueError):
|
|
promote.main()
|
|
self.assertFalse(any(method == 'PATCH' for method, _ in calls))
|
|
if corrupt_artifact or wrong_commit:
|
|
self.assertFalse(any(method == 'POST' for method, _ in calls))
|
|
else:
|
|
promote.main()
|
|
self.assertEqual(calls[-1][0], 'PATCH')
|
|
self.assertEqual(set(stored), set(contents))
|
|
if resume:
|
|
self.assertFalse(any(method == 'POST' for method, _ in calls))
|
|
|
|
def test_complete_draft_is_verified_before_publication(self):
|
|
self.scenario()
|
|
|
|
def test_matching_draft_can_be_resumed_without_replacing_assets(self):
|
|
self.scenario(resume=True)
|
|
|
|
def test_stale_or_corrupt_remote_attachment_keeps_release_draft(self):
|
|
self.scenario(extra=True)
|
|
self.scenario(corrupt_remote=True)
|
|
|
|
def test_tampered_artifact_never_creates_release(self):
|
|
self.scenario(corrupt_artifact=True)
|
|
|
|
def test_successful_ci_for_other_commit_cannot_publish(self):
|
|
self.scenario(wrong_commit=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|