Files
gns3-server/tests/controller/test_notification.py
T
YueGuobin 8c98142d26 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.
2026-11-06 15:03:34 +08:00

182 lines
6.3 KiB
Python

#!/usr/bin/env python
#
# Copyright (C) 2020 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest.mock import MagicMock
import pytest
import pytest_asyncio
from tests.utils import AsyncioMagicMock
@pytest_asyncio.fixture
async def node(project):
compute = MagicMock()
compute.id = "remote1"
compute.host = "example.org"
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
return await project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"})
@pytest.mark.asyncio
async def test_emit_to_all(controller, project):
"""
Send an event to all if we don't have a project id in the event
"""
notif = controller.notification
with notif.project_queue(project.id) as queue:
assert len(notif._project_listeners[project.id]) == 1
await queue.get(0.1) # ping
notif.project_emit("test", {})
msg = await queue.get(5)
assert msg == ("test", {}, {})
assert len(notif._project_listeners[project.id]) == 0
@pytest.mark.asyncio
async def test_emit_to_project(controller, project):
"""
Send an event to a project listeners
"""
notif = controller.notification
with notif.project_queue(project.id) as queue:
assert len(notif._project_listeners[project.id]) == 1
await queue.get(0.1) # ping
# This event has not listener
notif.project_emit("ignore", {"project_id": 42})
notif.project_emit("test", {"project_id": project.id})
msg = await queue.get(5)
assert msg == ("test", {"project_id": project.id}, {})
assert len(notif._project_listeners[project.id]) == 0
@pytest.mark.asyncio
async def test_dispatch(controller, project):
notif = controller.notification
with notif.project_queue(project.id) as queue:
assert len(notif._project_listeners[project.id]) == 1
await queue.get(0.1) # ping
await notif.dispatch("test", {}, project_id=project.id, compute_id=1)
msg = await queue.get(5)
assert msg == ("test", {}, {})
@pytest.mark.asyncio
async def test_dispatch_ping(controller, project):
notif = controller.notification
with notif.project_queue(project.id) as queue:
assert len(notif._project_listeners[project.id]) == 1
await queue.get(0.1) # ping
await notif.dispatch("ping", {}, project_id=project.id, compute_id=12)
msg = await queue.get(5)
assert msg == ("ping", {"compute_id": 12}, {})
@pytest.mark.asyncio
async def test_dispatch_node_updated(controller, node, project):
"""
When we receive a node.updated notification from compute
we need to update the client
"""
notif = controller.notification
with notif.project_queue(project.id) as queue:
assert len(notif._project_listeners[project.id]) == 1
await queue.get(0.1) # ping
await notif.dispatch(
"node.updated",
{"node_id": node.id, "project_id": project.id, "name": "hello", "startup_config": "ip 192"},
project_id=project.id,
compute_id=1,
)
assert node.name == "hello"
action, event, _ = await queue.get(5)
assert action == "node.updated"
assert event["name"] == "hello"
assert event["properties"]["startup_config"] == "ip 192"
@pytest.mark.asyncio
async def test_dispatch_docker_image_routed_to_controller_stream(controller, project):
"""
docker.image.* events are scoped to a compute, not to a project: they must
reach the controller stream even when no project is open, and not the
project stream.
"""
notif = controller.notification
with notif.controller_queue() as controller_q, notif.project_queue(project.id) as project_q:
await controller_q.get(0.1) # ping
await project_q.get(0.1) # ping
await notif.dispatch(
"docker.image.pull", {"image": "debian", "state": "started"}, project_id=None, compute_id="local"
)
msg = await controller_q.get(5)
assert msg == (
"docker.image.pull",
{"image": "debian", "state": "started", "compute_id": "local"},
{},
)
# the project stream only carries its periodic ping
msg = await project_q.get(0.2)
assert msg[0] == "ping"
@pytest.mark.asyncio
async def test_dispatch_marker_routed_to_marker_channel(controller, project):
"""
marker.* events are dispatched to the dedicated marker channel, not the
main project queue, so high-frequency matches cannot block topology events.
"""
notif = controller.notification
with notif.project_queue(project.id) as project_q, notif.project_marker_queue(project.id) as marker_q:
assert len(notif._project_marker_listeners[project.id]) == 1
await project_q.get(0.1) # consume initial ping
await marker_q.get(0.1) # consume initial ping
await notif.dispatch("marker.match", {"link_id": "abc"}, project_id=project.id, compute_id=1)
# marker.match lands on the marker channel...
msg = await marker_q.get(5)
assert msg == ("marker.match", {"link_id": "abc"}, {})
# ...and does NOT land on the main project queue (times out -> ping)
msg = await project_q.get(0.1)
assert msg[0] == "ping"
assert len(notif._project_marker_listeners[project.id]) == 0
def test_various_notification(controller, node):
notif = controller.notification
notif.project_emit("log.info", {"message": "Image uploaded"})
notif.project_emit("log.warning", {"message": "Warning ASA 8 is not officially supported by GNS3"})
notif.project_emit("log.error", {"message": "Permission denied on /tmp"})
notif.project_emit("node.updated", node.asdict())