12d532da7b
Closes #1, #4, #6, #7. Container lifecycle (#1, #7): - start_container, stop_container, restart_container, pause_container, unpause_container — all via SYNO.Docker.Container with JSON-encoded name parameter, routed through _resolve_container_name for hash- prefix resolution. stop is live-verified; the other four are implemented by symmetry on the same API surface. inspect_image (#4): - Returns full image detail (layers, env, ports, entrypoint/cmd, labels) via SYNO.Docker.Image/get. Accepts name:tag, registry- prefixed names, and bare hashes. Defensive response parsing handles both wrapped (details.*) and flat envelopes. system_overview (#6): - Aggregates CPU %, RAM, network and block I/O across all running containers plus running/stopped counts. No new DSM endpoint — composed from list + stats, reusing the container_stats CPU formula. Per-source errors are non-fatal. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
735 lines
24 KiB
Python
735 lines
24 KiB
Python
"""Tests for modules/images.py."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
|
|
def make_mock_mcp():
|
|
tools: dict = {}
|
|
|
|
class MockMCP:
|
|
def tool(self):
|
|
def decorator(fn):
|
|
tools[fn.__name__] = fn
|
|
return fn
|
|
|
|
return decorator
|
|
|
|
return MockMCP(), tools
|
|
|
|
|
|
def make_config():
|
|
from mcp_synology_container.config import AppConfig, ConnectionConfig
|
|
|
|
return AppConfig(
|
|
schema_version=1,
|
|
connection=ConnectionConfig(host="nas.local", port=443, https=True, verify_ssl=True),
|
|
)
|
|
|
|
|
|
SAMPLE_IMAGES = {
|
|
"images": [
|
|
{
|
|
"id": "sha256:aaaa",
|
|
"repository": "nginx",
|
|
"tags": ["1.24"],
|
|
"size": 50 * 1024 * 1024,
|
|
"created": 1700000000,
|
|
"upgradable": True,
|
|
},
|
|
{
|
|
"id": "sha256:bbbb",
|
|
"repository": "postgres",
|
|
"tags": ["15"],
|
|
"size": 80 * 1024 * 1024,
|
|
"created": 1700000000,
|
|
"upgradable": False,
|
|
},
|
|
{
|
|
"id": "sha256:cccc",
|
|
"repository": "redis",
|
|
"tags": ["7"],
|
|
"size": 30 * 1024 * 1024,
|
|
"created": 1700000000,
|
|
"upgradable": False,
|
|
},
|
|
]
|
|
}
|
|
|
|
SAMPLE_CONTAINERS = {
|
|
"containers": [
|
|
{"name": "my-nginx", "image_id": "sha256:aaaa", "status": "running"},
|
|
]
|
|
}
|
|
|
|
|
|
# ──────────────────────────────────────────────────────────────────────────────
|
|
# list_images
|
|
# ──────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_images_sorted_by_size():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Container":
|
|
return SAMPLE_CONTAINERS
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["list_images"]()
|
|
# postgres (80 MiB) should appear before nginx (50 MiB) before redis (30 MiB)
|
|
pos_postgres = result.index("postgres")
|
|
pos_nginx = result.index("nginx")
|
|
pos_redis = result.index("redis")
|
|
assert pos_postgres < pos_nginx < pos_redis
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_images_shows_in_use():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Container":
|
|
return SAMPLE_CONTAINERS
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["list_images"]()
|
|
assert "[in use]" in result
|
|
assert "[update available]" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_images_no_images():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image":
|
|
return {"images": []}
|
|
return {"containers": []}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["list_images"]()
|
|
assert "No local images found" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_images_api_error():
|
|
from mcp_synology_container.dsm_client import SynologyError
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.request.side_effect = SynologyError("API unavailable", code=102)
|
|
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["list_images"]()
|
|
assert "Error" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_images_container_error_graceful():
|
|
"""Container list failure must not prevent image listing."""
|
|
from mcp_synology_container.dsm_client import SynologyError
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image":
|
|
return SAMPLE_IMAGES
|
|
raise SynologyError("containers unavailable", code=102)
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["list_images"]()
|
|
assert "postgres" in result # images still listed
|
|
|
|
|
|
# ──────────────────────────────────────────────────────────────────────────────
|
|
# delete_image
|
|
# ──────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_image_preview():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Container":
|
|
return {"containers": []}
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["delete_image"](image_id="redis:7")
|
|
assert "Preview" in result
|
|
assert "redis:7" in result
|
|
# Should not have called the delete method
|
|
calls = [str(c) for c in client.request.call_args_list]
|
|
assert not any("delete" in c for c in calls)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_image_confirmed():
|
|
import json
|
|
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.post_request = AsyncMock(return_value={})
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Container":
|
|
return {"containers": []}
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["delete_image"](image_id="redis:7", confirmed=True)
|
|
assert "Deleted" in result
|
|
assert "redis:7" in result
|
|
assert "freed" in result
|
|
|
|
# post_request must be called with images JSON param, not name/tag/id
|
|
client.post_request.assert_called_once()
|
|
params = client.post_request.call_args.kwargs.get("params", {})
|
|
images = json.loads(params["images"])
|
|
assert images == [{"repository": "redis", "tags": ["7"]}]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_image_not_found():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.post_request = AsyncMock(return_value={})
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return SAMPLE_IMAGES
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["delete_image"](image_id="nonexistent:latest", confirmed=True)
|
|
assert "not found" in result
|
|
client.post_request.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_image_in_use_by_running_blocked():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.post_request = AsyncMock(return_value={})
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Container":
|
|
return SAMPLE_CONTAINERS # nginx is in use (running)
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["delete_image"](image_id="nginx:1.24", confirmed=True)
|
|
assert "Cannot delete" in result
|
|
assert "running" in result.lower()
|
|
assert "my-nginx" in result
|
|
client.post_request.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_image_in_use_by_stopped_blocked():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.post_request = AsyncMock(return_value={})
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Container":
|
|
return {
|
|
"containers": [
|
|
{"name": "stopped-nginx", "image_id": "sha256:aaaa", "status": "exited"}
|
|
]
|
|
}
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["delete_image"](image_id="nginx:1.24", confirmed=True)
|
|
assert "Cannot delete" in result
|
|
assert "stopped" in result.lower()
|
|
assert "stopped-nginx" in result
|
|
assert "system_prune" in result
|
|
client.post_request.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_image_by_hash():
|
|
import json
|
|
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.post_request = AsyncMock(return_value={})
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Container":
|
|
return {"containers": []}
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["delete_image"](image_id="sha256:cccc", confirmed=True)
|
|
assert "Deleted" in result
|
|
assert "redis" in result
|
|
|
|
# Verify images param uses the resolved name+tag (not the hash)
|
|
call_kwargs = client.post_request.call_args
|
|
params = call_kwargs.kwargs.get("params") or {}
|
|
images = json.loads(params.get("images", "[]"))
|
|
assert images == [{"repository": "redis", "tags": ["7"]}]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_image_registry_prefixed_name():
|
|
"""Registry-prefixed image names (e.g. ghcr.io/foo/bar:v1) must split at last ':'."""
|
|
import json
|
|
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
registry_images = {
|
|
"images": [
|
|
{
|
|
"id": "sha256:dddd",
|
|
"repository": "ghcr.io/open-webui/open-webui",
|
|
"tags": ["v0.8.10"],
|
|
"size": 100 * 1024 * 1024,
|
|
"created": 1700000000,
|
|
"upgradable": False,
|
|
}
|
|
]
|
|
}
|
|
|
|
client = AsyncMock()
|
|
client.post_request = AsyncMock(return_value={})
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return registry_images
|
|
if api == "SYNO.Docker.Container":
|
|
return {"containers": []}
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["delete_image"](
|
|
image_id="ghcr.io/open-webui/open-webui:v0.8.10", confirmed=True
|
|
)
|
|
assert "Deleted" in result
|
|
assert "open-webui" in result
|
|
|
|
# images param must use full registry-prefixed repository name
|
|
call_kwargs = client.post_request.call_args
|
|
params = call_kwargs.kwargs.get("params") or {}
|
|
images = json.loads(params.get("images", "[]"))
|
|
assert images == [{"repository": "ghcr.io/open-webui/open-webui", "tags": ["v0.8.10"]}]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_image_api_error():
|
|
from mcp_synology_container.dsm_client import SynologyError
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.post_request = AsyncMock(side_effect=SynologyError("delete failed", code=114))
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Container":
|
|
return {"containers": []}
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["delete_image"](image_id="redis:7", confirmed=True)
|
|
assert "Error" in result
|
|
assert "114" in result
|
|
|
|
|
|
# ──────────────────────────────────────────────────────────────────────────────
|
|
# inspect_image
|
|
# ──────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
SAMPLE_INSPECT = {
|
|
"details": {
|
|
"Id": "sha256:aaaa",
|
|
"RepoTags": ["nginx:1.24"],
|
|
"Size": 50 * 1024 * 1024,
|
|
"Created": "2024-01-01T00:00:00Z",
|
|
"Config": {
|
|
"Env": ["NGINX_VERSION=1.24", "PATH=/usr/local/sbin"],
|
|
"ExposedPorts": {"80/tcp": {}, "443/tcp": {}},
|
|
"Entrypoint": ["/docker-entrypoint.sh"],
|
|
"Cmd": ["nginx", "-g", "daemon off;"],
|
|
"WorkingDir": "/",
|
|
"Labels": {"maintainer": "NGINX Docker Maintainers"},
|
|
},
|
|
"RootFS": {
|
|
"Type": "layers",
|
|
"Layers": ["sha256:layer1", "sha256:layer2"],
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
def _make_inspect_client(inspect_payload=None, images_payload=None):
|
|
"""Build a mock DsmClient that returns SAMPLE_IMAGES for list and inspect_payload for get."""
|
|
client = AsyncMock()
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return images_payload if images_payload is not None else SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Image" and method == "get":
|
|
return inspect_payload if inspect_payload is not None else SAMPLE_INSPECT
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
return client
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_by_name_tag():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = _make_inspect_client()
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="nginx:1.24")
|
|
assert "nginx" in result
|
|
assert "1.24" in result
|
|
assert "MiB" in result # size formatted via _human_size
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_by_hash():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
# Inspect data shaped for redis (sha256:cccc)
|
|
redis_inspect = {
|
|
"details": {
|
|
"Id": "sha256:cccc",
|
|
"RepoTags": ["redis:7"],
|
|
"Size": 30 * 1024 * 1024,
|
|
"Config": {"Env": [], "ExposedPorts": {}, "Cmd": ["redis-server"]},
|
|
"RootFS": {"Layers": ["sha256:rlayer1"]},
|
|
}
|
|
}
|
|
client = _make_inspect_client(inspect_payload=redis_inspect)
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="sha256:cccc")
|
|
assert "redis" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_not_found():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = _make_inspect_client()
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="bogus:latest")
|
|
assert "not found" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_shows_env_vars():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = _make_inspect_client()
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="nginx:1.24")
|
|
assert "NGINX_VERSION=1.24" in result
|
|
assert "PATH=/usr/local/sbin" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_shows_exposed_ports():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = _make_inspect_client()
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="nginx:1.24")
|
|
assert "80/tcp" in result
|
|
assert "443/tcp" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_shows_layers():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = _make_inspect_client()
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="nginx:1.24")
|
|
assert "Layers" in result
|
|
# Layer hashes truncated to 12 chars after sha256:
|
|
assert "layer1" in result
|
|
assert "layer2" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_shows_entrypoint_cmd():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = _make_inspect_client()
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="nginx:1.24")
|
|
assert "/docker-entrypoint.sh" in result
|
|
assert "nginx" in result
|
|
assert "daemon off;" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_registry_prefixed():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
registry_images = {
|
|
"images": [
|
|
{
|
|
"id": "sha256:dddd",
|
|
"repository": "ghcr.io/foo/bar",
|
|
"tags": ["v1"],
|
|
"size": 100 * 1024 * 1024,
|
|
"created": 1700000000,
|
|
"upgradable": False,
|
|
}
|
|
]
|
|
}
|
|
registry_inspect = {
|
|
"details": {
|
|
"Id": "sha256:dddd",
|
|
"RepoTags": ["ghcr.io/foo/bar:v1"],
|
|
"Size": 100 * 1024 * 1024,
|
|
"Config": {"Env": [], "ExposedPorts": {}, "Cmd": ["/app"]},
|
|
"RootFS": {"Layers": ["sha256:rlayer1"]},
|
|
}
|
|
}
|
|
client = _make_inspect_client(inspect_payload=registry_inspect, images_payload=registry_images)
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="ghcr.io/foo/bar:v1")
|
|
assert "ghcr.io/foo/bar" in result
|
|
assert "v1" in result
|
|
|
|
# Verify the get call used the full registry-prefixed repository name
|
|
get_calls = [
|
|
c for c in client.request.call_args_list if c.args[:2] == ("SYNO.Docker.Image", "get")
|
|
]
|
|
assert get_calls, "inspect_image must call SYNO.Docker.Image/get"
|
|
params = get_calls[0].kwargs.get("params") or {}
|
|
assert params.get("name") == "ghcr.io/foo/bar"
|
|
assert params.get("tag") == "v1"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_inspect_image_api_error():
|
|
from mcp_synology_container.dsm_client import SynologyError
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image" and method == "list":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Image" and method == "get":
|
|
raise SynologyError("inspect failed", code=120)
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["inspect_image"](image_id="nginx:1.24")
|
|
assert "Error" in result
|
|
|
|
|
|
# ──────────────────────────────────────────────────────────────────────────────
|
|
# check_image_updates (existing tests preserved)
|
|
# ──────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_image_updates_all():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.request.return_value = SAMPLE_IMAGES
|
|
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["check_image_updates"]()
|
|
assert "nginx:1.24" in result
|
|
assert "UPDATE AVAILABLE" in result
|
|
assert "postgres:15" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_image_updates_all_up_to_date():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.request.return_value = {
|
|
"images": [
|
|
{
|
|
"id": "sha256:aaaa",
|
|
"repository": "nginx",
|
|
"tags": ["1.24"],
|
|
"size": 50 * 1024 * 1024,
|
|
"upgradable": False,
|
|
},
|
|
]
|
|
}
|
|
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["check_image_updates"]()
|
|
assert "All images are up to date" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_image_updates_no_images():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.request.return_value = {"images": []}
|
|
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["check_image_updates"]()
|
|
assert "No images found" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_image_updates_api_error():
|
|
from mcp_synology_container.dsm_client import SynologyError
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
client = AsyncMock()
|
|
client.request.side_effect = SynologyError("API unavailable", code=102)
|
|
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["check_image_updates"]()
|
|
assert "Error" in result
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_image_updates_for_project():
|
|
from mcp_synology_container.modules.images import register_images
|
|
|
|
project_list = {
|
|
"uuid-1": {
|
|
"id": "uuid-1",
|
|
"name": "myapp",
|
|
"status": "RUNNING",
|
|
"containerIds": ["abc123"],
|
|
}
|
|
}
|
|
project_detail = {
|
|
"containers": [
|
|
{"Image": "sha256:aaaa", "Config": {"Image": "nginx:1.24"}},
|
|
]
|
|
}
|
|
|
|
client = AsyncMock()
|
|
|
|
async def mock_request(api, method, **kwargs):
|
|
if api == "SYNO.Docker.Image":
|
|
return SAMPLE_IMAGES
|
|
if api == "SYNO.Docker.Project" and method == "list":
|
|
return project_list
|
|
if api == "SYNO.Docker.Project" and method == "get":
|
|
return project_detail
|
|
return {}
|
|
|
|
client.request.side_effect = mock_request
|
|
|
|
mcp, tools = make_mock_mcp()
|
|
register_images(mcp, make_config(), client)
|
|
|
|
result = await tools["check_image_updates"](project_name="myapp")
|
|
assert "myapp" in result
|
|
assert "nginx:1.24" in result
|