This commit is contained in:
2026-03-16 08:06:25 +02:00
parent 78d4064853
commit d0f8315cf1
35 changed files with 2493 additions and 624 deletions

View File

@@ -1,13 +1,11 @@
"""Tests for ContainerService."""
import subprocess
from pathlib import Path
from unittest.mock import MagicMock, patch
from flow.core.config import AppConfig, FlowContext
from flow.core.console import Console
from flow.core.platform import PlatformInfo
from flow.core.runtime import CommandRunner, FileSystem, SystemRuntime
from flow.core.runtime import CommandRunner, SystemRuntime
from flow.core import paths
from flow.services.containers import ContainerService
@@ -19,6 +17,11 @@ class FakeRunner(CommandRunner):
def run(self, argv, *, cwd=None, env=None, capture_output=True, check=False, timeout=None):
self.calls.append(("run", list(argv)))
command = list(argv)
if command[:4] == ["docker", "container", "ls", "-a"]:
return subprocess.CompletedProcess(argv, 0, stdout="dev-api\n", stderr="")
if command[:3] == ["docker", "container", "ls"]:
return subprocess.CompletedProcess(argv, 0, stdout="dev-api\n", stderr="")
return subprocess.CompletedProcess(argv, 0, stdout="", stderr="")
def run_shell(self, command, *, cwd=None, env=None, capture_output=True, check=False, timeout=None):
@@ -43,31 +46,35 @@ class TestContainerService:
def test_create_dry_run(self, tmp_path, capsys, monkeypatch):
monkeypatch.setattr(paths, "HOME", tmp_path)
monkeypatch.setattr(paths, "DOTFILES_DIR", tmp_path / "dotfiles")
monkeypatch.setattr("flow.services.containers.runtime", lambda: "docker")
ctx = _make_ctx(tmp_path)
svc = ContainerService(ctx)
svc.create("devbox", "personal", dry_run=True)
svc.create("api", "tm0/node", dry_run=True)
output = capsys.readouterr().out
assert "devbox" in output
assert "dev-api" in output
def test_list_no_docker(self, tmp_path, capsys):
def test_list_no_containers(self, tmp_path, capsys, monkeypatch):
runner = FakeRunner()
monkeypatch.setattr("flow.services.containers.runtime", lambda: "docker")
runner.run = lambda argv, **kwargs: subprocess.CompletedProcess(argv, 0, stdout="", stderr="")
ctx = _make_ctx(tmp_path, runner=runner)
svc = ContainerService(ctx)
svc.list()
# FakeRunner returns empty stdout -> "No flow containers"
output = capsys.readouterr().out
assert "No flow containers" in output
def test_stop_calls_docker(self, tmp_path):
def test_stop_calls_docker(self, tmp_path, monkeypatch):
runner = FakeRunner()
monkeypatch.setattr("flow.services.containers.runtime", lambda: "docker")
ctx = _make_ctx(tmp_path, runner=runner)
svc = ContainerService(ctx)
svc.stop("flow-personal-devbox")
svc.stop("api")
assert any("docker" in str(c) and "stop" in str(c) for c in runner.calls)
def test_remove_calls_docker(self, tmp_path):
def test_remove_calls_docker(self, tmp_path, monkeypatch):
runner = FakeRunner()
monkeypatch.setattr("flow.services.containers.runtime", lambda: "docker")
ctx = _make_ctx(tmp_path, runner=runner)
svc = ContainerService(ctx)
svc.remove("flow-personal-devbox")
svc.remove("api")
assert any("docker" in str(c) and "rm" in str(c) for c in runner.calls)