163 lines
7.9 KiB
Python
163 lines
7.9 KiB
Python
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
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[:3] == ["docker", "volume", "ls"]:
|
|
stdout = "pipelines_pipelines-data\npipelines_ntfy-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"
|
|
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", "old password")
|
|
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_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:2] == ["ps"] or command[1:3] == ["volume", "ls"]:
|
|
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.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))
|