feat: v0.3.0 — review welle 2 (M-4, M-5, M-6)

Three resilience and honesty fixes from the v0.2.8 review. Minor
version bump because redeploy_project and system_prune return
different strings.

M-4: trigger_build_stream now converts every non-ReadTimeout
httpx.HTTPError (ConnectError, ConnectTimeout, WriteError,
RemoteProtocolError, ...) into a SynologyError with a clear
message. Previously only ReadTimeout was handled; everything else
propagated as a raw httpx exception. redeploy_project now tracks
whether stop was actually issued and, when build_stream fails after
a successful stop, tells the user the project is in STOPPED state
and recommends start_project / retry rather than the misleading
"use stop + start separately" workaround.

M-5: _wait_for_project_running exits early on BUILD_FAILED / ERROR
(new _TERMINAL_FAILURE_STATUSES frozenset). DSM signals these
statuses within seconds of a failed image pull; the old polling
loop kept waiting up to 5 minutes for RUNNING. redeploy_project
now surfaces the terminal status with a BUILD_FAILED-specific hint
to update_image_tag.

M-6: system_prune preview now enumerates user-created networks
that have no containers attached (excluding the three built-in
networks bridge/host/none, which Docker never prunes). Previously
the preview noted "Unused networks: (not counted)" even though
SYNO.Docker.Utils/prune does delete them — users could lose
networks they had not been warned about.

Tests:
- 2 new dsm_client tests: ConnectError and RemoteProtocolError
  both raise SynologyError, not raw httpx exceptions.
- 2 new project tests: recovery hint after stop+build_stream
  failure (RUNNING case); old workaround retained for the
  STOPPED case where no stop was issued.
- 3 new polling tests: BUILD_FAILED and ERROR each trigger early
  exit; redeploy_project surfaces BUILD_FAILED with update_image_tag
  hint.
- 2 new system_prune preview tests: counts unused networks
  correctly, excludes built-ins; network-fetch failure is non-fatal.

245 tests pass. ruff check + ruff format clean.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-18 09:57:20 +02:00
parent 6ba4c7ca92
commit 13e10fa52f
9 changed files with 383 additions and 4 deletions
+89
View File
@@ -305,3 +305,92 @@ async def test_system_prune_api_error():
result = await tools["system_prune"](confirmed=True)
assert "Error" in result
# ──────────────────────────────────────────────────────────────────────
# M-6: system_prune preview now counts unused networks
# ──────────────────────────────────────────────────────────────────────
SAMPLE_NETWORKS_FOR_PRUNE = {
"network": [
# User-created, no containers attached → will be pruned
{"name": "orphan_net", "driver": "bridge", "containers": []},
# User-created, in use → must NOT be counted
{
"name": "myapp_default",
"driver": "bridge",
"containers": ["web", "db"],
},
# Built-in networks: Docker never prunes these even if empty
{"name": "bridge", "driver": "bridge", "containers": []},
{"name": "host", "driver": "host", "containers": []},
{"name": "none", "driver": "null", "containers": []},
# Another user-created empty network
{"name": "legacy_net", "driver": "bridge", "containers": []},
]
}
@pytest.mark.asyncio
async def test_system_prune_preview_counts_unused_networks() -> None:
"""M-6: preview must enumerate user-created networks with no containers,
skipping the three built-in networks (bridge/host/none)."""
from mcp_synology_container.modules.system import register_system
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
if api == "SYNO.Docker.Network":
return SAMPLE_NETWORKS_FOR_PRUNE
return {}
client.request.side_effect = mock_request
mcp, tools = make_mock_mcp()
register_system(mcp, make_config(), client)
result = await tools["system_prune"]()
# Two unused user-created networks; the three built-ins must not appear.
assert "Unused networks: 2" in result
assert "orphan_net" in result
assert "legacy_net" in result
# Built-in network names must not appear in the prune preview.
assert " - bridge " not in result
assert " - host " not in result
assert " - none " not in result
# Network with containers must not be listed.
assert "myapp_default" not in result
# Old "not counted" placeholder must be gone.
assert "not counted" not in result
@pytest.mark.asyncio
async def test_system_prune_preview_network_fetch_failure_is_nonfatal() -> None:
"""If the network list fetch fails, the preview still works (0 networks)."""
from mcp_synology_container.dsm_client import SynologyError
from mcp_synology_container.modules.system import register_system
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
if api == "SYNO.Docker.Network":
raise SynologyError("network list failed", code=100)
return {}
client.request.side_effect = mock_request
mcp, tools = make_mock_mcp()
register_system(mcp, make_config(), client)
result = await tools["system_prune"]()
# Preview still renders; networks count falls back to 0.
assert "preview" in result.lower()
assert "Unused networks: 0" in result