mirror of
https://github.com/Monadical-SAS/cubbi.git
synced 2025-12-20 20:29:06 +00:00
feat: add user port support (#26)
* feat: add user port support * fix: fix unit test and improve isolation * refactor: remove some fixture
This commit is contained in:
@@ -2,17 +2,18 @@
|
||||
Common test fixtures for Cubbi Container tests.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
import tempfile
|
||||
import pytest
|
||||
import docker
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from cubbi.container import ContainerManager
|
||||
from cubbi.session import SessionManager
|
||||
import docker
|
||||
import pytest
|
||||
|
||||
from cubbi.config import ConfigManager
|
||||
from cubbi.container import ContainerManager
|
||||
from cubbi.models import Session, SessionStatus
|
||||
from cubbi.session import SessionManager
|
||||
from cubbi.user_config import UserConfigManager
|
||||
|
||||
|
||||
@@ -41,13 +42,6 @@ requires_docker = pytest.mark.skipif(
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_dir():
|
||||
"""Create a temporary directory for test files."""
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
yield Path(tmp_dir)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_config_dir():
|
||||
"""Create a temporary directory for configuration files."""
|
||||
@@ -56,76 +50,26 @@ def temp_config_dir():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def isolated_config(temp_config_dir):
|
||||
"""Provide an isolated UserConfigManager instance."""
|
||||
config_path = temp_config_dir / "config.yaml"
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
return UserConfigManager(str(config_path))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def isolated_session_manager(temp_config_dir):
|
||||
"""Create an isolated session manager for testing."""
|
||||
sessions_path = temp_config_dir / "sessions.yaml"
|
||||
return SessionManager(sessions_path)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def isolated_config_manager():
|
||||
"""Create an isolated config manager for testing."""
|
||||
config_manager = ConfigManager()
|
||||
# Ensure we're using the built-in images, not trying to load from user config
|
||||
return config_manager
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_session_manager():
|
||||
"""Mock the SessionManager class."""
|
||||
with patch("cubbi.cli.session_manager") as mock_manager:
|
||||
yield mock_manager
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_container_manager():
|
||||
"""Mock the ContainerManager class with proper initialization."""
|
||||
def mock_container_manager(isolate_cubbi_config):
|
||||
"""Mock the ContainerManager class with proper behaviors for testing."""
|
||||
mock_session = Session(
|
||||
id="test-session-id",
|
||||
name="test-session",
|
||||
image="goose",
|
||||
status=SessionStatus.RUNNING,
|
||||
ports={"8080": "8080"},
|
||||
ports={8080: 32768},
|
||||
)
|
||||
|
||||
with patch("cubbi.cli.container_manager") as mock_manager:
|
||||
# Set behaviors to avoid TypeErrors
|
||||
mock_manager.list_sessions.return_value = []
|
||||
mock_manager.create_session.return_value = mock_session
|
||||
mock_manager.close_session.return_value = True
|
||||
mock_manager.close_all_sessions.return_value = (3, True)
|
||||
# MCP-related mocks
|
||||
mock_manager.get_mcp_status.return_value = {
|
||||
"status": "running",
|
||||
"container_id": "test-id",
|
||||
}
|
||||
mock_manager.start_mcp.return_value = {
|
||||
"status": "running",
|
||||
"container_id": "test-id",
|
||||
}
|
||||
mock_manager.stop_mcp.return_value = True
|
||||
mock_manager.restart_mcp.return_value = {
|
||||
"status": "running",
|
||||
"container_id": "test-id",
|
||||
}
|
||||
mock_manager.get_mcp_logs.return_value = "Test log output"
|
||||
yield mock_manager
|
||||
container_manager = isolate_cubbi_config["container_manager"]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def container_manager(isolated_session_manager, isolated_config_manager):
|
||||
"""Create a container manager with isolated components."""
|
||||
return ContainerManager(
|
||||
config_manager=isolated_config_manager, session_manager=isolated_session_manager
|
||||
)
|
||||
# Patch the container manager methods for mocking
|
||||
with (
|
||||
patch.object(container_manager, "list_sessions", return_value=[]),
|
||||
patch.object(container_manager, "create_session", return_value=mock_session),
|
||||
patch.object(container_manager, "close_session", return_value=True),
|
||||
patch.object(container_manager, "close_all_sessions", return_value=(3, True)),
|
||||
):
|
||||
yield container_manager
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -137,28 +81,23 @@ def cli_runner():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_file_content(temp_dir):
|
||||
"""Create a test file with content in the temporary directory."""
|
||||
def test_file_content(temp_config_dir):
|
||||
"""Create a test file with content in a temporary directory."""
|
||||
test_content = "This is a test file for volume mounting"
|
||||
test_file = temp_dir / "test_volume_file.txt"
|
||||
test_file = temp_config_dir / "test_volume_file.txt"
|
||||
with open(test_file, "w") as f:
|
||||
f.write(test_content)
|
||||
return test_file, test_content
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_network_name():
|
||||
"""Generate a unique network name for testing."""
|
||||
return f"cubbi-test-network-{uuid.uuid4().hex[:8]}"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def docker_test_network(test_network_name):
|
||||
def docker_test_network():
|
||||
"""Create a Docker network for testing and clean it up after."""
|
||||
if not is_docker_available():
|
||||
pytest.skip("Docker is not available")
|
||||
return None
|
||||
|
||||
test_network_name = f"cubbi-test-network-{uuid.uuid4().hex[:8]}"
|
||||
client = docker.from_env()
|
||||
network = client.networks.create(test_network_name, driver="bridge")
|
||||
|
||||
@@ -172,8 +111,59 @@ def docker_test_network(test_network_name):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="function")
|
||||
def isolate_cubbi_config(temp_config_dir):
|
||||
"""
|
||||
Automatically isolate all Cubbi configuration for every test.
|
||||
|
||||
This fixture ensures that tests never touch the user's real configuration
|
||||
by patching both ConfigManager and UserConfigManager in cli.py to use
|
||||
temporary directories.
|
||||
"""
|
||||
# Create isolated config instances with temporary paths
|
||||
config_path = temp_config_dir / "config.yaml"
|
||||
user_config_path = temp_config_dir / "user_config.yaml"
|
||||
|
||||
# Create the ConfigManager with a custom config path
|
||||
isolated_config_manager = ConfigManager(config_path)
|
||||
|
||||
# Create the UserConfigManager with a custom config path
|
||||
isolated_user_config = UserConfigManager(str(user_config_path))
|
||||
|
||||
# Create isolated session manager
|
||||
sessions_path = temp_config_dir / "sessions.yaml"
|
||||
isolated_session_manager = SessionManager(sessions_path)
|
||||
|
||||
# Create isolated container manager
|
||||
isolated_container_manager = ContainerManager(
|
||||
isolated_config_manager, isolated_session_manager, isolated_user_config
|
||||
)
|
||||
|
||||
# Patch all the global instances in cli.py and the UserConfigManager class
|
||||
with (
|
||||
patch("cubbi.cli.config_manager", isolated_config_manager),
|
||||
patch("cubbi.cli.user_config", isolated_user_config),
|
||||
patch("cubbi.cli.session_manager", isolated_session_manager),
|
||||
patch("cubbi.cli.container_manager", isolated_container_manager),
|
||||
patch("cubbi.cli.UserConfigManager", return_value=isolated_user_config),
|
||||
):
|
||||
# Create isolated MCP manager with isolated user config
|
||||
from cubbi.mcp import MCPManager
|
||||
|
||||
isolated_mcp_manager = MCPManager(config_manager=isolated_user_config)
|
||||
|
||||
# Patch the global mcp_manager instance
|
||||
with patch("cubbi.cli.mcp_manager", isolated_mcp_manager):
|
||||
yield {
|
||||
"config_manager": isolated_config_manager,
|
||||
"user_config": isolated_user_config,
|
||||
"session_manager": isolated_session_manager,
|
||||
"container_manager": isolated_container_manager,
|
||||
"mcp_manager": isolated_mcp_manager,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_config_manager(isolated_config):
|
||||
"""Patch the UserConfigManager in cli.py to use our isolated instance."""
|
||||
with patch("cubbi.cli.user_config", isolated_config):
|
||||
yield isolated_config
|
||||
def patched_config_manager(isolate_cubbi_config):
|
||||
"""Compatibility fixture - returns the isolated user config."""
|
||||
return isolate_cubbi_config["user_config"]
|
||||
|
||||
Reference in New Issue
Block a user