Rename hypervisor to compute

Fix #487
This commit is contained in:
Julien Duponchelle committed 2016-04-15 17:59:59 +02:00
1 parent 3185baff0d
commit 58d4a529d4
186 files changed
+1173 -1206

No files matched your search

@@ -20,8 +20,8 @@ import asyncio
from unittest.mock import MagicMock
from tests.utils import asyncio_patch
from gns3server.hypervisor.docker import Docker
from gns3server.hypervisor.docker.docker_error import DockerError
from gns3server.compute.docker import Docker
from gns3server.compute.docker.docker_error import DockerError
def test_query_success(loop):
@@ -125,7 +125,7 @@ def test_list_images(loop):
}
]
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
images = loop.run_until_complete(asyncio.async(Docker.instance().list_images()))
mock.assert_called_with("GET", "images/json", params={"all": 0})
assert len(images) == 5
@@ -22,9 +22,9 @@ import os
from tests.utils import asyncio_patch, AsyncioMagicMock
from gns3server.ubridge.ubridge_error import UbridgeNamespaceError
from gns3server.hypervisor.docker.docker_vm import DockerVM
from gns3server.hypervisor.docker.docker_error import *
from gns3server.hypervisor.docker import Docker
from gns3server.compute.docker.docker_vm import DockerVM
from gns3server.compute.docker.docker_error import *
from gns3server.compute.docker import Docker
from gns3server.utils.get_resource import get_resource
@@ -79,8 +79,8 @@ def test_create(loop, project, manager):
"Id": "e90e34656806",
"Warnings": []
}
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
loop.run_until_complete(asyncio.async(vm.create()))
mock.assert_called_with("POST", "containers/create", data={
@@ -115,8 +115,8 @@ def test_create_vnc(loop, project, manager):
"Warnings": []
}
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", console_type="vnc", console=5900)
vm._start_vnc = MagicMock()
vm._display = 42
@@ -155,8 +155,8 @@ def test_create_start_cmd(loop, project, manager):
"Id": "e90e34656806",
"Warnings": []
}
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
vm._start_command = "/bin/ls"
loop.run_until_complete(asyncio.async(vm.create()))
@@ -191,8 +191,8 @@ def test_create_environment(loop, project, manager):
"Id": "e90e34656806",
"Warnings": []
}
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
vm.environment = "YES=1\nNO=0"
loop.run_until_complete(asyncio.async(vm.create()))
@@ -243,8 +243,8 @@ def test_create_image_not_available(loop, project, manager):
vm._get_image_informations = MagicMock()
vm._get_image_informations.side_effect = informations
with asyncio_patch("gns3server.hypervisor.docker.DockerVM.pull_image", return_value=True) as mock_pull:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.DockerVM.pull_image", return_value=True) as mock_pull:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
loop.run_until_complete(asyncio.async(vm.create()))
mock.assert_called_with("POST", "containers/create", data={
"Tty": True,
@@ -286,17 +286,17 @@ def test_get_container_state(loop, vm):
"StartedAt": "2015-01-06T15:47:32.072697474Z"
}
}
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
assert loop.run_until_complete(asyncio.async(vm._get_container_state())) == "running"
response["State"]["Running"] = False
response["State"]["Paused"] = True
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
assert loop.run_until_complete(asyncio.async(vm._get_container_state())) == "paused"
response["State"]["Running"] = False
response["State"]["Paused"] = False
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
assert loop.run_until_complete(asyncio.async(vm._get_container_state())) == "exited"
@@ -307,17 +307,17 @@ def test_is_running(loop, vm):
"Paused": False
}
}
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
assert loop.run_until_complete(asyncio.async(vm.is_running())) is False
response["State"]["Running"] = True
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
assert loop.run_until_complete(asyncio.async(vm.is_running())) is True
def test_pause(loop, vm):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock:
loop.run_until_complete(asyncio.async(vm.pause()))
mock.assert_called_with("POST", "containers/e90e34656842/pause")
@@ -326,7 +326,7 @@ def test_pause(loop, vm):
def test_unpause(loop, vm):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock:
loop.run_until_complete(asyncio.async(vm.unpause()))
mock.assert_called_with("POST", "containers/e90e34656842/unpause")
@@ -349,7 +349,7 @@ def test_start(loop, vm, manager, free_console_port):
nio = manager.create_nio(0, {"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
loop.run_until_complete(asyncio.async(vm.start()))
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
@@ -368,12 +368,12 @@ def test_start_namespace_failed(loop, vm, manager, free_console_port):
nio = manager.create_nio(0, {"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._start_ubridge") as mock_start_ubridge:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._add_ubridge_connection", side_effect=UbridgeNamespaceError()) as mock_add_ubridge_connection:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_log", return_value='Hello not available') as mock_log:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection", side_effect=UbridgeNamespaceError()) as mock_add_ubridge_connection:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_log", return_value='Hello not available') as mock_log:
with pytest.raises(DockerError):
loop.run_until_complete(asyncio.async(vm.start()))
@@ -392,12 +392,12 @@ def test_start_without_nio(loop, vm, manager, free_console_port):
assert vm.status != "started"
vm.adapters = 1
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._start_ubridge") as mock_start_ubridge:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._add_ubridge_connection") as mock_add_ubridge_connection:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._start_console") as mock_start_console:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection") as mock_add_ubridge_connection:
with asyncio_patch("gns3server.compute.docker.DockerVM._start_console") as mock_start_console:
loop.run_until_complete(asyncio.async(vm.start()))
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
@@ -409,8 +409,8 @@ def test_start_without_nio(loop, vm, manager, free_console_port):
def test_start_unpause(loop, vm, manager, free_console_port):
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="paused"):
with asyncio_patch("gns3server.hypervisor.docker.DockerVM.unpause", return_value="paused") as mock:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="paused"):
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause", return_value="paused") as mock:
loop.run_until_complete(asyncio.async(vm.start()))
assert mock.called
assert vm.status == "started"
@@ -418,7 +418,7 @@ def test_start_unpause(loop, vm, manager, free_console_port):
def test_restart(loop, vm):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock:
loop.run_until_complete(asyncio.async(vm.restart()))
mock.assert_called_with("POST", "containers/e90e34656842/restart")
@@ -428,8 +428,8 @@ def test_stop(loop, vm):
vm._ubridge_hypervisor = MagicMock()
vm._ubridge_hypervisor.is_running.return_value = True
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="running"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="running"):
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
loop.run_until_complete(asyncio.async(vm.stop()))
mock_query.assert_called_with("POST", "containers/e90e34656842/stop", params={"t": 5})
assert vm._ubridge_hypervisor.stop.called
@@ -437,9 +437,9 @@ def test_stop(loop, vm):
def test_stop_paused_container(loop, vm):
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="paused"):
with asyncio_patch("gns3server.hypervisor.docker.DockerVM.unpause") as mock_unpause:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="paused"):
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause") as mock_unpause:
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
loop.run_until_complete(asyncio.async(vm.stop()))
mock_query.assert_called_with("POST", "containers/e90e34656842/stop", params={"t": 5})
assert mock_unpause.called
@@ -455,9 +455,9 @@ def test_update(loop, vm):
original_console = vm.console
original_aux = vm.aux
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock_query:
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock_query:
loop.run_until_complete(asyncio.async(vm.update()))
mock_query.assert_any_call("DELETE", "containers/e90e34656842", params={"force": 1})
@@ -500,10 +500,10 @@ def test_update_vnc(loop, vm):
original_console = vm.console
original_aux = vm.aux
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._start_vnc"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock_query:
with asyncio_patch("gns3server.compute.docker.DockerVM._start_vnc"):
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock_query:
loop.run_until_complete(asyncio.async(vm.update()))
assert vm.console == original_console
@@ -520,9 +520,9 @@ def test_update_running(loop, vm):
original_console = vm.console
vm.start = MagicMock()
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="running"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock_query:
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="running"):
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock_query:
loop.run_until_complete(asyncio.async(vm.update()))
mock_query.assert_any_call("DELETE", "containers/e90e34656842", params={"force": 1})
@@ -555,8 +555,8 @@ def test_update_running(loop, vm):
def test_delete(loop, vm):
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
loop.run_until_complete(asyncio.async(vm.delete()))
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1})
@@ -569,8 +569,8 @@ def test_close(loop, vm, port_manager):
nio = vm.manager.create_nio(0, nio)
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
loop.run_until_complete(asyncio.async(vm.close()))
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1})
@@ -584,8 +584,8 @@ def test_close_vnc(loop, vm, port_manager):
vm._x11vnc_process = MagicMock()
vm._xvfb_process = MagicMock()
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
loop.run_until_complete(asyncio.async(vm.close()))
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1})
@@ -599,7 +599,7 @@ def test_get_namespace(loop, vm):
"Pid": 42
}
}
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock_query:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock_query:
assert loop.run_until_complete(asyncio.async(vm._get_namespace())) == 42
mock_query.assert_called_with("GET", "containers/e90e34656842/json")
@@ -717,7 +717,7 @@ def test_adapter_remove_nio_binding(vm, loop):
"rhost": "127.0.0.1"}
nio = vm.manager.create_nio(0, nio)
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._delete_ubridge_connection") as delete_ubridge_mock:
with asyncio_patch("gns3server.compute.docker.DockerVM._delete_ubridge_connection") as delete_ubridge_mock:
loop.run_until_complete(asyncio.async(vm.adapter_remove_nio_binding(0)))
assert vm._ethernet_adapters[0].get_nio(0) is None
delete_ubridge_mock.assert_called_with(0)
@@ -750,7 +750,7 @@ def test_pull_image(loop, vm):
mock_query = MagicMock()
mock_query.content.return_value = Response()
with asyncio_patch("gns3server.hypervisor.docker.Docker.http_query", return_value=mock_query) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.http_query", return_value=mock_query) as mock:
images = loop.run_until_complete(asyncio.async(vm.pull_image("ubuntu")))
mock.assert_called_with("POST", "images/create", params={"fromImage": "ubuntu"})
@@ -783,7 +783,7 @@ def test_get_log(loop, vm):
mock_query = MagicMock()
mock_query.read = read
with asyncio_patch("gns3server.hypervisor.docker.Docker.http_query", return_value=mock_query) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.http_query", return_value=mock_query) as mock:
images = loop.run_until_complete(asyncio.async(vm._get_log()))
mock.assert_called_with("GET", "containers/e90e34656842/logs", params={"stderr": 1, "stdout": 1}, data={})
@@ -791,7 +791,7 @@ def test_get_log(loop, vm):
def test_get_image_informations(project, manager, loop):
response = {
}
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
loop.run_until_complete(asyncio.async(vm._get_image_informations()))
mock.assert_called_with("GET", "images/ubuntu/json")
@@ -819,7 +819,7 @@ def test_mount_binds(vm, tmpdir):
def test_start_vnc(vm, loop):
vm.console_resolution = "1280x1024"
with patch("shutil.which", return_value="/bin/x"):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.wait_for_file_creation") as mock_wait:
with asyncio_patch("gns3server.compute.docker.docker_vm.wait_for_file_creation") as mock_wait:
with asyncio_patch("asyncio.create_subprocess_exec") as mock_exec:
loop.run_until_complete(asyncio.async(vm._start_vnc()))
assert vm._display is not None
@@ -23,8 +23,8 @@ import uuid
import os
import asyncio
from gns3server.hypervisor.dynamips import Dynamips
from gns3server.hypervisor.dynamips.dynamips_error import DynamipsError
from gns3server.compute.dynamips import Dynamips
from gns3server.compute.dynamips.dynamips_error import DynamipsError
from unittest.mock import patch
@@ -20,9 +20,9 @@ import asyncio
import configparser
from unittest.mock import patch
from gns3server.hypervisor.dynamips.nodes.router import Router
from gns3server.hypervisor.dynamips.dynamips_error import DynamipsError
from gns3server.hypervisor.dynamips import Dynamips
from gns3server.compute.dynamips.nodes.router import Router
from gns3server.compute.dynamips.dynamips_error import DynamipsError
from gns3server.compute.dynamips import Dynamips
from gns3server.config import Config
@@ -25,10 +25,10 @@ import sys
pytestmark = pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
if not sys.platform.startswith("win"):
from gns3server.hypervisor.iou import IOU
from gns3server.hypervisor.iou.iou_error import IOUError
from gns3server.compute.iou import IOU
from gns3server.compute.iou.iou_error import IOUError
from gns3server.hypervisor.project_manager import ProjectManager
from gns3server.compute.project_manager import ProjectManager
@pytest.fixture(scope="function")
@@ -32,9 +32,9 @@ from unittest.mock import patch, MagicMock, PropertyMock
pytestmark = pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
if not sys.platform.startswith("win"):
from gns3server.hypervisor.iou.iou_vm import IOUVM
from gns3server.hypervisor.iou.iou_error import IOUError
from gns3server.hypervisor.iou import IOU
from gns3server.compute.iou.iou_vm import IOUVM
from gns3server.compute.iou.iou_error import IOUError
from gns3server.compute.iou import IOU
from gns3server.config import Config
@@ -108,10 +108,10 @@ def test_vm_invalid_iouyap_path(project, manager, loop, fake_iou_bin):
def test_start(loop, vm, monkeypatch):
mock_process = MagicMock()
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._check_iou_licence", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with patch("gns3server.compute.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_iou_licence", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as mock_exec:
mock_process.returncode = None
loop.run_until_complete(asyncio.async(vm.start()))
@@ -126,10 +126,10 @@ def test_start_with_iourc(loop, vm, monkeypatch, tmpdir):
f.write("1")
mock_process = MagicMock()
with patch("gns3server.config.Config.get_section_config", return_value={"iourc_path": fake_file, "iouyap_path": vm.iouyap_path}):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._check_iou_licence", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_iou_licence", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as exec_mock:
mock_process.returncode = None
loop.run_until_complete(asyncio.async(vm.start()))
@@ -162,9 +162,9 @@ def test_stop(loop, vm):
future.set_result(True)
process.wait.return_value = future
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
loop.run_until_complete(asyncio.async(vm.start()))
@@ -184,9 +184,9 @@ def test_reload(loop, vm, fake_iou_bin):
process.wait.return_value = future
process.returncode = None
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
loop.run_until_complete(asyncio.async(vm.start()))
@@ -197,7 +197,7 @@ def test_reload(loop, vm, fake_iou_bin):
def test_close(vm, port_manager, loop):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
vm.start()
port = vm.console
@@ -22,8 +22,8 @@ import sys
import pytest
import platform
from gns3server.hypervisor.qemu import Qemu
from gns3server.hypervisor.qemu.qemu_error import QemuError
from gns3server.compute.qemu import Qemu
from gns3server.compute.qemu.qemu_error import QemuError
from tests.utils import asyncio_patch
from unittest.mock import patch, MagicMock
@@ -40,7 +40,7 @@ def fake_qemu_img_binary(tmpdir):
def test_get_qemu_version(loop):
with asyncio_patch("gns3server.hypervisor.qemu.subprocess_check_output", return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard") as mock:
with asyncio_patch("gns3server.compute.qemu.subprocess_check_output", return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard") as mock:
version = loop.run_until_complete(asyncio.async(Qemu.get_qemu_version("/tmp/qemu-test")))
if sys.platform.startswith("win"):
assert version == ""
@@ -58,7 +58,7 @@ def test_binary_list(loop):
f.write("1")
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
with asyncio_patch("gns3server.hypervisor.qemu.subprocess_check_output", return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard") as mock:
with asyncio_patch("gns3server.compute.qemu.subprocess_check_output", return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard") as mock:
if sys.platform.startswith("win"):
version = ""
else:
@@ -97,7 +97,7 @@ def test_img_binary_list(loop):
f.write("1")
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
with asyncio_patch("gns3server.hypervisor.qemu.subprocess_check_output", return_value="qemu-img version 2.2.0, Copyright (c) 2004-2008 Fabrice Bellard") as mock:
with asyncio_patch("gns3server.compute.qemu.subprocess_check_output", return_value="qemu-img version 2.2.0, Copyright (c) 2004-2008 Fabrice Bellard") as mock:
qemus = loop.run_until_complete(asyncio.async(Qemu.img_binary_list()))
version = "2.2.0"
@@ -151,7 +151,7 @@ def test_create_image_relative_path(loop, tmpdir, fake_qemu_img_binary):
"size": 100
}
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
with patch("gns3server.hypervisor.qemu.Qemu.get_images_directory", return_value=str(tmpdir)):
with patch("gns3server.compute.qemu.Qemu.get_images_directory", return_value=str(tmpdir)):
loop.run_until_complete(asyncio.async(Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options)))
args, kwargs = process.call_args
assert args == (
@@ -172,7 +172,7 @@ def test_create_image_exist(loop, tmpdir, fake_qemu_img_binary):
"size": 100
}
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
with patch("gns3server.hypervisor.qemu.Qemu.get_images_directory", return_value=str(tmpdir)):
with patch("gns3server.compute.qemu.Qemu.get_images_directory", return_value=str(tmpdir)):
with pytest.raises(QemuError):
loop.run_until_complete(asyncio.async(Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options)))
assert not process.called
@@ -28,11 +28,11 @@ from tests.utils import asyncio_patch
from unittest import mock
from unittest.mock import patch, MagicMock
from gns3server.hypervisor.qemu.qemu_vm import QemuVM
from gns3server.hypervisor.qemu.qemu_error import QemuError
from gns3server.hypervisor.qemu import Qemu
from gns3server.compute.qemu.qemu_vm import QemuVM
from gns3server.compute.qemu.qemu_error import QemuError
from gns3server.compute.qemu import Qemu
from gns3server.utils import force_unix_path
from gns3server.hypervisor.notification_manager import NotificationManager
from gns3server.compute.notification_manager import NotificationManager
@pytest.fixture(scope="module")
@@ -177,7 +177,7 @@ def test_termination_callback_error(vm, tmpdir, async_run):
def test_reload(loop, vm):
with asyncio_patch("gns3server.hypervisor.qemu.QemuVM._control_vm") as mock:
with asyncio_patch("gns3server.compute.qemu.QemuVM._control_vm") as mock:
loop.run_until_complete(asyncio.async(vm.reload()))
assert mock.called_with("system_reset")
@@ -186,7 +186,7 @@ def test_suspend(loop, vm):
control_vm_result = MagicMock()
control_vm_result.match.group.decode.return_value = "running"
with asyncio_patch("gns3server.hypervisor.qemu.QemuVM._control_vm", return_value=control_vm_result) as mock:
with asyncio_patch("gns3server.compute.qemu.QemuVM._control_vm", return_value=control_vm_result) as mock:
loop.run_until_complete(asyncio.async(vm.suspend()))
assert mock.called_with("system_reset")
@@ -199,7 +199,7 @@ def test_add_nio_binding_udp(vm, loop):
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
def test_add_nio_binding_ethernet(vm, loop, ethernet_device):
with patch("gns3server.hypervisor.base_manager.BaseManager.has_privileged_access", return_value=True):
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
nio = Qemu.instance().create_nio(vm.qemu_path, {"type": "nio_generic_ethernet", "ethernet_device": ethernet_device})
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
assert nio.ethernet_device == ethernet_device
@@ -307,7 +307,7 @@ def test_set_qemu_path_kvm_binary(vm, tmpdir, fake_qemu_binary):
def test_set_platform(project, manager):
with patch("shutil.which", return_value="/bin/qemu-system-x86_64") as which_mock:
with patch("gns3server.hypervisor.qemu.QemuVM._check_qemu_path"):
with patch("gns3server.compute.qemu.QemuVM._check_qemu_path"):
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, platform="x86_64")
if sys.platform.startswith("win"):
which_mock.assert_called_with("qemu-system-x86_64w.exe", path=mock.ANY)
@@ -506,7 +506,7 @@ def test_initrd(vm, tmpdir):
vm.manager.config.set("Server", "images_path", str(tmpdir))
with patch("gns3server.hypervisor.project.Project.emit") as mock:
with patch("gns3server.compute.project.Project.emit") as mock:
vm.initrd = str(tmpdir / "test")
assert vm.initrd == force_unix_path(str(tmpdir / "test"))
vm.initrd = "test"
@@ -518,7 +518,7 @@ def test_initrd_asa(vm, tmpdir):
vm.manager.config.set("Server", "images_path", str(tmpdir))
with patch("gns3server.hypervisor.project.Project.emit") as mock:
with patch("gns3server.compute.project.Project.emit") as mock:
vm.initrd = str(tmpdir / "asa842-initrd.gz")
assert vm.initrd == force_unix_path(str(tmpdir / "asa842-initrd.gz"))
vm.initrd = "asa842-initrd.gz"
@@ -23,11 +23,11 @@ from tests.utils import asyncio_patch
from unittest.mock import patch, MagicMock
from gns3server.hypervisor.vpcs.vpcs_vm import VPCSVM
from gns3server.hypervisor.docker.docker_vm import DockerVM
from gns3server.hypervisor.vpcs.vpcs_error import VPCSError
from gns3server.hypervisor.vm_error import VMError
from gns3server.hypervisor.vpcs import VPCS
from gns3server.compute.vpcs.vpcs_vm import VPCSVM
from gns3server.compute.docker.docker_vm import DockerVM
from gns3server.compute.vpcs.vpcs_error import VPCSError
from gns3server.compute.vm_error import VMError
from gns3server.compute.vpcs import VPCS
@pytest.fixture(scope="module")
@@ -21,9 +21,9 @@ import pytest
from unittest.mock import patch
from gns3server.hypervisor.vpcs import VPCS
from gns3server.hypervisor.qemu import Qemu
from gns3server.hypervisor.vm_error import VMError
from gns3server.compute.vpcs import VPCS
from gns3server.compute.qemu import Qemu
from gns3server.compute.vm_error import VMError
from gns3server.utils import force_unix_path
@@ -67,7 +67,7 @@ def test_create_vm_new_topology_without_uuid(loop, project, vpcs):
def test_create_vm_old_topology(loop, project, tmpdir, vpcs):
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
# Create an old topology directory
project_dir = str(tmpdir / "testold")
vm_dir = os.path.join(project_dir, "testold-files", "vpcs", "pc-1")
@@ -158,7 +158,7 @@ def test_list_images(loop, qemu, tmpdir):
with open(str(tmpdir / image), "w+") as f:
f.write("1")
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir)):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir)):
assert loop.run_until_complete(qemu.list_images()) == [
{"filename": "a.bin", "path": "a.bin"},
{"filename": "b.bin", "path": "b.bin"}
@@ -177,7 +177,7 @@ def test_list_images_recursives(loop, qemu, tmpdir):
with open(str(tmpdir / "c" / image), "w+") as f:
f.write("1")
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir)):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir)):
assert loop.run_until_complete(qemu.list_images()) == [
{"filename": "a.bin", "path": "a.bin"},
{"filename": "b.bin", "path": "b.bin"},
@@ -186,10 +186,10 @@ def test_list_images_recursives(loop, qemu, tmpdir):
def test_list_images_empty(loop, qemu, tmpdir):
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir)):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir)):
assert loop.run_until_complete(qemu.list_images()) == []
def test_list_images_directory_not_exist(loop, qemu):
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value="/bla"):
with patch("gns3server.compute.Qemu.get_images_directory", return_value="/bla"):
assert loop.run_until_complete(qemu.list_images()) == []
@@ -17,7 +17,7 @@
import uuid
from gns3server.hypervisor.notification_manager import NotificationManager
from gns3server.compute.notification_manager import NotificationManager
def test_queue(async_run):
@@ -21,15 +21,15 @@ import sys
import uuid
from unittest.mock import patch
from gns3server.hypervisor.port_manager import PortManager
from gns3server.hypervisor.project import Project
from gns3server.compute.port_manager import PortManager
from gns3server.compute.project import Project
def test_reserve_tcp_port():
pm = PortManager()
project = Project(project_id=str(uuid.uuid4()))
pm.reserve_tcp_port(2001, project)
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
with patch("gns3server.compute.project.Project.emit") as mock_emit:
port = pm.reserve_tcp_port(2001, project)
assert port != 2001
@@ -37,7 +37,7 @@ def test_reserve_tcp_port():
def test_reserve_tcp_port_outside_range():
pm = PortManager()
project = Project(project_id=str(uuid.uuid4()))
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
with patch("gns3server.compute.project.Project.emit") as mock_emit:
port = pm.reserve_tcp_port(80, project)
assert port != 80
@@ -50,7 +50,7 @@ def test_reserve_tcp_port_already_used_by_another_program():
pm = PortManager()
project = Project(project_id=str(uuid.uuid4()))
with patch("gns3server.hypervisor.port_manager.PortManager._check_port") as mock_check:
with patch("gns3server.compute.port_manager.PortManager._check_port") as mock_check:
def execute_mock(host, port, *args):
if port == 2001:
@@ -60,7 +60,7 @@ def test_reserve_tcp_port_already_used_by_another_program():
mock_check.side_effect = execute_mock
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
with patch("gns3server.compute.project.Project.emit") as mock_emit:
port = pm.reserve_tcp_port(2001, project)
assert port != 2001
@@ -73,7 +73,7 @@ def test_reserve_tcp_port_already_used():
pm = PortManager()
project = Project(project_id=str(uuid.uuid4()))
with patch("gns3server.hypervisor.port_manager.PortManager._check_port") as mock_check:
with patch("gns3server.compute.port_manager.PortManager._check_port") as mock_check:
def execute_mock(host, port, *args):
if port == 2001:
@@ -83,7 +83,7 @@ def test_reserve_tcp_port_already_used():
mock_check.side_effect = execute_mock
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
with patch("gns3server.compute.project.Project.emit") as mock_emit:
port = pm.reserve_tcp_port(2001, project)
assert port != 2001
@@ -27,9 +27,9 @@ from uuid import uuid4
from unittest.mock import patch
from tests.utils import asyncio_patch
from gns3server.hypervisor.project import Project
from gns3server.hypervisor.notification_manager import NotificationManager
from gns3server.hypervisor.vpcs import VPCS, VPCSVM
from gns3server.compute.project import Project
from gns3server.compute.notification_manager import NotificationManager
from gns3server.compute.vpcs import VPCS, VPCSVM
from gns3server.config import Config
@@ -55,8 +55,8 @@ def test_path(tmpdir):
directory = Config.instance().get_section_config("Server").get("project_directory")
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
with patch("gns3server.hypervisor.project.Project._get_default_project_directory", return_value=directory):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
with patch("gns3server.compute.project.Project._get_default_project_directory", return_value=directory):
p = Project(project_id=str(uuid4()))
assert p.path == os.path.join(directory, p.id)
assert os.path.exists(os.path.join(directory, p.id))
@@ -65,14 +65,14 @@ def test_path(tmpdir):
def test_init_path(tmpdir):
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
p = Project(path=str(tmpdir), project_id=str(uuid4()))
assert p.path == str(tmpdir)
def test_changing_path_temporary_flag(tmpdir):
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
p = Project(temporary=True, project_id=str(uuid4()))
assert os.path.exists(p.path)
original_path = p.path
@@ -96,14 +96,14 @@ def test_remove_temporary_flag():
def test_changing_path_not_allowed(tmpdir):
with patch("gns3server.hypervisor.project.Project.is_local", return_value=False):
with patch("gns3server.compute.project.Project.is_local", return_value=False):
with pytest.raises(aiohttp.web.HTTPForbidden):
p = Project(project_id=str(uuid4()))
p.path = str(tmpdir)
def test_changing_path_with_quote_not_allowed(tmpdir):
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
with pytest.raises(aiohttp.web.HTTPForbidden):
p = Project(project_id=str(uuid4()))
p.path = str(tmpdir / "project\"53")
@@ -117,7 +117,7 @@ def test_json(tmpdir):
def test_vm_working_directory(tmpdir, vm):
directory = Config.instance().get_section_config("Server").get("project_directory")
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
p = Project(project_id=str(uuid4()))
assert p.vm_working_directory(vm) == os.path.join(directory, p.id, 'project-files', vm.module_name, vm.id)
assert os.path.exists(p.vm_working_directory(vm))
@@ -186,7 +186,7 @@ def test_project_add_vm(manager):
def test_project_close(loop, vm, project):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM.close") as mock:
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.close") as mock:
loop.run_until_complete(asyncio.async(project.close()))
assert mock.called
assert vm.id not in vm.manager._vms
@@ -372,7 +372,7 @@ def test_export_with_images(tmpdir):
with open(os.path.join(path, "test.gns3"), 'w+') as f:
json.dump(topology, f)
with patch("gns3server.hypervisor.Dynamips.get_images_directory", return_value=str(tmpdir / "IOS"),):
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir / "IOS"),):
z = project.export(include_images=True)
with open(str(tmpdir / 'zipfile.zip'), 'wb') as f:
for data in z:
@@ -17,7 +17,7 @@
import aiohttp
import pytest
from gns3server.hypervisor.project_manager import ProjectManager
from gns3server.compute.project_manager import ProjectManager
def test_create_project():
@@ -25,8 +25,8 @@ import asyncio
from unittest.mock import patch
from gns3server.hypervisor.virtualbox import VirtualBox
from gns3server.hypervisor.virtualbox.virtualbox_error import VirtualBoxError
from gns3server.compute.virtualbox import VirtualBox
from gns3server.compute.virtualbox.virtualbox_error import VirtualBoxError
from tests.utils import asyncio_patch
@@ -90,7 +90,7 @@ def test_list_images(manager, loop):
return ["memory=256"]
assert False, "Unknow {} {}".format(cmd, args)
with asyncio_patch("gns3server.hypervisor.virtualbox.VirtualBox.execute") as mock:
with asyncio_patch("gns3server.compute.virtualbox.VirtualBox.execute") as mock:
mock.side_effect = execute_mock
vms = loop.run_until_complete(asyncio.async(manager.list_images()))
assert vms == [
@@ -19,9 +19,9 @@ import pytest
import asyncio
from tests.utils import asyncio_patch
from gns3server.hypervisor.virtualbox.virtualbox_vm import VirtualBoxVM
from gns3server.hypervisor.virtualbox.virtualbox_error import VirtualBoxError
from gns3server.hypervisor.virtualbox import VirtualBox
from gns3server.compute.virtualbox.virtualbox_vm import VirtualBoxVM
from gns3server.compute.virtualbox.virtualbox_error import VirtualBoxError
from gns3server.compute.virtualbox import VirtualBox
@pytest.fixture(scope="module")
@@ -44,13 +44,13 @@ def test_vm(project, manager):
def test_vm_valid_virtualbox_api_version(loop, project, manager):
with asyncio_patch("gns3server.hypervisor.virtualbox.VirtualBox.execute", return_value=["API version: 4_3"]):
with asyncio_patch("gns3server.compute.virtualbox.VirtualBox.execute", return_value=["API version: 4_3"]):
vm = VirtualBoxVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, "test", False)
loop.run_until_complete(asyncio.async(vm.create()))
def test_vm_invalid_virtualbox_api_version(loop, project, manager):
with asyncio_patch("gns3server.hypervisor.virtualbox.VirtualBox.execute", return_value=["API version: 4_2"]):
with asyncio_patch("gns3server.compute.virtualbox.VirtualBox.execute", return_value=["API version: 4_2"]):
with pytest.raises(VirtualBoxError):
vm = VirtualBoxVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, "test", False)
loop.run_until_complete(asyncio.async(vm.create()))
@@ -25,7 +25,7 @@ import asyncio
from unittest.mock import patch
from gns3server.hypervisor.vmware import VMware
from gns3server.compute.vmware import VMware
from tests.utils import asyncio_patch
@@ -19,9 +19,9 @@ import pytest
import asyncio
from tests.utils import asyncio_patch
from gns3server.hypervisor.vmware.vmware_vm import VMwareVM
from gns3server.hypervisor.vmware.vmware_error import VMwareError
from gns3server.hypervisor.vmware import VMware
from gns3server.compute.vmware.vmware_vm import VMwareVM
from gns3server.compute.vmware.vmware_error import VMwareError
from gns3server.compute.vmware import VMware
@pytest.fixture(scope="module")
@@ -20,9 +20,9 @@ import pytest
import uuid
from gns3server.hypervisor.vpcs import VPCS
from gns3server.hypervisor.vpcs.vpcs_error import VPCSError
from gns3server.hypervisor.project_manager import ProjectManager
from gns3server.compute.vpcs import VPCS
from gns3server.compute.vpcs.vpcs_error import VPCSError
from gns3server.compute.project_manager import ProjectManager
def test_get_mac_id(loop, project, port_manager):
@@ -25,10 +25,10 @@ from tests.utils import asyncio_patch
from pkg_resources import parse_version
from unittest.mock import patch, MagicMock
from gns3server.hypervisor.vpcs.vpcs_vm import VPCSVM
from gns3server.hypervisor.vpcs.vpcs_error import VPCSError
from gns3server.hypervisor.vpcs import VPCS
from gns3server.hypervisor.notification_manager import NotificationManager
from gns3server.compute.vpcs.vpcs_vm import VPCSVM
from gns3server.compute.vpcs.vpcs_error import VPCSError
from gns3server.compute.vpcs import VPCS
from gns3server.compute.notification_manager import NotificationManager
@pytest.fixture(scope="module")
@@ -52,19 +52,19 @@ def test_vm(project, manager):
def test_vm_check_vpcs_version(loop, vm, manager):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.9"):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.9"):
loop.run_until_complete(asyncio.async(vm._check_vpcs_version()))
assert vm._vpcs_version == parse_version("0.9")
def test_vm_check_vpcs_version_0_6_1(loop, vm, manager):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.6.1"):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.6.1"):
loop.run_until_complete(asyncio.async(vm._check_vpcs_version()))
assert vm._vpcs_version == parse_version("0.6.1")
def test_vm_invalid_vpcs_version(loop, manager, vm):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.1"):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.1"):
with pytest.raises(VPCSError):
nio = manager.create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.port_add_nio_binding(0, nio)
@@ -74,7 +74,7 @@ def test_vm_invalid_vpcs_version(loop, manager, vm):
def test_vm_invalid_vpcs_path(vm, manager, loop):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM.vpcs_path", return_value="/tmp/fake/path/vpcs"):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.vpcs_path", return_value="/tmp/fake/path/vpcs"):
with pytest.raises(VPCSError):
nio = manager.create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.port_add_nio_binding(0, nio)
@@ -90,7 +90,7 @@ def test_start(loop, vm, async_run):
with NotificationManager.instance().queue() as queue:
async_run(queue.get(0)) #  Ping
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process) as mock_exec:
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.port_add_nio_binding(0, nio)
@@ -125,7 +125,7 @@ def test_start_0_6_1(loop, vm):
process.returncode = None
vm._vpcs_version = parse_version("0.6.1")
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process) as mock_exec:
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.port_add_nio_binding(0, nio)
@@ -156,7 +156,7 @@ def test_stop(loop, vm, async_run):
process.returncode = None
with NotificationManager.instance().queue() as queue:
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.port_add_nio_binding(0, nio)
@@ -190,7 +190,7 @@ def test_reload(loop, vm):
process.wait.return_value = future
process.returncode = None
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.port_add_nio_binding(0, nio)
@@ -215,14 +215,14 @@ def test_add_nio_binding_udp(vm):
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
def test_add_nio_binding_tap(vm, ethernet_device):
with patch("gns3server.hypervisor.base_manager.BaseManager.has_privileged_access", return_value=True):
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_tap", "tap_device": ethernet_device})
vm.port_add_nio_binding(0, nio)
assert nio.tap_device == ethernet_device
# def test_add_nio_binding_tap_no_privileged_access(vm):
# with patch("gns3server.hypervisor.base_manager.BaseManager.has_privileged_access", return_value=False):
# with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=False):
# with pytest.raises(aiohttp.web.HTTPForbidden):
# nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_tap", "tap_device": "test"})
# vm.port_add_nio_binding(0, nio)
@@ -286,7 +286,7 @@ def test_change_name(vm, tmpdir):
def test_close(vm, port_manager, loop):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
vm.start()
loop.run_until_complete(asyncio.async(vm.close()))
+7 -7
View File
@@ -37,9 +37,9 @@ from gns3server.config import Config
from gns3server.web.route import Route
# TODO: get rid of *
from gns3server.handlers import *
from gns3server.hypervisor import MODULES
from gns3server.hypervisor.port_manager import PortManager
from gns3server.hypervisor.project_manager import ProjectManager
from gns3server.compute import MODULES
from gns3server.compute.port_manager import PortManager
from gns3server.compute.project_manager import ProjectManager
from gns3server.controller import Controller
from tests.handlers.api.base import Query
@@ -105,7 +105,7 @@ def http_server(request, loop, port_manager, monkeypatch):
def tear_down():
for module in MODULES:
instance = module.instance()
monkeypatch.setattr('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.close', lambda self: True)
monkeypatch.setattr('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.close', lambda self: True)
loop.run_until_complete(instance.unload())
srv.close()
srv.wait_closed()
@@ -132,12 +132,12 @@ def http_controller(loop, http_server):
@pytest.fixture
def http_hypervisor(loop, http_server):
def http_compute(loop, http_server):
"""
Return an helper allowing you to call the hypervisor API via HTTP
"""
host, port = http_server
return Query(loop, host=host, port=port, prefix="/hypervisor", api_version=2)
return Query(loop, host=host, port=port, prefix="/compute", api_version=2)
@pytest.fixture(scope="function")
@@ -204,7 +204,7 @@ def run_around_tests(monkeypatch, port_manager, controller):
# Force turn off KVM because it's not available on CI
config.set("Qemu", "enable_kvm", False)
monkeypatch.setattr("gns3server.hypervisor.project.Project._get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
monkeypatch.setattr("gns3server.compute.project.Project._get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
# Force sys.platform to the original value. Because it seem not be restore correctly at each tests
sys.platform = sys.original_platform
@@ -23,124 +23,124 @@ import asyncio
from unittest.mock import patch, MagicMock
from gns3server.controller.project import Project
from gns3server.controller.hypervisor import Hypervisor, HypervisorError
from gns3server.controller.compute import Compute, ComputeError
from gns3server.version import __version__
from tests.utils import asyncio_patch, AsyncioMagicMock
@pytest.fixture
def hypervisor():
hypervisor = Hypervisor("my_hypervisor_id", protocol="https", host="example.com", port=84, controller=MagicMock())
hypervisor._connected = True
return hypervisor
def compute():
compute = Compute("my_compute_id", protocol="https", host="example.com", port=84, controller=MagicMock())
compute._connected = True
return compute
def test_init(hypervisor):
assert hypervisor.id == "my_hypervisor_id"
def test_init(compute):
assert compute.id == "my_compute_id"
def test_hypervisor_local(hypervisor):
def test_compute_local(compute):
"""
If the hypervisor is local but the hypervisor id is local
If the compute is local but the compute id is local
it's a configuration issue
"""
with patch("gns3server.config.Config.get_section_config", return_value={"local": False}):
with pytest.raises(HypervisorError):
s = Hypervisor("local", controller=MagicMock())
with pytest.raises(ComputeError):
s = Compute("local", controller=MagicMock())
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
s = Hypervisor("test", controller=MagicMock())
s = Compute("test", controller=MagicMock())
def test_hypervisor_httpQuery(hypervisor, async_run):
def test_compute_httpQuery(compute, async_run):
response = MagicMock()
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
response.status = 200
async_run(hypervisor.post("/projects", {"a": "b"}))
mock.assert_called_with("POST", "https://example.com:84/v2/hypervisor/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=None)
assert hypervisor._auth is None
async_run(compute.post("/projects", {"a": "b"}))
mock.assert_called_with("POST", "https://example.com:84/v2/compute/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=None)
assert compute._auth is None
def test_hypervisor_httpQueryAuth(hypervisor, async_run):
def test_compute_httpQueryAuth(compute, async_run):
response = MagicMock()
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
response.status = 200
hypervisor.user = "root"
hypervisor.password = "toor"
async_run(hypervisor.post("/projects", {"a": "b"}))
mock.assert_called_with("POST", "https://example.com:84/v2/hypervisor/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=hypervisor._auth)
assert hypervisor._auth.login == "root"
assert hypervisor._auth.password == "toor"
compute.user = "root"
compute.password = "toor"
async_run(compute.post("/projects", {"a": "b"}))
mock.assert_called_with("POST", "https://example.com:84/v2/compute/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=compute._auth)
assert compute._auth.login == "root"
assert compute._auth.password == "toor"
def test_hypervisor_httpQueryNotConnected(hypervisor, async_run):
hypervisor._connected = False
def test_compute_httpQueryNotConnected(compute, async_run):
compute._connected = False
response = AsyncioMagicMock()
response.read = AsyncioMagicMock(return_value=json.dumps({"version": __version__}).encode())
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
async_run(hypervisor.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/hypervisor/version", headers={'content-type': 'application/json'}, data=None, auth=None)
mock.assert_any_call("POST", "https://example.com:84/v2/hypervisor/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=None)
assert hypervisor._connected
async_run(compute.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/compute/version", headers={'content-type': 'application/json'}, data=None, auth=None)
mock.assert_any_call("POST", "https://example.com:84/v2/compute/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=None)
assert compute._connected
def test_hypervisor_httpQueryNotConnectedInvalidVersion(hypervisor, async_run):
hypervisor._connected = False
def test_compute_httpQueryNotConnectedInvalidVersion(compute, async_run):
compute._connected = False
response = AsyncioMagicMock()
response.read = AsyncioMagicMock(return_value=json.dumps({"version": "1.42.4"}).encode())
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
with pytest.raises(aiohttp.web.HTTPConflict):
async_run(hypervisor.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/hypervisor/version", headers={'content-type': 'application/json'}, data=None, auth=None)
async_run(compute.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/compute/version", headers={'content-type': 'application/json'}, data=None, auth=None)
def test_hypervisor_httpQueryNotConnectedNonGNS3Server(hypervisor, async_run):
hypervisor._connected = False
def test_compute_httpQueryNotConnectedNonGNS3Server(compute, async_run):
compute._connected = False
response = AsyncioMagicMock()
response.read = AsyncioMagicMock(return_value=b'Blocked by super antivirus')
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
with pytest.raises(aiohttp.web.HTTPConflict):
async_run(hypervisor.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/hypervisor/version", headers={'content-type': 'application/json'}, data=None, auth=None)
async_run(compute.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/compute/version", headers={'content-type': 'application/json'}, data=None, auth=None)
def test_hypervisor_httpQueryNotConnectedNonGNS3Server2(hypervisor, async_run):
hypervisor._connected = False
def test_compute_httpQueryNotConnectedNonGNS3Server2(compute, async_run):
compute._connected = False
response = AsyncioMagicMock()
response.read = AsyncioMagicMock(return_value=b'{}')
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
with pytest.raises(aiohttp.web.HTTPConflict):
async_run(hypervisor.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/hypervisor/version", headers={'content-type': 'application/json'}, data=None, auth=None)
async_run(compute.post("/projects", {"a": "b"}))
mock.assert_any_call("GET", "https://example.com:84/v2/compute/version", headers={'content-type': 'application/json'}, data=None, auth=None)
def test_hypervisor_httpQueryError(hypervisor, async_run):
def test_compute_httpQueryError(compute, async_run):
response = MagicMock()
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
response.status = 409
with pytest.raises(aiohttp.web.HTTPConflict):
async_run(hypervisor.post("/projects", {"a": "b"}))
async_run(compute.post("/projects", {"a": "b"}))
def test_hypervisor_httpQuery_project(hypervisor, async_run):
def test_compute_httpQuery_project(compute, async_run):
response = MagicMock()
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
response.status = 200
project = Project()
async_run(hypervisor.post("/projects", project))
mock.assert_called_with("POST", "https://example.com:84/v2/hypervisor/projects", data=json.dumps(project.__json__()), headers={'content-type': 'application/json'}, auth=None)
async_run(compute.post("/projects", project))
mock.assert_called_with("POST", "https://example.com:84/v2/compute/projects", data=json.dumps(project.__json__()), headers={'content-type': 'application/json'}, auth=None)
def test_connectNotification(hypervisor, async_run):
def test_connectNotification(compute, async_run):
ws_mock = AsyncioMagicMock()
call = 0
@@ -159,20 +159,20 @@ def test_connectNotification(hypervisor, async_run):
response.tp = aiohttp.MsgType.closed
return response
hypervisor._controller = MagicMock()
hypervisor._session = AsyncioMagicMock(return_value=ws_mock)
hypervisor._session.ws_connect = AsyncioMagicMock(return_value=ws_mock)
compute._controller = MagicMock()
compute._session = AsyncioMagicMock(return_value=ws_mock)
compute._session.ws_connect = AsyncioMagicMock(return_value=ws_mock)
ws_mock.receive = receive
async_run(hypervisor._connectNotification())
async_run(compute._connectNotification())
hypervisor._controller.emit.assert_called_with('test', {'a': 1}, hypervisor_id=hypervisor.id, project_id='42')
assert hypervisor._connected is False
compute._controller.emit.assert_called_with('test', {'a': 1}, compute_id=compute.id, project_id='42')
assert compute._connected is False
def test_json(hypervisor):
hypervisor.user = "test"
assert hypervisor.__json__() == {
"hypervisor_id": "my_hypervisor_id",
def test_json(compute):
compute.user = "test"
assert compute.__json__() == {
"compute_id": "my_compute_id",
"protocol": "https",
"host": "example.com",
"port": 84,
+17 -17
View File
@@ -22,7 +22,7 @@ from unittest.mock import MagicMock
from gns3server.controller import Controller
from gns3server.controller.hypervisor import Hypervisor
from gns3server.controller.compute import Compute
from gns3server.controller.project import Project
from gns3server.config import Config
@@ -34,22 +34,22 @@ def test_isEnabled(controller):
assert controller.isEnabled()
def test_addHypervisor(controller, async_run):
async_run(controller.addHypervisor("test1"))
assert len(controller.hypervisors) == 1
async_run(controller.addHypervisor("test1"))
assert len(controller.hypervisors) == 1
async_run(controller.addHypervisor("test2"))
assert len(controller.hypervisors) == 2
def test_addCompute(controller, async_run):
async_run(controller.addCompute("test1"))
assert len(controller.computes) == 1
async_run(controller.addCompute("test1"))
assert len(controller.computes) == 1
async_run(controller.addCompute("test2"))
assert len(controller.computes) == 2
def test_getHypervisor(controller, async_run):
def test_getCompute(controller, async_run):
hypervisor = async_run(controller.addHypervisor("test1"))
compute = async_run(controller.addCompute("test1"))
assert controller.getHypervisor("test1") == hypervisor
assert controller.getCompute("test1") == compute
with pytest.raises(aiohttp.web.HTTPNotFound):
assert controller.getHypervisor("dsdssd")
assert controller.getCompute("dsdssd")
def test_addProject(controller, async_run):
@@ -74,15 +74,15 @@ def test_removeProject(controller, async_run):
assert len(controller.projects) == 0
def test_addProject_with_hypervisor(controller, async_run):
def test_addProject_with_compute(controller, async_run):
uuid1 = str(uuid.uuid4())
hypervisor = Hypervisor("test1", controller=MagicMock())
hypervisor.post = MagicMock()
controller._hypervisors = {"test1": hypervisor}
compute = Compute("test1", controller=MagicMock())
compute.post = MagicMock()
controller._computes = {"test1": compute}
project1 = async_run(controller.addProject(project_id=uuid1))
hypervisor.post.assert_called_with("/projects", project1)
compute.post.assert_called_with("/projects", project1)
def test_getProject(controller, async_run):
+8 -8
View File
@@ -20,7 +20,7 @@ from unittest.mock import MagicMock
from gns3server.controller.link import Link
from gns3server.controller.vm import VM
from gns3server.controller.hypervisor import Hypervisor
from gns3server.controller.compute import Compute
from gns3server.controller.project import Project
@@ -30,12 +30,12 @@ def project():
@pytest.fixture
def hypervisor():
return Hypervisor("example.com", controller=MagicMock())
def compute():
return Compute("example.com", controller=MagicMock())
def test_addVM(async_run, project, hypervisor):
vm1 = VM(project, hypervisor)
def test_addVM(async_run, project, compute):
vm1 = VM(project, compute)
link = Link(project)
async_run(link.addVM(vm1, 0, 4))
@@ -48,9 +48,9 @@ def test_addVM(async_run, project, hypervisor):
]
def test_json(async_run, project, hypervisor):
vm1 = VM(project, hypervisor)
vm2 = VM(project, hypervisor)
def test_json(async_run, project, compute):
vm1 = VM(project, compute)
vm2 = VM(project, compute)
link = Link(project)
async_run(link.addVM(vm1, 0, 4))
+17 -17
View File
@@ -39,31 +39,31 @@ def test_json(tmpdir):
def test_addVM(async_run):
hypervisor = MagicMock()
compute = MagicMock()
project = Project()
response = MagicMock()
response.json = {"console": 2048}
hypervisor.post = AsyncioMagicMock(return_value=response)
compute.post = AsyncioMagicMock(return_value=response)
vm = async_run(project.addVM(hypervisor, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
vm = async_run(project.addVM(compute, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
hypervisor.post.assert_called_with('/projects/{}/vpcs/vms'.format(project.id),
data={'vm_id': vm.id,
'console_type': 'telnet',
'startup_config': 'test.cfg',
'name': 'test'})
compute.post.assert_called_with('/projects/{}/vpcs/vms'.format(project.id),
data={'vm_id': vm.id,
'console_type': 'telnet',
'startup_config': 'test.cfg',
'name': 'test'})
def test_getVM(async_run):
hypervisor = MagicMock()
compute = MagicMock()
project = Project()
response = MagicMock()
response.json = {"console": 2048}
hypervisor.post = AsyncioMagicMock(return_value=response)
compute.post = AsyncioMagicMock(return_value=response)
vm = async_run(project.addVM(hypervisor, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
vm = async_run(project.addVM(compute, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
assert project.getVM(vm.id) == vm
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
@@ -71,15 +71,15 @@ def test_getVM(async_run):
def test_addLink(async_run):
hypervisor = MagicMock()
compute = MagicMock()
project = Project()
response = MagicMock()
response.json = {"console": 2048}
hypervisor.post = AsyncioMagicMock(return_value=response)
compute.post = AsyncioMagicMock(return_value=response)
vm1 = async_run(project.addVM(hypervisor, None, name="test1", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
vm2 = async_run(project.addVM(hypervisor, None, name="test2", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
vm1 = async_run(project.addVM(compute, None, name="test1", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
vm2 = async_run(project.addVM(compute, None, name="test2", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
link = async_run(project.addLink())
async_run(link.addVM(vm1, 3, 1))
async_run(link.addVM(vm2, 4, 2))
@@ -87,12 +87,12 @@ def test_addLink(async_run):
def test_getLink(async_run):
hypervisor = MagicMock()
compute = MagicMock()
project = Project()
response = MagicMock()
response.json = {"console": 2048}
hypervisor.post = AsyncioMagicMock(return_value=response)
compute.post = AsyncioMagicMock(return_value=response)
link = async_run(project.addLink())
assert project.getLink(link.id) == link
+21 -21
View File
@@ -21,7 +21,7 @@ import aiohttp
from unittest.mock import MagicMock
from gns3server.controller.project import Project
from gns3server.controller.hypervisor import Hypervisor
from gns3server.controller.compute import Compute
from gns3server.controller.udp_link import UDPLink
from gns3server.controller.vm import VM
@@ -32,18 +32,18 @@ def project():
def test_create(async_run, project):
hypervisor1 = MagicMock()
hypervisor2 = MagicMock()
compute1 = MagicMock()
compute2 = MagicMock()
vm1 = VM(project, hypervisor1, vm_type="vpcs")
vm2 = VM(project, hypervisor2, vm_type="vpcs")
vm1 = VM(project, compute1, vm_type="vpcs")
vm2 = VM(project, compute2, vm_type="vpcs")
link = UDPLink(project)
async_run(link.addVM(vm1, 0, 4))
async_run(link.addVM(vm2, 3, 1))
@asyncio.coroutine
def hypervisor1_callback(path, data={}):
def compute1_callback(path, data={}):
"""
Fake server
"""
@@ -53,7 +53,7 @@ def test_create(async_run, project):
return response
@asyncio.coroutine
def hypervisor2_callback(path, data={}):
def compute2_callback(path, data={}):
"""
Fake server
"""
@@ -62,32 +62,32 @@ def test_create(async_run, project):
response.json = {"udp_port": 2048}
return response
hypervisor1.post.side_effect = hypervisor1_callback
hypervisor1.host = "example.com"
hypervisor2.post.side_effect = hypervisor2_callback
hypervisor2.host = "example.org"
compute1.post.side_effect = compute1_callback
compute1.host = "example.com"
compute2.post.side_effect = compute2_callback
compute2.host = "example.org"
async_run(link.create())
hypervisor1.post.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/0/ports/4/nio".format(project.id, vm1.id), data={
compute1.post.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/0/ports/4/nio".format(project.id, vm1.id), data={
"lport": 1024,
"rhost": hypervisor2.host,
"rhost": compute2.host,
"rport": 2048,
"type": "nio_udp"
})
hypervisor2.post.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/3/ports/1/nio".format(project.id, vm2.id), data={
compute2.post.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/3/ports/1/nio".format(project.id, vm2.id), data={
"lport": 2048,
"rhost": hypervisor1.host,
"rhost": compute1.host,
"rport": 1024,
"type": "nio_udp"
})
def test_delete(async_run, project):
hypervisor1 = MagicMock()
hypervisor2 = MagicMock()
compute1 = MagicMock()
compute2 = MagicMock()
vm1 = VM(project, hypervisor1, vm_type="vpcs")
vm2 = VM(project, hypervisor2, vm_type="vpcs")
vm1 = VM(project, compute1, vm_type="vpcs")
vm2 = VM(project, compute2, vm_type="vpcs")
link = UDPLink(project)
async_run(link.addVM(vm1, 0, 4))
@@ -95,5 +95,5 @@ def test_delete(async_run, project):
async_run(link.delete())
hypervisor1.delete.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/0/ports/4/nio".format(project.id, vm1.id))
hypervisor2.delete.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/3/ports/1/nio".format(project.id, vm2.id))
compute1.delete.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/0/ports/4/nio".format(project.id, vm1.id))
compute2.delete.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/3/ports/1/nio".format(project.id, vm2.id))
+26 -26
View File
@@ -28,16 +28,16 @@ from gns3server.controller.project import Project
@pytest.fixture
def hypervisor():
def compute():
s = AsyncioMagicMock()
s.id = "http://test.com:42"
return s
@pytest.fixture
def vm(hypervisor):
def vm(compute):
project = Project(str(uuid.uuid4()))
vm = VM(project, hypervisor,
vm = VM(project, compute,
name="demo",
vm_id=str(uuid.uuid4()),
vm_type="vpcs",
@@ -46,9 +46,9 @@ def vm(hypervisor):
return vm
def test_json(vm, hypervisor):
def test_json(vm, compute):
assert vm.__json__() == {
"hypervisor_id": hypervisor.id,
"compute_id": compute.id,
"project_id": vm.project.id,
"vm_id": vm.id,
"vm_type": vm.vm_type,
@@ -59,19 +59,19 @@ def test_json(vm, hypervisor):
}
def test_init_without_uuid(project, hypervisor):
vm = VM(project, hypervisor,
def test_init_without_uuid(project, compute):
vm = VM(project, compute,
vm_type="vpcs",
console_type="vnc")
assert vm.id is not None
def test_create(vm, hypervisor, project, async_run):
def test_create(vm, compute, project, async_run):
vm._console = 2048
response = MagicMock()
response.json = {"console": 2048}
hypervisor.post = AsyncioMagicMock(return_value=response)
compute.post = AsyncioMagicMock(return_value=response)
async_run(vm.create())
data = {
@@ -81,43 +81,43 @@ def test_create(vm, hypervisor, project, async_run):
"startup_script": "echo test",
"name": "demo"
}
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)
compute.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)
assert vm._console == 2048
assert vm._properties == {"startup_script": "echo test"}
def test_start(vm, hypervisor, project, async_run):
def test_start(vm, compute, project, async_run):
hypervisor.post = AsyncioMagicMock()
compute.post = AsyncioMagicMock()
async_run(vm.start())
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/start".format(vm.project.id, vm.id))
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/start".format(vm.project.id, vm.id))
def test_stop(vm, hypervisor, project, async_run):
def test_stop(vm, compute, project, async_run):
hypervisor.post = AsyncioMagicMock()
compute.post = AsyncioMagicMock()
async_run(vm.stop())
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/stop".format(vm.project.id, vm.id))
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/stop".format(vm.project.id, vm.id))
def test_suspend(vm, hypervisor, project, async_run):
def test_suspend(vm, compute, project, async_run):
hypervisor.post = AsyncioMagicMock()
compute.post = AsyncioMagicMock()
async_run(vm.suspend())
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/suspend".format(vm.project.id, vm.id))
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/suspend".format(vm.project.id, vm.id))
def test_create_without_console(vm, hypervisor, project, async_run):
def test_create_without_console(vm, compute, project, async_run):
"""
None properties should be send. Because it can mean the emulator doesn"t support it
"""
response = MagicMock()
response.json = {"console": 2048, "test_value": "success"}
hypervisor.post = AsyncioMagicMock(return_value=response)
compute.post = AsyncioMagicMock(return_value=response)
async_run(vm.create())
data = {
@@ -126,16 +126,16 @@ def test_create_without_console(vm, hypervisor, project, async_run):
"startup_script": "echo test",
"name": "demo"
}
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)
compute.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)
assert vm._console == 2048
assert vm._properties == {"test_value": "success", "startup_script": "echo test"}
def test_post(vm, hypervisor, async_run):
def test_post(vm, compute, async_run):
async_run(vm.post("/test", {"a": "b"}))
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/test".format(vm.project.id, vm.id), data={"a": "b"})
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/test".format(vm.project.id, vm.id), data={"a": "b"})
def test_delete(vm, hypervisor, async_run):
def test_delete(vm, compute, async_run):
async_run(vm.delete("/test"))
hypervisor.delete.assert_called_with("/projects/{}/vpcs/vms/{}/test".format(vm.project.id, vm.id))
compute.delete.assert_called_with("/projects/{}/vpcs/vms/{}/test".format(vm.project.id, vm.id))
+4 -4
View File
@@ -32,7 +32,7 @@ class Query:
def __init__(self, loop, host='localhost', port=8001, prefix='', api_version=None):
"""
:param prefix: Prefix added before path (ex: /hypervisor)
:param prefix: Prefix added before path (ex: /compute)
:param api_version: Version of the api
"""
self._loop = loop
@@ -110,6 +110,8 @@ class Query:
response.route = x_route.replace("/v{}".format(self._api_version), "")
response.route = response.route .replace(self._prefix, "")
response.json = {}
response.html = ""
if response.body is not None:
if response.headers.get("CONTENT-TYPE", "") == "application/json":
try:
@@ -121,9 +123,7 @@ class Query:
response.html = response.body.decode("utf-8")
except UnicodeDecodeError:
response.html = None
else:
response.json = {}
response.html = ""
if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
self._dump_example(method, response.route, path, body, response)
return response
@@ -19,7 +19,7 @@ from unittest.mock import MagicMock, patch
from gns3server.config import Config
def test_reload_accepted(http_hypervisor):
def test_reload_accepted(http_compute):
gns_config = MagicMock()
config = Config.instance()
@@ -27,17 +27,17 @@ def test_reload_accepted(http_hypervisor):
gns_config.get_section_config.return_value = config.get_section_config("Server")
with patch("gns3server.config.Config.instance", return_value=gns_config):
response = http_hypervisor.post('/config/reload', example=True)
response = http_compute.post('/config/reload', example=True)
assert response.status == 201
assert gns_config.reload.called
def test_reload_forbidden(http_hypervisor):
def test_reload_forbidden(http_compute):
config = Config.instance()
config.set("Server", "local", "false")
response = http_hypervisor.post('/config/reload')
response = http_compute.post('/config/reload')
assert response.status == 403
+185
View File
@@ -0,0 +1,185 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 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
import aiohttp
from tests.utils import asyncio_patch
from unittest.mock import patch, MagicMock, PropertyMock
from gns3server.compute.docker import Docker
@pytest.fixture
def base_params():
"""Return standard parameters"""
return {"name": "PC TEST 1", "image": "nginx", "start_command": "nginx-daemon", "adapters": 2, "environment": "YES=1\nNO=0", "console_type": "telnet", "console_resolution": "1280x1024"}
@pytest.yield_fixture(autouse=True)
def mock_connection():
docker = Docker.instance()
docker._connected = True
docker._connector = MagicMock()
yield
Docker._instance = None
@pytest.fixture
def vm(http_compute, project, base_params):
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "nginx"}]) as mock_list:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
response = http_compute.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
if response.status != 201:
print(response.body)
assert response.status == 201
return response.json
def test_docker_create(http_compute, project, base_params):
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "nginx"}]) as mock_list:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
response = http_compute.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
assert response.status == 201
assert response.route == "/projects/{project_id}/docker/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
assert response.json["container_id"] == "8bd8153ea8f5"
assert response.json["image"] == "nginx"
assert response.json["adapters"] == 2
assert response.json["environment"] == "YES=1\nNO=0"
assert response.json["console_resolution"] == "1280x1024"
def test_docker_start(http_compute, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_stop(http_compute, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.stop", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_reload(http_compute, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.restart", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_delete(http_compute, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.delete", return_value=True) as mock:
response = http_compute.delete("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_reload(http_compute, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.pause", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_nio_create_udp(http_compute, vm):
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_docker_delete_nio(http_compute, vm):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.adapter_remove_nio_binding") as mock:
response = http_compute.delete("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_docker_update(http_compute, vm, tmpdir, free_console_port):
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.update") as mock:
response = http_compute.put("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port,
"start_command": "yes",
"environment": "GNS3=1\nGNS4=0"},
example=True)
assert mock.called
assert response.status == 200
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"
def test_docker_start_capture(http_compute, vm, tmpdir, project):
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=True) as mock:
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start_capture") as start_capture:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params, example=True)
assert response.status == 200
assert start_capture.called
assert "test.pcap" in response.json["pcap_file_path"]
def test_docker_start_capture_not_started(http_compute, vm, tmpdir):
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=False) as mock:
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start_capture") as start_capture:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params)
assert not start_capture.called
assert response.status == 409
def test_docker_stop_capture(http_compute, vm, tmpdir, project):
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=True) as mock:
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.stop_capture") as stop_capture:
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert stop_capture.called
def test_docker_stop_capture_not_started(http_compute, vm, tmpdir):
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=False) as mock:
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.stop_capture") as stop_capture:
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert not stop_capture.called
assert response.status == 409
@@ -24,25 +24,25 @@ from tests.utils import asyncio_patch
# @pytest.yield_fixture(scope="module")
# def vm(http_hypervisor, project):
# def vm(http_compute, project):
#
# dynamips_path = "/fake/dynamips"
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.create", return_value=True) as mock:
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.create", return_value=True) as mock:
# response = http_compute.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
# "platform": "c3745",
# "image": "somewhere",
# "ram": 128})
# assert mock.called
# assert response.status == 201
#
# with asyncio_patch("gns3server.hypervisor.dynamips.Dynamips.find_dynamips", return_value=dynamips_path):
# with asyncio_patch("gns3server.compute.dynamips.Dynamips.find_dynamips", return_value=dynamips_path):
# yield response.json
#
#
# def test_dynamips_vm_create(http_hypervisor, project):
# def test_dynamips_vm_create(http_compute, project):
#
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.create", return_value=True):
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.create", return_value=True):
# response = http_compute.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
# "platform": "c3745",
# "image": "somewhere",
# "ram": 128},
@@ -53,46 +53,46 @@ from tests.utils import asyncio_patch
# assert response.json["dynamips_id"]
#
#
# def test_dynamips_vm_get(http_hypervisor, project, vm):
# response = http_hypervisor.get("/projects/{project_id}/dynamips/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
# def test_dynamips_vm_get(http_compute, project, vm):
# response = http_compute.get("/projects/{project_id}/dynamips/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
# assert response.status == 200
# assert response.route == "/projects/{project_id}/dynamips/vms/{vm_id}"
# assert response.json["name"] == "My router"
# assert response.json["project_id"] == project.id
#
#
# def test_dynamips_vm_start(http_hypervisor, vm):
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.start", return_value=True) as mock:
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# def test_dynamips_vm_start(http_compute, vm):
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.start", return_value=True) as mock:
# response = http_compute.post("/projects/{project_id}/dynamips/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_stop(http_hypervisor, vm):
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.stop", return_value=True) as mock:
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# def test_dynamips_vm_stop(http_compute, vm):
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.stop", return_value=True) as mock:
# response = http_compute.post("/projects/{project_id}/dynamips/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_suspend(http_hypervisor, vm):
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.suspend", return_value=True) as mock:
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# def test_dynamips_vm_suspend(http_compute, vm):
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.suspend", return_value=True) as mock:
# response = http_compute.post("/projects/{project_id}/dynamips/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_resume(http_hypervisor, vm):
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.resume", return_value=True) as mock:
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# def test_dynamips_vm_resume(http_compute, vm):
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.resume", return_value=True) as mock:
# response = http_compute.post("/projects/{project_id}/dynamips/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# assert mock.called
# assert response.status == 204
# def test_vbox_nio_create_udp(http_hypervisor, vm):
# def test_vbox_nio_create_udp(http_compute, vm):
#
# with asyncio_patch('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
# response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"],
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
# response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"],
# vm_id=vm["vm_id"]), {"type": "nio_udp",
# "lport": 4242,
# "rport": 4343,
@@ -108,10 +108,10 @@ from tests.utils import asyncio_patch
# assert response.json["type"] == "nio_udp"
#
#
# def test_vbox_delete_nio(http_hypervisor, vm):
# def test_vbox_delete_nio(http_compute, vm):
#
# with asyncio_patch('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
# response = http_hypervisor.delete("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
# response = http_compute.delete("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
#
# assert mock.called
# args, kwgars = mock.call_args
@@ -121,8 +121,8 @@ from tests.utils import asyncio_patch
# assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio"
#
#
# def test_vbox_update(http_hypervisor, vm, free_console_port):
# response = http_hypervisor.put("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
# def test_vbox_update(http_compute, vm, free_console_port):
# response = http_compute.put("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
# "console": free_console_port})
# assert response.status == 200
# assert response.json["name"] == "test"
@@ -151,17 +151,17 @@ def fake_file(tmpdir):
return path
def test_vms(http_hypervisor, tmpdir, fake_dynamips, fake_file):
def test_vms(http_compute, tmpdir, fake_dynamips, fake_file):
with patch("gns3server.hypervisor.Dynamips.get_images_directory", return_value=str(tmpdir), example=True):
response = http_hypervisor.get("/dynamips/vms")
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir), example=True):
response = http_compute.get("/dynamips/vms")
assert response.status == 200
assert response.json == [{"filename": "7200.bin", "path": "7200.bin"}]
def test_upload_vm(http_hypervisor, tmpdir):
with patch("gns3server.hypervisor.Dynamips.get_images_directory", return_value=str(tmpdir),):
response = http_hypervisor.post("/dynamips/vms/test2", body="TEST", raw=True)
def test_upload_vm(http_compute, tmpdir):
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir),):
response = http_compute.post("/dynamips/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
@@ -172,11 +172,11 @@ def test_upload_vm(http_hypervisor, tmpdir):
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
def test_upload_vm_permission_denied(http_hypervisor, tmpdir):
def test_upload_vm_permission_denied(http_compute, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.hypervisor.Dynamips.get_images_directory", return_value=str(tmpdir),):
response = http_hypervisor.post("/dynamips/vms/test2", body="TEST", raw=True)
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir),):
response = http_compute.post("/dynamips/vms/test2", body="TEST", raw=True)
assert response.status == 409
@@ -26,14 +26,14 @@ import aiohttp
from gns3server.version import __version__
def test_stream(http_hypervisor, tmpdir, loop):
def test_stream(http_compute, tmpdir, loop):
with open(str(tmpdir / "test"), 'w+') as f:
f.write("hello")
def go(future):
query = json.dumps({"location": str(tmpdir / "test")})
headers = {'content-type': 'application/json'}
response = yield from aiohttp.request("GET", http_hypervisor.get_url("/files/stream"), data=query, headers=headers)
response = yield from aiohttp.request("GET", http_compute.get_url("/files/stream"), data=query, headers=headers)
response.body = yield from response.content.read(5)
with open(str(tmpdir / "test"), 'a') as f:
f.write("world")
@@ -48,11 +48,11 @@ def test_stream(http_hypervisor, tmpdir, loop):
assert response.body == b'helloworld'
def test_stream_file_not_found(http_hypervisor, tmpdir, loop):
def test_stream_file_not_found(http_compute, tmpdir, loop):
def go(future):
query = json.dumps({"location": str(tmpdir / "test")})
headers = {'content-type': 'application/json'}
response = yield from aiohttp.request("GET", http_hypervisor.get_url("/files/stream"), data=query, headers=headers)
response = yield from aiohttp.request("GET", http_compute.get_url("/files/stream"), data=query, headers=headers)
response.close()
future.set_result(response)
@@ -46,8 +46,8 @@ def base_params(tmpdir, fake_iou_bin):
@pytest.fixture
def vm(http_hypervisor, project, base_params):
response = http_hypervisor.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
def vm(http_compute, project, base_params):
response = http_compute.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
assert response.status == 201
return response.json
@@ -58,8 +58,8 @@ def startup_config_file(project, vm):
return os.path.join(directory, "startup-config.cfg")
def test_iou_create(http_hypervisor, project, base_params):
response = http_hypervisor.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
def test_iou_create(http_compute, project, base_params):
response = http_compute.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms"
assert response.json["name"] == "PC TEST 1"
@@ -71,7 +71,7 @@ def test_iou_create(http_hypervisor, project, base_params):
assert response.json["l1_keepalives"] is False
def test_iou_create_with_params(http_hypervisor, project, base_params):
def test_iou_create_with_params(http_compute, project, base_params):
params = base_params
params["ram"] = 1024
params["nvram"] = 512
@@ -82,7 +82,7 @@ def test_iou_create_with_params(http_hypervisor, project, base_params):
params["use_default_iou_values"] = True
params["iourc_content"] = "test"
response = http_hypervisor.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True)
response = http_compute.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms"
assert response.json["name"] == "PC TEST 1"
@@ -101,7 +101,7 @@ def test_iou_create_with_params(http_hypervisor, project, base_params):
assert "iourc" in response.json["iourc_path"]
def test_iou_create_startup_config_already_exist(http_hypervisor, project, base_params):
def test_iou_create_startup_config_already_exist(http_compute, project, base_params):
"""We don't erase a startup-config if already exist at project creation"""
vm_id = str(uuid.uuid4())
@@ -113,7 +113,7 @@ def test_iou_create_startup_config_already_exist(http_hypervisor, project, base_
params["vm_id"] = vm_id
params["startup_config_content"] = "hostname test"
response = http_hypervisor.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True)
response = http_compute.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms"
@@ -122,8 +122,8 @@ def test_iou_create_startup_config_already_exist(http_hypervisor, project, base_
assert f.read() == "echo hello"
def test_iou_get(http_hypervisor, project, vm):
response = http_hypervisor.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_iou_get(http_compute, project, vm):
response = http_compute.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}"
assert response.json["name"] == "PC TEST 1"
@@ -135,50 +135,50 @@ def test_iou_get(http_hypervisor, project, vm):
assert response.json["l1_keepalives"] is False
def test_iou_start(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
def test_iou_start(http_compute, vm):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 200
assert response.json["name"] == "PC TEST 1"
def test_iou_start_with_iourc(http_hypervisor, vm, tmpdir):
def test_iou_start_with_iourc(http_compute, vm, tmpdir):
body = {"iourc_content": "test"}
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=body, example=True)
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=body, example=True)
assert mock.called
assert response.status == 200
response = http_hypervisor.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
response = http_compute.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert response.status == 200
with open(response.json["iourc_path"]) as f:
assert f.read() == "test"
def test_iou_stop(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.stop", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_iou_stop(http_compute, vm):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_iou_reload(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.reload", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_iou_reload(http_compute, vm):
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.reload", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_iou_delete(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.iou.IOU.delete_vm", return_value=True) as mock:
response = http_hypervisor.delete("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_iou_delete(http_compute, vm):
with asyncio_patch("gns3server.compute.iou.IOU.delete_vm", return_value=True) as mock:
response = http_compute.delete("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_iou_update(http_hypervisor, vm, tmpdir, free_console_port, project):
def test_iou_update(http_compute, vm, tmpdir, free_console_port, project):
params = {
"name": "test",
"console": free_console_port,
@@ -191,7 +191,7 @@ def test_iou_update(http_hypervisor, vm, tmpdir, free_console_port, project):
"use_default_iou_values": True,
"iourc_content": "test"
}
response = http_hypervisor.put("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True)
response = http_compute.put("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True)
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
@@ -208,65 +208,65 @@ def test_iou_update(http_hypervisor, vm, tmpdir, free_console_port, project):
assert "iourc" in response.json["iourc_path"]
def test_iou_nio_create_udp(http_hypervisor, vm):
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
def test_iou_nio_create_udp(http_compute, vm):
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_iou_nio_create_ethernet(http_hypervisor, vm, ethernet_device):
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": ethernet_device,
},
example=True)
def test_iou_nio_create_ethernet(http_compute, vm, ethernet_device):
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": ethernet_device,
},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_generic_ethernet"
assert response.json["ethernet_device"] == ethernet_device
def test_iou_nio_create_ethernet_different_port(http_hypervisor, vm, ethernet_device):
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/3/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": ethernet_device,
},
example=False)
def test_iou_nio_create_ethernet_different_port(http_compute, vm, ethernet_device):
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/3/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": ethernet_device,
},
example=False)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_generic_ethernet"
assert response.json["ethernet_device"] == ethernet_device
def test_iou_nio_create_tap(http_hypervisor, vm, ethernet_device):
with patch("gns3server.hypervisor.base_manager.BaseManager.has_privileged_access", return_value=True):
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap",
"tap_device": ethernet_device})
def test_iou_nio_create_tap(http_compute, vm, ethernet_device):
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap",
"tap_device": ethernet_device})
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_tap"
def test_iou_delete_nio(http_hypervisor, vm):
http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = http_hypervisor.delete("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_iou_delete_nio(http_compute, vm):
http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = http_compute.delete("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_iou_start_capture(http_hypervisor, vm, tmpdir, project):
def test_iou_start_capture(http_compute, vm, tmpdir, project):
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM.is_running", return_value=True) as mock:
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.start_capture") as start_capture:
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=True) as mock:
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start_capture") as start_capture:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params, example=True)
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params, example=True)
assert response.status == 200
@@ -274,71 +274,71 @@ def test_iou_start_capture(http_hypervisor, vm, tmpdir, project):
assert "test.pcap" in response.json["pcap_file_path"]
def test_iou_start_capture_not_started(http_hypervisor, vm, tmpdir):
def test_iou_start_capture_not_started(http_compute, vm, tmpdir):
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM.is_running", return_value=False) as mock:
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.start_capture") as start_capture:
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=False) as mock:
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start_capture") as start_capture:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params)
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params)
assert not start_capture.called
assert response.status == 409
def test_iou_stop_capture(http_hypervisor, vm, tmpdir, project):
def test_iou_stop_capture(http_compute, vm, tmpdir, project):
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM.is_running", return_value=True) as mock:
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.stop_capture") as stop_capture:
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=True) as mock:
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop_capture") as stop_capture:
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert stop_capture.called
def test_iou_stop_capture_not_started(http_hypervisor, vm, tmpdir):
def test_iou_stop_capture_not_started(http_compute, vm, tmpdir):
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM.is_running", return_value=False) as mock:
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.stop_capture") as stop_capture:
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=False) as mock:
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop_capture") as stop_capture:
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert not stop_capture.called
assert response.status == 409
def test_get_configs_without_configs_file(http_hypervisor, vm):
def test_get_configs_without_configs_file(http_compute, vm):
response = http_hypervisor.get("/projects/{project_id}/iou/vms/{vm_id}/configs".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
response = http_compute.get("/projects/{project_id}/iou/vms/{vm_id}/configs".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert "startup_config" not in response.json
assert "private_config" not in response.json
def test_get_configs_with_startup_config_file(http_hypervisor, project, vm):
def test_get_configs_with_startup_config_file(http_compute, project, vm):
path = startup_config_file(project, vm)
with open(path, "w+") as f:
f.write("TEST")
response = http_hypervisor.get("/projects/{project_id}/iou/vms/{vm_id}/configs".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
response = http_compute.get("/projects/{project_id}/iou/vms/{vm_id}/configs".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.json["startup_config_content"] == "TEST"
def test_vms(http_hypervisor, tmpdir, fake_iou_bin):
def test_vms(http_compute, tmpdir, fake_iou_bin):
with patch("gns3server.hypervisor.IOU.get_images_directory", return_value=str(tmpdir)):
response = http_hypervisor.get("/iou/vms", example=True)
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir)):
response = http_compute.get("/iou/vms", example=True)
assert response.status == 200
assert response.json == [{"filename": "iou.bin", "path": "iou.bin"}]
def test_upload_vm(http_hypervisor, tmpdir):
with patch("gns3server.hypervisor.IOU.get_images_directory", return_value=str(tmpdir),):
response = http_hypervisor.post("/iou/vms/test2", body="TEST", raw=True)
def test_upload_vm(http_compute, tmpdir):
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir),):
response = http_compute.post("/iou/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
@@ -349,11 +349,11 @@ def test_upload_vm(http_hypervisor, tmpdir):
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
def test_upload_vm_permission_denied(http_hypervisor, tmpdir):
def test_upload_vm_permission_denied(http_compute, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.hypervisor.IOU.get_images_directory", return_value=str(tmpdir),):
response = http_hypervisor.post("/iou/vms/test2", body="TEST", raw=True)
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir),):
response = http_compute.post("/iou/vms/test2", body="TEST", raw=True)
assert response.status == 409
@@ -19,15 +19,15 @@ import os
import pytest
def test_udp_allocation(http_hypervisor, project):
response = http_hypervisor.post('/projects/{}/ports/udp'.format(project.id), {}, example=True)
def test_udp_allocation(http_compute, project):
response = http_compute.post('/projects/{}/ports/udp'.format(project.id), {}, example=True)
assert response.status == 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")
def test_interfaces(http_hypervisor):
response = http_hypervisor.get('/interfaces', example=True)
def test_interfaces(http_compute):
response = http_compute.get('/interfaces', example=True)
assert response.status == 200
assert isinstance(response.json, list)
@@ -17,11 +17,11 @@
import json
from gns3server.hypervisor.notification_manager import NotificationManager
from gns3server.compute.notification_manager import NotificationManager
def test_notification_ws(http_hypervisor, async_run):
ws = http_hypervisor.websocket("/notifications/ws")
def test_notification_ws(http_compute, async_run):
ws = http_compute.websocket("/notifications/ws")
answer = async_run(ws.receive())
answer = json.loads(answer.data)
assert answer["action"] == "ping"
@@ -32,4 +32,4 @@ def test_notification_ws(http_hypervisor, async_run):
answer = json.loads(answer.data)
assert answer["action"] == "test"
async_run(http_hypervisor.close())
async_run(http_compute.close())
@@ -28,97 +28,96 @@ import zipfile
from unittest.mock import patch
from tests.utils import asyncio_patch
from gns3server.handlers.api.hypervisor.project_handler import ProjectHandler
from gns3server.hypervisor.project_manager import ProjectManager
from gns3server.handlers.api.compute.project_handler import ProjectHandler
from gns3server.compute.project_manager import ProjectManager
def test_create_project_with_path(http_hypervisor, tmpdir):
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
response = http_hypervisor.post("/projects", {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"})
def test_create_project_with_path(http_compute, tmpdir):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
response = http_compute.post("/projects", {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"})
assert response.status == 201
assert response.json["name"] == "test"
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
def test_create_project_without_dir(http_hypervisor):
def test_create_project_without_dir(http_compute):
query = {"name": "test", "project_id": "10010203-0405-0607-0809-0a0b0c0d0e0f"}
response = http_hypervisor.post("/projects", query, example=True)
response = http_compute.post("/projects", query, example=True)
assert response.status == 201
assert response.json["project_id"] == "10010203-0405-0607-0809-0a0b0c0d0e0f"
assert response.json["temporary"] is False
assert response.json["name"] == "test"
def test_create_temporary_project(http_hypervisor):
def test_create_temporary_project(http_compute):
query = {"name": "test", "temporary": True, "project_id": "20010203-0405-0607-0809-0a0b0c0d0e0f"}
response = http_hypervisor.post("/projects", query)
response = http_compute.post("/projects", query)
assert response.status == 201
assert response.json["project_id"] == "20010203-0405-0607-0809-0a0b0c0d0e0f"
assert response.json["temporary"] is True
assert response.json["name"] == "test"
def test_create_project_with_uuid(http_hypervisor):
def test_create_project_with_uuid(http_compute):
query = {"name": "test", "project_id": "30010203-0405-0607-0809-0a0b0c0d0e0f"}
response = http_hypervisor.post("/projects", query)
response = http_compute.post("/projects", query)
assert response.status == 201
assert response.json["project_id"] == "30010203-0405-0607-0809-0a0b0c0d0e0f"
assert response.json["name"] == "test"
def test_show_project(http_hypervisor):
def test_show_project(http_compute):
query = {"name": "test", "project_id": "40010203-0405-0607-0809-0a0b0c0d0e02", "temporary": False}
response = http_hypervisor.post("/projects", query)
response = http_compute.post("/projects", query)
assert response.status == 201
response = http_hypervisor.get("/projects/40010203-0405-0607-0809-0a0b0c0d0e02", example=True)
response = http_compute.get("/projects/40010203-0405-0607-0809-0a0b0c0d0e02", example=True)
assert len(response.json.keys()) == 3
assert response.json["project_id"] == "40010203-0405-0607-0809-0a0b0c0d0e02"
assert response.json["temporary"] is False
assert response.json["name"] == "test"
def test_show_project_invalid_uuid(http_hypervisor):
response = http_hypervisor.get("/projects/50010203-0405-0607-0809-0a0b0c0d0e42")
def test_show_project_invalid_uuid(http_compute):
response = http_compute.get("/projects/50010203-0405-0607-0809-0a0b0c0d0e42")
assert response.status == 404
def test_list_projects(http_hypervisor):
def test_list_projects(http_compute):
ProjectManager.instance()._projects = {}
query = {"name": "test", "project_id": "51010203-0405-0607-0809-0a0b0c0d0e0f"}
response = http_hypervisor.post("/projects", query)
response = http_compute.post("/projects", query)
assert response.status == 201
query = {"name": "test", "project_id": "52010203-0405-0607-0809-0a0b0c0d0e0b"}
response = http_hypervisor.post("/projects", query)
response = http_compute.post("/projects", query)
assert response.status == 201
response = http_hypervisor.get("/projects", example=True)
response = http_compute.get("/projects", example=True)
assert response.status == 200
assert len(response.json) == 2
assert "51010203-0405-0607-0809-0a0b0c0d0e0f" in [p["project_id"] for p in response.json]
def test_update_temporary_project(http_hypervisor):
def test_update_temporary_project(http_compute):
query = {"name": "test", "temporary": True, "project_id": "60010203-0405-0607-0809-0a0b0c0d0e0b"}
response = http_hypervisor.post("/projects", query)
response = http_compute.post("/projects", query)
assert response.status == 201
query = {"name": "test", "temporary": False}
response = http_hypervisor.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200
assert response.json["temporary"] is False
def test_update_path_project_temporary(http_hypervisor, tmpdir):
def test_update_path_project_temporary(http_compute, tmpdir):
os.makedirs(str(tmpdir / "a"))
os.makedirs(str(tmpdir / "b"))
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
response = http_hypervisor.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "temporary": True, "project_id": "70010203-0405-0607-0809-0a0b0c0d0e0b"})
with patch("gns3server.compute.project.Project.is_local", return_value=True):
response = http_compute.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "temporary": True, "project_id": "70010203-0405-0607-0809-0a0b0c0d0e0b"})
assert response.status == 201
assert response.json["name"] == "first_name"
query = {"name": "second_name", "path": str(tmpdir / "b")}
response = http_hypervisor.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200
assert response.json["name"] == "second_name"
@@ -126,17 +125,17 @@ def test_update_path_project_temporary(http_hypervisor, tmpdir):
assert os.path.exists(str(tmpdir / "b"))
def test_update_path_project_non_temporary(http_hypervisor, tmpdir):
def test_update_path_project_non_temporary(http_compute, tmpdir):
os.makedirs(str(tmpdir / "a"))
os.makedirs(str(tmpdir / "b"))
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
response = http_hypervisor.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "project_id": "80010203-0405-0607-0809-0a0b0c0d0e0b"})
with patch("gns3server.compute.project.Project.is_local", return_value=True):
response = http_compute.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "project_id": "80010203-0405-0607-0809-0a0b0c0d0e0b"})
assert response.status == 201
assert response.json["name"] == "first_name"
query = {"name": "second_name", "path": str(tmpdir / "b")}
response = http_hypervisor.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200
assert response.json["name"] == "second_name"
@@ -144,63 +143,63 @@ def test_update_path_project_non_temporary(http_hypervisor, tmpdir):
assert os.path.exists(str(tmpdir / "b"))
def test_update_path_project_non_local(http_hypervisor, tmpdir):
def test_update_path_project_non_local(http_compute, tmpdir):
with patch("gns3server.hypervisor.project.Project.is_local", return_value=False):
response = http_hypervisor.post("/projects", {"name": "first_name", "project_id": "90010203-0405-0607-0809-0a0b0c0d0e0b"})
with patch("gns3server.compute.project.Project.is_local", return_value=False):
response = http_compute.post("/projects", {"name": "first_name", "project_id": "90010203-0405-0607-0809-0a0b0c0d0e0b"})
assert response.status == 201
query = {"name": "second_name", "path": str(tmpdir)}
response = http_hypervisor.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 403
def test_commit_project(http_hypervisor, project):
with asyncio_patch("gns3server.hypervisor.project.Project.commit", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/commit".format(project_id=project.id), example=True)
def test_commit_project(http_compute, project):
with asyncio_patch("gns3server.compute.project.Project.commit", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/commit".format(project_id=project.id), example=True)
assert response.status == 204
assert mock.called
def test_commit_project_invalid_uuid(http_hypervisor):
response = http_hypervisor.post("/projects/{project_id}/commit".format(project_id=uuid.uuid4()))
def test_commit_project_invalid_uuid(http_compute):
response = http_compute.post("/projects/{project_id}/commit".format(project_id=uuid.uuid4()))
assert response.status == 404
def test_delete_project(http_hypervisor, project):
with asyncio_patch("gns3server.hypervisor.project.Project.delete", return_value=True) as mock:
response = http_hypervisor.delete("/projects/{project_id}".format(project_id=project.id), example=True)
def test_delete_project(http_compute, project):
with asyncio_patch("gns3server.compute.project.Project.delete", return_value=True) as mock:
response = http_compute.delete("/projects/{project_id}".format(project_id=project.id), example=True)
assert response.status == 204
assert mock.called
def test_delete_project_invalid_uuid(http_hypervisor):
response = http_hypervisor.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
def test_delete_project_invalid_uuid(http_compute):
response = http_compute.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
assert response.status == 404
def test_close_project(http_hypervisor, project):
with asyncio_patch("gns3server.hypervisor.project.Project.close", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
def test_close_project(http_compute, project):
with asyncio_patch("gns3server.compute.project.Project.close", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
assert response.status == 204
assert mock.called
def test_close_project_two_client_connected(http_hypervisor, project):
def test_close_project_two_client_connected(http_compute, project):
ProjectHandler._notifications_listening = {project.id: 2}
with asyncio_patch("gns3server.hypervisor.project.Project.close", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
with asyncio_patch("gns3server.compute.project.Project.close", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
assert response.status == 204
assert not mock.called
def test_close_project_invalid_uuid(http_hypervisor):
response = http_hypervisor.post("/projects/{project_id}/close".format(project_id=uuid.uuid4()))
def test_close_project_invalid_uuid(http_compute):
response = http_compute.post("/projects/{project_id}/close".format(project_id=uuid.uuid4()))
assert response.status == 404
def test_get_file(http_hypervisor, tmpdir):
def test_get_file(http_compute, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
@@ -208,24 +207,24 @@ def test_get_file(http_hypervisor, tmpdir):
with open(os.path.join(project.path, "hello"), "w+") as f:
f.write("world")
response = http_hypervisor.get("/projects/{project_id}/files/hello".format(project_id=project.id), raw=True)
response = http_compute.get("/projects/{project_id}/files/hello".format(project_id=project.id), raw=True)
assert response.status == 200
assert response.body == b"world"
response = http_hypervisor.get("/projects/{project_id}/files/false".format(project_id=project.id), raw=True)
response = http_compute.get("/projects/{project_id}/files/false".format(project_id=project.id), raw=True)
assert response.status == 404
response = http_hypervisor.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
response = http_compute.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
assert response.status == 403
def test_export(http_hypervisor, tmpdir, loop, project):
def test_export(http_compute, tmpdir, loop, project):
os.makedirs(project.path, exist_ok=True)
with open(os.path.join(project.path, 'a'), 'w+') as f:
f.write('hello')
response = http_hypervisor.get("/projects/{project_id}/export".format(project_id=project.id), raw=True)
response = http_compute.get("/projects/{project_id}/export".format(project_id=project.id), raw=True)
assert response.status == 200
assert response.headers['CONTENT-TYPE'] == 'application/gns3z'
assert response.headers['CONTENT-DISPOSITION'] == 'attachment; filename="{}.gns3z"'.format(project.name)
@@ -239,7 +238,7 @@ def test_export(http_hypervisor, tmpdir, loop, project):
assert content == b"hello"
def test_import(http_hypervisor, tmpdir, loop, project):
def test_import(http_compute, tmpdir, loop, project):
with zipfile.ZipFile(str(tmpdir / "test.zip"), 'w') as myzip:
myzip.writestr("demo", b"hello")
@@ -247,7 +246,7 @@ def test_import(http_hypervisor, tmpdir, loop, project):
project_id = project.id
with open(str(tmpdir / "test.zip"), "rb") as f:
response = http_hypervisor.post("/projects/{project_id}/import".format(project_id=project_id), body=f.read(), raw=True)
response = http_compute.post("/projects/{project_id}/import".format(project_id=project_id), body=f.read(), raw=True)
assert response.status == 201
project = ProjectManager.instance().get_project(project_id=project_id)
@@ -57,14 +57,14 @@ def base_params(tmpdir, fake_qemu_bin):
@pytest.fixture
def vm(http_hypervisor, project, base_params):
response = http_hypervisor.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
def vm(http_compute, project, base_params):
response = http_compute.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
assert response.status == 201
return response.json
def test_qemu_create(http_hypervisor, project, base_params, fake_qemu_bin):
response = http_hypervisor.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
def test_qemu_create(http_compute, project, base_params, fake_qemu_bin):
response = http_compute.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
assert response.status == 201
assert response.route == "/projects/{project_id}/qemu/vms"
assert response.json["name"] == "PC TEST 1"
@@ -73,11 +73,11 @@ def test_qemu_create(http_hypervisor, project, base_params, fake_qemu_bin):
assert response.json["platform"] == "x86_64"
def test_qemu_create_platform(http_hypervisor, project, base_params, fake_qemu_bin):
def test_qemu_create_platform(http_compute, project, base_params, fake_qemu_bin):
base_params["qemu_path"] = None
base_params["platform"] = "x86_64"
response = http_hypervisor.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
response = http_compute.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
assert response.status == 201
assert response.route == "/projects/{project_id}/qemu/vms"
assert response.json["name"] == "PC TEST 1"
@@ -86,12 +86,12 @@ def test_qemu_create_platform(http_hypervisor, project, base_params, fake_qemu_b
assert response.json["platform"] == "x86_64"
def test_qemu_create_with_params(http_hypervisor, project, base_params, fake_qemu_vm):
def test_qemu_create_with_params(http_compute, project, base_params, fake_qemu_vm):
params = base_params
params["ram"] = 1024
params["hda_disk_image"] = "linux载.img"
response = http_hypervisor.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), params, example=True)
response = http_compute.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), params, example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/qemu/vms"
@@ -102,8 +102,8 @@ def test_qemu_create_with_params(http_hypervisor, project, base_params, fake_qem
assert response.json["hda_disk_image_md5sum"] == "c4ca4238a0b923820dcc509a6f75849b"
def test_qemu_get(http_hypervisor, project, vm):
response = http_hypervisor.get("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_qemu_get(http_compute, project, vm):
response = http_compute.get("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/qemu/vms/{vm_id}"
assert response.json["name"] == "PC TEST 1"
@@ -111,57 +111,57 @@ def test_qemu_get(http_hypervisor, project, vm):
assert response.json["vm_directory"] == os.path.join(project.path, "project-files", "qemu", vm["vm_id"])
def test_qemu_start(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.start", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_qemu_start(http_compute, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.start", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 200
assert response.json["name"] == "PC TEST 1"
def test_qemu_stop(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.stop", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_qemu_stop(http_compute, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.stop", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_qemu_reload(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.reload", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_qemu_reload(http_compute, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.reload", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_qemu_suspend(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.suspend", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_qemu_suspend(http_compute, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.suspend", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_qemu_resume(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.resume", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_qemu_resume(http_compute, vm):
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.resume", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_qemu_delete(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.qemu.Qemu.delete_vm", return_value=True) as mock:
response = http_hypervisor.delete("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_qemu_delete(http_compute, vm):
with asyncio_patch("gns3server.compute.qemu.Qemu.delete_vm", return_value=True) as mock:
response = http_compute.delete("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_qemu_update(http_hypervisor, vm, tmpdir, free_console_port, project, fake_qemu_vm):
def test_qemu_update(http_compute, vm, tmpdir, free_console_port, project, fake_qemu_vm):
params = {
"name": "test",
"console": free_console_port,
"ram": 1024,
"hdb_disk_image": "linux.img"
}
response = http_hypervisor.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True)
response = http_compute.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True)
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
@@ -169,71 +169,71 @@ def test_qemu_update(http_hypervisor, vm, tmpdir, free_console_port, project, fa
assert response.json["ram"] == 1024
def test_qemu_nio_create_udp(http_hypervisor, vm):
http_hypervisor.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
def test_qemu_nio_create_udp(http_compute, vm):
http_compute.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/qemu/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_qemu_nio_create_ethernet(http_hypervisor, vm):
http_hypervisor.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": "eth0",
},
example=True)
def test_qemu_nio_create_ethernet(http_compute, vm):
http_compute.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": "eth0",
},
example=True)
assert response.status == 409
def test_qemu_delete_nio(http_hypervisor, vm):
http_hypervisor.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = http_hypervisor.delete("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_qemu_delete_nio(http_compute, vm):
http_compute.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = http_compute.delete("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/qemu/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_qemu_list_binaries(http_hypervisor, vm):
def test_qemu_list_binaries(http_compute, vm):
ret = [{"path": "/tmp/1", "version": "2.2.0"},
{"path": "/tmp/2", "version": "2.1.0"}]
with asyncio_patch("gns3server.hypervisor.qemu.Qemu.binary_list", return_value=ret) as mock:
response = http_hypervisor.get("/qemu/binaries".format(project_id=vm["project_id"]), example=True)
with asyncio_patch("gns3server.compute.qemu.Qemu.binary_list", return_value=ret) as mock:
response = http_compute.get("/qemu/binaries".format(project_id=vm["project_id"]), example=True)
assert mock.called_with(None)
assert response.status == 200
assert response.json == ret
def test_qemu_list_binaries_filter(http_hypervisor, vm):
def test_qemu_list_binaries_filter(http_compute, 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.hypervisor.qemu.Qemu.binary_list", return_value=ret) as mock:
response = http_hypervisor.get("/qemu/binaries".format(project_id=vm["project_id"]), body={"archs": ["i386"]}, example=True)
with asyncio_patch("gns3server.compute.qemu.Qemu.binary_list", return_value=ret) as mock:
response = http_compute.get("/qemu/binaries".format(project_id=vm["project_id"]), body={"archs": ["i386"]}, example=True)
assert response.status == 200
assert mock.called_with(["i386"])
assert response.json == ret
def test_vms(http_hypervisor, tmpdir, fake_qemu_vm):
def test_vms(http_compute, tmpdir, fake_qemu_vm):
response = http_hypervisor.get("/qemu/vms")
response = http_compute.get("/qemu/vms")
assert response.status == 200
assert response.json == [{"filename": "linux载.img", "path": "linux载.img"}]
def test_upload_vm(http_hypervisor, tmpdir):
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir),):
response = http_hypervisor.post("/qemu/vms/test2", body="TEST", raw=True)
def test_upload_vm(http_compute, tmpdir):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
response = http_compute.post("/qemu/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
@@ -244,9 +244,9 @@ def test_upload_vm(http_hypervisor, tmpdir):
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
def test_upload_vm_ova(http_hypervisor, tmpdir):
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir),):
response = http_hypervisor.post("/qemu/vms/test2.ova/test2.vmdk", body="TEST", raw=True)
def test_upload_vm_ova(http_compute, tmpdir):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
response = http_compute.post("/qemu/vms/test2.ova/test2.vmdk", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2.ova" / "test2.vmdk")) as f:
@@ -257,23 +257,23 @@ def test_upload_vm_ova(http_hypervisor, tmpdir):
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
def test_upload_vm_forbiden_location(http_hypervisor, tmpdir):
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir),):
response = http_hypervisor.post("/qemu/vms/../../test2", body="TEST", raw=True)
def test_upload_vm_forbiden_location(http_compute, tmpdir):
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
response = http_compute.post("/qemu/vms/../../test2", body="TEST", raw=True)
assert response.status == 403
def test_upload_vm_permission_denied(http_hypervisor, tmpdir):
def test_upload_vm_permission_denied(http_compute, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir),):
response = http_hypervisor.post("/qemu/vms/test2", body="TEST", raw=True)
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
response = http_compute.post("/qemu/vms/test2", body="TEST", raw=True)
assert response.status == 409
def test_create_img_relative(http_hypervisor):
def test_create_img_relative(http_compute):
body = {
"qemu_img": "/tmp/qemu-img",
"path": "hda.qcow2",
@@ -284,13 +284,13 @@ def test_create_img_relative(http_hypervisor):
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.hypervisor.Qemu.create_disk"):
response = http_hypervisor.post("/qemu/img", body=body, example=True)
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = http_compute.post("/qemu/img", body=body, example=True)
assert response.status == 201
def test_create_img_absolute_non_local(http_hypervisor):
def test_create_img_absolute_non_local(http_compute):
config = Config.instance()
config.set("Server", "local", "false")
@@ -305,13 +305,13 @@ def test_create_img_absolute_non_local(http_hypervisor):
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.hypervisor.Qemu.create_disk"):
response = http_hypervisor.post("/qemu/img", body=body, example=True)
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = http_compute.post("/qemu/img", body=body, example=True)
assert response.status == 403
def test_create_img_absolute_local(http_hypervisor):
def test_create_img_absolute_local(http_compute):
config = Config.instance()
config.set("Server", "local", "true")
@@ -326,13 +326,13 @@ def test_create_img_absolute_local(http_hypervisor):
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.hypervisor.Qemu.create_disk"):
response = http_hypervisor.post("/qemu/img", body=body, example=True)
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = http_compute.post("/qemu/img", body=body, example=True)
assert response.status == 201
def test_capabilities(http_hypervisor):
with asyncio_patch("gns3server.hypervisor.Qemu.get_kvm_archs", return_value=["x86_64"]):
response = http_hypervisor.get("/qemu/capabilities", example=True)
def test_capabilities(http_compute):
with asyncio_patch("gns3server.compute.Qemu.get_kvm_archs", return_value=["x86_64"]):
response = http_compute.get("/qemu/capabilities", example=True)
assert response.json["kvm"] == ["x86_64"]
@@ -25,37 +25,37 @@ from gns3server.config import Config
from gns3server.version import __version__
def test_version_output(http_hypervisor):
def test_version_output(http_compute):
config = Config.instance()
config.set("Server", "local", "true")
response = http_hypervisor.get('/version', example=True)
response = http_compute.get('/version', example=True)
assert response.status == 200
assert response.json == {'local': True, 'version': __version__}
def test_version_input(http_hypervisor):
def test_version_input(http_compute):
query = {'version': __version__}
response = http_hypervisor.post('/version', query, example=True)
response = http_compute.post('/version', query, example=True)
assert response.status == 200
assert response.json == {'version': __version__}
def test_version_invalid_input(http_hypervisor):
def test_version_invalid_input(http_compute):
query = {'version': "0.4.2"}
response = http_hypervisor.post('/version', query)
response = http_compute.post('/version', query)
assert response.status == 409
assert response.json == {'message': 'Client version 0.4.2 differs with server version {}'.format(__version__),
'status': 409}
def test_version_invalid_input_schema(http_hypervisor):
def test_version_invalid_input_schema(http_compute):
query = {'version': "0.4.2", "bla": "blu"}
response = http_hypervisor.post('/version', query)
response = http_compute.post('/version', query)
assert response.status == 400
def test_version_invalid_json(http_hypervisor):
def test_version_invalid_json(http_compute):
query = "BOUM"
response = http_hypervisor.post('/version', query, raw=True)
response = http_compute.post('/version', query, raw=True)
assert response.status == 400
@@ -0,0 +1,133 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 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.yield_fixture(scope="function")
def vm(http_compute, project, monkeypatch):
vboxmanage_path = "/fake/VboxManage"
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VMTEST",
"vmname": "VMTEST",
"linked_clone": False})
assert mock.called
assert response.status == 201
with patch("gns3server.compute.virtualbox.VirtualBox.find_vboxmanage", return_value=vboxmanage_path):
yield response.json
def test_vbox_create(http_compute, project):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True):
response = http_compute.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VM1",
"vmname": "VM1",
"linked_clone": False},
example=True)
assert response.status == 201
assert response.json["name"] == "VM1"
assert response.json["project_id"] == project.id
def test_vbox_get(http_compute, project, vm):
response = http_compute.get("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}"
assert response.json["name"] == "VMTEST"
assert response.json["project_id"] == project.id
def test_vbox_start(http_compute, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.check_hw_virtualization", return_value=True) as mock:
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.start", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_stop(http_compute, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.stop", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_suspend(http_compute, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.suspend", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_resume(http_compute, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.resume", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_reload(http_compute, vm):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.reload", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_nio_create_udp(http_compute, vm):
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"],
vm_id=vm["vm_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/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_vbox_delete_nio(http_compute, vm):
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
response = http_compute.delete("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_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/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_vbox_update(http_compute, vm, free_console_port):
response = http_compute.put("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port},
example=True)
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
@@ -23,22 +23,22 @@ from unittest.mock import patch
@pytest.fixture(scope="function")
def vm(http_hypervisor, project):
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1"})
def vm(http_compute, project):
response = http_compute.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1"})
assert response.status == 201
return response.json
def test_vpcs_create(http_hypervisor, project):
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1"}, example=True)
def test_vpcs_create(http_compute, project):
response = http_compute.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1"}, example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
def test_vpcs_get(http_hypervisor, project, vm):
response = http_hypervisor.get("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_vpcs_get(http_compute, project, vm):
response = http_compute.get("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}"
assert response.json["name"] == "PC TEST 1"
@@ -47,8 +47,8 @@ def test_vpcs_get(http_hypervisor, project, vm):
assert response.json["status"] == "stopped"
def test_vpcs_create_startup_script(http_hypervisor, project):
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1", "startup_script": "ip 192.168.1.2\necho TEST"})
def test_vpcs_create_startup_script(http_compute, project):
response = http_compute.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1", "startup_script": "ip 192.168.1.2\necho TEST"})
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms"
assert response.json["name"] == "PC TEST 1"
@@ -57,8 +57,8 @@ def test_vpcs_create_startup_script(http_hypervisor, project):
assert response.json["startup_script_path"] == "startup.vpc"
def test_vpcs_create_port(http_hypervisor, project, free_console_port):
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1", "console": free_console_port})
def test_vpcs_create_port(http_compute, project, free_console_port):
response = http_compute.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1", "console": free_console_port})
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms"
assert response.json["name"] == "PC TEST 1"
@@ -66,72 +66,72 @@ def test_vpcs_create_port(http_hypervisor, project, free_console_port):
assert response.json["console"] == free_console_port
def test_vpcs_nio_create_udp(http_hypervisor, vm):
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
def test_vpcs_nio_create_udp(http_compute, vm):
response = http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
def test_vpcs_nio_create_tap(http_hypervisor, vm, ethernet_device):
with patch("gns3server.hypervisor.base_manager.BaseManager.has_privileged_access", return_value=True):
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap",
"tap_device": ethernet_device})
def test_vpcs_nio_create_tap(http_compute, vm, ethernet_device):
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
response = http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap",
"tap_device": ethernet_device})
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_tap"
def test_vpcs_delete_nio(http_hypervisor, vm):
http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = http_hypervisor.delete("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_vpcs_delete_nio(http_compute, vm):
http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = http_compute.delete("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_vpcs_start(http_hypervisor, vm):
def test_vpcs_start(http_compute, vm):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 200
assert response.json["name"] == "PC TEST 1"
def test_vpcs_stop(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM.stop", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_vpcs_stop(http_compute, vm):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.stop", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vpcs_reload(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM.reload", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_vpcs_reload(http_compute, vm):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.reload", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vpcs_delete(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.vpcs.VPCS.delete_vm", return_value=True) as mock:
response = http_hypervisor.delete("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
def test_vpcs_delete(http_compute, vm):
with asyncio_patch("gns3server.compute.vpcs.VPCS.delete_vm", return_value=True) as mock:
response = http_compute.delete("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vpcs_update(http_hypervisor, vm, tmpdir, free_console_port):
response = http_hypervisor.put("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port,
"startup_script": "ip 192.168.1.1"},
example=True)
def test_vpcs_update(http_compute, vm, tmpdir, free_console_port):
response = http_compute.put("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port,
"startup_script": "ip 192.168.1.1"},
example=True)
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
@@ -16,21 +16,21 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def test_hypervisor_create(http_controller, controller):
def test_compute_create(http_controller, controller):
params = {
"hypervisor_id": "my_hypervisor_id",
"compute_id": "my_compute_id",
"protocol": "http",
"host": "example.com",
"port": 84,
"user": "julien",
"password": "secure"
}
response = http_controller.post("/hypervisors", params, example=True)
response = http_controller.post("/computes", params, example=True)
assert response.status == 201
assert response.route == "/hypervisors"
assert response.route == "/computes"
assert response.json["user"] == "julien"
assert "password" not in response.json
assert len(controller.hypervisors) == 1
assert controller.hypervisors["my_hypervisor_id"].host == "example.com"
assert len(controller.computes) == 1
assert controller.computes["my_compute_id"].host == "example.com"
+16 -16
View File
@@ -35,11 +35,11 @@ from gns3server.controller.vm import VM
@pytest.fixture
def hypervisor(http_controller, async_run):
hypervisor = MagicMock()
hypervisor.id = "example.com"
Controller.instance()._hypervisors = {"example.com": hypervisor}
return hypervisor
def compute(http_controller, async_run):
compute = MagicMock()
compute.id = "example.com"
Controller.instance()._computes = {"example.com": compute}
return compute
@pytest.fixture
@@ -48,21 +48,21 @@ def project(http_controller, async_run):
@pytest.fixture
def vm(project, hypervisor, async_run):
vm = VM(project, hypervisor, name="test", vm_type="vpcs")
def vm(project, compute, async_run):
vm = VM(project, compute, name="test", vm_type="vpcs")
project._vms[vm.id] = vm
return vm
def test_create_vm(http_controller, tmpdir, project, hypervisor):
def test_create_vm(http_controller, tmpdir, project, compute):
response = MagicMock()
response.json = {"console": 2048}
hypervisor.post = AsyncioMagicMock(return_value=response)
compute.post = AsyncioMagicMock(return_value=response)
response = http_controller.post("/projects/{}/vms".format(project.id), {
"name": "test",
"vm_type": "vpcs",
"hypervisor_id": "example.com",
"compute_id": "example.com",
"properties": {
"startup_script": "echo test"
}
@@ -72,27 +72,27 @@ def test_create_vm(http_controller, tmpdir, project, hypervisor):
assert "name" not in response.json["properties"]
def test_start_vm(http_controller, tmpdir, project, hypervisor, vm):
def test_start_vm(http_controller, tmpdir, project, compute, vm):
response = MagicMock()
hypervisor.post = AsyncioMagicMock()
compute.post = AsyncioMagicMock()
response = http_controller.post("/projects/{}/vms/{}/start".format(project.id, vm.id), example=True)
assert response.status == 201
assert response.json["name"] == vm.name
def test_stop_vm(http_controller, tmpdir, project, hypervisor, vm):
def test_stop_vm(http_controller, tmpdir, project, compute, vm):
response = MagicMock()
hypervisor.post = AsyncioMagicMock()
compute.post = AsyncioMagicMock()
response = http_controller.post("/projects/{}/vms/{}/stop".format(project.id, vm.id), example=True)
assert response.status == 201
assert response.json["name"] == vm.name
def test_suspend_vm(http_controller, tmpdir, project, hypervisor, vm):
def test_suspend_vm(http_controller, tmpdir, project, compute, vm):
response = MagicMock()
hypervisor.post = AsyncioMagicMock()
compute.post = AsyncioMagicMock()
response = http_controller.post("/projects/{}/vms/{}/suspend".format(project.id, vm.id), example=True)
assert response.status == 201
@@ -1,185 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 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
import aiohttp
from tests.utils import asyncio_patch
from unittest.mock import patch, MagicMock, PropertyMock
from gns3server.hypervisor.docker import Docker
@pytest.fixture
def base_params():
"""Return standard parameters"""
return {"name": "PC TEST 1", "image": "nginx", "start_command": "nginx-daemon", "adapters": 2, "environment": "YES=1\nNO=0", "console_type": "telnet", "console_resolution": "1280x1024"}
@pytest.yield_fixture(autouse=True)
def mock_connection():
docker = Docker.instance()
docker._connected = True
docker._connector = MagicMock()
yield
Docker._instance = None
@pytest.fixture
def vm(http_hypervisor, project, base_params):
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "nginx"}]) as mock_list:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
response = http_hypervisor.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
if response.status != 201:
print(response.body)
assert response.status == 201
return response.json
def test_docker_create(http_hypervisor, project, base_params):
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "nginx"}]) as mock_list:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
response = http_hypervisor.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
assert response.status == 201
assert response.route == "/projects/{project_id}/docker/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
assert response.json["container_id"] == "8bd8153ea8f5"
assert response.json["image"] == "nginx"
assert response.json["adapters"] == 2
assert response.json["environment"] == "YES=1\nNO=0"
assert response.json["console_resolution"] == "1280x1024"
def test_docker_start(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.start", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_stop(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.stop", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_reload(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.restart", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_delete(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.delete", return_value=True) as mock:
response = http_hypervisor.delete("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_reload(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.pause", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_docker_nio_create_udp(http_hypervisor, vm):
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_docker_delete_nio(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.adapter_remove_nio_binding") as mock:
response = http_hypervisor.delete("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_docker_update(http_hypervisor, vm, tmpdir, free_console_port):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.update") as mock:
response = http_hypervisor.put("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port,
"start_command": "yes",
"environment": "GNS3=1\nGNS4=0"},
example=True)
assert mock.called
assert response.status == 200
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"
def test_docker_start_capture(http_hypervisor, vm, tmpdir, project):
with patch("gns3server.hypervisor.docker.docker_vm.DockerVM.is_running", return_value=True) as mock:
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.start_capture") as start_capture:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params, example=True)
assert response.status == 200
assert start_capture.called
assert "test.pcap" in response.json["pcap_file_path"]
def test_docker_start_capture_not_started(http_hypervisor, vm, tmpdir):
with patch("gns3server.hypervisor.docker.docker_vm.DockerVM.is_running", return_value=False) as mock:
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.start_capture") as start_capture:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params)
assert not start_capture.called
assert response.status == 409
def test_docker_stop_capture(http_hypervisor, vm, tmpdir, project):
with patch("gns3server.hypervisor.docker.docker_vm.DockerVM.is_running", return_value=True) as mock:
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.stop_capture") as stop_capture:
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert stop_capture.called
def test_docker_stop_capture_not_started(http_hypervisor, vm, tmpdir):
with patch("gns3server.hypervisor.docker.docker_vm.DockerVM.is_running", return_value=False) as mock:
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.stop_capture") as stop_capture:
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert not stop_capture.called
assert response.status == 409
@@ -1,133 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 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.yield_fixture(scope="function")
def vm(http_hypervisor, project, monkeypatch):
vboxmanage_path = "/fake/VboxManage"
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VMTEST",
"vmname": "VMTEST",
"linked_clone": False})
assert mock.called
assert response.status == 201
with patch("gns3server.hypervisor.virtualbox.VirtualBox.find_vboxmanage", return_value=vboxmanage_path):
yield response.json
def test_vbox_create(http_hypervisor, project):
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True):
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VM1",
"vmname": "VM1",
"linked_clone": False},
example=True)
assert response.status == 201
assert response.json["name"] == "VM1"
assert response.json["project_id"] == project.id
def test_vbox_get(http_hypervisor, project, vm):
response = http_hypervisor.get("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}"
assert response.json["name"] == "VMTEST"
assert response.json["project_id"] == project.id
def test_vbox_start(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.check_hw_virtualization", return_value=True) as mock:
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.start", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_stop(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.stop", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_suspend(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.suspend", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_resume(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.resume", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_reload(http_hypervisor, vm):
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.reload", return_value=True) as mock:
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_vbox_nio_create_udp(http_hypervisor, vm):
with asyncio_patch('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"],
vm_id=vm["vm_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/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_vbox_delete_nio(http_hypervisor, vm):
with asyncio_patch('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
response = http_hypervisor.delete("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_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/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_vbox_update(http_hypervisor, vm, free_console_port):
response = http_hypervisor.put("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port},
example=True)
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
+2 -2
View File
@@ -39,8 +39,8 @@ def test_controller(http_root, async_run):
assert response.status == 200
def test_hypervisor(http_root):
response = http_root.get('/hypervisor')
def test_compute(http_root):
response = http_root.get('/compute')
assert response.status == 200
+8 -8
View File
@@ -24,19 +24,19 @@ from gns3server.web.route import Route
def test_documentation_write(tmpdir):
os.makedirs(str(tmpdir / "api/examples"))
with open(str(tmpdir / "api/examples/hypervisor_post_projectsprojectidvirtualboxvms.txt"), "w+") as f:
with open(str(tmpdir / "api/examples/compute_post_projectsprojectidvirtualboxvms.txt"), "w+") as f:
f.write("curl test")
Documentation(Route, str(tmpdir)).write()
assert os.path.exists(str(tmpdir / "api"))
assert os.path.exists(str(tmpdir / "api" / "v2" / "hypervisor"))
assert os.path.exists(str(tmpdir / "api" / "v2" / "hypervisor" / "virtualbox.rst"))
assert os.path.exists(str(tmpdir / "api" / "v2" / "hypervisor" / "virtualbox"))
assert os.path.exists(str(tmpdir / "api" / "v2" / "hypervisor" / "virtualbox" / "virtualboxvms.rst"))
with open(str(tmpdir / "api" / "v2" / "hypervisor" / "virtualbox" / "projectsprojectidvirtualboxvms.rst")) as f:
assert os.path.exists(str(tmpdir / "api" / "v2" / "compute"))
assert os.path.exists(str(tmpdir / "api" / "v2" / "compute" / "virtualbox.rst"))
assert os.path.exists(str(tmpdir / "api" / "v2" / "compute" / "virtualbox"))
assert os.path.exists(str(tmpdir / "api" / "v2" / "compute" / "virtualbox" / "virtualboxvms.rst"))
with open(str(tmpdir / "api" / "v2" / "compute" / "virtualbox" / "projectsprojectidvirtualboxvms.rst")) as f:
content = f.read()
assert "Sample session" in content
assert "literalinclude:: ../../../examples/hypervisor_post_projectsprojectidvirtualboxvms.txt" in content
assert "literalinclude:: ../../../examples/compute_post_projectsprojectidvirtualboxvms.txt" in content
assert os.path.exists(str(tmpdir / "api" / "v2" / "controller" / "hypervisor.rst"))
assert os.path.exists(str(tmpdir / "api" / "v2" / "controller" / "compute.rst"))