| 1 | #!/usr/bin/env python3 |
| 2 | """Run the actual lftp command sequence against an isolated local FTP server.""" |
| 3 | import os |
| 4 | import logging |
| 5 | from pathlib import Path |
| 6 | import subprocess |
| 7 | import tempfile |
| 8 | import threading |
| 9 | import unittest |
| 10 | |
| 11 | from pyftpdlib.log import config_logging |
| 12 | config_logging(level=logging.ERROR) |
| 13 | |
| 14 | from pyftpdlib.authorizers import DummyAuthorizer |
| 15 | from pyftpdlib.handlers import FTPHandler |
| 16 | from pyftpdlib.servers import FTPServer |
| 17 | from deploy import ROOT, deployment_commands, write_asset_version |
| 18 | |
| 19 | class DeployTests(unittest.TestCase): |
| 20 | def test_mirror_preserves_data(self): |
| 21 | artifacts=ROOT/'web/tests/artifacts' |
| 22 | artifacts.mkdir(exist_ok=True) |
| 23 | with tempfile.TemporaryDirectory(dir=artifacts,prefix='deploy-') as directory: |
| 24 | base=Path(directory); local=base/'local web'; remote=base/'remote' |
| 25 | local.mkdir();remote.mkdir();target=remote/'chess project';target.mkdir() |
| 26 | for folder in [local/'data', local/'tests',local/'assets',target/'data',target/'data/backups']: |
| 27 | folder.mkdir() |
| 28 | (local/'data/.htaccess').write_text('Require all denied\n') |
| 29 | (local/'data/config.php').write_text('DO NOT UPLOAD LOCAL SECRET') |
| 30 | (local/'data/synhex.sqlite').write_text('DO NOT UPLOAD LOCAL DB') |
| 31 | (local/'tests/check.php').write_text('test code') |
| 32 | (local/'router.php').write_text('dev router') |
| 33 | (local/'README.md').write_text('readme') |
| 34 | (local/'index.php').write_text('new application') |
| 35 | (local/'.htaccess').write_text('Options -Indexes\n') |
| 36 | (local/'assets/app.js').write_text('new frontend') |
| 37 | (target/'obsolete.php').write_text('stale application code') |
| 38 | retained={'config.php':'PRODUCTION SECRET','synhex.sqlite':'PRODUCTION DB', |
| 39 | 'synhex.sqlite-wal':'LIVE WAL','backups/older.sqlite':'BACKUP'} |
| 40 | for file,data in retained.items():(target/'data'/file).write_text(data) |
| 41 | authorizer=DummyAuthorizer();authorizer.add_user('fixture-user','fixture-password',str(remote),perm='elradfmwMT') |
| 42 | class Handler(FTPHandler): |
| 43 | pass |
| 44 | Handler.authorizer=authorizer |
| 45 | server=FTPServer(('127.0.0.1',0),Handler) |
| 46 | thread=threading.Thread(target=server.serve_forever,kwargs={'timeout':.1},daemon=True);thread.start() |
| 47 | env={'timftphost':f'ftp://127.0.0.1:{server.socket.getsockname()[1]}','timftpuser':'fixture-user', |
| 48 | 'timftppassword':'fixture-password','projectftp':'chess project'} |
| 49 | try: |
| 50 | commands=deployment_commands(env,local) |
| 51 | self.assertNotIn(env['timftppassword'],commands) |
| 52 | for _ in range(2): |
| 53 | result=subprocess.run(['lftp','--norc'],input=commands,text=True, |
| 54 | env={**os.environ,'LFTP_PASSWORD':env['timftppassword'], 'LFTP_HOME':str(base/'lftp')}, |
| 55 | capture_output=True,timeout=30) |
| 56 | self.assertEqual(result.returncode,0,result.stdout+result.stderr) |
| 57 | for file,data in retained.items():self.assertEqual((target/'data'/file).read_text(),data) |
| 58 | self.assertEqual((target/'data/.htaccess').read_text(),'Require all denied\n') |
| 59 | self.assertEqual((target/'index.php').read_text(),'new application') |
| 60 | self.assertEqual((target/'.htaccess').read_text(),'Options -Indexes\n') |
| 61 | self.assertEqual((target/'assets/app.js').read_text(),'new frontend') |
| 62 | for excluded in ['tests','router.php','README.md','obsolete.php']: |
| 63 | self.assertFalse((target/excluded).exists(),excluded) |
| 64 | finally: |
| 65 | server.close_all();thread.join(timeout=2) |
| 66 | |
| 67 | |
| 68 | def test_write_asset_version_from_ci(self): |
| 69 | with tempfile.TemporaryDirectory() as directory: |
| 70 | webroot = Path(directory) |
| 71 | write_asset_version(webroot, {'CI_COMMIT_SHA': 'deadbeefcafebabe0123456789abcdef01234567'}) |
| 72 | self.assertEqual((webroot/'asset-version.txt').read_text(), 'deadbeefcafebabe0123456789abcdef01234567\n') |
| 73 | |
| 74 | def test_invalid_config_fails_before_connecting(self): |
| 75 | with self.assertRaises(ValueError):deployment_commands({},ROOT/'web') |
| 76 | env={'timftphost':'ftp://example.test','timftpuser':'user','timftppassword':'secret','projectftp':'x\nrm -r data'} |
| 77 | with self.assertRaises(ValueError):deployment_commands(env,ROOT/'web') |
| 78 | |
| 79 | if __name__=='__main__': |
| 80 | unittest.main() |
projects-timfalken-com microchess