mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-23 22:34:35 +02:00
feat: name nodes and templates in Docker image delete conflicts
The compute translates the container ids in a daemon delete conflict into the GNS3 node and project behind each container. Leftover containers from closed projects and containers created outside GNS3 are named as such. The controller lists the templates referencing the image in the conflict error, matched through the canonical image name. After a successful deletion it emits a docker.image.deleted notification with those templates: their nodes can no longer start.
This commit is contained in:
6 files changed
+341
-7
No files matched your search
@@ -24,6 +24,7 @@ from fastapi import FastAPI, status
|
||||
from httpx import AsyncClient
|
||||
|
||||
from gns3server.controller import Controller
|
||||
from gns3server.controller.controller_error import ComputeConflictError
|
||||
from gns3server.schemas.controller.computes import Compute
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
@@ -260,6 +261,82 @@ class TestComputeFeatures:
|
||||
compute.http_query.assert_not_called()
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
|
||||
|
||||
async def test_compute_delete_docker_image_conflict_names_referencing_templates(
|
||||
self, app: FastAPI, client: AsyncClient, test_compute: Compute
|
||||
) -> None:
|
||||
"""
|
||||
Templates do not block the deletion but are named in the conflict
|
||||
error, matched through their image reference spelling.
|
||||
"""
|
||||
|
||||
new_template = {
|
||||
"name": "Alpine conflict test",
|
||||
"compute_id": "local",
|
||||
# a different spelling of the deleted 'alpine:3'
|
||||
"image": "docker.io/library/alpine:3",
|
||||
"template_type": "docker",
|
||||
}
|
||||
response = await client.post(app.url_path_for("create_template"), json=new_template)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
|
||||
compute = MagicMock()
|
||||
compute.http_query = AsyncioMagicMock(
|
||||
side_effect=ComputeConflictError(
|
||||
"http://localhost:3080/docker/images/alpine:3",
|
||||
{
|
||||
"message": "Cannot delete image 'alpine:3': it is still used by node 'alpine-1' "
|
||||
"in project 'lab' (running). Delete the node or container first, or repeat with force=true."
|
||||
},
|
||||
)
|
||||
)
|
||||
with patch("gns3server.api.routes.controller.computes.Controller.instance") as controller:
|
||||
controller.return_value.get_compute.return_value = compute
|
||||
response = await client.delete(
|
||||
app.url_path_for("docker_delete_image", compute_id=test_compute.compute_id, image="alpine:3")
|
||||
)
|
||||
assert response.status_code == status.HTTP_409_CONFLICT
|
||||
message = response.json()["message"]
|
||||
assert "node 'alpine-1' in project 'lab'" in message
|
||||
assert "referenced by template(s): Alpine conflict test" in message
|
||||
|
||||
async def test_compute_delete_docker_image_notifies_referencing_templates(
|
||||
self, app: FastAPI, client: AsyncClient, test_compute: Compute
|
||||
) -> None:
|
||||
"""
|
||||
A successful deletion emits a docker.image.deleted notification with
|
||||
the templates referencing the deleted image.
|
||||
"""
|
||||
|
||||
new_template = {
|
||||
"name": "Alpine delete test",
|
||||
"compute_id": "local",
|
||||
# an image no other test in this class references: the assertion
|
||||
# below is exact and the class shares one database
|
||||
"image": "gns3/delete-event-test:1",
|
||||
"template_type": "docker",
|
||||
}
|
||||
response = await client.post(app.url_path_for("create_template"), json=new_template)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
|
||||
compute = MagicMock()
|
||||
compute.http_query = 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.delete(
|
||||
app.url_path_for(
|
||||
"docker_delete_image", compute_id=test_compute.compute_id, image="gns3/delete-event-test:1"
|
||||
)
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
controller.return_value.notification.controller_emit.assert_called_once_with(
|
||||
"docker.image.deleted",
|
||||
{
|
||||
"compute_id": str(test_compute.compute_id),
|
||||
"image": "gns3/delete-event-test:1",
|
||||
"referenced_by_templates": ["Alpine delete test"],
|
||||
},
|
||||
)
|
||||
|
||||
async def test_compute_load_docker_image(self, app: FastAPI, client: AsyncClient, test_compute: Compute) -> None:
|
||||
|
||||
compute = MagicMock()
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
@@ -27,7 +28,8 @@ from gns3server.compute.docker import (
|
||||
PULL_IDLE_TIMEOUT,
|
||||
Docker,
|
||||
)
|
||||
from gns3server.compute.docker.docker_error import DockerError, DockerHttp404Error
|
||||
from gns3server.compute.docker.docker_error import DockerError, DockerHttp404Error, DockerHttp409Error
|
||||
from gns3server.compute.project_manager import ProjectManager
|
||||
from tests.utils import AsyncioMagicMock, asyncio_patch
|
||||
|
||||
|
||||
@@ -434,6 +436,93 @@ async def test_delete_image_error_keeps_translation():
|
||||
assert docker.image_id_translation("ubuntu:latest") == translation
|
||||
|
||||
|
||||
DAEMON_DELETE_CONFLICT = (
|
||||
"Docker has returned an error: 409 conflict: unable to remove repository reference "
|
||||
'"alpine:3" (must force) - container 6d59f00236ff is using its referenced image d529dd0c6e55'
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_image_conflict_names_the_node_and_project():
|
||||
"""
|
||||
A container blocking the deletion is reported as the GNS3 node and project
|
||||
it belongs to, not as a raw container id.
|
||||
"""
|
||||
|
||||
docker = Docker.instance()
|
||||
project_id = str(uuid.uuid4())
|
||||
project = ProjectManager.instance().create_project(name="test-lab", project_id=project_id)
|
||||
|
||||
async def query(method, path, **kwargs):
|
||||
assert path.startswith("images/") or path.startswith("containers/")
|
||||
if method == "DELETE":
|
||||
raise DockerHttp409Error(DAEMON_DELETE_CONFLICT)
|
||||
return {"Name": f"/GNS3.alpine-1.{project_id}", "State": {"Running": True}}
|
||||
|
||||
try:
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", side_effect=query):
|
||||
with pytest.raises(DockerHttp409Error) as excinfo:
|
||||
await docker.delete_image("alpine:3")
|
||||
message = str(excinfo.value)
|
||||
assert "node 'alpine-1' in project 'test-lab' (running)" in message
|
||||
assert "force=true" in message
|
||||
assert "6d59f00236ff" not in message
|
||||
finally:
|
||||
ProjectManager.instance().remove_project(project.id)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_image_conflict_reports_a_leftover_container():
|
||||
"""
|
||||
When the project of the blocking container is closed on this compute the
|
||||
container is reported as a leftover.
|
||||
"""
|
||||
|
||||
docker = Docker.instance()
|
||||
|
||||
async def query(method, path, **kwargs):
|
||||
if method == "DELETE":
|
||||
raise DockerHttp409Error(DAEMON_DELETE_CONFLICT)
|
||||
return {"Name": f"/GNS3.E2E-2.{uuid.uuid4()}", "State": {"Running": False}}
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", side_effect=query):
|
||||
with pytest.raises(DockerHttp409Error, match="leftover container of node 'E2E-2' \\(stopped"):
|
||||
await docker.delete_image("alpine:3")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_image_conflict_reports_a_non_gns3_container():
|
||||
"""
|
||||
A container created outside GNS3 is reported by its plain container name.
|
||||
"""
|
||||
|
||||
docker = Docker.instance()
|
||||
|
||||
async def query(method, path, **kwargs):
|
||||
if method == "DELETE":
|
||||
raise DockerHttp409Error(DAEMON_DELETE_CONFLICT)
|
||||
return {"Name": "/my-own-container", "State": {"Running": True}}
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", side_effect=query):
|
||||
with pytest.raises(DockerHttp409Error, match="container 'my-own-container' \\(running\\)"):
|
||||
await docker.delete_image("alpine:3")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_image_conflict_without_containers_is_not_rewritten():
|
||||
"""
|
||||
A daemon conflict naming no container (e.g. a multi repository reference)
|
||||
is passed through unchanged.
|
||||
"""
|
||||
|
||||
docker = Docker.instance()
|
||||
original = "Docker has returned an error: 409 conflict: image is referenced in multiple repositories"
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", side_effect=DockerHttp409Error(original)):
|
||||
with pytest.raises(DockerHttp409Error, match="multiple repositories"):
|
||||
await docker.delete_image("alpine:3")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_docker_check_connection_docker_minimum_version(vm):
|
||||
|
||||
|
||||
@@ -20,7 +20,13 @@ import threading
|
||||
import pytest
|
||||
|
||||
from gns3server.utils import force_unix_path
|
||||
from gns3server.utils.images import images_directories, list_images, md5sum, remove_checksum
|
||||
from gns3server.utils.images import (
|
||||
canonical_docker_image_name,
|
||||
images_directories,
|
||||
list_images,
|
||||
md5sum,
|
||||
remove_checksum,
|
||||
)
|
||||
|
||||
|
||||
def test_images_directories(tmpdir, config):
|
||||
@@ -108,6 +114,29 @@ def test_remove_checksum(tmpdir):
|
||||
remove_checksum(str(tmpdir / "not_exists"))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"reference, expected",
|
||||
(
|
||||
("alpine", "library/alpine:latest"),
|
||||
("alpine:3", "library/alpine:3"),
|
||||
("library/alpine:3", "library/alpine:3"),
|
||||
("docker.io/library/alpine:3", "library/alpine:3"),
|
||||
("index.docker.io/library/alpine:3", "library/alpine:3"),
|
||||
("localhost/alpine:3", "library/alpine:3"),
|
||||
("gns3/frr", "gns3/frr:latest"),
|
||||
("gns3/frr:latest", "gns3/frr:latest"),
|
||||
("quay.io/some/namespace/image:2.0", "some/namespace/image:2.0"),
|
||||
("alpine@sha256:" + "a" * 64, "library/alpine@sha256:" + "a" * 64),
|
||||
("alpine:3@sha256:" + "a" * 64, "library/alpine@sha256:" + "a" * 64),
|
||||
),
|
||||
)
|
||||
def test_canonical_docker_image_name(reference, expected):
|
||||
|
||||
assert canonical_docker_image_name(reference) == expected
|
||||
# the canonical form is stable: canonicalizing twice does not change it
|
||||
assert canonical_docker_image_name(expected) == expected
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_images(tmpdir, config):
|
||||
|
||||
|
||||
Reference in new issue
Block a user