Files
AutoDeployYun/tests/test_core.py

356 lines
17 KiB
Python
Raw Permalink Normal View History

2026-10-30 11:07:49 +08:00
import base64
import io
import json
import os
from pathlib import Path
import socket
import sys
import tarfile
import tempfile
import threading
import types
import unittest
from unittest.mock import patch
import zipfile
import paramiko
from autodeploy import remote
from autodeploy.engine import package_front, Worker
from autodeploy.store import Store, DEFAULTS
class ConfigTests(unittest.TestCase):
def test_time_nonce_falls_back_on_python_36(self):
with patch.object(remote.time, 'time_ns', None), patch.object(remote.time, 'time', return_value=123.456789):
self.assertEqual(remote.time_nonce(), int(123.456789 * 1000000000) % 1000000000)
def test_pid_matching_is_exact_and_excludes_other_instances(self):
def process(pid, args):
return types.SimpleNamespace(parent=types.SimpleNamespace(name=str(pid)), read_bytes=lambda: '\0'.join(args).encode())
candidates = [
process(101, ['java', '-Dcatalina.base=/usr/local/tomcat', 'org.apache.catalina.startup.Bootstrap', 'start']),
process(102, ['java', '-Dcatalina.base=/usr/local/tomcat-other', 'org.apache.catalina.startup.Bootstrap', 'start']),
process(103, ['java', '-Dcatalina.base=/usr/local/tomcat', 'org.apache.catalina.startup.Bootstrap', 'stop']),
process(104, ['sh', 'java -Dcatalina.base=/usr/local/tomcat org.apache.catalina.startup.Bootstrap start'])]
with patch.object(remote.Path, 'glob', return_value=candidates):
self.assertEqual(remote.pids(DEFAULTS), [101])
def test_defaults_valid_on_windows(self):
remote.validate(DEFAULTS)
def test_dangerous_paths_rejected(self):
for path in ('/', '/home', '/home/../etc', 'relative/dist', 'C:\\dist'):
with self.subTest(path=path), self.assertRaises(ValueError):
remote.validate(dict(DEFAULTS, front=path))
def test_overlapping_paths_rejected(self):
for key, value in [('temp','/home/dist/tmp'), ('front','/usr/local/tomcat'), ('backups','/usr/local/tomcat/webapps/backups')]:
with self.subTest(key=key), self.assertRaises(ValueError):
remote.validate(dict(DEFAULTS, **{key: value}))
def test_service_injection_rejected(self):
with self.assertRaises(ValueError):
remote.validate(dict(DEFAULTS, service='tomcat; reboot'))
def test_nginx_service_and_certificate_names_are_safe(self):
with self.assertRaises(ValueError):
remote.validate_nginx(dict(DEFAULTS, nginx_service='nginx; reboot'))
with self.assertRaises(ValueError):
remote.validate_nginx(dict(DEFAULTS, nginx_cert_name='../server.pem', nginx_key_name='server.key'), True)
with self.assertRaises(ValueError):
remote.validate_nginx(dict(DEFAULTS, nginx_cert_name='same.pem', nginx_key_name='same.pem'), True)
class FilesTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = Path(self.tmp.name)
def tearDown(self):
self.tmp.cleanup()
def test_corrupted_upload_fails_before_stopping_service(self):
stage = self.root / ('autodeploy-' + 'a' * 32)
stage.mkdir()
(stage / 'ROOT.war').write_bytes(b'corrupted upload')
payload = dict(config=dict(DEFAULTS, temp=str(self.root)), action='deploy', mode='backend',
stage=str(stage), hashes={'ROOT.war': '0' * 64})
encoded = base64.b64encode(json.dumps(payload).encode()).decode()
with patch.object(sys, 'argv', ['remote.py', encoded]), patch.object(remote, 'validate'), patch.object(remote, 'stop') as stop:
with self.assertRaisesRegex(ValueError, '校验失败'):
remote.main()
stop.assert_not_called()
self.assertFalse(stage.exists())
def test_front_directory_roundtrip_and_stale_file_removed(self):
dist = self.root / 'source'
dist.mkdir()
(dist / 'index.html').write_text('new')
(dist / '中文.js').write_text('js', encoding='utf-8')
archive = self.root / 'dist.tar.gz'
package_front(dist, archive)
remote.unpack(archive, self.root / 'unpacked')
target = self.root / 'target'
target.mkdir()
(target / 'stale.js').write_text('old')
remote.install(self.root / 'unpacked', target, {})
self.assertEqual((target / 'index.html').read_text(), 'new')
self.assertFalse((target / 'stale.js').exists())
self.assertTrue((target / '中文.js').exists())
def test_zip_dist_wrapper_supported(self):
source = self.root / 'source.zip'
with zipfile.ZipFile(source, 'w') as z:
z.writestr('dist/index.html', 'hello')
package_front(source, self.root / 'out.tgz')
with tarfile.open(self.root / 'out.tgz') as tf:
self.assertEqual(tf.getnames(), ['index.html'])
def test_zip_traversal_rejected(self):
source = self.root / 'bad.zip'
with zipfile.ZipFile(source, 'w') as z:
z.writestr('../escape.txt', 'bad')
with self.assertRaises(ValueError):
package_front(source, self.root / 'out.tgz')
self.assertFalse((self.root / 'escape.txt').exists())
def test_tar_traversal_and_links_rejected(self):
for link in (False, True):
archive = self.root / 'bad.tar.gz'
with tarfile.open(archive, 'w:gz') as tf:
info = tarfile.TarInfo('link' if link else '../escape.txt')
if link:
info.type = tarfile.SYMTYPE
info.linkname = '/etc/passwd'
tf.addfile(info)
with self.assertRaises(ValueError):
remote.unpack(archive, self.root / ('out' + str(link)))
def test_missing_index_rejected(self):
source = self.root / 'empty'
source.mkdir()
with self.assertRaises(ValueError):
package_front(source, self.root / 'out.tgz')
class TransactionTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = Path(self.tmp.name)
self.c = dict(DEFAULTS, front=str(self.root / 'dist'), war=str(self.root / 'tomcat/webapps/ROOT.war'),
tomcat=str(self.root / 'tomcat'), backups=str(self.root / 'backups'))
self.stage = self.root / 'stage'
self.stage.mkdir()
(self.stage / 'dist').mkdir()
(self.stage / 'dist/index.html').write_text('new-front')
(self.stage / 'ROOT.war').write_text('new-war')
Path(self.c['front']).mkdir()
(Path(self.c['front']) / 'index.html').write_text('old-front')
Path(self.c['war']).parent.mkdir(parents=True)
Path(self.c['war']).write_text('old-war')
Path(self.c['war']).with_suffix('').mkdir()
(Path(self.c['war']).with_suffix('') / 'old.txt').write_text('old-expanded')
self.events = []
self.event_patch = patch.object(remote, 'event', side_effect=lambda msg, progress=None, **extra: self.events.append((msg, extra)))
self.event_patch.start()
self.fcntl = patch.dict(sys.modules, fcntl=types.SimpleNamespace(LOCK_EX=2, LOCK_NB=4, flock=lambda *args: None))
self.fcntl.start()
def tearDown(self):
self.fcntl.stop()
self.event_patch.stop()
self.tmp.cleanup()
def test_frontend_does_not_stop_tomcat(self):
with patch.object(remote, 'stop') as stop, patch.object(remote, 'start') as start:
remote.transaction(self.c, 'frontend', self.stage)
stop.assert_not_called()
start.assert_not_called()
self.assertEqual((Path(self.c['front']) / 'index.html').read_text(), 'new-front')
self.assertEqual(Path(self.c['war']).read_text(), 'old-war')
def test_success_replaces_both_and_removes_expanded_root(self):
with patch.object(remote, 'pids', return_value=[42]), patch.object(remote, 'stop'), patch.object(remote, 'start'):
remote.transaction(self.c, 'all', self.stage)
self.assertEqual(Path(self.c['war']).read_text(), 'new-war')
self.assertFalse(Path(self.c['war']).with_suffix('').exists())
def test_failed_start_restores_both_and_restarts_old_service(self):
with patch.object(remote, 'pids', return_value=[42]), patch.object(remote, 'stop') as stop, patch.object(remote, 'start', side_effect=[RuntimeError('health failed'), None]) as start:
with self.assertRaises(RuntimeError):
remote.transaction(self.c, 'all', self.stage)
self.assertEqual(stop.call_count, 2)
self.assertEqual(start.call_count, 2)
self.assertEqual(Path(self.c['war']).read_text(), 'old-war')
self.assertEqual((Path(self.c['front']) / 'index.html').read_text(), 'old-front')
self.assertTrue((Path(self.c['war']).with_suffix('') / 'old.txt').exists())
def test_stop_failure_never_replaces_files(self):
with patch.object(remote, 'pids', return_value=[42]), patch.object(remote, 'stop', side_effect=RuntimeError('cannot stop')):
with self.assertRaises(RuntimeError):
remote.transaction(self.c, 'all', self.stage)
self.assertEqual(Path(self.c['war']).read_text(), 'old-war')
def test_lock_conflict_never_stops_service(self):
with patch.dict(sys.modules, fcntl=types.SimpleNamespace(LOCK_EX=2, LOCK_NB=4, flock=lambda *args: (_ for _ in ()).throw(BlockingIOError('locked')))):
with patch.object(remote, 'pids', return_value=[42]), patch.object(remote, 'stop') as stop:
with self.assertRaises(BlockingIOError):
remote.transaction(self.c, 'all', self.stage)
stop.assert_not_called()
def test_manual_restore_and_two_backup_retention(self):
backup = remote.snapshot(self.c, 'all')
Path(self.c['war']).write_text('changed')
remote.restore(self.c, backup)
self.assertEqual(Path(self.c['war']).read_text(), 'old-war')
for _ in range(3):
remote.snapshot(self.c, 'frontend')
remote.prune(self.c)
kept = list((Path(self.c['backups']) / remote.project_key(self.c)).iterdir())
self.assertEqual(len(kept), 2)
def test_missing_and_foreign_backup_rejected(self):
with self.assertRaises(ValueError):
remote.load_backup(self.c, str(self.root / 'foreign'))
missing = Path(self.c['backups']) / remote.project_key(self.c) / 'missing'
with self.assertRaises(FileNotFoundError):
remote.load_backup(self.c, str(missing))
def test_first_deploy_restore_absence(self):
remote.remove(Path(self.c['front']))
backup = remote.snapshot(self.c, 'frontend')
remote.copy(self.stage / 'dist', Path(self.c['front']))
remote.restore(self.c, backup)
self.assertFalse(Path(self.c['front']).exists())
class StoreTests(unittest.TestCase):
def test_multiple_profiles_plain_password_and_password_free_history(self):
with tempfile.TemporaryDirectory() as tmp:
store = Store(Path(tmp) / 'db.sqlite')
a = store.save(None, dict(DEFAULTS, name='A', password='secret-A'))
b = store.save(None, dict(DEFAULTS, name='B', password='secret-B'))
self.assertNotEqual(a, b)
self.assertEqual(store.profiles()[0][1]['password'], 'secret-A')
store.record(a, '部署', '成功', 'ok', '/backup', dict(DEFAULTS, password='secret-A'))
self.assertNotIn('secret-A', store.history(a)[0]['config'])
store.delete(a)
self.assertEqual(len(store.profiles()), 1)
class NginxTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = Path(self.tmp.name)
self.cert_dir = self.root / 'ssl'
self.cert_dir.mkdir()
self.backups = self.root / 'backups'
self.stage = self.root / 'stage'
self.stage.mkdir()
(self.stage / 'certificate.pem').write_text('new-cert')
(self.stage / 'private.key').write_text('new-key')
self.c = dict(DEFAULTS, backups=str(self.backups), nginx_cert_dir=str(self.cert_dir),
nginx_cert_name='server.pem', nginx_key_name='server.key')
(self.cert_dir / 'server.pem').write_text('old-cert')
(self.cert_dir / 'server.key').write_text('old-key')
self.fcntl = patch.dict(sys.modules, fcntl=types.SimpleNamespace(LOCK_EX=2, LOCK_NB=4, flock=lambda *args: None))
self.fcntl.start()
self.lock = patch.object(remote, 'nginx_lock_path', return_value=self.root / 'nginx.lock')
self.lock.start()
self.events = patch.object(remote, 'event')
self.events.start()
def tearDown(self):
self.events.stop()
self.lock.stop()
self.fcntl.stop()
self.tmp.cleanup()
def test_certificate_transaction_replaces_pair_and_keeps_backup(self):
with patch.object(remote, 'validate_certificate_pair'), patch.object(remote, 'nginx_active', return_value=True), \
patch.object(remote, 'nginx_test') as test, patch.object(remote, 'restart_nginx') as restart:
remote.certificate_transaction(self.c, self.stage)
self.assertEqual((self.cert_dir / 'server.pem').read_text(), 'new-cert')
self.assertEqual((self.cert_dir / 'server.key').read_text(), 'new-key')
self.assertEqual(test.call_count, 1)
self.assertEqual(restart.call_count, 1)
self.assertEqual(len(list(remote.nginx_backup_root(self.c).iterdir())), 1)
def test_failed_nginx_test_restores_old_pair_and_service(self):
with patch.object(remote, 'validate_certificate_pair'), patch.object(remote, 'nginx_active', return_value=True), \
patch.object(remote, 'nginx_test', side_effect=[RuntimeError('bad config'), None]) as test, \
patch.object(remote, 'restart_nginx') as restart:
with self.assertRaisesRegex(RuntimeError, 'bad config'):
remote.certificate_transaction(self.c, self.stage)
self.assertEqual((self.cert_dir / 'server.pem').read_text(), 'old-cert')
self.assertEqual((self.cert_dir / 'server.key').read_text(), 'old-key')
self.assertEqual(test.call_count, 2)
self.assertEqual(restart.call_count, 1)
def test_standalone_restart_stops_when_config_test_fails(self):
with patch.object(remote, 'nginx_test', side_effect=RuntimeError('bad config')), \
patch.object(remote, 'restart_nginx') as restart:
with self.assertRaisesRegex(RuntimeError, 'bad config'):
remote.nginx_restart_transaction(self.c)
restart.assert_not_called()
def test_mismatched_certificate_pair_is_rejected(self):
outputs = [b'', b'', b'certificate-public-key', b'private-public-key']
with patch.object(remote, 'openssl_output', side_effect=outputs):
with self.assertRaisesRegex(ValueError, '不匹配'):
remote.validate_certificate_pair(self.stage / 'certificate.pem', self.stage / 'private.key')
class SSHTests(unittest.TestCase):
def test_real_ssh_password_auth_and_host_key_persist(self):
class Server(paramiko.ServerInterface):
def check_auth_password(self, username, password):
return paramiko.AUTH_SUCCESSFUL if (username, password) == ('test', 'secret') else paramiko.AUTH_FAILED
def get_allowed_auths(self, username):
return 'password'
def check_channel_request(self, kind, chanid):
return paramiko.OPEN_SUCCEEDED
def check_channel_exec_request(self, channel, command):
def respond():
channel.send(b'Python 3.6.8\n')
channel.send_exit_status(0)
channel.shutdown_write()
threading.Timer(.1, respond).start()
return True
listener = socket.socket()
listener.bind(('127.0.0.1', 0))
listener.listen(1)
key = paramiko.RSAKey.generate(2048)
transports = []
def serve():
connection, _ = listener.accept()
transport = paramiko.Transport(connection)
transports.append(transport)
transport.add_server_key(key)
transport.start_server(server=Server())
channel = transport.accept(10)
if channel:
channel.closed_event.wait(10) if hasattr(channel, 'closed_event') else threading.Event().wait(.5)
thread = threading.Thread(target=serve, daemon=True)
thread.start()
try:
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, AUTODEPLOY_DATA=tmp):
c = dict(DEFAULTS, host='127.0.0.1', port=listener.getsockname()[1], username='test', password='secret')
worker = Worker(c, 'test')
results = []
worker.completed.connect(lambda ok, log, backup: results.append(ok))
worker.run()
self.assertEqual(results, [True])
self.assertTrue((Path(tmp) / 'known_hosts').exists())
self.assertNotIn('secret', '\n'.join(worker.logs))
finally:
for transport in transports:
transport.close()
listener.close()
thread.join(2)
if __name__ == '__main__':
unittest.main()