mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-04 06:21:15 +02:00
refactor: ruff autofixes
This commit is contained in:
455 files changed
+12322
-14762
No files matched your search
@@ -32,55 +32,44 @@ pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestIOUNodesRoutes:
|
||||
|
||||
@pytest.fixture
|
||||
def fake_iou_bin(self, images_dir) -> str:
|
||||
"""Create a fake IOU image on disk"""
|
||||
|
||||
path = os.path.join(images_dir, "IOU", "iou.bin")
|
||||
with open(path, "w+") as f:
|
||||
f.write('\x7fELF\x01\x01\x01')
|
||||
f.write("\x7fELF\x01\x01\x01")
|
||||
os.chmod(path, stat.S_IREAD | stat.S_IEXEC)
|
||||
return path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def base_params(self, tmpdir, fake_iou_bin) -> dict:
|
||||
"""Return standard parameters"""
|
||||
|
||||
return {"application_id": 42, "name": "IOU-TEST-1", "path": "iou.bin"}
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def vm(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
compute_project: Project,
|
||||
base_params: dict
|
||||
) -> dict:
|
||||
async def vm(self, app: FastAPI, compute_client: AsyncClient, compute_project: Project, base_params: dict) -> dict:
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:create_iou_node", project_id=compute_project.id), json=base_params)
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:create_iou_node", project_id=compute_project.id), json=base_params
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
return response.json()
|
||||
|
||||
|
||||
def startup_config_file(self, compute_project: Project, vm: dict) -> str:
|
||||
|
||||
directory = os.path.join(compute_project.path, "project-files", "iou", vm["node_id"])
|
||||
os.makedirs(directory, exist_ok=True)
|
||||
return os.path.join(directory, "startup-config.cfg")
|
||||
|
||||
|
||||
async def test_iou_create(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
compute_project: Project,
|
||||
base_params: dict
|
||||
self, app: FastAPI, compute_client: AsyncClient, compute_project: Project, base_params: dict
|
||||
) -> None:
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:create_iou_node", project_id=compute_project.id), json=base_params)
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:create_iou_node", project_id=compute_project.id), json=base_params
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["name"] == "IOU-TEST-1"
|
||||
assert response.json()["project_id"] == compute_project.id
|
||||
@@ -90,13 +79,8 @@ class TestIOUNodesRoutes:
|
||||
assert response.json()["nvram"] == 256
|
||||
assert response.json()["l1_keepalives"] is False
|
||||
|
||||
|
||||
async def test_iou_create_with_params(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
compute_project: Project,
|
||||
base_params: dict
|
||||
self, app: FastAPI, compute_client: AsyncClient, compute_project: Project, base_params: dict
|
||||
) -> None:
|
||||
|
||||
params = base_params
|
||||
@@ -108,7 +92,9 @@ class TestIOUNodesRoutes:
|
||||
params["startup_config_content"] = "hostname test"
|
||||
params["use_default_iou_values"] = False
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:create_iou_node", project_id=compute_project.id), json=params)
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:create_iou_node", project_id=compute_project.id), json=params
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["name"] == "IOU-TEST-1"
|
||||
assert response.json()["project_id"] == compute_project.id
|
||||
@@ -122,7 +108,6 @@ class TestIOUNodesRoutes:
|
||||
with open(self.startup_config_file(compute_project, response.json())) as f:
|
||||
assert f.read() == "hostname test"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"name, status_code",
|
||||
(
|
||||
@@ -136,13 +121,13 @@ class TestIOUNodesRoutes:
|
||||
),
|
||||
)
|
||||
async def test_iou_create_with_invalid_name(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
compute_project: Project,
|
||||
base_params: dict,
|
||||
name: str,
|
||||
status_code: int
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
compute_project: Project,
|
||||
base_params: dict,
|
||||
name: str,
|
||||
status_code: int,
|
||||
) -> None:
|
||||
|
||||
base_params["name"] = name
|
||||
@@ -151,40 +136,33 @@ class TestIOUNodesRoutes:
|
||||
)
|
||||
assert response.status_code == status_code
|
||||
|
||||
|
||||
async def test_iou_create_startup_config_already_exist(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
compute_project: Project,
|
||||
base_params: dict) -> None:
|
||||
self, app: FastAPI, compute_client: AsyncClient, compute_project: Project, base_params: dict
|
||||
) -> None:
|
||||
"""We don't erase a startup-config if already exist at project creation"""
|
||||
|
||||
node_id = str(uuid.uuid4())
|
||||
startup_config_file_path = self.startup_config_file(compute_project, {'node_id': node_id})
|
||||
with open(startup_config_file_path, 'w+') as f:
|
||||
startup_config_file_path = self.startup_config_file(compute_project, {"node_id": node_id})
|
||||
with open(startup_config_file_path, "w+") as f:
|
||||
f.write("echo hello")
|
||||
|
||||
params = base_params
|
||||
params["node_id"] = node_id
|
||||
params["startup_config_content"] = "hostname test"
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:create_iou_node", project_id=compute_project.id), json=params)
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:create_iou_node", project_id=compute_project.id), json=params
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
|
||||
with open(self.startup_config_file(compute_project, response.json())) as f:
|
||||
assert f.read() == "echo hello"
|
||||
|
||||
async def test_iou_get(self, app: FastAPI, compute_client: AsyncClient, compute_project: Project, vm: dict) -> None:
|
||||
|
||||
async def test_iou_get(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
compute_project: Project,
|
||||
vm: dict
|
||||
) -> None:
|
||||
|
||||
response = await compute_client.get(app.url_path_for("compute:get_iou_node", project_id=vm["project_id"], node_id=vm["node_id"]))
|
||||
response = await compute_client.get(
|
||||
app.url_path_for("compute:get_iou_node", project_id=vm["project_id"], node_id=vm["node_id"])
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["name"] == "IOU-TEST-1"
|
||||
assert response.json()["project_id"] == compute_project.id
|
||||
@@ -194,73 +172,62 @@ class TestIOUNodesRoutes:
|
||||
assert response.json()["nvram"] == 256
|
||||
assert response.json()["l1_keepalives"] is False
|
||||
|
||||
|
||||
async def test_iou_start(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start", return_value=True) as mock:
|
||||
response = await compute_client.post(app.url_path_for("compute:start_iou_node",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"]), json={})
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:start_iou_node", project_id=vm["project_id"], node_id=vm["node_id"]), json={}
|
||||
)
|
||||
assert mock.called
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
|
||||
async def test_iou_start_with_iourc(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
params = {"iourc_content": "test"}
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start", return_value=True) as mock:
|
||||
response = await compute_client.post(app.url_path_for("compute:start_iou_node",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"]), json=params)
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:start_iou_node", project_id=vm["project_id"], node_id=vm["node_id"]),
|
||||
json=params,
|
||||
)
|
||||
assert mock.called
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
|
||||
async def test_iou_stop(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop", return_value=True) as mock:
|
||||
response = await compute_client.post(app.url_path_for("compute:stop_iou_node",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"]))
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:stop_iou_node", project_id=vm["project_id"], node_id=vm["node_id"])
|
||||
)
|
||||
assert mock.called
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
|
||||
async def test_iou_reload(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.reload", return_value=True) as mock:
|
||||
response = await compute_client.post(app.url_path_for("compute:reload_iou_node",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"]))
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:reload_iou_node", project_id=vm["project_id"], node_id=vm["node_id"])
|
||||
)
|
||||
assert mock.called
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
|
||||
async def test_iou_delete(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
with asyncio_patch("gns3server.compute.iou.IOU.delete_node", return_value=True) as mock:
|
||||
response = await compute_client.delete(app.url_path_for("compute:delete_iou_node",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"]))
|
||||
response = await compute_client.delete(
|
||||
app.url_path_for("compute:delete_iou_node", project_id=vm["project_id"], node_id=vm["node_id"])
|
||||
)
|
||||
assert mock.called
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
|
||||
async def test_iou_suspend(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
response = await compute_client.delete(
|
||||
app.url_path_for(
|
||||
"compute:suspend_iou_node",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"])
|
||||
app.url_path_for("compute:suspend_iou_node", project_id=vm["project_id"], node_id=vm["node_id"])
|
||||
)
|
||||
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
|
||||
|
||||
async def test_iou_update(
|
||||
self, app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
vm: dict,
|
||||
free_console_port: int
|
||||
self, app: FastAPI, compute_client: AsyncClient, vm: dict, free_console_port: int
|
||||
) -> None:
|
||||
|
||||
params = {
|
||||
@@ -274,9 +241,9 @@ class TestIOUNodesRoutes:
|
||||
"use_default_iou_values": True,
|
||||
}
|
||||
|
||||
response = await compute_client.put(app.url_path_for("compute:update_iou_node",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"]), json=params)
|
||||
response = await compute_client.put(
|
||||
app.url_path_for("compute:update_iou_node", project_id=vm["project_id"], node_id=vm["node_id"]), json=params
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["name"] == "test"
|
||||
assert response.json()["console"] == free_console_port
|
||||
@@ -287,173 +254,141 @@ class TestIOUNodesRoutes:
|
||||
assert response.json()["l1_keepalives"] is True
|
||||
assert response.json()["use_default_iou_values"] is True
|
||||
|
||||
|
||||
async def test_iou_nio_create_udp(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
params = {"type": "nio_udp",
|
||||
"lport": 4242,
|
||||
"rport": 4343,
|
||||
"rhost": "127.0.0.1"}
|
||||
params = {"type": "nio_udp", "lport": 4242, "rport": 4343, "rhost": "127.0.0.1"}
|
||||
|
||||
url = app.url_path_for("compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0",
|
||||
)
|
||||
response = await compute_client.post(url, json=params)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["type"] == "nio_udp"
|
||||
|
||||
|
||||
async def test_iou_nio_update_udp(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
params = {
|
||||
"type": "nio_udp",
|
||||
"lport": 4242,
|
||||
"rport": 4343,
|
||||
"rhost": "127.0.0.1",
|
||||
"filters": {"packet_loss": 10}
|
||||
}
|
||||
params = {"type": "nio_udp", "lport": 4242, "rport": 4343, "rhost": "127.0.0.1", "filters": {"packet_loss": 10}}
|
||||
|
||||
url = app.url_path_for("compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0",
|
||||
)
|
||||
|
||||
response = await compute_client.post(url, json=params)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["filters"] == {"packet_loss": 10}
|
||||
params["filters"].clear()
|
||||
|
||||
url = app.url_path_for("compute:update_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:update_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0",
|
||||
)
|
||||
response = await compute_client.put(url, json=params)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["type"] == "nio_udp"
|
||||
assert response.json()["filters"] == {}
|
||||
|
||||
|
||||
async def test_iou_nio_create_ethernet(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
vm: dict,
|
||||
ethernet_device: str
|
||||
self, app: FastAPI, compute_client: AsyncClient, vm: dict, ethernet_device: str
|
||||
) -> None:
|
||||
|
||||
params = {
|
||||
"type": "nio_ethernet",
|
||||
"ethernet_device": ethernet_device
|
||||
}
|
||||
params = {"type": "nio_ethernet", "ethernet_device": ethernet_device}
|
||||
|
||||
url = app.url_path_for("compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0",
|
||||
)
|
||||
|
||||
response = await compute_client.post(url, json=params)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["type"] == "nio_ethernet"
|
||||
assert response.json()["ethernet_device"] == ethernet_device
|
||||
|
||||
|
||||
async def test_iou_nio_create_ethernet_different_port(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
vm: dict,
|
||||
ethernet_device: str
|
||||
self, app: FastAPI, compute_client: AsyncClient, vm: dict, ethernet_device: str
|
||||
) -> None:
|
||||
|
||||
params = {
|
||||
"type": "nio_ethernet",
|
||||
"ethernet_device": ethernet_device
|
||||
}
|
||||
params = {"type": "nio_ethernet", "ethernet_device": ethernet_device}
|
||||
|
||||
url = app.url_path_for("compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="0",
|
||||
port_number="3")
|
||||
url = app.url_path_for(
|
||||
"compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="0",
|
||||
port_number="3",
|
||||
)
|
||||
response = await compute_client.post(url, json=params)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["type"] == "nio_ethernet"
|
||||
assert response.json()["ethernet_device"] == ethernet_device
|
||||
|
||||
|
||||
async def test_iou_nio_create_tap(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
vm: dict,
|
||||
ethernet_device: str
|
||||
self, app: FastAPI, compute_client: AsyncClient, vm: dict, ethernet_device: str
|
||||
) -> None:
|
||||
|
||||
params = {
|
||||
"type": "nio_tap",
|
||||
"tap_device": ethernet_device
|
||||
}
|
||||
params = {"type": "nio_tap", "tap_device": ethernet_device}
|
||||
|
||||
url = app.url_path_for("compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0",
|
||||
)
|
||||
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
|
||||
response = await compute_client.post(url, json=params)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["type"] == "nio_tap"
|
||||
|
||||
async def test_iou_delete_nio(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
async def test_iou_delete_nio(
|
||||
self,
|
||||
app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
vm: dict
|
||||
) -> None:
|
||||
params = {"type": "nio_udp", "lport": 4242, "rport": 4343, "rhost": "127.0.0.1"}
|
||||
|
||||
params = {
|
||||
"type": "nio_udp",
|
||||
"lport": 4242,
|
||||
"rport": 4343,
|
||||
"rhost": "127.0.0.1"
|
||||
}
|
||||
|
||||
url = app.url_path_for("compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:create_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0",
|
||||
)
|
||||
|
||||
await compute_client.post(url, json=params)
|
||||
|
||||
url = app.url_path_for("compute:delete_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:delete_iou_node_nio",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="1",
|
||||
port_number="0",
|
||||
)
|
||||
|
||||
response = await compute_client.delete(url)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
|
||||
async def test_iou_start_capture(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
params = {
|
||||
"capture_file_name": "test.pcap",
|
||||
"data_link_type": "DLT_EN10MB"
|
||||
}
|
||||
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
|
||||
|
||||
url = app.url_path_for("compute:start_iou_node_capture",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="0",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:start_iou_node_capture",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="0",
|
||||
port_number="0",
|
||||
)
|
||||
|
||||
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=True):
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start_capture") as mock:
|
||||
@@ -462,14 +397,15 @@ class TestIOUNodesRoutes:
|
||||
assert mock.called
|
||||
assert "test.pcap" in response.json()["pcap_file_path"]
|
||||
|
||||
|
||||
async def test_iou_stop_capture(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
url = app.url_path_for("compute:stop_iou_node_capture",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="0",
|
||||
port_number="0")
|
||||
url = app.url_path_for(
|
||||
"compute:stop_iou_node_capture",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"],
|
||||
adapter_number="0",
|
||||
port_number="0",
|
||||
)
|
||||
|
||||
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=True):
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop_capture") as mock:
|
||||
@@ -477,7 +413,6 @@ class TestIOUNodesRoutes:
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
assert mock.called
|
||||
|
||||
|
||||
# @pytest.mark.asyncio
|
||||
# async def test_iou_pcap(compute_api, vm, compute_project):
|
||||
#
|
||||
@@ -486,18 +421,20 @@ class TestIOUNodesRoutes:
|
||||
# response = await compute_client.get("/projects/{project_id}/iou/nodes/{node_id}/adapters/0/ports/0/pcap".format(project_id=compute_project.id, node_id=vm["node_id"]), raw=True)
|
||||
# assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
|
||||
async def test_images(self, app: FastAPI, compute_client: AsyncClient, fake_iou_bin: str) -> None:
|
||||
|
||||
response = await compute_client.get(app.url_path_for("compute:get_iou_images"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json() == [{"filename": "iou.bin", "path": "iou.bin", "filesize": 7, "md5sum": "e573e8f5c93c6c00783f20c7a170aa6c"}]
|
||||
|
||||
assert response.json() == [
|
||||
{"filename": "iou.bin", "path": "iou.bin", "filesize": 7, "md5sum": "e573e8f5c93c6c00783f20c7a170aa6c"}
|
||||
]
|
||||
|
||||
async def test_upload_image(self, app: FastAPI, compute_client: AsyncClient, tmpdir) -> None:
|
||||
|
||||
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir)):
|
||||
response = await compute_client.post(app.url_path_for("compute:upload_iou_image", filename="test2"), content=b"TEST")
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:upload_iou_image", filename="test2"), content=b"TEST"
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
with open(str(tmpdir / "test2")) as f:
|
||||
@@ -507,39 +444,42 @@ class TestIOUNodesRoutes:
|
||||
checksum = f.read()
|
||||
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
|
||||
|
||||
|
||||
async def test_upload_image_forbidden_location(self, app: FastAPI, compute_client: AsyncClient) -> None:
|
||||
|
||||
file_path = "%2e%2e/hello"
|
||||
response = await compute_client.post(app.url_path_for("compute:upload_dynamips_image", filename=file_path), content=b"TEST")
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:upload_dynamips_image", filename=file_path), content=b"TEST"
|
||||
)
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
|
||||
async def test_download_image(self, app: FastAPI, compute_client: AsyncClient, images_dir: str) -> None:
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:upload_dynamips_image", filename="test3"), content=b"TEST")
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:upload_dynamips_image", filename="test3"), content=b"TEST"
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
response = await compute_client.get(app.url_path_for("compute:download_dynamips_image", filename="test3"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
|
||||
async def test_download_image_forbidden(self, app: FastAPI, compute_client: AsyncClient, tmpdir) -> None:
|
||||
|
||||
file_path = "foo/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd"
|
||||
response = await compute_client.get(app.url_path_for("compute:download_iou_image", filename=file_path))
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
|
||||
async def test_iou_duplicate(self, app: FastAPI, compute_client: AsyncClient, vm: dict, base_params: dict) -> None:
|
||||
|
||||
# create destination node first
|
||||
response = await compute_client.post(app.url_path_for("compute:create_iou_node", project_id=vm["project_id"]), json=base_params)
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:create_iou_node", project_id=vm["project_id"]), json=base_params
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
|
||||
params = {"destination_node_id": response.json()["node_id"]}
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:duplicate_iou_node",
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"]), json=params)
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:duplicate_iou_node", project_id=vm["project_id"], node_id=vm["node_id"]),
|
||||
json=params,
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
Reference in new issue
Block a user