Files
bot-bottle/tests/unit/test_cli_backend.py
T
didericis 656966c2c4
lint / lint (push) Failing after 1m56s
test / unit (pull_request) Successful in 52s
test / integration (pull_request) Successful in 17s
test / coverage (pull_request) Failing after 1m2s
feat(backend): add teardown() to the contract; backend teardown
Add an abstract teardown() classmethod to BottleBackend — the inverse of
setup(), surfaced as `./cli.py backend teardown [--backend=NAME]`
(uninstall). Symmetric with setup: it prints the privileged commands /
declarative config change to remove the host prerequisites.

- firecracker: NixOS-aware — disable the flake module (or drop the
  import) and rebuild, or `firecracker-netpool.sh down` imperatively.
- docker / macos-container: nothing to undo (no privileged host state);
  print a short note.

Not called by the launch path or the test suite. Extends test_cli_backend
for the new dispatch.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WBMWTEtQdJ4W5UrWuLHCck
2026-07-11 16:56:17 -04:00

92 lines
3.5 KiB
Python

"""Unit: the generic `backend` CLI command.
`./cli.py backend {setup,status} [--backend=NAME]` resolves a backend
and dispatches to its `setup()` / `status()` classmethods — no
backend-specific commands. Also covers the docker backend's own
setup/status logic (the firecracker path is exercised by
test_firecracker_backend via netpool)."""
from __future__ import annotations
import subprocess
import unittest
from unittest.mock import MagicMock, patch
from bot_bottle.cli import backend as cmd
from bot_bottle.backend.docker import setup as docker_setup
class TestCmdBackendDispatch(unittest.TestCase):
def _fake_backend(self):
b = MagicMock()
b.setup.return_value = 0
b.status.return_value = 3
b.teardown.return_value = 0
return b
def test_setup_dispatches_to_backend(self):
b = self._fake_backend()
with patch.object(cmd, "get_bottle_backend", return_value=b) as gbb:
rc = cmd.cmd_backend(["setup", "--backend", "firecracker"])
gbb.assert_called_once_with("firecracker")
b.setup.assert_called_once_with()
b.status.assert_not_called()
self.assertEqual(0, rc)
def test_status_dispatches_and_returns_backend_code(self):
b = self._fake_backend()
with patch.object(cmd, "get_bottle_backend", return_value=b):
rc = cmd.cmd_backend(["status", "--backend", "docker"])
b.status.assert_called_once_with()
self.assertEqual(3, rc)
def test_teardown_dispatches_to_backend(self):
b = self._fake_backend()
with patch.object(cmd, "get_bottle_backend", return_value=b):
rc = cmd.cmd_backend(["teardown", "--backend", "firecracker"])
b.teardown.assert_called_once_with()
b.setup.assert_not_called()
self.assertEqual(0, rc)
def test_no_backend_flag_uses_host_default(self):
b = self._fake_backend()
with patch.object(cmd, "get_bottle_backend", return_value=b) as gbb:
cmd.cmd_backend(["status"])
gbb.assert_called_once_with(None)
def test_unknown_action_exits(self):
with self.assertRaises(SystemExit):
cmd.cmd_backend(["bogus"])
def test_unknown_backend_rejected_by_argparse(self):
with self.assertRaises(SystemExit):
cmd.cmd_backend(["status", "--backend", "nope"])
class TestDockerSetupStatus(unittest.TestCase):
def _ok(self):
return subprocess.CompletedProcess([], 0, stdout="", stderr="")
def test_setup_ok_when_docker_and_daemon_present(self):
with patch.object(docker_setup.shutil, "which", return_value="/usr/bin/docker"), \
patch.object(docker_setup, "_daemon_reachable", return_value=True), \
patch.object(docker_setup._util, "runsc_available", return_value=True):
self.assertEqual(0, docker_setup.setup())
def test_setup_fails_when_docker_missing(self):
with patch.object(docker_setup.shutil, "which", return_value=None):
self.assertEqual(1, docker_setup.setup())
def test_status_fails_when_daemon_unreachable(self):
with patch.object(docker_setup.shutil, "which", return_value="/usr/bin/docker"), \
patch.object(docker_setup, "_daemon_reachable", return_value=False), \
patch.object(docker_setup._util, "runsc_available", return_value=False):
self.assertEqual(1, docker_setup.status())
def test_teardown_is_noop_success(self):
self.assertEqual(0, docker_setup.teardown())
if __name__ == "__main__":
unittest.main()