1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-25 00:08:11 +00:00

Fix tests

This commit is contained in:
Julien Duponchelle 2015-02-09 10:18:37 +01:00
parent 0d7d0a05c3
commit 64c197c719
3 changed files with 54 additions and 28 deletions

View File

@ -18,24 +18,27 @@
import pytest import pytest
from tests.utils import asyncio_patch 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: 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", response = server.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VMTEST",
"vmname": "VMTEST", "vmname": "VMTEST",
"linked_clone": False}) "linked_clone": False})
assert mock.called assert mock.called
assert response.status == 201 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): def test_vbox_create(server, project):
with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True): 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", response = server.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VM1",
"vmname": "VM1", "vmname": "VM1",
"linked_clone": False}, "linked_clone": False},
example=True) example=True)
assert response.status == 201 assert response.status == 201
assert response.json["name"] == "VM1" assert response.json["name"] == "VM1"
@ -86,31 +89,40 @@ def test_vbox_reload(server, vm):
def test_vbox_nio_create_udp(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", with asyncio_patch('gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
"lport": 4242, response = server.post("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"],
"rport": 4343, vm_id=vm["vm_id"]), {"type": "nio_udp",
"rhost": "127.0.0.1"}, "lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True) example=True)
assert mock.called
args, kwgars = mock.call_args
assert args[0] == 0
assert response.status == 201 assert response.status == 201
assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio" assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio"
assert response.json["type"] == "nio_udp" assert response.json["type"] == "nio_udp"
def test_vbox_delete_nio(server, vm): 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", with asyncio_patch('gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
"lport": 4242, 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)
"rport": 4343,
"rhost": "127.0.0.1"}) assert mock.called
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) args, kwgars = mock.call_args
assert args[0] == 0
assert response.status == 204 assert response.status == 204
assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio" assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio"
def test_vbox_update(server, vm, free_console_port): 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", 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.status == 200
assert response.json["name"] == "test" assert response.json["name"] == "test"
assert response.json["console"] == free_console_port assert response.json["console"] == free_console_port

View File

@ -64,9 +64,9 @@ def test_vpcs_create_port(server, project, free_console_port):
def test_vpcs_nio_create_udp(server, vm): 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", 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, "lport": 4242,
"rport": 4343, "rport": 4343,
"rhost": "127.0.0.1"}, "rhost": "127.0.0.1"},
example=True) example=True)
assert response.status == 201 assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/ports/{port_number:\d+}/nio" 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): def test_vpcs_nio_create_tap(server, vm):
with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True): 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", 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.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/ports/{port_number:\d+}/nio" assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_tap" 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): 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", 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, "lport": 4242,
"rport": 4343, "rport": 4343,
"rhost": "127.0.0.1"}) "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) 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.status == 204
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/ports/{port_number:\d+}/nio" 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): 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", 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, "console": free_console_port,
"startup_script": "ip 192.168.1.1"}) "startup_script": "ip 192.168.1.1"})
assert response.status == 200 assert response.status == 200
assert response.json["name"] == "test" assert response.json["name"] == "test"
assert response.json["console"] == free_console_port assert response.json["console"] == free_console_port

View File

@ -20,6 +20,7 @@ import socket
import asyncio import asyncio
import tempfile import tempfile
import shutil import shutil
import os
from aiohttp import web from aiohttp import web
from gns3server.config import Config from gns3server.config import Config
@ -32,6 +33,10 @@ from gns3server.modules.project_manager import ProjectManager
from tests.api.base import Query from tests.api.base import Query
# Prevent execution of external binaries
os.environ["PATH"] = tempfile.mkdtemp()
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def loop(request): def loop(request):
"""Return an event loop and destroy it at the end of test""" """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 server_section["project_directory"] = tmppath
config.set_section_config("Server", server_section) 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) monkeypatch.setattr("gns3server.modules.project.Project._get_default_project_directory", lambda *args: tmppath)
yield yield