Files
bot-bottle/tests/unit/test_cli_backend.py
T
didericis dd2e83b8a9
lint / lint (push) Failing after 2m1s
test / unit (pull_request) Successful in 1m3s
test / integration (pull_request) Successful in 23s
test / coverage (pull_request) Failing after 1m11s
refactor(backend): generic setup/status; drop firecracker-only CLI
Add abstract `setup()`/`status()` classmethods to BottleBackend (same
per-host, no-instance shape as is_available) so host provisioning is
part of the backend contract, not a per-backend command. Replace the
`./cli.py firecracker {setup,status}` command with a generic
`./cli.py backend {setup,status} [--backend=NAME]` that resolves a
backend (flag / $BOT_BOTTLE_BACKEND / host default) and dispatches —
swapping backends is just a different --backend.

Implementations:
- firecracker: moved out of cli/ into backend/firecracker/setup.py
  (network pool module/script + range-overlap check), unchanged output.
- docker: new backend/docker/setup.py — reports docker on PATH, daemon
  reachability, and gVisor runsc; setup notes no privileged pool is
  needed. Minimal placeholder; richer version tracked in #345.
- macos-container: new setup.py — container CLI + system-service checks.

Also retarget the launch-preflight / isolation-probe pointers to the new
command. New test_cli_backend covers dispatch + docker setup/status.

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

80 lines
3.0 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
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_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())
if __name__ == "__main__":
unittest.main()