diff --git a/tests/api/test_virtualbox.py b/tests/api/test_virtualbox.py index c1eb556c..f1f729d6 100644 --- a/tests/api/test_virtualbox.py +++ b/tests/api/test_virtualbox.py @@ -18,24 +18,27 @@ import pytest from tests.utils import asyncio_patch +@pytest.yield_fixture(scope="module") +def vm(server, project, monkeypatch): + + vboxmanage_path = "/fake/VboxManage" -@pytest.fixture(scope="module") -def vm(server, project): with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock: response = server.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VMTEST", - "vmname": "VMTEST", - "linked_clone": False}) + "vmname": "VMTEST", + "linked_clone": False}) assert mock.called assert response.status == 201 - return response.json + with asyncio_patch("gns3server.modules.virtualbox.VirtualBox.find_vboxmanage", return_value=vboxmanage_path): + yield response.json def test_vbox_create(server, project): with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True): response = server.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VM1", - "vmname": "VM1", - "linked_clone": False}, + "vmname": "VM1", + "linked_clone": False}, example=True) assert response.status == 201 assert response.json["name"] == "VM1" @@ -86,31 +89,40 @@ def test_vbox_reload(server, vm): def test_vbox_nio_create_udp(server, vm): - response = server.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, - "rhost": "127.0.0.1"}, + + with asyncio_patch('gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock: + response = server.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, + "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_id:\d+}/nio" assert response.json["type"] == "nio_udp" def test_vbox_delete_nio(server, vm): - server.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, - "rhost": "127.0.0.1"}) - response = server.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.modules.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock: + response = server.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 + assert args[0] == 0 + assert response.status == 204 assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio" def test_vbox_update(server, vm, free_console_port): response = server.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}) + "console": free_console_port}) assert response.status == 200 assert response.json["name"] == "test" assert response.json["console"] == free_console_port diff --git a/tests/api/test_vpcs.py b/tests/api/test_vpcs.py index 9c4dcfe0..ba42c45c 100644 --- a/tests/api/test_vpcs.py +++ b/tests/api/test_vpcs.py @@ -64,9 +64,9 @@ def test_vpcs_create_port(server, project, free_console_port): def test_vpcs_nio_create_udp(server, vm): response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/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"}, + "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}/ports/{port_number:\d+}/nio" @@ -76,7 +76,7 @@ def test_vpcs_nio_create_udp(server, vm): def test_vpcs_nio_create_tap(server, vm): with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True): response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap", - "tap_device": "test"}) + "tap_device": "test"}) assert response.status == 201 assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/ports/{port_number:\d+}/nio" assert response.json["type"] == "nio_tap" @@ -84,9 +84,9 @@ def test_vpcs_nio_create_tap(server, vm): def test_vpcs_delete_nio(server, vm): server.post("/projects/{project_id}/vpcs/vms/{vm_id}/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"}) + "lport": 4242, + "rport": 4343, + "rhost": "127.0.0.1"}) response = server.delete("/projects/{project_id}/vpcs/vms/{vm_id}/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}/ports/{port_number:\d+}/nio" @@ -122,8 +122,8 @@ def test_vpcs_delete(server, vm): def test_vpcs_update(server, vm, tmpdir, free_console_port): response = server.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"}) + "console": free_console_port, + "startup_script": "ip 192.168.1.1"}) assert response.status == 200 assert response.json["name"] == "test" assert response.json["console"] == free_console_port diff --git a/tests/conftest.py b/tests/conftest.py index 0ab02c19..490a35cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,7 @@ import socket import asyncio import tempfile import shutil +import os from aiohttp import web from gns3server.config import Config @@ -32,6 +33,10 @@ from gns3server.modules.project_manager import ProjectManager from tests.api.base import Query +# Prevent execution of external binaries +os.environ["PATH"] = tempfile.mkdtemp() + + @pytest.fixture(scope="session") def loop(request): """Return an event loop and destroy it at the end of test""" @@ -119,6 +124,15 @@ def run_around_tests(monkeypatch): server_section["project_directory"] = tmppath config.set_section_config("Server", server_section) + # Prevent exectuions of the VM if we forgot to mock something + vbox_section = config.get_section_config("VirtualBox") + vbox_section["vboxmanage_path"] = tmppath + config.set_section_config("VirtualBox", vbox_section) + + vbox_section = config.get_section_config("VPCS") + vbox_section["vpcs_path"] = tmppath + config.set_section_config("VPCS", vbox_section) + monkeypatch.setattr("gns3server.modules.project.Project._get_default_project_directory", lambda *args: tmppath) yield