Files
Heimdall/scripts/ci/test_ci.py
T

84 lines
3.7 KiB
Python
Raw Normal View History

"""Contract tests for security gates and draft release uploads, without network."""
import contextlib
import io
import json
import os
from pathlib import Path
import runpy
import subprocess
import sys
import tempfile
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parent
class AdvisoryTests(unittest.TestCase):
def check(self, data):
with tempfile.TemporaryDirectory() as temp:
path = Path(temp)/'report.json'
path.write_text(json.dumps(data))
return subprocess.run([sys.executable, str(ROOT/'check-advisories.py'), str(path)],
capture_output=True, text=True).returncode
def test_high_and_critical_transitive_advisories_fail(self):
for severity in ('High', 'Critical'):
report = {'version': 1, 'projects': [{'frameworks': [{'transitivePackages': [{
'id': 'Example', 'vulnerabilities': [{'severity': severity, 'advisoryurl': 'https://example.test'}]
}]}]}]}
self.assertNotEqual(self.check(report), 0)
def test_clean_report_passes(self):
self.assertEqual(self.check({'version': 1, 'projects': [{'frameworks': []}]}), 0)
def test_failed_empty_and_invalid_reports_fail_closed(self):
for report in ({}, {'version': 1, 'projects': []},
{'version': 1, 'projects': [{}], 'problems': [{'message': 'offline'}]}):
self.assertNotEqual(self.check(report), 0)
class ReleaseTests(unittest.TestCase):
def execute(self, public=False):
calls = []
class Response(io.BytesIO):
def __enter__(self): return self
def __exit__(self, *args): self.close()
def fake_request(req, timeout):
calls.append(req)
if req.method == 'GET' and '/releases/tags/' in req.full_url:
return Response(json.dumps({'id': 7, 'draft': not public,
'html_url': 'https://example.test/releases/7'}).encode())
if req.method == 'GET':
return Response(b'[{"id":8}]')
if req.method == 'DELETE': return Response(b'')
return Response(b'{"id":9}')
with tempfile.TemporaryDirectory() as temp:
directory = Path(temp)/'artifacts/packages'
directory.mkdir(parents=True)
(directory/'provenance.json').write_text(json.dumps({'commit':'abc123','run_id':'42'}))
(directory/'package.tar.gz').write_bytes(b'archive')
original = Path.cwd()
try:
os.chdir(temp)
with patch.dict(os.environ, {'BUILD_SERVER_URL':'https://example.test',
'BUILD_REPOSITORY':'owner/repo','BUILD_REF':'refs/tags/v1.2.3','RELEASE_TOKEN':'dummy'}), \
patch('urllib.request.urlopen', fake_request), contextlib.redirect_stdout(io.StringIO()):
runpy.run_path(str(ROOT/'release-gitea.py'), run_name='__main__')
finally:
os.chdir(original)
return calls
def test_draft_assets_use_multipart_and_delete_accepts_204(self):
calls = self.execute()
uploads = [x for x in calls if x.method == 'POST']
self.assertEqual(len(uploads), 2)
for request in uploads:
self.assertTrue(request.get_header('Content-type').startswith('multipart/form-data; boundary='))
self.assertIn(b'name="attachment"', request.data)
self.assertEqual(sum(x.method == 'DELETE' for x in calls), 1)
def test_public_release_cannot_be_mutated(self):
with self.assertRaisesRegex(SystemExit, 'already public'):
self.execute(public=True)
if __name__ == '__main__':
unittest.main()