mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-24 14:54:33 +02:00
feat: report Docker pull progress and add an idle timeout
A pull now reports docker.image.pull notifications on the controller stream: started, throttled downloading events with the layer counts and the latest daemon message, then completed or failed. An optional progress_id is echoed in every event so a client can correlate them with its own request. Image operations belong to a compute, not to a project, so the events go to the controller stream and reach clients that have no project open. The pull also has an idle socket read timeout of 60 seconds. Without it an unreachable registry left the pull, the compute request and the client request hanging forever with no feedback at all. The timeout is idle time, not total time: the daemon streams progress during the whole pull, so a minute without data means the registry is gone. When it fires, the daemon connection closes and Docker cancels the pull. Docker.http_query now accepts an aiohttp ClientTimeout object so a caller can ask for a policy finer than a total timeout.
This commit is contained in:
9 files changed
+266
-27
No files matched your search
@@ -18,6 +18,7 @@ import pytest
|
||||
from fastapi import FastAPI, status
|
||||
from httpx import AsyncClient
|
||||
|
||||
from gns3server.compute.docker.docker_error import DockerError
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
@@ -27,11 +28,64 @@ class TestImagesRoutes:
|
||||
async def test_pull_docker_image(self, app: FastAPI, compute_client: AsyncClient) -> None:
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.pull_image") as mock:
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:pull_docker_image"), json={"image": "nginx:latest"}
|
||||
)
|
||||
mock.assert_called_once_with("nginx:latest", force=True)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
with asyncio_patch("gns3server.compute.notification_manager.NotificationManager.emit") as emit_mock:
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:pull_docker_image"), json={"image": "nginx:latest"}
|
||||
)
|
||||
assert mock.call_args[0] == ("nginx:latest",)
|
||||
assert mock.call_args[1]["force"] is True
|
||||
assert callable(mock.call_args[1]["progress_callback"])
|
||||
# started and completed events are emitted to the notification stream
|
||||
assert [(c.args[0], c.args[1]["state"]) for c in emit_mock.call_args_list] == [
|
||||
("docker.image.pull", "started"),
|
||||
("docker.image.pull", "completed"),
|
||||
]
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
async def test_pull_docker_image_progress_id(self, app: FastAPI, compute_client: AsyncClient) -> None:
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.pull_image"):
|
||||
with asyncio_patch("gns3server.compute.notification_manager.NotificationManager.emit") as emit_mock:
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:pull_docker_image"),
|
||||
json={"image": "nginx:latest"},
|
||||
params={"progress_id": "abc123"},
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
assert all(c.args[1]["progress_id"] == "abc123" for c in emit_mock.call_args_list)
|
||||
|
||||
async def test_pull_docker_image_reports_downloading(self, app: FastAPI, compute_client: AsyncClient) -> None:
|
||||
|
||||
async def fake_pull(image, progress_callback=None, force=False):
|
||||
progress_callback({"layers_total": 2, "layers_done": 1, "message": "[==> ] 40/100"})
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.pull_image", side_effect=fake_pull):
|
||||
with asyncio_patch("gns3server.compute.notification_manager.NotificationManager.emit") as emit_mock:
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:pull_docker_image"), json={"image": "nginx:latest"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
states = [c.args[1]["state"] for c in emit_mock.call_args_list]
|
||||
assert states == ["started", "downloading", "completed"]
|
||||
downloading = emit_mock.call_args_list[1].args[1]
|
||||
assert downloading["layers_total"] == 2
|
||||
assert downloading["layers_done"] == 1
|
||||
assert downloading["message"] == "[==> ] 40/100"
|
||||
|
||||
async def test_pull_docker_image_emits_failed(self, app: FastAPI, compute_client: AsyncClient) -> None:
|
||||
|
||||
async def fake_pull(image, progress_callback=None, force=False):
|
||||
raise DockerError("no data received for 60 seconds")
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.pull_image", side_effect=fake_pull):
|
||||
with asyncio_patch("gns3server.compute.notification_manager.NotificationManager.emit") as emit_mock:
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:pull_docker_image"), json={"image": "nginx:latest"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_409_CONFLICT
|
||||
failed = emit_mock.call_args_list[-1].args[1]
|
||||
assert failed["state"] == "failed"
|
||||
assert "no data received" in failed["message"]
|
||||
|
||||
@pytest.mark.parametrize("image", ["", " ", "nginx latest"])
|
||||
async def test_pull_docker_image_rejects_invalid_name(
|
||||
|
||||
@@ -197,7 +197,31 @@ class TestComputeFeatures:
|
||||
app.url_path_for("docker_pull_image", compute_id=test_compute.compute_id),
|
||||
json={"image": "nginx:latest"},
|
||||
)
|
||||
compute.forward.assert_called_with("POST", "docker", "images/pull", data={"image": "nginx:latest"})
|
||||
compute.forward.assert_called_with(
|
||||
"POST", "docker", "images/pull", data={"image": "nginx:latest"}, params=None
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
async def test_compute_pull_docker_image_progress_id(
|
||||
self, app: FastAPI, client: AsyncClient, test_compute: Compute
|
||||
) -> None:
|
||||
|
||||
compute = MagicMock()
|
||||
compute.forward = AsyncioMagicMock(return_value={})
|
||||
with patch("gns3server.api.routes.controller.computes.Controller.instance") as controller:
|
||||
controller.return_value.get_compute.return_value = compute
|
||||
response = await client.post(
|
||||
app.url_path_for("docker_pull_image", compute_id=test_compute.compute_id),
|
||||
json={"image": "nginx:latest"},
|
||||
params={"progress_id": "abc123"},
|
||||
)
|
||||
compute.forward.assert_called_with(
|
||||
"POST",
|
||||
"docker",
|
||||
"images/pull",
|
||||
data={"image": "nginx:latest"},
|
||||
params={"progress_id": "abc123"},
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
async def test_compute_delete_docker_image(self, app: FastAPI, client: AsyncClient, test_compute: Compute) -> None:
|
||||
|
||||
Reference in new issue
Block a user