a3a9ec065e
`./cli.py cleanup` previously called only the env-var-selected backend's `prepare_cleanup` / `cleanup` — so a leftover smolvm machine + bundle container + bundle network from a crashed smolmachines bottle would survive a default `docker`-mode cleanup indefinitely. Smolmachines now has a real `cleanup` module (alongside `enumerate.py` from issue #77) that walks: - smolvm machines named `claude-bottle-*` (via `smolvm machine ls --json`) - bundle containers `claude-bottle-sidecars-*` - bundle networks `claude-bottle-bundle-*` Cleanup runs stop+delete on the machines, force-rm on the containers, network rm on the networks. Each step is best-effort so a failed rm doesn't block the rest. `cli.py cleanup` walks every backend in `known_backend_names()` and runs each backend's `cleanup` after a single y/N prompt that shows a combined plan. State dirs (`~/.claude-bottle/state/<slug>/`) are shared layout with the docker backend, which still owns the orphan-state-dir bucket. It now consults `enumerate_active_bottles()` for the cross-backend live identity set so a running smolmachines bottle's state dir isn't reaped during a cleanup. Tests: smolmachines cleanup (prepare + cleanup ordering + failure handling); cross-backend orphan protection on the docker state-dir check; CLI cmd_cleanup walks both backends, short- circuits on all-empty, aborts on N. 617 unit tests pass. End-to-end verified on this host: $ smolvm machine ls --json | jq '.[].name' "claude-bottle-researcher-m3hxd" $ ./cli.py cleanup --- smolmachines backend --- smolvm machine: claude-bottle-researcher-m3hxd remove all of the above? [y/N] Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
"""Unit: `cli.py cleanup` walks every backend (issue follow-up).
|
|
|
|
Asserts cmd_cleanup queries each backend's `prepare_cleanup`,
|
|
combines the y/N output, and runs each backend's `cleanup` when
|
|
the operator confirms. Mocks the backends and stdin."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
from claude_bottle.cli import cleanup as cmd
|
|
|
|
|
|
def _make_backend(empty: bool = True):
|
|
backend = MagicMock()
|
|
plan = MagicMock(empty=empty)
|
|
backend.prepare_cleanup.return_value = plan
|
|
backend.cleanup = MagicMock()
|
|
return backend, plan
|
|
|
|
|
|
class TestCmdCleanup(unittest.TestCase):
|
|
def test_iterates_every_backend(self):
|
|
docker, docker_plan = _make_backend(empty=False)
|
|
smol, smol_plan = _make_backend(empty=False)
|
|
backends_by_name = {"docker": docker, "smolmachines": smol}
|
|
|
|
with patch.object(
|
|
cmd, "known_backend_names",
|
|
return_value=("docker", "smolmachines"),
|
|
), patch.object(
|
|
cmd, "get_bottle_backend",
|
|
side_effect=lambda name: backends_by_name[name],
|
|
), patch.object(
|
|
cmd, "_prompt_yes", return_value=True,
|
|
):
|
|
self.assertEqual(0, cmd.cmd_cleanup([]))
|
|
|
|
docker.prepare_cleanup.assert_called_once()
|
|
smol.prepare_cleanup.assert_called_once()
|
|
docker.cleanup.assert_called_once_with(docker_plan)
|
|
smol.cleanup.assert_called_once_with(smol_plan)
|
|
|
|
def test_short_circuits_when_all_empty(self):
|
|
docker, _ = _make_backend(empty=True)
|
|
smol, _ = _make_backend(empty=True)
|
|
backends_by_name = {"docker": docker, "smolmachines": smol}
|
|
|
|
with patch.object(
|
|
cmd, "known_backend_names",
|
|
return_value=("docker", "smolmachines"),
|
|
), patch.object(
|
|
cmd, "get_bottle_backend",
|
|
side_effect=lambda name: backends_by_name[name],
|
|
), patch.object(
|
|
cmd, "_prompt_yes",
|
|
) as prompt:
|
|
self.assertEqual(0, cmd.cmd_cleanup([]))
|
|
prompt.assert_not_called()
|
|
docker.cleanup.assert_not_called()
|
|
smol.cleanup.assert_not_called()
|
|
|
|
def test_abort_at_prompt_runs_nothing(self):
|
|
docker, _ = _make_backend(empty=False)
|
|
smol, _ = _make_backend(empty=True)
|
|
backends_by_name = {"docker": docker, "smolmachines": smol}
|
|
|
|
with patch.object(
|
|
cmd, "known_backend_names",
|
|
return_value=("docker", "smolmachines"),
|
|
), patch.object(
|
|
cmd, "get_bottle_backend",
|
|
side_effect=lambda name: backends_by_name[name],
|
|
), patch.object(
|
|
cmd, "_prompt_yes", return_value=False,
|
|
):
|
|
self.assertEqual(0, cmd.cmd_cleanup([]))
|
|
docker.cleanup.assert_not_called()
|
|
smol.cleanup.assert_not_called()
|
|
|
|
def test_skips_empty_plans_when_others_have_work(self):
|
|
# docker has work, smolmachines doesn't — only docker.cleanup
|
|
# is called.
|
|
docker, docker_plan = _make_backend(empty=False)
|
|
smol, _ = _make_backend(empty=True)
|
|
backends_by_name = {"docker": docker, "smolmachines": smol}
|
|
|
|
with patch.object(
|
|
cmd, "known_backend_names",
|
|
return_value=("docker", "smolmachines"),
|
|
), patch.object(
|
|
cmd, "get_bottle_backend",
|
|
side_effect=lambda name: backends_by_name[name],
|
|
), patch.object(
|
|
cmd, "_prompt_yes", return_value=True,
|
|
):
|
|
cmd.cmd_cleanup([])
|
|
docker.cleanup.assert_called_once_with(docker_plan)
|
|
smol.cleanup.assert_not_called()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|