test: add comprehensive tests for tool workshop functionality
Backend tests: - Unit tests for docker_build service (successful/failed builds, context, paths) - Unit tests for readiness_probe service (success, timeout, retries, edge cases) - Integration tests for config_folders API (CRUD + project overrides) - Integration tests for tool_types API with new fields - Integration tests for tool_configs API with new fields Frontend tests: - ToolWorkshopPage component tests (all 3 tabs, create/edit/delete) - API client tests for tool_types and config_folders Fixes: - Add field_validator import to tool_configs.py - Add JSON import to tool_config model - Update frontend test button names to match UI (Create Tool Type, Add Config, Create Folder) Quality gates: backend unit tests passing (23/23)
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
"""Unit tests for docker build service."""
|
||||
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from src.services.docker_build import build_image
|
||||
|
||||
|
||||
class TestBuildImage:
|
||||
"""Tests for build_image function."""
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_builds_image_successfully(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=0,
|
||||
stdout="Successfully built abc123",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = build_image(tmpdir, "FROM python:3.11", "test-image:latest")
|
||||
|
||||
assert result[0] == 0
|
||||
assert "Successfully built" in result[1]
|
||||
mock_run.assert_called_once()
|
||||
call_args = mock_run.call_args
|
||||
assert "test-image:latest" in call_args[0][0]
|
||||
assert "build" in call_args[0][0]
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_fails(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=1,
|
||||
stdout="",
|
||||
stderr="Error: failed to build",
|
||||
)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = build_image(tmpdir, "FROM invalid:image", "test-image:latest")
|
||||
|
||||
assert result[0] == 1
|
||||
assert "failed to build" in result[2]
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_with_tag(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=0,
|
||||
stdout="",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
build_image(tmpdir, "FROM python:3.11", "my-registry/tool:v1.0")
|
||||
|
||||
call_args = mock_run.call_args[0][0]
|
||||
assert "my-registry/tool:v1.0" in call_args
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_command_structure(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
build_image(tmpdir, "FROM python:3.11", "test:latest")
|
||||
|
||||
cmd = mock_run.call_args[0][0]
|
||||
assert cmd[0] == "docker"
|
||||
assert cmd[1] == "build"
|
||||
assert "-t" in cmd
|
||||
assert "test:latest" in cmd
|
||||
assert tmpdir in cmd
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_writes_dockerfile(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
dockerfile_content = "FROM python:3.11\\nRUN pip install flask"
|
||||
build_image(tmpdir, dockerfile_content, "test:latest")
|
||||
|
||||
dockerfile_path = Path(tmpdir) / "Dockerfile"
|
||||
assert dockerfile_path.exists()
|
||||
assert dockerfile_path.read_text() == dockerfile_content
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_writes_context_files(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
build_context = {
|
||||
"requirements.txt": "flask==2.0\\nnumpy==1.21",
|
||||
"app.py": "from flask import Flask\\napp = Flask(__name__)",
|
||||
}
|
||||
build_image(tmpdir, "FROM python:3.11", "test:latest", build_context)
|
||||
|
||||
req_path = Path(tmpdir) / "requirements.txt"
|
||||
app_path = Path(tmpdir) / "app.py"
|
||||
assert req_path.exists()
|
||||
assert req_path.read_text() == "flask==2.0\\nnumpy==1.21"
|
||||
assert app_path.exists()
|
||||
assert app_path.read_text() == "from flask import Flask\\napp = Flask(__name__)"
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_creates_nested_directories(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
build_context = {
|
||||
"src/app.py": "print('hello')",
|
||||
}
|
||||
build_image(tmpdir, "FROM python:3.11", "test:latest", build_context)
|
||||
|
||||
app_path = Path(tmpdir) / "src" / "app.py"
|
||||
assert app_path.exists()
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_prevents_path_traversal(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
build_context = {
|
||||
"../../../etc/passwd": "root:x:0:0",
|
||||
}
|
||||
|
||||
with pytest.raises(ValueError, match="escapes instance directory"):
|
||||
build_image(tmpdir, "FROM python:3.11", "test:latest", build_context)
|
||||
|
||||
mock_run.assert_not_called()
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_timeout(self, mock_run) -> None:
|
||||
mock_run.side_effect = subprocess.TimeoutExpired(cmd=["docker", "build"], timeout=300)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = build_image(tmpdir, "FROM python:3.11", "test:latest")
|
||||
|
||||
assert result[0] == 1
|
||||
assert "timed out" in result[2].lower()
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_build_exception(self, mock_run) -> None:
|
||||
mock_run.side_effect = OSError("Docker not available")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = build_image(tmpdir, "FROM python:3.11", "test:latest")
|
||||
|
||||
assert result[0] == 1
|
||||
assert "Docker not available" in result[2]
|
||||
@@ -0,0 +1,217 @@
|
||||
"""Unit tests for readiness probe service."""
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from src.services.readiness_probe import execute_probe
|
||||
|
||||
|
||||
class TestExecuteProbe:
|
||||
"""Tests for execute_probe function."""
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_probe_succeeds_first_attempt(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=0,
|
||||
stdout="healthy",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
result, logs = await execute_probe("container-123", "curl -f http://localhost:8080")
|
||||
|
||||
assert result is True
|
||||
assert any("Success" in log for log in logs)
|
||||
mock_run.assert_called_once_with(
|
||||
["docker", "exec", "container-123", "sh", "-c", "curl -f http://localhost:8080"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=2,
|
||||
)
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_probe_fails_then_succeeds(self, mock_run) -> None:
|
||||
mock_run.side_effect = [
|
||||
MagicMock(returncode=1, stdout="", stderr="Connection refused"),
|
||||
MagicMock(returncode=1, stdout="", stderr="Connection refused"),
|
||||
MagicMock(returncode=0, stdout="healthy", stderr=""),
|
||||
]
|
||||
|
||||
result, logs = await execute_probe("container-123", "curl -f http://localhost:8080", timeout=10, interval=0.1)
|
||||
|
||||
assert result is True
|
||||
assert mock_run.call_count == 3
|
||||
assert any("Attempt 1: Failed" in log for log in logs)
|
||||
assert any("Attempt 3: Success" in log for log in logs)
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_probe_times_out(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=1,
|
||||
stdout="",
|
||||
stderr="Connection refused",
|
||||
)
|
||||
|
||||
result, logs = await execute_probe("container-123", "curl -f http://localhost:8080", timeout=0.5, interval=0.2)
|
||||
|
||||
assert result is False
|
||||
assert any("timed out" in log.lower() for log in logs)
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_probe_command_not_found(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=127,
|
||||
stdout="",
|
||||
stderr="command not found",
|
||||
)
|
||||
|
||||
result, logs = await execute_probe("container-123", "nonexistent-command", timeout=1, interval=0.3)
|
||||
|
||||
assert result is False
|
||||
assert any("exit code 127" in log for log in logs)
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_probe_exception(self, mock_run) -> None:
|
||||
mock_run.side_effect = OSError("Docker not available")
|
||||
|
||||
result, logs = await execute_probe("container-123", "curl http://localhost", timeout=1, interval=0.3)
|
||||
|
||||
assert result is False
|
||||
assert any("Error" in log for log in logs)
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_probe_with_special_characters(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=0,
|
||||
stdout="",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
cmd = "bash -c 'echo \"hello world\" && exit 0'"
|
||||
await execute_probe("container-123", cmd)
|
||||
|
||||
call_args = mock_run.call_args
|
||||
assert cmd in call_args[0][0]
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_probe_captures_stdout(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=0,
|
||||
stdout="Server is ready\\nVersion: 1.0",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
result, logs = await execute_probe("container-123", "cat /app/status")
|
||||
|
||||
assert result is True
|
||||
assert any("Server is ready" in log for log in logs)
|
||||
|
||||
|
||||
class TestIntegrationScenarios:
|
||||
"""Integration-style tests with realistic scenarios."""
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_web_server_probe(self, mock_run) -> None:
|
||||
"""Test typical web server health check."""
|
||||
mock_run.side_effect = [
|
||||
MagicMock(returncode=1, stdout="", stderr=""),
|
||||
MagicMock(returncode=1, stdout="", stderr=""),
|
||||
MagicMock(returncode=1, stdout="", stderr=""),
|
||||
MagicMock(returncode=0, stdout="OK", stderr=""),
|
||||
]
|
||||
|
||||
result, logs = await execute_probe(
|
||||
"web-container",
|
||||
"curl -f http://localhost:8080/health",
|
||||
timeout=10,
|
||||
interval=0.2,
|
||||
)
|
||||
|
||||
assert result is True
|
||||
assert mock_run.call_count == 4
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_command_probe(self, mock_run) -> None:
|
||||
"""Test command availability check."""
|
||||
mock_run.return_value = MagicMock(
|
||||
returncode=0,
|
||||
stdout="opencode 1.0.0",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
result, logs = await execute_probe(
|
||||
"tool-container",
|
||||
"which opencode && opencode --version",
|
||||
timeout=30,
|
||||
interval=2,
|
||||
)
|
||||
|
||||
assert result is True
|
||||
assert any("opencode 1.0.0" in log for log in logs)
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_database_probe(self, mock_run) -> None:
|
||||
"""Test database readiness check."""
|
||||
mock_run.side_effect = [
|
||||
MagicMock(returncode=1, stdout="", stderr=""),
|
||||
MagicMock(returncode=1, stdout="", stderr=""),
|
||||
MagicMock(returncode=0, stdout="/var/run/postgresql:5432 - accepting connections", stderr=""),
|
||||
]
|
||||
|
||||
result, logs = await execute_probe(
|
||||
"db-container",
|
||||
"pg_isready -U postgres",
|
||||
timeout=10,
|
||||
interval=0.3,
|
||||
)
|
||||
|
||||
assert result is True
|
||||
assert mock_run.call_count == 3
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_file_probe(self, mock_run) -> None:
|
||||
"""Test file existence check."""
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
|
||||
|
||||
result, logs = await execute_probe(
|
||||
"app-container",
|
||||
"[ -f /app/ready ]",
|
||||
timeout=10,
|
||||
interval=1,
|
||||
)
|
||||
|
||||
assert result is True
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_slow_starting_service(self, mock_run) -> None:
|
||||
"""Test service that takes time to start."""
|
||||
# Simulate 5 failures before success
|
||||
side_effects = [MagicMock(returncode=1, stdout="", stderr="")] * 5
|
||||
side_effects.append(MagicMock(returncode=0, stdout="Ready", stderr=""))
|
||||
mock_run.side_effect = side_effects
|
||||
|
||||
result, logs = await execute_probe(
|
||||
"slow-container",
|
||||
"curl -f http://localhost:8080",
|
||||
timeout=10,
|
||||
interval=0.2,
|
||||
)
|
||||
|
||||
assert result is True
|
||||
assert mock_run.call_count == 6
|
||||
assert any("Attempt 6: Success" in log for log in logs)
|
||||
|
||||
@patch("subprocess.run")
|
||||
async def test_zero_timeout_immediate_return(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(returncode=1, stdout="", stderr="")
|
||||
|
||||
result, logs = await execute_probe(
|
||||
"container",
|
||||
"test",
|
||||
timeout=0,
|
||||
interval=1,
|
||||
)
|
||||
|
||||
assert result is False
|
||||
assert any("timed out" in log.lower() for log in logs)
|
||||
Reference in New Issue
Block a user