chore(deploy): switch publisher to Bash

This commit is contained in:
2026-11-23 16:22:43 +01:00
parent 18c1010f98
commit e46108fecc
4 changed files with 168 additions and 118 deletions

View File

@ -1,62 +1,87 @@
import importlib.util
"""Exercise the shell CLI using local fake executables, never real Git/Docker."""
import json
import os
import pathlib
import subprocess
import tempfile
import unittest
from unittest.mock import patch
SCRIPT = pathlib.Path(__file__).with_name('publish-images.py')
SCRIPT = pathlib.Path(__file__).with_name('publish-images.sh').resolve()
SHA = 'a' * 40
PIN = 'e150aa92a0d4059ac621875b39b620847cf1b88a'
PREFIX = 'gitea.anthonybouteiller.ovh/blomios/tradarr-'
class PublishTests(unittest.TestCase):
def invoke(self, args, fail='', sha=SHA):
with tempfile.TemporaryDirectory(prefix='publish tests ') as directory:
root = pathlib.Path(directory)
log = root / 'calls'
for name in ['git', 'docker']:
executable = root / name
executable.write_text('''#!/usr/bin/env python3
import json, os, sys
with open(os.environ['CALL_LOG'], 'a') as stream:
stream.write(json.dumps([os.path.basename(sys.argv[0])] + sys.argv[1:]) + '\\n')
if os.path.basename(sys.argv[0]) == 'git':
print(os.environ['FAKE_SHA'])
else:
print('SECRET_FROM_DOCKER', file=sys.stderr)
if os.environ['FAIL'] and os.environ['FAIL'] in sys.argv:
sys.exit(1)
''')
executable.chmod(0o755)
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'],
CALL_LOG=str(log), FAKE_SHA=sha, FAIL=fail)
result = subprocess.run(['bash', str(SCRIPT), *args], cwd=root,
env=env, capture_output=True, text=True)
calls = [json.loads(line) for line in log.read_text().splitlines()] if log.exists() else []
self.assertNotIn('SECRET_FROM_DOCKER', result.stdout + result.stderr)
return result, calls
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)
for args, builds, pushes in [([], 4, 0), (['--push'], 4, 4),
(['--dry-run', '--push'], 0, 0)]:
with self.subTest(args=args):
result, calls = self.invoke(args)
self.assertEqual(result.returncode, 0, result.stderr)
docker = [c for c in calls if c[0] == 'docker']
self.assertEqual([c[1] for c in docker], ['build'] * builds + ['push'] * pushes)
for i, name in enumerate(['backend', 'frontend', 'scraper', 'laya']):
self.assertIn(PREFIX + name + ':' + SHA, result.stdout)
if builds:
command = docker[i]
revision = PIN if name == 'laya' else SHA
self.assertIn('org.opencontainers.image.revision=' + revision, command)
self.assertIn('org.opencontainers.image.version=' + SHA, command)
self.assertTrue(any(a.startswith('org.opencontainers.image.source=https://') for a in command))
if builds:
self.assertIn('TORCH_INDEX=cpu', docker[3])
self.assertEqual(docker[3][-1], 'https://github.com/NandhaKishorM/laya.git#' + PIN)
def test_failures_stop_publication(self):
for failure, count in [('build', 1), ('push', 5)]:
result, calls = self.invoke(['--push', '--tag', 'v2'], fail=failure)
self.assertNotEqual(result.returncode, 0)
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))
self.assertEqual(len(docker), count)
self.assertIn(PREFIX + 'backend:v2', docker[0])
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_bad_arguments_rejected_before_commands(self):
for args in [['--tag', tag] for tag in ['', 'bad/tag', 'secret=value', '-bad', 'x' * 129, '$(touch bad)', 'a\nb']] + [['--tag'], ['--unknown'], ['extra']]:
with self.subTest(args=args):
result, calls = self.invoke(args)
self.assertNotEqual(result.returncode, 0)
self.assertEqual(calls, [])
def test_bad_revision_and_help(self):
result, calls = self.invoke([], sha='short')
self.assertNotEqual(result.returncode, 0)
self.assertEqual(len(calls), 1)
result, calls = self.invoke(['--help'])
self.assertEqual(result.returncode, 0)
self.assertEqual(calls, [])
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()