63 lines
2.8 KiB
Python
63 lines
2.8 KiB
Python
import importlib.util
|
|
import pathlib
|
|
import subprocess
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
SCRIPT = pathlib.Path(__file__).with_name('publish-images.py')
|
|
|
|
class PublishTests(unittest.TestCase):
|
|
def test_publication_contract(self):
|
|
spec = importlib.util.spec_from_file_location('publish', SCRIPT)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
sha = 'a' * 40
|
|
for args, builds, pushes in [([], 4, 0), (['--push'], 4, 4), (['--dry-run', '--push'], 0, 0)]:
|
|
calls = []
|
|
def run(cmd, **kwargs):
|
|
calls.append(cmd)
|
|
return subprocess.CompletedProcess(cmd, 0, stdout=sha + '\n')
|
|
with patch.object(module.subprocess, 'run', side_effect=run):
|
|
module.main(args)
|
|
docker = [c for c in calls if c[0] == 'docker']
|
|
self.assertEqual(sum(c[1] == 'build' for c in docker), builds)
|
|
self.assertEqual(sum(c[1] == 'push' for c in docker), pushes)
|
|
for cmd in docker:
|
|
self.assertTrue(any(sha in arg for arg in cmd))
|
|
if builds:
|
|
laya = docker[3]
|
|
self.assertIn('TORCH_INDEX=cpu', laya)
|
|
self.assertTrue(laya[-1].endswith(module.LAYA_REVISION))
|
|
for cmd in docker[:4]:
|
|
for label in ['source', 'revision', 'version']:
|
|
self.assertTrue(any(a.startswith('org.opencontainers.image.' + label + '=') for a in cmd))
|
|
|
|
def test_failed_build_prevents_all_pushes(self):
|
|
spec = importlib.util.spec_from_file_location('publish', SCRIPT)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
calls = []
|
|
def run(cmd, **kwargs):
|
|
calls.append(cmd)
|
|
if cmd[:2] == ['docker', 'build']:
|
|
raise subprocess.CalledProcessError(1, cmd)
|
|
return subprocess.CompletedProcess(cmd, 0, stdout='a' * 40)
|
|
with patch.object(module.subprocess, 'run', side_effect=run):
|
|
with self.assertRaises(subprocess.CalledProcessError):
|
|
module.main(['--push', '--tag', 'v2'])
|
|
self.assertFalse(any(c[:2] == ['docker', 'push'] for c in calls))
|
|
self.assertIn(module.REGISTRY + '/tradarr-backend:v2', calls[-1])
|
|
|
|
def test_invalid_tags_fail_before_commands(self):
|
|
spec = importlib.util.spec_from_file_location('publish', SCRIPT)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
for tag in ['bad/tag', 'secret=value', '-bad', 'x' * 129]:
|
|
with patch.object(module.subprocess, 'run') as run:
|
|
with self.assertRaises(SystemExit):
|
|
module.main(['--tag', tag])
|
|
run.assert_not_called()
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|