Files
pipelines/scripts/test_uninstall.py
T

265 lines
13 KiB
Python

import importlib.util
import json
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
from unittest.mock import patch
spec = importlib.util.spec_from_file_location("pipelines_uninstall", Path(__file__).with_name("uninstall.py"))
uninstall = importlib.util.module_from_spec(spec)
spec.loader.exec_module(uninstall)
class Recorder:
def __init__(self):
self.calls = []
def __call__(self, command, **kwargs):
command = [str(part) for part in command]
self.calls.append(command)
stdout = ""
if command[:3] == ["docker", "ps", "-aq"]:
stdout = "fixture-container\n"
if command[:4] == ["docker", "ps", "-a", "--format"]:
stdout = "ab12cd34 pipelines-flutter-android:3.44.6\naaabbb pipelines-flutter-web:3.44.6\ndeadbeef unrelated:latest\nbaadf00d pipelines-flutter-other:1\n"
if command[:3] == ["docker", "volume", "ls"]:
stdout = ("pipelines-flutter-pub\npipelines-flutter-gradle\npipelines-android-sdk\npipelines_pipelines-data\npipelines_ntfy-data\nunrelated-volume\n"
if "--format" in command else "pipelines_labelled-data\n")
if command[:3] == ["docker", "image", "ls"]:
stdout = "pipelines-flutter-web:3.44.6\npipelines-flutter-android:3.44.6\nunrelated:latest\n"
return subprocess.CompletedProcess(command, 0, stdout=stdout, stderr="")
class UninstallTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(prefix="pipelines-reset-test-")
self.addCleanup(self.temporary.cleanup)
self.root = Path(self.temporary.name)
self.home = (self.root / "home").resolve()
self.home.mkdir()
self.repo = self.home / "repos/pipelines"
self.repo.mkdir(parents=True)
self.prefix = self.home / ".local/share/pipelines"
self.recorder = Recorder()
self.environment = {}
def resetter(self):
return uninstall.Uninstall(home=self.home, repo=self.repo,
environ=self.environment, runner=self.recorder, system="Linux")
def put(self, path, content="fixture"):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
return path
def test_failed_install_without_manifest_is_fully_removed(self):
self.put(self.prefix / "bin/ntfy", "partial binary")
self.put(self.prefix / "data/pipelines.db", "private history")
self.put(self.home / ".config/pipelines/web-auth.json", "old password")
self.put(self.home / ".cache/pipelines/app-builds/web-key/index.html")
self.put(self.home / ".config/opencode/skills/pipelines/SKILL.md")
self.put(self.home / ".claude/skills/pipelines/SKILL.md")
self.put(self.home / ".codex/skills/pipelines/SKILL.md")
source = self.put(self.repo / "install", "keep checkout")
provider = self.put(self.home / ".local/share/opencode/auth.json", "keep login")
other_skill = self.put(self.home / ".claude/skills/personal/SKILL.md", "keep personal skill")
self.resetter().run()
self.assertFalse(self.prefix.exists())
self.assertFalse((self.home / ".config/pipelines").exists())
self.assertFalse((self.home / ".cache/pipelines").exists())
for folder in (".config/opencode", ".claude", ".codex"):
self.assertFalse((self.home / folder / "skills/pipelines").exists())
self.assertEqual(source.read_text(), "keep checkout")
self.assertEqual(provider.read_text(), "keep login")
self.assertEqual(other_skill.read_text(), "keep personal skill")
def test_xdg_settings_and_default_password_are_both_removed(self):
config = self.home / "custom-config"
cache = self.home / "custom-cache"
codex = self.home / "custom-codex"
claude = self.home / "custom-claude"
self.environment.update(XDG_CONFIG_HOME=str(config), XDG_CACHE_HOME=str(cache),
CODEX_HOME=str(codex), CLAUDE_CONFIG_DIR=str(claude))
targets = [config / "pipelines/harnesses.json", config / "opencode/skills/pipelines/SKILL.md",
self.home / ".config/pipelines/web-auth.json", cache / "pipelines/app-builds/blob",
codex / "skills/pipelines/SKILL.md", claude / "skills/pipelines/SKILL.md"]
for target in targets:
self.put(target)
self.resetter().run()
for target in targets:
self.assertFalse(target.exists(), str(target))
def test_recorded_custom_password_and_config_are_removed(self):
auth = self.put(self.home / "secrets/pipelines-login.json", json.dumps({"password": "old password", "secret": "old secret"}))
old_config = self.home / "recorded-config"
harness = self.put(old_config / "pipelines/harnesses.json", "old harness")
self.put(self.prefix / "pipelines-install.json", json.dumps({
"format": 1, "prefix": str(self.prefix),
"web": {"auth_file": str(auth)},
"opencode_env": {"XDG_CONFIG_HOME": str(old_config)},
}))
self.resetter().run()
self.assertFalse(auth.exists())
self.assertFalse(harness.exists())
self.assertFalse(self.prefix.exists())
def test_second_uninstall_is_idempotent(self):
self.put(self.prefix / "bin/pipelines")
self.resetter().run()
self.resetter().run()
self.assertFalse(self.prefix.exists())
self.assertTrue(self.repo.is_dir())
def test_dangerous_prefixes_are_refused_before_stopping_or_deleting(self):
for target in (self.home, self.repo, self.home / "repos", Path("/")):
with self.subTest(target=target):
self.environment["PIPELINES_PREFIX"] = str(target)
sentinel = self.put(self.repo / "keep.txt", "keep")
self.recorder.calls.clear()
with self.assertRaises((ValueError, RuntimeError)):
self.resetter().run()
self.assertEqual(sentinel.read_text(), "keep")
self.assertEqual(self.recorder.calls, [])
def test_symlinked_installation_is_not_followed(self):
elsewhere = self.home / "unrelated"
sentinel = self.put(elsewhere / "keep", "keep")
self.prefix.parent.mkdir(parents=True)
self.prefix.symlink_to(elsewhere, target_is_directory=True)
with self.assertRaises((ValueError, RuntimeError)):
self.resetter().run()
self.assertEqual(sentinel.read_text(), "keep")
self.assertEqual(self.recorder.calls, [])
def test_unmanaged_service_is_refused_before_destructive_actions(self):
unit = self.put(self.home / ".config/systemd/user/net.a-wan.pipelines.service", "Unrelated service")
marker = self.put(self.prefix / "keep", "keep")
with self.assertRaises((ValueError, RuntimeError)):
self.resetter().run()
self.assertEqual(unit.read_text(), "Unrelated service")
self.assertEqual(marker.read_text(), "keep")
self.assertEqual(self.recorder.calls, [])
def test_managed_service_is_removed_and_only_its_unit_is_disabled(self):
unit = self.put(self.home / ".config/systemd/user/net.a-wan.pipelines.service",
"# Managed by Pipelines native installer\n[Service]\n")
neighbor = self.put(unit.parent / "other.service", "keep")
self.resetter().run()
self.assertFalse(unit.exists())
self.assertEqual(neighbor.read_text(), "keep")
commands = [c for c in self.recorder.calls if c and c[0] == "systemctl"]
self.assertTrue(any("net.a-wan.pipelines.service" in c and "disable" in c for c in commands))
self.assertFalse(any("other.service" in c for c in commands))
def test_unavailable_docker_aborts_before_files_or_services_change(self):
marker = self.put(self.prefix / "keep", "keep")
original = self.recorder
def stopped_docker(command, **kwargs):
if command[:2] == ["docker", "info"]:
original.calls.append(command)
return subprocess.CompletedProcess(command, 1, stdout="", stderr="not running")
return original(command, **kwargs)
resetter = uninstall.Uninstall(home=self.home, repo=self.repo,
environ={}, runner=stopped_docker, system="Linux")
with self.assertRaisesRegex(RuntimeError, "Docker"):
resetter.run()
self.assertEqual(marker.read_text(), "keep")
self.assertFalse(any(c[0] == "systemctl" for c in original.calls))
def test_absent_docker_does_not_block_native_partial_cleanup(self):
self.put(self.prefix / "keep")
original = self.recorder
def no_docker(command, **kwargs):
if command[0] == "docker":
raise FileNotFoundError("docker")
return original(command, **kwargs)
uninstall.Uninstall(home=self.home, repo=self.repo,
environ={}, runner=no_docker, system="Linux").run()
self.assertFalse(self.prefix.exists())
def test_service_stop_failure_preserves_installation_and_password(self):
self.put(self.home / ".config/systemd/user/net.a-wan.pipelines.service",
"# Managed by Pipelines native installer\n")
auth = self.put(self.home / ".config/pipelines/web-auth.json", "keep password")
marker = self.put(self.prefix / "keep", "keep")
original = self.recorder
def fail_stop(command, **kwargs):
if command[0] == "systemctl" and "disable" in command:
original.calls.append(command)
return subprocess.CompletedProcess(command, 1, stdout="", stderr="failed")
return original(command, **kwargs)
with self.assertRaises(RuntimeError):
uninstall.Uninstall(home=self.home, repo=self.repo,
environ={}, runner=fail_stop, system="Linux").run()
self.assertEqual(auth.read_text(), "keep password")
self.assertEqual(marker.read_text(), "keep")
self.assertFalse(any("rm" in c for c in original.calls))
def test_docker_cleanup_is_scoped_to_pipelines(self):
self.resetter().run()
docker = [c for c in self.recorder.calls if c and c[0] == "docker"]
for command in docker:
if command[1:3] == ["ps", "-aq"] or (command[1:3] == ["volume", "ls"] and "-q" in command):
self.assertIn("label=com.docker.compose.project=pipelines", command)
self.assertNotIn("prune", command)
self.assertFalse(any("unrelated:latest" in c and "rm" in c for c in docker))
self.assertFalse(any("unrelated-volume" in c and "rm" in c for c in docker))
self.assertTrue(any("fixture-container" in c and "rm" in c for c in docker))
self.assertTrue(any("pipelines-flutter-pub" in c and "rm" in c for c in docker))
self.assertTrue(any("pipelines-flutter-web:3.44.6" in c and ("rm" in c or "rmi" in c) for c in docker))
for owned in ("ab12cd34", "aaabbb", "pipelines_pipelines-data", "pipelines_ntfy-data"):
self.assertTrue(any(owned in c and "rm" in c for c in docker), owned)
for unrelated in ("deadbeef", "baadf00d"):
self.assertFalse(any(unrelated in c and "rm" in c for c in docker), unrelated)
def test_custom_cache_cannot_delete_checkout_source(self):
source = self.put(self.repo / "app/lib/main.dart", "keep source")
self.environment["PIPELINES_APP_CACHE"] = str(self.repo / "app")
with self.assertRaises(ValueError):
self.resetter().run()
self.assertEqual(source.read_text(), "keep source")
self.assertEqual(self.recorder.calls, [])
def test_only_owned_hosts_and_manual_supervisor_are_terminated(self):
original = self.recorder
commands = [f"{self.prefix}/bin/pipelines-claude-host --socket fixture",
f"python3 {self.prefix}/lib/manage.py supervise --prefix {self.prefix}",
f"node {self.prefix}/lib/web/serve.mjs --port 8082",
"codex app-server", f"{self.home}/other/bin/pipelines-claude-host"]
def processes(command, **kwargs):
if command[0] == "ps":
return subprocess.CompletedProcess(command, 0, stdout="\n".join(
f"{40001 + i} {text}" for i, text in enumerate(commands)), stderr="")
return original(command, **kwargs)
signalled = []
def signal_process(pid, number):
if number == 0:
raise ProcessLookupError()
signalled.append(pid)
with patch.object(uninstall.os, "kill", side_effect=signal_process):
uninstall.Uninstall(home=self.home, repo=self.repo, environ={},
runner=processes, system="Linux").run()
self.assertEqual(signalled, [40001, 40002, 40003])
def test_macos_reset_uses_only_its_launch_agent(self):
unit = self.put(self.home / "Library/LaunchAgents/net.a-wan.pipelines.plist",
"Managed by Pipelines native installer")
neighbor = self.put(unit.parent / "other.plist", "keep")
uninstall.Uninstall(home=self.home, repo=self.repo, environ={},
runner=self.recorder, system="Darwin").run()
self.assertFalse(unit.exists())
self.assertEqual(neighbor.read_text(), "keep")
self.assertTrue(any(c[:2] == ["launchctl", "bootout"] for c in self.recorder.calls))
self.assertFalse(any(c[0] == "systemctl" for c in self.recorder.calls))