mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-29 08:54:34 +02:00
Merge branch '3.1' into api-issue-05-2xftmd
This commit is contained in:
46 files changed
+2108
-210
No files matched your search
@@ -34,7 +34,7 @@ from pydantic import SecretStr
|
||||
from gns3server.api.routes.controller.nodes import vnc_console, ws_console
|
||||
from gns3server.config import Config
|
||||
from gns3server.controller.compute import Compute
|
||||
from gns3server.controller.controller_error import ControllerError
|
||||
from gns3server.controller.controller_error import ControllerError, ComputeError
|
||||
from gns3server.controller.node import Node
|
||||
from gns3server.controller.project import Project
|
||||
from gns3server.services import auth_service
|
||||
@@ -254,6 +254,54 @@ class TestNodeRoutes:
|
||||
else:
|
||||
assert len(response.json()) == 0
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"name, tags, expected_names",
|
||||
(
|
||||
(None, None, ["test", "test2"]),
|
||||
("test", None, ["test"]),
|
||||
("test2", None, ["test2"]),
|
||||
("TEST", None, []),
|
||||
("missing", None, []),
|
||||
("test", ["tag1"], ["test"]),
|
||||
("test", ["tag3"], []),
|
||||
("test2", ["tag3", "tag4"], ["test2"]),
|
||||
),
|
||||
)
|
||||
async def test_list_nodes_filter_by_name(
|
||||
self,
|
||||
app: FastAPI,
|
||||
client: AsyncClient,
|
||||
project: Project,
|
||||
compute: Compute,
|
||||
name: Optional[str],
|
||||
tags: Optional[list],
|
||||
expected_names: list,
|
||||
) -> None:
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
for node_name, node_tags in (("test", ["tag1", "tag2"]), ("test2", ["tag3", "tag4"])):
|
||||
await client.post(
|
||||
app.url_path_for("create_node", project_id=project.id),
|
||||
json={
|
||||
"name": node_name,
|
||||
"node_type": "vpcs",
|
||||
"compute_id": "example.com",
|
||||
"tags": node_tags,
|
||||
"properties": {"startup_script": "echo test"},
|
||||
},
|
||||
)
|
||||
|
||||
params = {}
|
||||
if name is not None:
|
||||
params["name"] = name
|
||||
if tags is not None:
|
||||
params["tags"] = tags
|
||||
response = await client.get(app.url_path_for("get_nodes", project_id=project.id), params=params)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert sorted(n["name"] for n in response.json()) == expected_names
|
||||
|
||||
async def test_get_node(self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute) -> None:
|
||||
|
||||
response = MagicMock()
|
||||
@@ -300,6 +348,77 @@ class TestNodeRoutes:
|
||||
assert "name" not in response.json()["properties"]
|
||||
assert response.json()["tags"] == ["tag1", "tag2"]
|
||||
|
||||
async def test_node_etag_on_get_and_put(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
compute.put = AsyncioMagicMock(return_value=MagicMock(json={"console": 2048}))
|
||||
url = app.url_path_for("update_node", project_id=project.id, node_id=node.id)
|
||||
|
||||
response = await client.get(url)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
etag = response.headers["ETag"]
|
||||
assert etag.startswith('"') and etag.endswith('"')
|
||||
assert (await client.get(url)).headers["ETag"] == etag
|
||||
|
||||
response = await client.put(url, json={"name": "renamed"})
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.headers["ETag"] != etag
|
||||
assert (await client.get(url)).headers["ETag"] == response.headers["ETag"]
|
||||
|
||||
async def test_update_node_with_matching_if_match(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
compute.put = AsyncioMagicMock(return_value=MagicMock(json={"console": 2048}))
|
||||
url = app.url_path_for("update_node", project_id=project.id, node_id=node.id)
|
||||
|
||||
etag = (await client.get(url)).headers["ETag"]
|
||||
response = await client.put(url, json={"name": "renamed"}, headers={"If-Match": etag})
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["name"] == "renamed"
|
||||
assert response.headers["ETag"] != etag
|
||||
|
||||
async def test_update_node_with_stale_if_match(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
compute.put = AsyncioMagicMock(return_value=MagicMock(json={"console": 2048}))
|
||||
url = app.url_path_for("update_node", project_id=project.id, node_id=node.id)
|
||||
|
||||
stale_etag = (await client.get(url)).headers["ETag"]
|
||||
assert (await client.put(url, json={"name": "first"})).status_code == status.HTTP_200_OK
|
||||
|
||||
response = await client.put(url, json={"name": "second"}, headers={"If-Match": stale_etag})
|
||||
assert response.status_code == status.HTTP_412_PRECONDITION_FAILED
|
||||
assert "message" in response.json()
|
||||
assert node.name == "first"
|
||||
|
||||
async def test_update_node_without_if_match(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
compute.put = AsyncioMagicMock(return_value=MagicMock(json={"console": 2048}))
|
||||
url = app.url_path_for("update_node", project_id=project.id, node_id=node.id)
|
||||
|
||||
assert (await client.put(url, json={"name": "first"})).status_code == status.HTTP_200_OK
|
||||
assert (await client.put(url, json={"name": "second"})).status_code == status.HTTP_200_OK
|
||||
assert node.name == "second"
|
||||
|
||||
async def test_update_node_with_wildcard_and_list_if_match(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
compute.put = AsyncioMagicMock(return_value=MagicMock(json={"console": 2048}))
|
||||
url = app.url_path_for("update_node", project_id=project.id, node_id=node.id)
|
||||
|
||||
etag = (await client.get(url)).headers["ETag"]
|
||||
response = await client.put(url, json={"name": "a"}, headers={"If-Match": "*"})
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
etag = (await client.get(url)).headers["ETag"]
|
||||
response = await client.put(url, json={"name": "b"}, headers={"If-Match": f'"other", {etag}'})
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
async def test_start_all_nodes(self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute) -> None:
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
@@ -342,7 +461,8 @@ class TestNodeRoutes:
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
response = await client.post(app.url_path_for("start_node", project_id=project.id, node_id=node.id), json={})
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["status"] == "started"
|
||||
|
||||
async def test_stop_node(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
@@ -350,6 +470,84 @@ class TestNodeRoutes:
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
response = await client.post(app.url_path_for("stop_node", project_id=project.id, node_id=node.id))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["status"] == "stopped"
|
||||
|
||||
async def test_start_node_wait(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
response = await client.post(
|
||||
app.url_path_for("start_node", project_id=project.id, node_id=node.id) + "?wait=true&timeout=5", json={}
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["status"] == "started"
|
||||
response = await client.get(app.url_path_for("get_node", project_id=project.id, node_id=node.id))
|
||||
assert response.json()["status"] == "started"
|
||||
|
||||
async def test_start_node_reports_starting_while_in_flight(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
seen = []
|
||||
|
||||
async def post(*args, **kwargs):
|
||||
seen.append(node.asdict()["status"])
|
||||
|
||||
compute.post = post
|
||||
response = await client.post(app.url_path_for("start_node", project_id=project.id, node_id=node.id), json={})
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert seen == ["starting"]
|
||||
assert response.json()["status"] == "started"
|
||||
|
||||
async def test_stop_node_reports_stopping_while_in_flight(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
seen = []
|
||||
|
||||
async def post(*args, **kwargs):
|
||||
seen.append(node.asdict()["status"])
|
||||
|
||||
compute.post = post
|
||||
node._status = "started"
|
||||
response = await client.post(app.url_path_for("stop_node", project_id=project.id, node_id=node.id))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert seen == ["stopping"]
|
||||
assert response.json()["status"] == "stopped"
|
||||
|
||||
async def test_start_node_wait_timeout(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
async def post(*args, **kwargs):
|
||||
await asyncio.sleep(10)
|
||||
|
||||
compute.post = post
|
||||
response = await client.post(
|
||||
app.url_path_for("start_node", project_id=project.id, node_id=node.id) + "?wait=true&timeout=1", json={}
|
||||
)
|
||||
assert response.status_code == status.HTTP_408_REQUEST_TIMEOUT
|
||||
assert "message" in response.json()
|
||||
assert node.asdict()["status"] == "stopped"
|
||||
|
||||
async def test_stop_node_reports_compute_error(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
compute.post = AsyncioMagicMock(side_effect=ComputeError("compute unreachable"))
|
||||
node._status = "started"
|
||||
response = await client.post(app.url_path_for("stop_node", project_id=project.id, node_id=node.id))
|
||||
assert response.status_code == status.HTTP_409_CONFLICT
|
||||
assert node.asdict()["status"] == "started"
|
||||
|
||||
async def test_stop_all_nodes_tolerates_compute_error(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
) -> None:
|
||||
|
||||
compute.post = AsyncioMagicMock(side_effect=ComputeError("compute unreachable"))
|
||||
response = await client.post(app.url_path_for("stop_all_nodes", project_id=project.id))
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
async def test_suspend_node(
|
||||
@@ -358,7 +556,8 @@ class TestNodeRoutes:
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
response = await client.post(app.url_path_for("suspend_node", project_id=project.id, node_id=node.id))
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["status"] == "suspended"
|
||||
|
||||
async def test_suspend_node_unsupported(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
@@ -388,7 +587,8 @@ class TestNodeRoutes:
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
response = await client.post(app.url_path_for("reload_node", project_id=project.id, node_id=node.id))
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["status"] == "started"
|
||||
|
||||
async def test_isolate_node(
|
||||
self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute, node: Node
|
||||
|
||||
Reference in new issue
Block a user