| 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 | from deploy_guard import DeployGuardError, project_dir, main as guard_main |
| 19 | |
| 20 | class DeployTests(unittest.TestCase): |
| 21 | def test_mirror_preserves_data(self): |
| 22 | artifacts=ROOT/'web/tests/artifacts' |
| 23 | artifacts.mkdir(exist_ok=True) |
| 24 | with tempfile.TemporaryDirectory(dir=artifacts,prefix='deploy-') as directory: |
| 25 | base=Path(directory); local=base/'local web'; remote=base/'remote' |
| 26 | local.mkdir();remote.mkdir();target=remote/'verhaaltjesboek';target.mkdir() |
| 27 | for folder in [local/'data', local/'tests',local/'assets',target/'data',target/'data/backups']: |
| 28 | folder.mkdir() |
| 29 | (local/'data/.htaccess').write_text('Require all denied\n') |
| 30 | (local/'data/config.php').write_text('DO NOT UPLOAD LOCAL SECRET') |
| 31 | (local/'data/verhalenboek.sqlite').write_text('DO NOT UPLOAD LOCAL DB') |
| 32 | (local/'tests/check.php').write_text('test code') |
| 33 | (local/'router.php').write_text('dev router') |
| 34 | (local/'README.md').write_text('readme') |
| 35 | (local/'index.php').write_text('new application') |
| 36 | (local/'.htaccess').write_text('Options -Indexes\n') |
| 37 | (local/'assets/app.js').write_text('new frontend') |
| 38 | (target/'obsolete.php').write_text('stale application code') |
| 39 | retained={'config.php':'PRODUCTION SECRET','verhalenboek.sqlite':'PRODUCTION DB', |
| 40 | 'verhalenboek.sqlite-wal':'LIVE WAL','backups/older.sqlite':'BACKUP'} |
| 41 | for file,data in retained.items():(target/'data'/file).write_text(data) |
| 42 | authorizer=DummyAuthorizer();authorizer.add_user('fixture-user','fixture-password',str(remote),perm='elradfmwMT') |
| 43 | class Handler(FTPHandler): |
| 44 | pass |
| 45 | Handler.authorizer=authorizer |
| 46 | server=FTPServer(('127.0.0.1',0),Handler) |
| 47 | thread=threading.Thread(target=server.serve_forever,kwargs={'timeout':.1},daemon=True);thread.start() |
| 48 | env={'timftphost':f'ftp://127.0.0.1:{server.socket.getsockname()[1]}','timftpuser':'fixture-user', |
| 49 | 'timftppassword':'fixture-password','projectftp':'verhaaltjesboek'} |
| 50 | try: |
| 51 | commands=deployment_commands(env,local) |
| 52 | self.assertNotIn(env['timftppassword'],commands) |
| 53 | for _ in range(2): |
| 54 | result=subprocess.run(['lftp','--norc'],input=commands,text=True, |
| 55 | env={**os.environ,'LFTP_PASSWORD':env['timftppassword'], 'LFTP_HOME':str(base/'lftp')}, |
| 56 | capture_output=True,timeout=30) |
| 57 | self.assertEqual(result.returncode,0,result.stdout+result.stderr) |
| 58 | for file,data in retained.items():self.assertEqual((target/'data'/file).read_text(),data) |
| 59 | self.assertEqual((target/'data/.htaccess').read_text(),'Require all denied\n') |
| 60 | self.assertEqual((target/'index.php').read_text(),'new application') |
| 61 | self.assertEqual((target/'.htaccess').read_text(),'Options -Indexes\n') |
| 62 | self.assertEqual((target/'assets/app.js').read_text(),'new frontend') |
| 63 | for excluded in ['tests','router.php','README.md','obsolete.php']: |
| 64 | self.assertFalse((target/excluded).exists(),excluded) |
| 65 | finally: |
| 66 | server.close_all();thread.join(timeout=2) |
| 67 | |
| 68 | |
| 69 | def test_write_asset_version_from_ci(self): |
| 70 | with tempfile.TemporaryDirectory() as directory: |
| 71 | webroot = Path(directory) |
| 72 | write_asset_version(webroot, {'CI_COMMIT_SHA': 'deadbeefcafebabe0123456789abcdef01234567'}) |
| 73 | self.assertEqual((webroot/'asset-version.txt').read_text(), 'deadbeefcafebabe0123456789abcdef01234567\n') |
| 74 | |
| 75 | def test_invalid_config_fails_before_connecting(self): |
| 76 | with self.assertRaises(ValueError):deployment_commands({},ROOT/'web') |
| 77 | env={'timftphost':'ftp://example.test','timftpuser':'user','timftppassword':'secret','projectftp':'x\nrm -r data'} |
| 78 | with self.assertRaises(ValueError):deployment_commands(env,ROOT/'web') |
| 79 | |
| 80 | class DeployGuardTests(unittest.TestCase): |
| 81 | BASE={'timftphost':'ftp://example.test','timftpuser':'user','timftppassword':'secret','timftproot':'/domains/projects.timfalken.com/public_html'} |
| 82 | |
| 83 | def blocked(self, **extra): |
| 84 | env=dict(self.BASE); env.update(extra) |
| 85 | env={k:v for k,v in env.items() if v is not None} |
| 86 | with self.assertRaises(DeployGuardError):project_dir(env) |
| 87 | with self.assertRaises(ValueError):deployment_commands(env,ROOT/'web') |
| 88 | |
| 89 | def test_not_set(self):self.blocked(projectftp=None) |
| 90 | def test_empty(self):self.blocked(projectftp='') |
| 91 | def test_whitespace(self):self.blocked(projectftp=' ') |
| 92 | def test_slash(self):self.blocked(projectftp='/') |
| 93 | def test_dot(self):self.blocked(projectftp='.') |
| 94 | def test_dot_slash(self):self.blocked(projectftp='./') |
| 95 | def test_root_literal(self):self.blocked(projectftp='/domains/projects.timfalken.com/public_html/') |
| 96 | def test_root_reference(self):self.blocked(projectftp='$timftproot') |
| 97 | def test_root_reference_slash(self):self.blocked(projectftp='${timftproot}/') |
| 98 | def test_root_reference_without_root(self):self.blocked(projectftp='$timftproot/verhaaltjesboek',timftproot=None) |
| 99 | def test_parent(self):self.blocked(projectftp='$timftproot/verhaaltjesboek/..') |
| 100 | def test_outside_root(self):self.blocked(projectftp='/domains/other.site/public_html/x') |
| 101 | def test_unknown_reference(self):self.blocked(projectftp='$HOME/x') |
| 102 | |
| 103 | def test_valid(self): |
| 104 | env=dict(self.BASE,projectftp='$timftproot/verhaaltjesboek') |
| 105 | self.assertEqual(project_dir(env),'/domains/projects.timfalken.com/public_html/verhaaltjesboek') |
| 106 | self.assertIn('cd "/domains/projects.timfalken.com/public_html/verhaaltjesboek"',deployment_commands(env,ROOT/'web')) |
| 107 | self.assertEqual(project_dir(dict(self.BASE,projectftp='verhaaltjesboek')),'verhaaltjesboek') |
| 108 | self.assertEqual(project_dir(dict(self.BASE,timftproot='/r/',projectftp='$timftproot/vb/')),'/r/vb') |
| 109 | |
| 110 | def test_cli_exit_code_and_no_values(self): |
| 111 | import io,contextlib |
| 112 | err=io.StringIO() |
| 113 | with contextlib.redirect_stderr(err):self.assertEqual(guard_main(dict(self.BASE,projectftp='/')),1) |
| 114 | self.assertIn('DEPLOY GEBLOKKEERD',err.getvalue()) |
| 115 | self.assertNotIn(self.BASE['timftproot'],err.getvalue()) |
| 116 | |
| 117 | if __name__=='__main__': |
| 118 | unittest.main() |
projects-timfalken-com verhalenboek