from __future__ import annotations import importlib.util import os import sys import tarfile import unittest from pathlib import Path from unittest.mock import patch ROOT = Path(__file__).resolve().parents[1] def load_module(): spec = importlib.util.spec_from_file_location( "deploy_full_paramiko_for_test", ROOT / "tools" / "deploy_full_paramiko.py", ) assert spec and spec.loader module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module try: spec.loader.exec_module(module) finally: sys.modules.pop(spec.name, None) return module class DeployFullParamikoTests(unittest.TestCase): @classmethod def setUpClass(cls) -> None: cls.deploy = load_module() def tarinfo(self, name: str) -> tarfile.TarInfo: return tarfile.TarInfo(name) def test_defaults_target_live_install_without_stored_password(self) -> None: with patch.dict(os.environ, {}, clear=True): config = self.deploy.load_config() self.assertEqual(config.host, "192.168.2.101") self.assertEqual(config.user, "eric") self.assertEqual(config.password, "") self.assertEqual(config.remote_path, "/opt/live/d2ypp") self.assertEqual(config.profile, "all") def test_main_requires_password_from_environment(self) -> None: with patch.dict(os.environ, {}, clear=True): with self.assertRaisesRegex(SystemExit, "LIVE_DEPLOY_PASS is required"): self.deploy.main() def test_remote_sync_preserves_runtime_state(self) -> None: excludes = set(self.deploy.REMOTE_RSYNC_EXCLUDES) for item in { "/config/", "/services/neko/data/", "/services/filebrowser/database/", "/services/filebrowser/config/", "/services/uptime-kuma/data/", "/services/mitmproxy/data/", "/services/vscode-server/config/", "/services/homepage/config/logs/", "/vendor/scrcpy-server.jar", }: self.assertIn(item, excludes) def test_archive_filter_excludes_runtime_and_sensitive_config(self) -> None: for rel in [ "config/config.ini", "config/google_oauth.json", "config/youtube.ini", "config/URL_config.ini", "config/web2_api_token.txt", "config/easytier_network.json", "config/hub_state.sqlite", "logs/app.log", "web-console/node_modules/pkg/index.js", "services/neko/data/profile/Default/Preferences", "services/filebrowser/database/filebrowser.db", "services/mitmproxy/data/mitmproxy-ca.pem", "vendor/scrcpy-server.jar", ]: with self.subTest(rel=rel): self.assertIsNone(self.deploy._tar_filter(self.tarinfo(rel))) def test_archive_filter_allows_publishable_templates(self) -> None: for rel in [ "config/system-stack.env.example", "config/runtime.env.example", "config/hardware.env.example", "config/proxychains-youtube.conf", "services/frigate/config/config.yml", "services/srs/data/index.html", ]: with self.subTest(rel=rel): info = self.tarinfo(rel) self.assertIs(self.deploy._tar_filter(info), info) def test_remote_script_runs_full_install_and_health_checks(self) -> None: script = self.deploy.build_remote_script() for snippet in [ "rsync -a --delete", "scripts/linux/install_stack.sh \"$PROFILE\"", "ensure_runtime_dirs_owned_by_live", "http://127.0.0.1:8001/health", "systemctl reset-failed dnsmasq.service", "write_missing_config youtube.ini", "write_missing_config URL_config.ini", ]: self.assertIn(snippet, script) if __name__ == "__main__": unittest.main()