135 lines
3.8 KiB
Python
135 lines
3.8 KiB
Python
"""Tests for configuration management."""
|
|
|
|
import pytest
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from mcp_server_cli.config import (
|
|
ConfigManager,
|
|
load_config_from_path,
|
|
create_config_template,
|
|
)
|
|
from mcp_server_cli.models import AppConfig, ServerConfig, LocalLLMConfig, SecurityConfig
|
|
|
|
|
|
class TestConfigManager:
|
|
"""Tests for ConfigManager."""
|
|
|
|
def test_load_default_config(self, tmp_path):
|
|
"""Test loading default configuration."""
|
|
config_path = tmp_path / "config.yaml"
|
|
config_path.write_text("")
|
|
|
|
manager = ConfigManager(config_path)
|
|
config = manager.load()
|
|
|
|
assert isinstance(config, AppConfig)
|
|
assert config.server.port == 3000
|
|
assert config.server.host == "127.0.0.1"
|
|
|
|
def test_load_config_with_values(self, tmp_path):
|
|
"""Test loading configuration with custom values."""
|
|
config_file = tmp_path / "config.yaml"
|
|
config_file.write_text("""
|
|
server:
|
|
host: \"127.0.0.1\"
|
|
port: 8080
|
|
log_level: \"DEBUG\"
|
|
|
|
llm:
|
|
enabled: false
|
|
base_url: \"http://localhost:11434\"
|
|
model: \"llama2\"
|
|
|
|
security:
|
|
allowed_commands:
|
|
- ls
|
|
- cat
|
|
- echo
|
|
blocked_paths:
|
|
- /etc
|
|
- /root
|
|
""")
|
|
|
|
manager = ConfigManager(config_file)
|
|
config = manager.load()
|
|
|
|
assert config.server.port == 8080
|
|
assert config.server.host == "127.0.0.1"
|
|
assert config.server.log_level == "DEBUG"
|
|
|
|
|
|
class TestConfigFromPath:
|
|
"""Tests for loading config from path."""
|
|
|
|
def test_load_from_path_success(self, tmp_path):
|
|
"""Test successful config loading from path."""
|
|
config_file = tmp_path / "config.yaml"
|
|
config_file.write_text("server:\n port: 8080")
|
|
|
|
config = load_config_from_path(str(config_file))
|
|
assert config.server.port == 8080
|
|
|
|
def test_load_from_path_not_found(self):
|
|
"""Test loading from nonexistent path."""
|
|
with pytest.raises(FileNotFoundError):
|
|
load_config_from_path("/nonexistent/path/config.yaml")
|
|
|
|
|
|
class TestConfigTemplate:
|
|
"""Tests for configuration template."""
|
|
|
|
def test_create_template(self):
|
|
"""Test creating a config template."""
|
|
template = create_config_template()
|
|
|
|
assert "server" in template
|
|
assert "llm" in template
|
|
assert "security" in template
|
|
assert "tools" in template
|
|
|
|
def test_template_has_required_fields(self):
|
|
"""Test that template has all required fields."""
|
|
template = create_config_template()
|
|
|
|
assert template["server"]["port"] == 3000
|
|
assert "allowed_commands" in template["security"]
|
|
|
|
|
|
class TestConfigValidation:
|
|
"""Tests for configuration validation."""
|
|
|
|
def test_valid_config(self):
|
|
"""Test creating a valid config."""
|
|
config = AppConfig(
|
|
server=ServerConfig(port=4000, host="localhost"),
|
|
llm=LocalLLMConfig(enabled=True, base_url="http://localhost:1234"),
|
|
)
|
|
assert config.server.port == 4000
|
|
assert config.llm.enabled is True
|
|
|
|
def test_config_with_empty_tools(self):
|
|
"""Test config with empty tools list."""
|
|
config = AppConfig(tools=[])
|
|
assert len(config.tools) == 0
|
|
|
|
|
|
class TestEnvVarMapping:
|
|
"""Tests for environment variable mappings."""
|
|
|
|
def test_get_env_var_name(self):
|
|
"""Test environment variable name generation."""
|
|
manager = ConfigManager()
|
|
assert manager.get_env_var_name("server.port") == "MCP_SERVER_PORT"
|
|
assert manager.get_env_var_name("host") == "MCP_HOST"
|
|
|
|
def test_get_from_env(self):
|
|
"""Test getting values from environment."""
|
|
manager = ConfigManager()
|
|
os.environ["MCP_TEST_VAR"] = "test_value"
|
|
try:
|
|
result = manager.get_from_env("test_var")
|
|
assert result == "test_value"
|
|
finally:
|
|
del os.environ["MCP_TEST_VAR"]
|