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

Fixes tests.

This commit is contained in:
grossmj 2015-03-14 13:16:27 -06:00
parent 25b778aec0
commit 6d56da03e5
8 changed files with 53 additions and 46 deletions

View File

@ -345,6 +345,10 @@ class IOUVM(BaseVM):
Checks for a valid IOU key in the iourc file (paranoid mode).
"""
license_check = self._manager.config.get_section_config("IOU").getboolean("license_check", False)
if license_check:
return
config = configparser.ConfigParser()
try:
with open(self.iourc_path) as f:
@ -401,10 +405,7 @@ class IOUVM(BaseVM):
if iourc_path and not os.path.isfile(iourc_path):
raise IOUError("A valid iourc file is necessary to start IOU")
license_check = self._manager.config.get_section_config("IOU").getboolean("license_check", False)
if license_check:
yield from self._check_iou_licence()
yield from self._check_iou_licence()
iouyap_path = self.iouyap_path
if not iouyap_path or not os.path.isfile(iouyap_path):
raise IOUError("iouyap is necessary to start IOU")

View File

@ -155,7 +155,8 @@ class VirtualBoxVM(BaseVM):
yield from self.set_adapters(self._adapters)
vm_info = yield from self._get_vm_info()
self._ram = int(vm_info["memory"])
if "memory" in vm_info:
self._ram = int(vm_info["memory"])
@asyncio.coroutine
def start(self):

View File

@ -53,7 +53,7 @@ PROJECT_UPDATE_SCHEMA = {
"properties": {
"name": {
"description": "Project name",
"type": "string",
"type": ["string", "null"],
"minLength": 1
},
"temporary": {

View File

@ -25,44 +25,49 @@ from tests.utils import asyncio_patch
def test_create_project_with_path(server, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
response = server.post("/projects", {"path": str(tmpdir)})
with patch("gns3server.modules.project.Project.is_local", return_value=True):
response = server.post("/projects", {"name": "test", "path": str(tmpdir)})
assert response.status == 201
assert response.json["path"] == str(tmpdir)
assert response.json["name"] == "test"
def test_create_project_without_dir(server):
query = {}
query = {"name": "test"}
response = server.post("/projects", query, example=True)
assert response.status == 201
assert response.json["project_id"] is not None
assert response.json["temporary"] is False
assert response.json["name"] == "test"
def test_create_temporary_project(server):
query = {"temporary": True}
query = {"name": "test", "temporary": True}
response = server.post("/projects", query)
assert response.status == 201
assert response.json["project_id"] is not None
assert response.json["temporary"] is True
assert response.json["name"] == "test"
def test_create_project_with_uuid(server):
query = {"project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
response = server.post("/projects", query)
assert response.status == 201
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
assert response.json["name"] == "test"
def test_show_project(server):
query = {"project_id": "00010203-0405-0607-0809-0a0b0c0d0e02", "temporary": False}
query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e02", "temporary": False}
response = server.post("/projects", query)
assert response.status == 201
response = server.get("/projects/00010203-0405-0607-0809-0a0b0c0d0e02", example=True)
assert len(response.json.keys()) == 4
assert len(response.json.keys()) == 5
assert len(response.json["location"]) > 0
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e02"
assert response.json["temporary"] is False
assert response.json["name"] == "test"
def test_show_project_invalid_uuid(server):
@ -71,10 +76,10 @@ def test_show_project_invalid_uuid(server):
def test_update_temporary_project(server):
query = {"temporary": True}
query = {"name": "test", "temporary": True}
response = server.post("/projects", query)
assert response.status == 201
query = {"temporary": False}
query = {"name": "test", "temporary": False}
response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200
assert response.json["temporary"] is False
@ -82,21 +87,23 @@ def test_update_temporary_project(server):
def test_update_path_project(server, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
response = server.post("/projects", {})
with patch("gns3server.modules.project.Project.is_local", return_value=True):
response = server.post("/projects", {"name": "first_name"})
assert response.status == 201
query = {"path": str(tmpdir)}
assert response.json["name"] == "first_name"
query = {"name": "second_name", "path": str(tmpdir)}
response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200
assert response.json["path"] == str(tmpdir)
assert response.json["name"] == "second_name"
def test_update_path_project_non_local(server, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": False}):
response = server.post("/projects", {})
with patch("gns3server.modules.project.Project.is_local", return_value=False):
response = server.post("/projects", {"name": "first_name"})
assert response.status == 201
query = {"path": str(tmpdir)}
query = {"name": "second_name", "path": str(tmpdir)}
response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 403

View File

@ -44,10 +44,7 @@ def test_router(project, manager):
def test_router_invalid_dynamips_path(project, manager, loop):
config = configparser.ConfigParser()
config.add_section("Dynamips")
config.set("Dynamips", "dynamips_path", "/bin/test_fake")
with patch("gns3server.config.Config", return_value=config):
with patch("gns3server.config.Config.get_section_config", return_value={"dynamips_path": "/bin/test_fake"}):
with pytest.raises(DynamipsError):
router = Router("test", "00010203-0405-0607-0809-0a0b0c0d0e0e", project, manager)
loop.run_until_complete(asyncio.async(router.create()))

View File

@ -85,11 +85,12 @@ def test_vm_invalid_iouyap_path(project, manager, loop):
def test_start(loop, vm, monkeypatch):
with patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running()
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_iou_licence", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running()
def test_start_with_iourc(loop, vm, monkeypatch, tmpdir):
@ -100,13 +101,14 @@ def test_start_with_iourc(loop, vm, monkeypatch, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"iourc_path": fake_file, "iouyap_path": vm.iouyap_path}):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as exec_mock:
loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running()
arsgs, kwargs = exec_mock.call_args
assert kwargs["env"]["IOURC"] == fake_file
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_iou_licence", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as exec_mock:
loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running()
arsgs, kwargs = exec_mock.call_args
assert kwargs["env"]["IOURC"] == fake_file
def test_rename_nvram_file(loop, vm, monkeypatch):

View File

@ -60,7 +60,7 @@ def test_create_vm_new_topology_without_uuid(loop, project, port_manager):
def test_create_vm_old_topology(loop, project, tmpdir, port_manager):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
with patch("gns3server.modules.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")

View File

@ -20,7 +20,6 @@ import os
import asyncio
import pytest
import aiohttp
import shutil
from uuid import uuid4
from unittest.mock import patch
@ -51,7 +50,7 @@ def test_affect_uuid():
def test_path(tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
with patch("gns3server.modules.project.Project.is_local", return_value=True):
p = Project(location=str(tmpdir))
assert p.path == os.path.join(str(tmpdir), p.id)
assert os.path.exists(os.path.join(str(tmpdir), p.id))
@ -60,14 +59,14 @@ def test_path(tmpdir):
def test_init_path(tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
with patch("gns3server.modules.project.Project.is_local", return_value=True):
p = Project(path=str(tmpdir))
assert p.path == str(tmpdir)
def test_changing_path_temporary_flag(tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
with patch("gns3server.modules.project.Project.is_local", return_value=True):
p = Project(temporary=True)
assert os.path.exists(p.path)
assert os.path.exists(os.path.join(p.path, ".gns3_temporary"))
@ -96,13 +95,13 @@ def test_remove_temporary_flag():
def test_changing_location_not_allowed(tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": False}):
with patch("gns3server.modules.project.Project.is_local", return_value=False):
with pytest.raises(aiohttp.web.HTTPForbidden):
p = Project(location=str(tmpdir))
def test_changing_path_not_allowed(tmpdir):
with patch("gns3server.config.Config.getboolean", return_value=False):
with patch("gns3server.modules.project.Project.is_local", return_value=False):
with pytest.raises(aiohttp.web.HTTPForbidden):
p = Project()
p.path = str(tmpdir)
@ -110,11 +109,11 @@ def test_changing_path_not_allowed(tmpdir):
def test_json(tmpdir):
p = Project()
assert p.__json__() == {"location": p.location, "path": p.path, "project_id": p.id, "temporary": False}
assert p.__json__() == {"name": p.name, "location": p.location, "path": p.path, "project_id": p.id, "temporary": False}
def test_vm_working_directory(tmpdir, vm):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
with patch("gns3server.modules.project.Project.is_local", return_value=True):
p = Project(location=str(tmpdir))
assert p.vm_working_directory(vm) == os.path.join(str(tmpdir), p.id, 'project-files', vm.module_name, vm.id)
assert os.path.exists(p.vm_working_directory(vm))