import os import subprocess import tempfile import unittest REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SCRIPT_PATH = os.path.join(REPO_DIR, "cloud-uptime", "recover-after-rebuild.sh") class TestRecoverAfterRebuild(unittest.TestCase): def test_script_exists_and_executable(self): self.assertTrue(os.path.exists(SCRIPT_PATH), f"Script missing: {SCRIPT_PATH}") self.assertTrue(os.access(SCRIPT_PATH, os.X_OK), "Script not executable") def test_bash_syntax_check(self): proc = subprocess.run(["bash", "-n", SCRIPT_PATH], capture_output=True, text=True) self.assertEqual(proc.returncode, 0, f"Bash syntax error: {proc.stderr}") def test_dry_run_execution(self): proc = subprocess.run([SCRIPT_PATH, "--dry-run"], capture_output=True, text=True) self.assertEqual(proc.returncode, 0, f"Dry-run failed: {proc.stderr}") self.assertIn("running in DRY-RUN mode", proc.stdout) self.assertIn("machine:", proc.stdout) def test_machine_env_override(self): with tempfile.TemporaryDirectory() as tmpdir: ws_tunnel = os.path.join(tmpdir, "workspace", "tunnel") os.makedirs(ws_tunnel, exist_ok=True) env_file = os.path.join(ws_tunnel, "machine.env") with open(env_file, "w") as f: f.write("MUSE_MACHINE=custom-test-node\nSSH_PORT=9922\nTERM_PORT=8877\n") env = os.environ.copy() env["HOME"] = tmpdir proc = subprocess.run([SCRIPT_PATH, "--dry-run"], env=env, capture_output=True, text=True) self.assertEqual(proc.returncode, 0, f"Run with env failed: {proc.stderr}") self.assertIn("custom-test-node", proc.stdout) self.assertIn("9922", proc.stdout) self.assertIn("8877", proc.stdout) if __name__ == "__main__": unittest.main()