Migration to FastAPI

This commit is contained in:
grossmj committed 2020-10-02 16:07:50 +09:30
1 parent c12b675691
commit eb3cb8a41f
224 files changed
+11254 -13517

No files matched your search

View File
Whitespace-only changes.
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import sys
import pytest
import psutil
from gns3server.version import __version__
from gns3server.utils.path import get_default_project_directory
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
@pytest.mark.asyncio
async def test_get(compute_api, windows_platform):
response = await compute_api.get('/capabilities')
assert response.status_code == 200
assert response.json == {'node_types': ['cloud', 'ethernet_hub', 'ethernet_switch', 'nat', 'vpcs', 'virtualbox', 'dynamips', 'frame_relay_switch', 'atm_switch', 'qemu', 'vmware', 'traceng', 'docker', 'iou'],
'version': __version__,
'platform': sys.platform,
'cpus': psutil.cpu_count(logical=True),
'memory': psutil.virtual_memory().total,
'disk_size': psutil.disk_usage(get_default_project_directory()).total,
}
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
@pytest.mark.asyncio
async def test_get_on_gns3vm(compute_api, on_gns3vm):
response = await compute_api.get('/capabilities')
assert response.status_code == 200
assert response.json == {'node_types': ['cloud', 'ethernet_hub', 'ethernet_switch', 'nat', 'vpcs', 'virtualbox', 'dynamips', 'frame_relay_switch', 'atm_switch', 'qemu', 'vmware', 'traceng', 'docker', 'iou'],
'version': __version__,
'platform': sys.platform,
'cpus': psutil.cpu_count(logical=True),
'memory': psutil.virtual_memory().total,
'disk_size': psutil.disk_usage(get_default_project_directory()).total,
}
+145
View File
@@ -0,0 +1,145 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
from unittest.mock import patch
from tests.utils import asyncio_patch
@pytest.fixture(scope="function")
@pytest.mark.asyncio
async def vm(compute_api, compute_project, on_gns3vm):
with asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._start_ubridge"):
response = await compute_api.post("/projects/{project_id}/cloud/nodes".format(project_id=compute_project.id), {"name": "Cloud 1"})
assert response.status_code == 201
return response.json
@pytest.mark.asyncio
async def test_cloud_create(compute_api, compute_project):
with asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._start_ubridge"):
response = await compute_api.post("/projects/{project_id}/cloud/nodes".format(project_id=compute_project.id), {"name": "Cloud 1"})
assert response.status_code == 201
assert response.json["name"] == "Cloud 1"
assert response.json["project_id"] == compute_project.id
@pytest.mark.asyncio
async def test_cloud_get(compute_api, compute_project, vm):
response = await compute_api.get("/projects/{project_id}/cloud/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 200
assert response.json["name"] == "Cloud 1"
assert response.json["project_id"] == compute_project.id
assert response.json["status"] == "started"
@pytest.mark.asyncio
async def test_cloud_nio_create_udp(compute_api, vm):
params = {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"}
response = await compute_api.post("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_cloud_nio_update_udp(compute_api, vm):
params = {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"}
await compute_api.post("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
params["filters"] = {}
response = await compute_api.put("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201, response.body.decode()
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_cloud_delete_nio(compute_api, vm):
params = {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"}
await compute_api.post("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
with asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._start_ubridge"):
response = await compute_api.delete("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
@pytest.mark.asyncio
async def test_cloud_delete(compute_api, vm):
response = await compute_api.delete("/projects/{project_id}/cloud/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
@pytest.mark.asyncio
async def test_cloud_update(compute_api, vm):
response = await compute_api.put("/projects/{project_id}/cloud/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"name": "test"})
assert response.status_code == 200
assert response.json["name"] == "test"
@pytest.mark.asyncio
async def test_cloud_start_capture(compute_api, vm):
params = {
"capture_file_name": "test.pcap",
"data_link_type": "DLT_EN10MB"
}
with asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud.start_capture") as mock:
response = await compute_api.post("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert mock.called
assert "test.pcap" in response.json["pcap_file_path"]
@pytest.mark.asyncio
async def test_cloud_stop_capture(compute_api, vm):
with asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud.stop_capture") as mock:
response = await compute_api.post("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
assert mock.called
# @pytest.mark.asyncio
# async def test_cloud_pcap(compute_api, vm, compute_project):
#
# from itertools import repeat
# stream = repeat(42, times=10)
#
# with asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud.get_nio"):
# with asyncio_patch("gns3server.compute.builtin.Builtin.stream_pcap_file", return_value=stream):
# response = await compute_api.get("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/pcap".format(project_id=compute_project.id, node_id=vm["node_id"]))
# assert response.status_code == 200
#
+62
View File
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import os
import pytest
from gns3server.version import __version__
@pytest.mark.asyncio
async def test_udp_allocation(compute_api, compute_project):
response = await compute_api.post('/projects/{}/ports/udp'.format(compute_project.id), {})
assert response.status_code == 201
assert response.json['udp_port'] is not None
# Netfifaces is not available on Travis
@pytest.mark.skipif(os.environ.get("TRAVIS", False) is not False, reason="Not supported on Travis")
@pytest.mark.asyncio
async def test_interfaces(compute_api):
response = await compute_api.get('/network/interfaces')
assert response.status_code == 200
assert isinstance(response.json, list)
@pytest.mark.asyncio
async def test_version_output(compute_api, config):
config.set("Server", "local", "true")
response = await compute_api.get('/version')
assert response.status_code == 200
assert response.json == {'local': True, 'version': __version__}
# @pytest.mark.asyncio
# async def test_debug_output(compute_api):
#
# response = await compute_api.get('/debug')
# assert response.status_code == 200
@pytest.mark.asyncio
async def test_statistics_output(compute_api):
response = await compute_api.get('/statistics')
assert response.status_code == 200
@@ -0,0 +1,233 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
import sys
import uuid
from tests.utils import asyncio_patch
from unittest.mock import patch
pytestmark = pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
@pytest.fixture
def base_params():
"""Return standard parameters"""
params = {
"name": "PC TEST 1",
"image": "nginx",
"start_command": "nginx-daemon",
"adapters": 2,
"environment": "YES=1\nNO=0",
"console_type": "telnet",
"console_resolution": "1280x1024",
"extra_hosts": "test:127.0.0.1"
}
return params
# @pytest.yield_fixture(autouse=True)
# def mock_connection():
#
# docker = Docker.instance()
# docker._connected = True
# docker._connector = MagicMock()
# yield
# Docker._instance = None
@pytest.fixture
@pytest.mark.asyncio
async def vm(compute_api, compute_project, base_params):
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "nginx"}]):
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}):
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="exited"):
response = await compute_api.post("/projects/{project_id}/docker/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
return response.json
@pytest.mark.asyncio
async def test_docker_create(compute_api, compute_project, base_params):
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "nginx"}]):
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}):
response = await compute_api.post("/projects/{project_id}/docker/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["container_id"] == "8bd8153ea8f5"
assert response.json["image"] == "nginx:latest"
assert response.json["adapters"] == 2
assert response.json["environment"] == "YES=1\nNO=0"
assert response.json["console_resolution"] == "1280x1024"
assert response.json["extra_hosts"] == "test:127.0.0.1"
@pytest.mark.asyncio
async def test_docker_start(compute_api, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/start".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_docker_stop(compute_api, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.stop", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/stop".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_docker_reload(compute_api, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.restart", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/reload".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_docker_delete(compute_api, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.delete", return_value=True) as mock:
response = await compute_api.delete("/projects/{project_id}/docker/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_docker_pause(compute_api, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.pause", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/pause".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_docker_unpause(compute_api, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.unpause", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/unpause".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_docker_nio_create_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"}
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_docker_update_nio(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.adapter_update_nio_binding"):
response = await compute_api.put("/projects/{project_id}/docker/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201, response.body.decode()
@pytest.mark.asyncio
async def test_docker_delete_nio(compute_api, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.adapter_remove_nio_binding"):
response = await compute_api.delete("/projects/{project_id}/docker/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
@pytest.mark.asyncio
async def test_docker_update(compute_api, vm, free_console_port):
params = {
"name": "test",
"console": free_console_port,
"start_command": "yes",
"environment": "GNS3=1\nGNS4=0",
"extra_hosts": "test:127.0.0.1"
}
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.update") as mock:
response = await compute_api.put("/projects/{project_id}/docker/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert mock.called
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
assert response.json["start_command"] == "yes"
assert response.json["environment"] == "GNS3=1\nGNS4=0"
assert response.json["extra_hosts"] == "test:127.0.0.1"
@pytest.mark.asyncio
async def test_docker_start_capture(compute_api, vm):
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start_capture") as mock:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]), body=params)
assert response.status_code == 200
assert mock.called
assert "test.pcap" in response.json["pcap_file_path"]
@pytest.mark.asyncio
async def test_docker_stop_capture(compute_api, vm):
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.stop_capture") as mock:
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
assert mock.called
@pytest.mark.asyncio
async def test_docker_duplicate(compute_api, compute_project, base_params, vm):
# create destination node first
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "nginx"}]):
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}):
response = await compute_api.post("/projects/{project_id}/docker/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
params = {"destination_node_id": response.json["node_id"]}
response = await compute_api.post("/projects/{project_id}/docker/nodes/{node_id}/duplicate".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
@@ -0,0 +1,200 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
import sys
import os
import stat
from unittest.mock import patch
from tests.utils import asyncio_patch
# @pytest.yield_fixture(scope="module")
# async def vm(compute_api, compute_project, fake_image):
#
# dynamips_path = "/fake/dynamips"
# params = {
# "name": "My router",
# "platform": "c3745",
# "image": fake_image,
# "ram": 128
# }
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.create", return_value=True) as mock:
# response = await compute_api.post("/projects/{project_id}/dynamips/nodes".format(project_id=compute_project.id), params)
# assert mock.called
# assert response.status == 201
#
# #with asyncio_patch("gns3server.compute.dynamips.Dynamips.find_dynamips", return_value=dynamips_path):
# # yield response.json
# async def test_dynamips_vm_create(compute_api, compute_project, fake_image):
#
# params = {
# "name": "My router",
# "platform": "c3745",
# "image": os.path.basename(fake_image),
# "ram": 128
# }
#
# print(fake_image)
#
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.create", return_value=True):
# response = await compute_api.post("/projects/{project_id}/dynamips/nodes".format(project_id=compute_project.id), params)
# assert response.status == 201
# assert response.json["name"] == "My router"
# assert response.json["project_id"] == compute_project.id
# assert response.json["dynamips_id"]
# def test_dynamips_vm_get(compute_api, project, vm):
# response = compute_api.get("/projects/{project_id}/dynamips/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), example=True)
# assert response.status == 200
# assert response.route == "/projects/{project_id}/dynamips/nodes/{node_id}"
# assert response.json["name"] == "My router"
# assert response.json["project_id"] == project.id
#
#
# def test_dynamips_vm_start(compute_api, vm):
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.start", return_value=True) as mock:
# response = compute_api.post("/projects/{project_id}/dynamips/nodes/{node_id}/start".format(project_id=vm["project_id"], node_id=vm["node_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_stop(compute_api, vm):
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.stop", return_value=True) as mock:
# response = compute_api.post("/projects/{project_id}/dynamips/nodes/{node_id}/stop".format(project_id=vm["project_id"], node_id=vm["node_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_suspend(compute_api, vm):
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.suspend", return_value=True) as mock:
# response = compute_api.post("/projects/{project_id}/dynamips/nodes/{node_id}/suspend".format(project_id=vm["project_id"], node_id=vm["node_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_resume(compute_api, vm):
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.resume", return_value=True) as mock:
# response = compute_api.post("/projects/{project_id}/dynamips/nodes/{node_id}/resume".format(project_id=vm["project_id"], node_id=vm["node_id"]))
# assert mock.called
# assert response.status == 204
# def test_vbox_nio_create_udp(compute_api, vm):
#
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
# response = compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/0/nio".format(project_id=vm["project_id"],
# node_id=vm["node_id"]), {"type": "nio_udp",
# "lport": 4242,
# "rport": 4343,
# "rhost": "127.0.0.1"},
# example=True)
#
# assert mock.called
# args, kwgars = mock.call_args
# assert args[0] == 0
#
# assert response.status == 201
# assert response.route == "/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/{adapter_id:\d+}/nio"
# assert response.json["type"] == "nio_udp"
#
#
# def test_vbox_delete_nio(compute_api, vm):
#
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
# response = compute_api.delete("/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), example=True)
#
# assert mock.called
# args, kwgars = mock.call_args
# assert args[0] == 0
#
# assert response.status == 204
# assert response.route == "/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/{adapter_id:\d+}/nio"
#
#
# def test_vbox_update(compute_api, vm, free_console_port):
# response = compute_api.put("/projects/{project_id}/virtualbox/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"name": "test",
# "console": free_console_port})
# assert response.status == 200
# assert response.json["name"] == "test"
# assert response.json["console"] == free_console_port
@pytest.fixture
def fake_image(tmpdir):
"""Create a fake Dynamips image on disk"""
path = str(tmpdir / "7200.bin")
with open(path, "wb+") as f:
f.write(b'\x7fELF\x01\x02\x01')
os.chmod(path, stat.S_IREAD)
return path
@pytest.fixture
def fake_file(tmpdir):
"""Create a fake file disk"""
path = str(tmpdir / "7200.txt")
with open(path, "w+") as f:
f.write('1')
os.chmod(path, stat.S_IREAD)
return path
@pytest.mark.asyncio
async def test_images(compute_api, tmpdir, fake_image, fake_file):
with patch("gns3server.utils.images.default_images_directory", return_value=str(tmpdir)):
response = await compute_api.get("/dynamips/images")
assert response.status_code == 200
assert response.json == [{"filename": "7200.bin",
"path": "7200.bin",
"filesize": 7,
"md5sum": "b0d5aa897d937aced5a6b1046e8f7e2e"
}]
@pytest.mark.asyncio
async def test_upload_image(compute_api, images_dir):
response = await compute_api.post("/dynamips/images/test2", body=b"TEST", raw=True)
assert response.status_code == 204
with open(os.path.join(images_dir, "IOS", "test2")) as f:
assert f.read() == "TEST"
with open(os.path.join(images_dir, "IOS", "test2.md5sum")) as f:
checksum = f.read()
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
@pytest.mark.skipif(not sys.platform.startswith("win") and os.getuid() == 0, reason="Root can delete any image")
@pytest.mark.asyncio
async def test_upload_image_permission_denied(compute_api, images_dir):
os.makedirs(os.path.join(images_dir, "IOS"), exist_ok=True)
with open(os.path.join(images_dir, "IOS", "test2.tmp"), "w+") as f:
f.write("")
os.chmod(os.path.join(images_dir, "IOS", "test2.tmp"), 0)
response = await compute_api.post("/dynamips/images/test2", body=b"TEST", raw=True)
assert response.status_code == 409
+366
View File
@@ -0,0 +1,366 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
import os
import stat
import sys
import uuid
from tests.utils import asyncio_patch
from unittest.mock import patch
pytestmark = pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
@pytest.fixture
def fake_iou_bin(images_dir):
"""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')
os.chmod(path, stat.S_IREAD | stat.S_IEXEC)
return path
@pytest.fixture
def base_params(tmpdir, fake_iou_bin):
"""Return standard parameters"""
return {"application_id": 42, "name": "PC TEST 1", "path": "iou.bin"}
@pytest.fixture
@pytest.mark.asyncio
async def vm(compute_api, compute_project, base_params):
response = await compute_api.post("/projects/{project_id}/iou/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
return response.json
def startup_config_file(compute_project, vm):
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")
@pytest.mark.asyncio
async def test_iou_create(compute_api, compute_project, base_params):
response = await compute_api.post("/projects/{project_id}/iou/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["serial_adapters"] == 2
assert response.json["ethernet_adapters"] == 2
assert response.json["ram"] == 256
assert response.json["nvram"] == 128
assert response.json["l1_keepalives"] is False
@pytest.mark.asyncio
async def test_iou_create_with_params(compute_api, compute_project, base_params):
params = base_params
params["ram"] = 1024
params["nvram"] = 512
params["serial_adapters"] = 4
params["ethernet_adapters"] = 0
params["l1_keepalives"] = True
params["startup_config_content"] = "hostname test"
params["use_default_iou_values"] = False
response = await compute_api.post("/projects/{project_id}/iou/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["serial_adapters"] == 4
assert response.json["ethernet_adapters"] == 0
assert response.json["ram"] == 1024
assert response.json["nvram"] == 512
assert response.json["l1_keepalives"] is True
assert response.json["use_default_iou_values"] is False
with open(startup_config_file(compute_project, response.json)) as f:
assert f.read() == "hostname test"
@pytest.mark.asyncio
async def test_iou_create_startup_config_already_exist(compute_api, compute_project, base_params):
"""We don't erase a startup-config if already exist at project creation"""
node_id = str(uuid.uuid4())
startup_config_file_path = 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_api.post("/projects/{project_id}/iou/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
with open(startup_config_file(compute_project, response.json)) as f:
assert f.read() == "echo hello"
@pytest.mark.asyncio
async def test_iou_get(compute_api, compute_project, vm):
response = await compute_api.get("/projects/{project_id}/iou/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 200
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["serial_adapters"] == 2
assert response.json["ethernet_adapters"] == 2
assert response.json["ram"] == 256
assert response.json["nvram"] == 128
assert response.json["l1_keepalives"] is False
@pytest.mark.asyncio
async def test_iou_start(compute_api, vm):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/start".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_iou_start_with_iourc(compute_api, vm):
params = {"iourc_content": "test"}
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/start".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert mock.called
assert response.status_code == 204
response = await compute_api.get("/projects/{project_id}/iou/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 200
@pytest.mark.asyncio
async def test_iou_stop(compute_api, vm):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/stop".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_iou_reload(compute_api, vm):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.reload", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/reload".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_iou_delete(compute_api, vm):
with asyncio_patch("gns3server.compute.iou.IOU.delete_node", return_value=True) as mock:
response = await compute_api.delete("/projects/{project_id}/iou/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_iou_update(compute_api, vm, free_console_port):
params = {
"name": "test",
"console": free_console_port,
"ram": 512,
"nvram": 2048,
"ethernet_adapters": 4,
"serial_adapters": 0,
"l1_keepalives": True,
"use_default_iou_values": True,
}
response = await compute_api.put("/projects/{project_id}/iou/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
assert response.json["ethernet_adapters"] == 4
assert response.json["serial_adapters"] == 0
assert response.json["ram"] == 512
assert response.json["nvram"] == 2048
assert response.json["l1_keepalives"] is True
assert response.json["use_default_iou_values"] is True
@pytest.mark.asyncio
async def test_iou_nio_create_udp(compute_api, vm):
params = {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"}
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_iou_nio_update_udp(compute_api, vm):
params = {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"}
await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
params["filters"] = {}
response = await compute_api.put("/projects/{project_id}/iou/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201, response.body.decode()
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_iou_nio_create_ethernet(compute_api, vm, ethernet_device):
params = {
"type": "nio_ethernet",
"ethernet_device": ethernet_device
}
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_ethernet"
assert response.json["ethernet_device"] == ethernet_device
@pytest.mark.asyncio
async def test_iou_nio_create_ethernet_different_port(compute_api, vm, ethernet_device):
params = {
"type": "nio_ethernet",
"ethernet_device": ethernet_device
}
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/adapters/0/ports/3/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_ethernet"
assert response.json["ethernet_device"] == ethernet_device
@pytest.mark.asyncio
async def test_iou_nio_create_tap(compute_api, vm, ethernet_device):
params = {
"type": "nio_tap",
"tap_device": ethernet_device
}
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_tap"
@pytest.mark.asyncio
async def test_iou_delete_nio(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
response = await compute_api.delete("/projects/{project_id}/iou/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
@pytest.mark.asyncio
async def test_iou_start_capture(compute_api, vm):
params = {
"capture_file_name": "test.pcap",
"data_link_type": "DLT_EN10MB"
}
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:
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert mock.called
assert "test.pcap" in response.json["pcap_file_path"]
@pytest.mark.asyncio
async def test_iou_stop_capture(compute_api, vm):
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:
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
assert mock.called
# @pytest.mark.asyncio
# async def test_iou_pcap(compute_api, vm, compute_project):
#
# with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.get_nio"):
# with asyncio_patch("gns3server.compute.iou.IOU.stream_pcap_file"):
# response = await compute_api.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 == 200
@pytest.mark.asyncio
async def test_images(compute_api, fake_iou_bin):
response = await compute_api.get("/iou/images")
assert response.status_code == 200
assert response.json == [{"filename": "iou.bin", "path": "iou.bin", "filesize": 7, "md5sum": "e573e8f5c93c6c00783f20c7a170aa6c"}]
@pytest.mark.asyncio
async def test_image_vm(compute_api, tmpdir):
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir)):
response = await compute_api.post("/iou/images/test2", body="TEST", raw=True)
assert response.status_code == 204
with open(str(tmpdir / "test2")) as f:
assert f.read() == "TEST"
with open(str(tmpdir / "test2.md5sum")) as f:
checksum = f.read()
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
@pytest.mark.asyncio
async def test_iou_duplicate(compute_api, compute_project, vm, base_params):
# create destination node first
response = await compute_api.post("/projects/{project_id}/iou/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
params = {"destination_node_id": response.json["node_id"]}
response = await compute_api.post("/projects/{project_id}/iou/nodes/{node_id}/duplicate".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
+149
View File
@@ -0,0 +1,149 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
from tests.utils import asyncio_patch
@pytest.fixture(scope="function")
@pytest.mark.asyncio
async def vm(compute_api, compute_project, ubridge_path, on_gns3vm):
with asyncio_patch("gns3server.compute.builtin.nodes.nat.Nat._start_ubridge"):
response = await compute_api.post("/projects/{project_id}/nat/nodes".format(project_id=compute_project.id), {"name": "Nat 1"})
assert response.status_code == 201
return response.json
@pytest.mark.asyncio
async def test_nat_create(compute_api, compute_project, on_gns3vm):
with asyncio_patch("gns3server.compute.builtin.nodes.nat.Nat._start_ubridge"):
response = await compute_api.post("/projects/{project_id}/nat/nodes".format(project_id=compute_project.id), {"name": "Nat 1"})
assert response.status_code == 201
assert response.json["name"] == "Nat 1"
assert response.json["project_id"] == compute_project.id
@pytest.mark.asyncio
async def test_nat_get(compute_api, compute_project, vm):
response = await compute_api.get("/projects/{project_id}/nat/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 200
assert response.json["name"] == "Nat 1"
assert response.json["project_id"] == compute_project.id
assert response.json["status"] == "started"
@pytest.mark.asyncio
async def test_nat_nio_create_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch("gns3server.compute.builtin.nodes.nat.Nat.add_nio"):
response = await compute_api.post("/projects/{project_id}/nat/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_nat_nio_update_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
await compute_api.post("/projects/{project_id}/nat/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
params["filters"] = {}
response = await compute_api.put("/projects/{project_id}/nat/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201, response.body.decode()
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_nat_delete_nio(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch("gns3server.compute.builtin.nodes.nat.Nat.add_nio"):
await compute_api.post("/projects/{project_id}/nat/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
with asyncio_patch("gns3server.compute.builtin.nodes.nat.Nat.remove_nio") as mock:
response = await compute_api.delete("/projects/{project_id}/nat/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_nat_delete(compute_api, vm):
response = await compute_api.delete("/projects/{project_id}/nat/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
@pytest.mark.asyncio
async def test_nat_update(compute_api, vm):
response = await compute_api.put("/projects/{project_id}/nat/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"name": "test"})
assert response.status_code == 200
assert response.json["name"] == "test"
@pytest.mark.asyncio
async def test_nat_start_capture(compute_api, vm):
params = {
"capture_file_name": "test.pcap",
"data_link_type": "DLT_EN10MB"
}
with asyncio_patch("gns3server.compute.builtin.nodes.nat.Nat.start_capture") as mock:
response = await compute_api.post("/projects/{project_id}/nat/nodes/{node_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]), body=params)
assert response.status_code == 200
assert mock.called
assert "test.pcap" in response.json["pcap_file_path"]
@pytest.mark.asyncio
async def test_nat_stop_capture(compute_api, vm):
with asyncio_patch("gns3server.compute.builtin.nodes.nat.Nat.stop_capture") as mock:
response = await compute_api.post("/projects/{project_id}/nat/nodes/{node_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
assert mock.called
# @pytest.mark.asyncio
# async def test_nat_pcap(compute_api, vm, compute_project):
#
# with asyncio_patch("gns3server.compute.builtin.nodes.nat.Nat.get_nio"):
# with asyncio_patch("gns3server.compute.builtin.Builtin.stream_pcap_file"):
# response = await compute_api.get("/projects/{project_id}/nat/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 == 200
@@ -0,0 +1,40 @@
#!/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/>.
import pytest
import json
from gns3server.compute.notification_manager import NotificationManager
# @pytest.mark.asyncio
# async def test_notification_ws(compute_api, http_client):
#
# ws = await http_client.ws_connect(compute_api.get_url("/notifications/ws"))
# answer = await ws.receive()
# answer = json.loads(answer.data)
#
# assert answer["action"] == "ping"
#
# NotificationManager.instance().emit("test", {})
#
# answer = await ws.receive()
# answer = json.loads(answer.data)
# assert answer["action"] == "test"
#
# if not ws.closed:
# await ws.close()
+216
View File
@@ -0,0 +1,216 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
import uuid
import os
from unittest.mock import patch
from tests.utils import asyncio_patch
from gns3server.compute.project_manager import ProjectManager
@pytest.fixture
def base_params(tmpdir):
"""Return standard parameters"""
params = {
"name": "test",
"path": str(tmpdir),
"project_id": str(uuid.uuid4())
}
return params
@pytest.mark.asyncio
async def test_create_project_with_path(compute_api, base_params):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
response = await compute_api.post("/projects", base_params)
assert response.status_code == 201
assert response.json["project_id"] == base_params["project_id"]
@pytest.mark.asyncio
async def test_create_project_with_path_and_empty_variables(compute_api, base_params):
base_params["variables"] = None
with patch("gns3server.compute.project.Project.is_local", return_value=True):
response = await compute_api.post("/projects", base_params)
assert response.status_code == 201
assert response.json["project_id"] == base_params["project_id"]
@pytest.mark.asyncio
async def test_create_project_without_dir(compute_api, base_params):
del base_params["path"]
response = await compute_api.post("/projects", base_params)
assert response.status_code == 201
assert response.json["project_id"] == base_params["project_id"]
assert response.json["name"] == base_params["name"]
@pytest.mark.asyncio
async def test_show_project(compute_api, base_params):
response = await compute_api.post("/projects", base_params)
assert response.status_code == 201
response = await compute_api.get("/projects/{project_id}".format(project_id=base_params["project_id"]))
#print(response.json.keys())
#assert len(response.json.keys()) == 3
assert response.json["project_id"] == base_params["project_id"]
assert response.json["name"] == base_params["name"]
assert response.json["variables"] is None
@pytest.mark.asyncio
async def test_show_project_invalid_uuid(compute_api):
response = await compute_api.get("/projects/50010203-0405-0607-0809-0a0b0c0d0e42")
assert response.status_code == 404
@pytest.mark.asyncio
async def test_list_projects(compute_api):
ProjectManager.instance()._projects = {}
params = {"name": "test", "project_id": "51010203-0405-0607-0809-0a0b0c0d0e0f"}
response = await compute_api.post("/projects", params)
assert response.status_code == 201
params = {"name": "test", "project_id": "52010203-0405-0607-0809-0a0b0c0d0e0b"}
response = await compute_api.post("/projects", params)
assert response.status_code == 201
response = await compute_api.get("/projects")
assert response.status_code == 200
assert len(response.json) == 2
assert "51010203-0405-0607-0809-0a0b0c0d0e0f" in [p["project_id"] for p in response.json]
@pytest.mark.asyncio
async def test_delete_project(compute_api, compute_project):
with asyncio_patch("gns3server.compute.project.Project.delete", return_value=True) as mock:
response = await compute_api.delete("/projects/{project_id}".format(project_id=compute_project.id))
assert response.status_code == 204
assert mock.called
@pytest.mark.asyncio
async def test_update_project(compute_api, base_params):
response = await compute_api.post("/projects", base_params)
assert response.status_code == 201
params = {"variables": [{"name": "TEST1", "value": "VAL1"}]}
response = await compute_api.put("/projects/{project_id}".format(project_id=base_params["project_id"]), params)
assert response.status_code == 200
assert response.json["variables"] == [{"name": "TEST1", "value": "VAL1"}]
@pytest.mark.asyncio
async def test_delete_project_invalid_uuid(compute_api):
response = await compute_api.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
assert response.status_code == 404
@pytest.mark.asyncio
async def test_close_project(compute_api, compute_project):
with asyncio_patch("gns3server.compute.project.Project.close", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/close".format(project_id=compute_project.id))
assert response.status_code == 204
assert mock.called
# @pytest.mark.asyncio
# async def test_close_project_two_client_connected(compute_api, compute_project):
#
# ProjectHandler._notifications_listening = {compute_project.id: 2}
# with asyncio_patch("gns3server.compute.project.Project.close", return_value=True) as mock:
# response = await compute_api.post("/projects/{project_id}/close".format(project_id=compute_project.id))
# assert response.status_code == 204
# assert not mock.called
@pytest.mark.asyncio
async def test_close_project_invalid_uuid(compute_api):
response = await compute_api.post("/projects/{project_id}/close".format(project_id=uuid.uuid4()))
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_file(compute_api, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
with open(os.path.join(project.path, "hello"), "w+") as f:
f.write("world")
response = await compute_api.get("/projects/{project_id}/files/hello".format(project_id=project.id), raw=True)
assert response.status_code == 200
assert response.content == b"world"
response = await compute_api.get("/projects/{project_id}/files/false".format(project_id=project.id), raw=True)
assert response.status_code == 404
response = await compute_api.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_write_file(compute_api, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
response = await compute_api.post("/projects/{project_id}/files/hello".format(project_id=project.id), body="world", raw=True)
assert response.status_code == 204
with open(os.path.join(project.path, "hello")) as f:
assert f.read() == "world"
response = await compute_api.post("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_stream_file(compute_api, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
with open(os.path.join(project.path, "hello"), "w+") as f:
f.write("world")
response = await compute_api.get("/projects/{project_id}/files/hello".format(project_id=project.id), raw=True)
assert response.status_code == 200
assert response.content == b"world"
response = await compute_api.get("/projects/{project_id}/files/false".format(project_id=project.id), raw=True)
assert response.status_code == 404
response = await compute_api.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
assert response.status_code == 404
+453
View File
@@ -0,0 +1,453 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
import uuid
import os
import sys
import stat
from tests.utils import asyncio_patch
from unittest.mock import patch
@pytest.fixture
def fake_qemu_bin(monkeypatch, tmpdir):
monkeypatch.setenv("PATH", str(tmpdir))
if sys.platform.startswith("win"):
bin_path = os.path.join(os.environ["PATH"], "qemu-system-x86_64w.exe")
else:
bin_path = os.path.join(os.environ["PATH"], "qemu-system-x86_64")
with open(bin_path, "w+") as f:
f.write("1")
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
return bin_path
@pytest.fixture
def fake_qemu_vm(images_dir):
img_dir = os.path.join(images_dir, "QEMU")
bin_path = os.path.join(img_dir, "linux载.img")
with open(bin_path, "w+") as f:
f.write("1")
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
return bin_path
@pytest.fixture
def base_params(tmpdir, fake_qemu_bin):
"""Return standard parameters"""
return {"name": "PC TEST 1", "qemu_path": fake_qemu_bin}
@pytest.fixture
@pytest.mark.asyncio
async def vm(compute_api, compute_project, base_params):
response = await compute_api.post("/projects/{project_id}/qemu/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
return response.json
@pytest.mark.asyncio
async def test_qemu_create(compute_api, compute_project, base_params, fake_qemu_bin):
response = await compute_api.post("/projects/{project_id}/qemu/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["qemu_path"] == fake_qemu_bin
assert response.json["platform"] == "x86_64"
@pytest.mark.asyncio
async def test_qemu_create_platform(compute_api, compute_project, base_params, fake_qemu_bin):
base_params["qemu_path"] = None
base_params["platform"] = "x86_64"
response = await compute_api.post("/projects/{project_id}/qemu/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["qemu_path"] == fake_qemu_bin
assert response.json["platform"] == "x86_64"
@pytest.mark.asyncio
async def test_qemu_create_with_params(compute_api, compute_project, base_params, fake_qemu_vm):
params = base_params
params["ram"] = 1024
params["hda_disk_image"] = "linux载.img"
response = await compute_api.post("/projects/{project_id}/qemu/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["ram"] == 1024
assert response.json["hda_disk_image"] == "linux载.img"
assert response.json["hda_disk_image_md5sum"] == "c4ca4238a0b923820dcc509a6f75849b"
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
@pytest.mark.asyncio
async def test_qemu_create_with_project_file(compute_api, compute_project, base_params, fake_qemu_vm):
response = await compute_api.post("/projects/{project_id}/files/hello.img".format(project_id=compute_project.id), body=b"world", raw=True)
assert response.status_code == 204
params = base_params
params["hda_disk_image"] = "hello.img"
response = await compute_api.post("/projects/{project_id}/qemu/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
assert response.json["hda_disk_image"] == "hello.img"
assert response.json["hda_disk_image_md5sum"] == "7d793037a0760186574b0282f2f435e7"
@pytest.mark.asyncio
async def test_qemu_get(compute_api, compute_project, vm):
response = await compute_api.get("/projects/{project_id}/qemu/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 200
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["node_directory"] == os.path.join(compute_project.path, "project-files", "qemu", vm["node_id"])
@pytest.mark.asyncio
async def test_qemu_start(compute_api, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.start", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/start".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_qemu_stop(compute_api, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.stop", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/stop".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_qemu_reload(compute_api, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.reload", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/reload".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_qemu_suspend(compute_api, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.suspend", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/suspend".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_qemu_resume(compute_api, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.resume", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/resume".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_qemu_delete(compute_api, vm):
with asyncio_patch("gns3server.compute.qemu.Qemu.delete_node", return_value=True) as mock:
response = await compute_api.delete("/projects/{project_id}/qemu/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_qemu_update(compute_api, vm, free_console_port, fake_qemu_vm):
params = {
"name": "test",
"console": free_console_port,
"ram": 1024,
"hdb_disk_image": "linux载.img"
}
response = await compute_api.put("/projects/{project_id}/qemu/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
assert response.json["hdb_disk_image"] == "linux载.img"
assert response.json["ram"] == 1024
@pytest.mark.asyncio
async def test_qemu_nio_create_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.add_ubridge_udp_connection"):
await compute_api.put("/projects/{project_id}/qemu/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"adapters": 2})
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_qemu_nio_update_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
await compute_api.put("/projects/{project_id}/qemu/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"adapters": 2})
await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
params["filters"] = {}
response = await compute_api.put("/projects/{project_id}/qemu/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201, response.body.decode()
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_qemu_delete_nio(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM._ubridge_send"):
await compute_api.put("/projects/{project_id}/qemu/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"adapters": 2})
await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
response = await compute_api.delete("/projects/{project_id}/qemu/nodes/{node_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
@pytest.mark.asyncio
async def test_qemu_list_binaries(compute_api, vm):
ret = [{"path": "/tmp/1", "version": "2.2.0"},
{"path": "/tmp/2", "version": "2.1.0"}]
with asyncio_patch("gns3server.compute.qemu.Qemu.binary_list", return_value=ret) as mock:
response = await compute_api.get("/qemu/binaries".format(project_id=vm["project_id"]))
assert mock.called_with(None)
assert response.status_code == 200
assert response.json == ret
@pytest.mark.asyncio
async def test_qemu_list_binaries_filter(compute_api, vm):
ret = [
{"path": "/tmp/x86_64", "version": "2.2.0"},
{"path": "/tmp/alpha", "version": "2.1.0"},
{"path": "/tmp/i386", "version": "2.1.0"}
]
with asyncio_patch("gns3server.compute.qemu.Qemu.binary_list", return_value=ret) as mock:
response = await compute_api.get("/qemu/binaries".format(project_id=vm["project_id"]), body={"archs": ["i386"]})
assert response.status_code == 200
assert mock.called_with(["i386"])
assert response.json == ret
@pytest.mark.asyncio
async def test_images(compute_api, fake_qemu_vm):
response = await compute_api.get("/qemu/images")
assert response.status_code == 200
assert {"filename": "linux载.img", "path": "linux载.img", "md5sum": "c4ca4238a0b923820dcc509a6f75849b", "filesize": 1} in response.json
@pytest.mark.asyncio
async def test_upload_image(compute_api, tmpdir):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir)):
response = await compute_api.post("/qemu/images/test2使", body="TEST", raw=True)
assert response.status_code == 204
print(os.listdir(tmpdir))
with open(str(tmpdir / "test2使")) as f:
assert f.read() == "TEST"
with open(str(tmpdir / "test2使.md5sum")) as f:
checksum = f.read()
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
@pytest.mark.asyncio
async def test_upload_image_ova(compute_api, tmpdir):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir)):
response = await compute_api.post("/qemu/images/test2.ova/test2.vmdk", body="TEST", raw=True)
assert response.status_code == 204
with open(str(tmpdir / "test2.ova" / "test2.vmdk")) as f:
assert f.read() == "TEST"
with open(str(tmpdir / "test2.ova" / "test2.vmdk.md5sum")) as f:
checksum = f.read()
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
@pytest.mark.asyncio
async def test_upload_image_forbiden_location(compute_api, tmpdir):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir)):
response = await compute_api.post("/qemu/images/../../test2", body="TEST", raw=True)
assert response.status_code == 404
@pytest.mark.skipif(not sys.platform.startswith("win") and os.getuid() == 0, reason="Root can delete any image")
@pytest.mark.asyncio
async def test_upload_image_permission_denied(compute_api, images_dir):
with open(os.path.join(images_dir, "QEMU", "test2.tmp"), "w+") as f:
f.write("")
os.chmod(os.path.join(images_dir, "QEMU", "test2.tmp"), 0)
response = await compute_api.post("/qemu/images/test2", body="TEST", raw=True)
assert response.status_code == 409
@pytest.mark.asyncio
async def test_create_img_relative(compute_api):
params = {
"qemu_img": "/tmp/qemu-img",
"path": "hda.qcow2",
"format": "qcow2",
"preallocation": "metadata",
"cluster_size": 64,
"refcount_bits": 12,
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = await compute_api.post("/qemu/img", params)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_create_img_absolute_non_local(compute_api, config):
config.set("Server", "local", "false")
params = {
"qemu_img": "/tmp/qemu-img",
"path": "/tmp/hda.qcow2",
"format": "qcow2",
"preallocation": "metadata",
"cluster_size": 64,
"refcount_bits": 12,
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = await compute_api.post("/qemu/img", params)
assert response.status_code == 403
@pytest.mark.asyncio
async def test_create_img_absolute_local(compute_api, config):
config.set("Server", "local", "true")
params = {
"qemu_img": "/tmp/qemu-img",
"path": "/tmp/hda.qcow2",
"format": "qcow2",
"preallocation": "metadata",
"cluster_size": 64,
"refcount_bits": 12,
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = await compute_api.post("/qemu/img", params)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_capabilities(compute_api):
with asyncio_patch("gns3server.compute.Qemu.get_kvm_archs", return_value=["x86_64"]):
response = await compute_api.get("/qemu/capabilities")
assert response.json["kvm"] == ["x86_64"]
@pytest.mark.asyncio
async def test_qemu_duplicate(compute_api, compute_project, vm, base_params):
# create destination node first
response = await compute_api.post("/projects/{project_id}/qemu/nodes".format(project_id=compute_project.id), base_params)
assert response.status_code == 201
params = {"destination_node_id": response.json["node_id"]}
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/duplicate".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
@pytest.mark.asyncio
async def test_qemu_start_capture(compute_api, vm):
params = {
"capture_file_name": "test.pcap",
"data_link_type": "DLT_EN10MB"
}
with patch("gns3server.compute.qemu.qemu_vm.QemuVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.start_capture") as mock:
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert mock.called
assert "test.pcap" in response.json["pcap_file_path"]
@pytest.mark.asyncio
async def test_qemu_stop_capture(compute_api, vm):
with patch("gns3server.compute.qemu.qemu_vm.QemuVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.stop_capture") as mock:
response = await compute_api.post("/projects/{project_id}/qemu/nodes/{node_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
assert mock.called
# @pytest.mark.asyncio
# async def test_qemu_pcap(compute_api, vm, compute_project):
#
# with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.get_nio"):
# with asyncio_patch("gns3server.compute.qemu.Qemu.stream_pcap_file"):
# response = await compute_api.get("/projects/{project_id}/qemu/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 == 200
@@ -0,0 +1,210 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
from tests.utils import asyncio_patch
from unittest.mock import patch
@pytest.fixture(scope="function")
@pytest.mark.asyncio
async def vm(compute_api, compute_project):
vboxmanage_path = "/fake/VboxManage"
params = {
"name": "VMTEST",
"vmname": "VMTEST",
"linked_clone": False
}
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes".format(project_id=compute_project.id), params)
assert mock.called
assert response.status_code == 201
with patch("gns3server.compute.virtualbox.VirtualBox.find_vboxmanage", return_value=vboxmanage_path):
return response.json
@pytest.mark.asyncio
async def test_vbox_create(compute_api, compute_project):
params = {
"name": "VM1",
"vmname": "VM1",
"linked_clone": False
}
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True):
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
assert response.json["name"] == "VM1"
assert response.json["project_id"] == compute_project.id
@pytest.mark.asyncio
async def test_vbox_get(compute_api, compute_project, vm):
response = await compute_api.get("/projects/{project_id}/virtualbox/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 200
assert response.json["name"] == "VMTEST"
assert response.json["project_id"] == compute_project.id
@pytest.mark.asyncio
async def test_vbox_start(compute_api, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.check_hw_virtualization", return_value=True):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.start", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/start".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vbox_stop(compute_api, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.stop", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/stop".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vbox_suspend(compute_api, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.suspend", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/suspend".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vbox_resume(compute_api, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.resume", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/resume".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vbox_reload(compute_api, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.reload", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/reload".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vbox_nio_create_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert mock.called
args, kwgars = mock.call_args
assert args[0] == 0
assert response.status_code == 201
assert response.json["type"] == "nio_udp"
# @pytest.mark.asyncio
# async def test_vbox_nio_update_udp(compute_api, vm):
#
# params = {
# "type": "nio_udp",
# "lport": 4242,
# "rport": 4343,
# "rhost": "127.0.0.1",
# "filters": {}
# }
#
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.ethernet_adapters'):
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding'):
# response = await compute_api.put("/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
#
# assert response.status_code == 201
# assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_vbox_delete_nio(compute_api, vm):
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
response = await compute_api.delete("/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
args, kwgars = mock.call_args
assert args[0] == 0
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vbox_update(compute_api, vm, free_console_port):
params = {
"name": "test",
"console": free_console_port
}
response = await compute_api.put("/projects/{project_id}/virtualbox/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
@pytest.mark.asyncio
async def test_virtualbox_start_capture(compute_api, vm):
params = {
"capture_file_name": "test.pcap",
"data_link_type": "DLT_EN10MB"
}
with patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.start_capture") as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert mock.called
assert "test.pcap" in response.json["pcap_file_path"]
@pytest.mark.asyncio
async def test_virtualbox_stop_capture(compute_api, vm):
with patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.stop_capture") as mock:
response = await compute_api.post("/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
assert mock.called
# @pytest.mark.asyncio
# async def test_virtualbox_pcap(compute_api, vm, compute_project):
#
# with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.get_nio"):
# with asyncio_patch("gns3server.compute.virtualbox.VirtualBox.stream_pcap_file"):
# response = await compute_api.get("/projects/{project_id}/virtualbox/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 == 200
@@ -0,0 +1,223 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
from tests.utils import asyncio_patch
from unittest.mock import patch
@pytest.fixture(scope="function")
@pytest.mark.asyncio
async def vm(compute_api, compute_project, vmx_path):
params = {
"name": "VMTEST",
"vmx_path": vmx_path,
"linked_clone": False
}
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.create", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/vmware/nodes".format(project_id=compute_project.id), params)
assert mock.called
assert response.status_code == 201, response.body.decode()
return response.json
@pytest.fixture
@pytest.mark.asyncio
def vmx_path(tmpdir):
"""
Return a fake VMX file
"""
path = str(tmpdir / "test.vmx")
with open(path, 'w+') as f:
f.write("1")
return path
@pytest.mark.asyncio
async def test_vmware_create(compute_api, compute_project, vmx_path):
params = {
"name": "VM1",
"vmx_path": vmx_path,
"linked_clone": False
}
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.create", return_value=True):
response = await compute_api.post("/projects/{project_id}/vmware/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201, response.body.decode()
assert response.json["name"] == "VM1"
assert response.json["project_id"] == compute_project.id
@pytest.mark.asyncio
async def test_vmware_get(compute_api, compute_project, vm):
response = await compute_api.get("/projects/{project_id}/vmware/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 200
assert response.json["name"] == "VMTEST"
assert response.json["project_id"] == compute_project.id
@pytest.mark.asyncio
async def test_vmware_start(compute_api, vm):
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.check_hw_virtualization", return_value=True) as mock1:
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.start", return_value=True) as mock2:
response = await compute_api.post("/projects/{project_id}/vmware/nodes/{node_id}/start".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock1.called
assert mock2.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vmware_stop(compute_api, vm):
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.stop", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/vmware/nodes/{node_id}/stop".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vmware_suspend(compute_api, vm):
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.suspend", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/vmware/nodes/{node_id}/suspend".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vmware_resume(compute_api, vm):
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.resume", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/vmware/nodes/{node_id}/resume".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vmware_reload(compute_api, vm):
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.reload", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/vmware/nodes/{node_id}/reload".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vmware_nio_create_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch('gns3server.compute.vmware.vmware_vm.VMwareVM.adapter_add_nio_binding') as mock:
response = await compute_api.post("/projects/{project_id}/vmware/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert mock.called
args, kwgars = mock.call_args
assert args[0] == 0
assert response.status_code == 201
assert response.json["type"] == "nio_udp"
# @pytest.mark.asyncio
# async def test_vmware_nio_update_udp(compute_api, vm):
#
# params = {
# "type": "nio_udp",
# "lport": 4242,
# "rport": 4343,
# "rhost": "127.0.0.1",
# "filters": {}
# }
#
# with asyncio_patch('gns3server.compute.vmware.vmware_vm.VMwareVM._ubridge_send'):
# with asyncio_patch('gns3server.compute.vmware.vmware_vm.VMwareVM.ethernet_adapters'):
# with patch('gns3server.compute.vmware.vmware_vm.VMwareVM._get_vnet') as mock:
# response = await compute_api.put("/projects/{project_id}/vmware/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
# assert response.status_code == 201
# assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_vmware_delete_nio(compute_api, vm):
with asyncio_patch('gns3server.compute.vmware.vmware_vm.VMwareVM.adapter_remove_nio_binding') as mock:
response = await compute_api.delete("/projects/{project_id}/vmware/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
args, kwgars = mock.call_args
assert args[0] == 0
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vmware_update(compute_api, vm, free_console_port):
params = {
"name": "test",
"console": free_console_port
}
response = await compute_api.put("/projects/{project_id}/vmware/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
@pytest.mark.asyncio
async def test_vmware_start_capture(compute_api, vm):
params = {
"capture_file_name": "test.pcap",
"data_link_type": "DLT_EN10MB"
}
with patch("gns3server.compute.vmware.vmware_vm.VMwareVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.start_capture") as mock:
response = await compute_api.post("/projects/{project_id}/vmware/nodes/{node_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]), body=params)
assert response.status_code == 200
assert mock.called
assert "test.pcap" in response.json["pcap_file_path"]
@pytest.mark.asyncio
async def test_vmware_stop_capture(compute_api, vm):
with patch("gns3server.compute.vmware.vmware_vm.VMwareVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.stop_capture") as mock:
response = await compute_api.post("/projects/{project_id}/vmware/nodes/{node_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
assert mock.called
# @pytest.mark.asyncio
# async def test_vmware_pcap(compute_api, vm, compute_project):
#
# with asyncio_patch("gns3server.compute.vmware.vmware_vm.VMwareVM.get_nio"):
# with asyncio_patch("gns3server.compute.vmware.VMware.stream_pcap_file"):
# response = await compute_api.get("/projects/{project_id}/vmware/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 == 200
+232
View File
@@ -0,0 +1,232 @@
# -*- coding: utf-8 -*-
#
# 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/>.
import pytest
import uuid
from tests.utils import asyncio_patch
from unittest.mock import patch
@pytest.fixture
@pytest.mark.asyncio
async def vm(compute_api, compute_project):
params = {"name": "PC TEST 1"}
response = await compute_api.post("/projects/{project_id}/vpcs/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
return response.json
@pytest.mark.asyncio
async def test_vpcs_create(compute_api, compute_project):
params = {"name": "PC TEST 1"}
response = await compute_api.post("/projects/{project_id}/vpcs/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
@pytest.mark.asyncio
async def test_vpcs_get(compute_api, compute_project, vm):
response = await compute_api.get("/projects/{project_id}/vpcs/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 200
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["status"] == "stopped"
@pytest.mark.asyncio
async def test_vpcs_create_startup_script(compute_api, compute_project):
params = {
"name": "PC TEST 1",
"startup_script": "ip 192.168.1.2\necho TEST"
}
response = await compute_api.post("/projects/{project_id}/vpcs/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
@pytest.mark.asyncio
async def test_vpcs_create_port(compute_api, compute_project, free_console_port):
params = {
"name": "PC TEST 1",
"console": free_console_port
}
response = await compute_api.post("/projects/{project_id}/vpcs/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == compute_project.id
assert response.json["console"] == free_console_port
@pytest.mark.asyncio
async def test_vpcs_nio_create_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.add_ubridge_udp_connection"):
response = await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_vpcs_nio_update_udp(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.add_ubridge_udp_connection"):
response = await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
params["filters"] = {}
response = await compute_api.put("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201, response.body.decode("utf-8")
assert response.json["type"] == "nio_udp"
@pytest.mark.asyncio
async def test_vpcs_delete_nio(compute_api, vm):
params = {
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"
}
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._ubridge_send"):
await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
response = await compute_api.delete("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204, response.body.decode()
@pytest.mark.asyncio
async def test_vpcs_start(compute_api, vm):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/start".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vpcs_stop(compute_api, vm):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.stop", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/stop".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vpcs_reload(compute_api, vm):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.reload", return_value=True) as mock:
response = await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/reload".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vpcs_delete(compute_api, vm):
with asyncio_patch("gns3server.compute.vpcs.VPCS.delete_node", return_value=True) as mock:
response = await compute_api.delete("/projects/{project_id}/vpcs/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert mock.called
assert response.status_code == 204
@pytest.mark.asyncio
async def test_vpcs_duplicate(compute_api, compute_project, vm):
# create destination node first
params = {"name": "PC TEST 1"}
response = await compute_api.post("/projects/{project_id}/vpcs/nodes".format(project_id=compute_project.id), params)
assert response.status_code == 201
params = {"destination_node_id": response.json["node_id"]}
response = await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/duplicate".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 201
@pytest.mark.asyncio
async def test_vpcs_update(compute_api, vm, free_console_port):
console_port = free_console_port
params = {
"name": "test",
"console": console_port
}
response = await compute_api.put("/projects/{project_id}/vpcs/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), params)
assert response.status_code == 200
assert response.json["name"] == "test"
assert response.json["console"] == console_port
@pytest.mark.asyncio
async def test_vpcs_start_capture(compute_api, vm):
params = {
"capture_file_name": "test.pcap",
"data_link_type": "DLT_EN10MB"
}
with patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start_capture") as mock:
response = await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]), body=params)
assert response.status_code == 200
assert mock.called
assert "test.pcap" in response.json["pcap_file_path"]
@pytest.mark.asyncio
async def test_vpcs_stop_capture(compute_api, vm):
with patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.is_running", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.stop_capture") as mock:
response = await compute_api.post("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], node_id=vm["node_id"]))
assert response.status_code == 204
assert mock.called
# @pytest.mark.asyncio
# async def test_vpcs_pcap(compute_api, vm, compute_project):
#
# with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.get_nio"):
# with asyncio_patch("gns3server.compute.vpcs.VPCS.stream_pcap_file"):
# response = await compute_api.get("/projects/{project_id}/vpcs/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 == 200