1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-24 15:58:08 +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,17 +18,20 @@
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})
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):
@ -86,24 +89,33 @@ def test_vbox_reload(server, vm):
def test_vbox_nio_create_udp(server, vm):
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"})
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"

View File

@ -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