mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-06 15:21:13 +02:00
Close, commit and delete supported for projects in controller
This commit is contained in:
19 files changed
+560
-117
No files matched your search
@@ -240,3 +240,11 @@ def linux_platform():
|
||||
sys.platform = "linuxdebian"
|
||||
yield
|
||||
sys.plaform = old_platform
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def async_run(loop):
|
||||
"""
|
||||
Shortcut for running in asyncio loop
|
||||
"""
|
||||
return lambda x: loop.run_until_complete(asyncio.async(x))
|
||||
Whitespace-only changes.
@@ -16,9 +16,14 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import pytest
|
||||
import uuid
|
||||
import aiohttp
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
from gns3server.controller import Controller
|
||||
from gns3server.controller.hypervisor import Hypervisor
|
||||
from gns3server.controller.project import Project
|
||||
from gns3server.config import Config
|
||||
|
||||
|
||||
@@ -38,3 +43,49 @@ def test_addHypervisor(controller):
|
||||
assert len(controller.hypervisors) == 1
|
||||
controller.addHypervisor(Hypervisor("test2"))
|
||||
assert len(controller.hypervisors) == 2
|
||||
|
||||
|
||||
def test_addProject(controller, async_run):
|
||||
uuid1 = str(uuid.uuid4())
|
||||
project1 = Project(project_id=uuid1)
|
||||
uuid2 = str(uuid.uuid4())
|
||||
|
||||
async_run(controller.addProject(project1))
|
||||
assert len(controller.projects) == 1
|
||||
async_run(controller.addProject(Project(project_id=uuid1)))
|
||||
assert len(controller.projects) == 1
|
||||
async_run(controller.addProject(Project(project_id=uuid2)))
|
||||
assert len(controller.projects) == 2
|
||||
|
||||
|
||||
def test_removeProject(controller, async_run):
|
||||
uuid1 = str(uuid.uuid4())
|
||||
project1 = Project(project_id=uuid1)
|
||||
|
||||
async_run(controller.addProject(project1))
|
||||
assert len(controller.projects) == 1
|
||||
|
||||
controller.removeProject(project1)
|
||||
assert len(controller.projects) == 0
|
||||
|
||||
|
||||
def test_addProject_with_hypervisor(controller, async_run):
|
||||
uuid1 = str(uuid.uuid4())
|
||||
project1 = Project(project_id=uuid1)
|
||||
|
||||
hypervisor = Hypervisor("test1")
|
||||
hypervisor.post = MagicMock()
|
||||
controller.addHypervisor(hypervisor)
|
||||
|
||||
async_run(controller.addProject(project1))
|
||||
hypervisor.post.assert_called_with("/projects", project1)
|
||||
|
||||
|
||||
def test_getProject(controller, async_run):
|
||||
uuid1 = str(uuid.uuid4())
|
||||
project = Project(project_id=uuid1)
|
||||
|
||||
async_run(controller.addProject(project))
|
||||
assert controller.getProject(uuid1) == project
|
||||
with pytest.raises(aiohttp.web.HTTPNotFound):
|
||||
assert controller.getProject("dsdssd")
|
||||
@@ -17,10 +17,13 @@
|
||||
|
||||
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
import json
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from gns3server.controller.project import Project
|
||||
from gns3server.controller.hypervisor import Hypervisor, HypervisorError
|
||||
from gns3server.version import __version__
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -46,6 +49,25 @@ def test_hypervisor_local(hypervisor):
|
||||
s = Hypervisor("test")
|
||||
|
||||
|
||||
def test_hypervisor_httpQuery(hypervisor, 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'})
|
||||
|
||||
|
||||
def test_hypervisor_httpQuery_project(hypervisor, 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'})
|
||||
|
||||
|
||||
def test_json(hypervisor):
|
||||
assert hypervisor.__json__() == {
|
||||
"hypervisor_id": "my_hypervisor_id",
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- 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/>.
|
||||
|
||||
|
||||
from gns3server.controller.project import Project
|
||||
|
||||
|
||||
def test_affect_uuid():
|
||||
p = Project()
|
||||
assert len(p.id) == 36
|
||||
|
||||
p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f')
|
||||
assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f'
|
||||
|
||||
|
||||
def test_json(tmpdir):
|
||||
p = Project()
|
||||
assert p.__json__() == {"name": p.name, "project_id": p.id, "temporary": False, "path": None}
|
||||
@@ -0,0 +1,109 @@
|
||||
# -*- 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/>.
|
||||
|
||||
"""
|
||||
This test suite check /project endpoint
|
||||
"""
|
||||
|
||||
import uuid
|
||||
import os
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import pytest
|
||||
|
||||
|
||||
from unittest.mock import patch
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
from gns3server.handlers.api.controller.project_handler import ProjectHandler
|
||||
from gns3server.controller import Controller
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project(http_controller):
|
||||
u = str(uuid.uuid4())
|
||||
query = {"name": "test", "project_id": u}
|
||||
response = http_controller.post("/projects", query)
|
||||
return Controller.instance().getProject(u)
|
||||
|
||||
|
||||
def test_create_project_with_path(http_controller, tmpdir):
|
||||
with asyncio_patch("gns3server.controller.Controller.addProject") as mock:
|
||||
response = http_controller.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"
|
||||
assert mock.called
|
||||
|
||||
|
||||
def test_create_project_without_dir(http_controller):
|
||||
query = {"name": "test", "project_id": "10010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_controller.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_controller):
|
||||
query = {"name": "test", "temporary": True, "project_id": "20010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_controller.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_controller):
|
||||
query = {"name": "test", "project_id": "30010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_controller.post("/projects", query)
|
||||
assert response.status == 201
|
||||
assert response.json["project_id"] == "30010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
assert response.json["name"] == "test"
|
||||
|
||||
|
||||
def test_commit_project(http_controller, project):
|
||||
with asyncio_patch("gns3server.controller.project.Project.commit", return_value=True) as mock:
|
||||
response = http_controller.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_controller):
|
||||
response = http_controller.post("/projects/{project_id}/commit".format(project_id=uuid.uuid4()))
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_delete_project(http_controller, project):
|
||||
with asyncio_patch("gns3server.controller.project.Project.delete", return_value=True) as mock:
|
||||
response = http_controller.delete("/projects/{project_id}".format(project_id=project.id), example=True)
|
||||
assert response.status == 204
|
||||
assert mock.called
|
||||
assert project not in Controller.instance().projects
|
||||
|
||||
|
||||
def test_delete_project_invalid_uuid(http_controller):
|
||||
response = http_controller.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_close_project(http_controller, project):
|
||||
with asyncio_patch("gns3server.controller.project.Project.close", return_value=True) as mock:
|
||||
response = http_controller.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
|
||||
assert response.status == 204
|
||||
assert mock.called
|
||||
assert project not in Controller.instance().projects
|
||||
@@ -33,74 +33,72 @@ from gns3server.hypervisor.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)})
|
||||
response = http_hypervisor.post("/projects", {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"})
|
||||
assert response.status == 201
|
||||
assert response.json["path"] == str(tmpdir)
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
|
||||
|
||||
def test_create_project_without_dir(http_hypervisor):
|
||||
query = {"name": "test"}
|
||||
query = {"name": "test", "project_id": "10010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_hypervisor.post("/projects", query, example=True)
|
||||
assert response.status == 201
|
||||
assert response.json["project_id"] is not None
|
||||
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):
|
||||
query = {"name": "test", "temporary": True}
|
||||
query = {"name": "test", "temporary": True, "project_id": "20010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
assert response.status == 201
|
||||
assert response.json["project_id"] is not None
|
||||
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):
|
||||
query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
query = {"name": "test", "project_id": "30010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
assert response.status == 201
|
||||
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
assert response.json["project_id"] == "30010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
assert response.json["name"] == "test"
|
||||
|
||||
|
||||
def test_show_project(http_hypervisor):
|
||||
query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e02", "temporary": False}
|
||||
query = {"name": "test", "project_id": "40010203-0405-0607-0809-0a0b0c0d0e02", "temporary": False}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
assert response.status == 201
|
||||
response = http_hypervisor.get("/projects/00010203-0405-0607-0809-0a0b0c0d0e02", example=True)
|
||||
assert len(response.json.keys()) == 5
|
||||
assert len(response.json["location"]) > 0
|
||||
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e02"
|
||||
response = http_hypervisor.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/00010203-0405-0607-0809-0a0b0c0d0e42")
|
||||
response = http_hypervisor.get("/projects/50010203-0405-0607-0809-0a0b0c0d0e42")
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_list_projects(http_hypervisor):
|
||||
ProjectManager.instance()._projects = {}
|
||||
|
||||
query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
query = {"name": "test", "project_id": "51010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
assert response.status == 201
|
||||
query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0b"}
|
||||
query = {"name": "test", "project_id": "52010203-0405-0607-0809-0a0b0c0d0e0b"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
assert response.status == 201
|
||||
|
||||
response = http_hypervisor.get("/projects", example=True)
|
||||
assert response.status == 200
|
||||
print(response.json)
|
||||
assert len(response.json) == 2
|
||||
assert response.json[0]["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0b" or response.json[1]["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0b"
|
||||
assert "51010203-0405-0607-0809-0a0b0c0d0e0f" in [p["project_id"] for p in response.json]
|
||||
|
||||
|
||||
def test_update_temporary_project(http_hypervisor):
|
||||
query = {"name": "test", "temporary": True}
|
||||
query = {"name": "test", "temporary": True, "project_id": "60010203-0405-0607-0809-0a0b0c0d0e0b"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
assert response.status == 201
|
||||
query = {"name": "test", "temporary": False}
|
||||
@@ -115,13 +113,12 @@ def test_update_path_project_temporary(http_hypervisor, tmpdir):
|
||||
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})
|
||||
response = http_hypervisor.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)
|
||||
assert response.status == 200
|
||||
assert response.json["path"] == str(tmpdir / "b")
|
||||
assert response.json["name"] == "second_name"
|
||||
|
||||
assert not os.path.exists(str(tmpdir / "a"))
|
||||
@@ -134,13 +131,12 @@ def test_update_path_project_non_temporary(http_hypervisor, tmpdir):
|
||||
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")})
|
||||
response = http_hypervisor.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)
|
||||
assert response.status == 200
|
||||
assert response.json["path"] == str(tmpdir / "b")
|
||||
assert response.json["name"] == "second_name"
|
||||
|
||||
assert os.path.exists(str(tmpdir / "a"))
|
||||
@@ -150,7 +146,7 @@ def test_update_path_project_non_temporary(http_hypervisor, tmpdir):
|
||||
def test_update_path_project_non_local(http_hypervisor, tmpdir):
|
||||
|
||||
with patch("gns3server.hypervisor.project.Project.is_local", return_value=False):
|
||||
response = http_hypervisor.post("/projects", {"name": "first_name"})
|
||||
response = http_hypervisor.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)
|
||||
@@ -247,7 +243,7 @@ def test_list_files(http_hypervisor, project):
|
||||
def test_get_file(http_hypervisor, tmpdir):
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
||||
project = ProjectManager.instance().create_project()
|
||||
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
|
||||
|
||||
with open(os.path.join(project.path, "hello"), "w+") as f:
|
||||
f.write("world")
|
||||
|
||||
@@ -58,8 +58,8 @@ def test_get_application_id_multiple_project(loop, iou):
|
||||
vm1_id = str(uuid.uuid4())
|
||||
vm2_id = str(uuid.uuid4())
|
||||
vm3_id = str(uuid.uuid4())
|
||||
project1 = ProjectManager.instance().create_project()
|
||||
project2 = ProjectManager.instance().create_project()
|
||||
project1 = ProjectManager.instance().create_project(project_id=str(uuid.uuid4()))
|
||||
project2 = ProjectManager.instance().create_project(project_id=str(uuid.uuid4()))
|
||||
loop.run_until_complete(iou.create_vm("PC 1", project1.id, vm1_id))
|
||||
loop.run_until_complete(iou.create_vm("PC 2", project1.id, vm2_id))
|
||||
loop.run_until_complete(iou.create_vm("PC 2", project2.id, vm3_id))
|
||||
|
||||
@@ -18,14 +18,16 @@
|
||||
import aiohttp
|
||||
import pytest
|
||||
import sys
|
||||
import uuid
|
||||
from unittest.mock import patch
|
||||
|
||||
from gns3server.hypervisor.port_manager import PortManager
|
||||
from gns3server.hypervisor.project import Project
|
||||
|
||||
|
||||
def test_reserve_tcp_port():
|
||||
pm = PortManager()
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
pm.reserve_tcp_port(2001, project)
|
||||
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
|
||||
port = pm.reserve_tcp_port(2001, project)
|
||||
@@ -35,7 +37,7 @@ def test_reserve_tcp_port():
|
||||
|
||||
def test_reserve_tcp_port_outside_range():
|
||||
pm = PortManager()
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
|
||||
port = pm.reserve_tcp_port(80, project)
|
||||
assert port != 80
|
||||
@@ -49,7 +51,7 @@ def test_reserve_tcp_port_already_used_by_another_program():
|
||||
"""
|
||||
|
||||
pm = PortManager()
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
with patch("gns3server.hypervisor.port_manager.PortManager._check_port") as mock_check:
|
||||
|
||||
def execute_mock(host, port, *args):
|
||||
@@ -73,7 +75,7 @@ def test_reserve_tcp_port_already_used():
|
||||
"""
|
||||
|
||||
pm = PortManager()
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
with patch("gns3server.hypervisor.port_manager.PortManager._check_port") as mock_check:
|
||||
|
||||
def execute_mock(host, port, *args):
|
||||
@@ -92,7 +94,7 @@ def test_reserve_tcp_port_already_used():
|
||||
|
||||
def test_reserve_udp_port():
|
||||
pm = PortManager()
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
pm.reserve_udp_port(10000, project)
|
||||
with pytest.raises(aiohttp.web.HTTPConflict):
|
||||
pm.reserve_udp_port(10000, project)
|
||||
@@ -100,14 +102,14 @@ def test_reserve_udp_port():
|
||||
|
||||
def test_reserve_udp_port_outside_range():
|
||||
pm = PortManager()
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
with pytest.raises(aiohttp.web.HTTPConflict):
|
||||
pm.reserve_udp_port(80, project)
|
||||
|
||||
|
||||
def test_release_udp_port():
|
||||
pm = PortManager()
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
pm.reserve_udp_port(10000, project)
|
||||
pm.release_udp_port(10000, project)
|
||||
pm.reserve_udp_port(10000, project)
|
||||
|
||||
@@ -26,6 +26,7 @@ from unittest.mock import patch
|
||||
from tests.utils import asyncio_patch
|
||||
from gns3server.hypervisor.project import Project
|
||||
from gns3server.hypervisor.vpcs import VPCS, VPCSVM
|
||||
from gns3server.config import Config
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
@@ -42,32 +43,33 @@ def vm(project, manager, loop):
|
||||
|
||||
|
||||
def test_affect_uuid():
|
||||
p = Project()
|
||||
assert len(p.id) == 36
|
||||
|
||||
p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f')
|
||||
assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f'
|
||||
|
||||
|
||||
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):
|
||||
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))
|
||||
assert not os.path.exists(os.path.join(p.path, ".gns3_temporary"))
|
||||
with patch("gns3server.hypervisor.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))
|
||||
assert not os.path.exists(os.path.join(p.path, ".gns3_temporary"))
|
||||
|
||||
|
||||
def test_init_path(tmpdir):
|
||||
|
||||
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
|
||||
p = Project(path=str(tmpdir))
|
||||
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):
|
||||
p = Project(temporary=True)
|
||||
p = Project(temporary=True, project_id=str(uuid4()))
|
||||
assert os.path.exists(p.path)
|
||||
original_path = p.path
|
||||
assert os.path.exists(os.path.join(p.path, ".gns3_temporary"))
|
||||
@@ -76,53 +78,49 @@ def test_changing_path_temporary_flag(tmpdir):
|
||||
|
||||
|
||||
def test_temporary_path():
|
||||
p = Project(temporary=True)
|
||||
p = Project(temporary=True, project_id=str(uuid4()))
|
||||
assert os.path.exists(p.path)
|
||||
assert os.path.exists(os.path.join(p.path, ".gns3_temporary"))
|
||||
|
||||
|
||||
def test_remove_temporary_flag():
|
||||
p = Project(temporary=True)
|
||||
p = Project(temporary=True, project_id=str(uuid4()))
|
||||
assert os.path.exists(p.path)
|
||||
assert os.path.exists(os.path.join(p.path, ".gns3_temporary"))
|
||||
p.temporary = False
|
||||
assert not os.path.exists(os.path.join(p.path, ".gns3_temporary"))
|
||||
|
||||
|
||||
def test_changing_location_not_allowed(tmpdir):
|
||||
with patch("gns3server.hypervisor.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.hypervisor.project.Project.is_local", return_value=False):
|
||||
with pytest.raises(aiohttp.web.HTTPForbidden):
|
||||
p = Project()
|
||||
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 pytest.raises(aiohttp.web.HTTPForbidden):
|
||||
p = Project()
|
||||
p = Project(project_id=str(uuid4()))
|
||||
p.path = str(tmpdir / "project\"53")
|
||||
|
||||
|
||||
def test_json(tmpdir):
|
||||
p = Project()
|
||||
assert p.__json__() == {"name": p.name, "location": p.location, "path": p.path, "project_id": p.id, "temporary": False}
|
||||
p = Project(project_id=str(uuid4()))
|
||||
assert p.__json__() == {"name": p.name, "project_id": p.id, "temporary": False}
|
||||
|
||||
|
||||
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):
|
||||
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)
|
||||
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))
|
||||
|
||||
|
||||
def test_mark_vm_for_destruction(vm):
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid4()))
|
||||
project.add_vm(vm)
|
||||
project.mark_vm_for_destruction(vm)
|
||||
assert len(project._vms_to_destroy) == 1
|
||||
@@ -130,7 +128,7 @@ def test_mark_vm_for_destruction(vm):
|
||||
|
||||
|
||||
def test_commit(manager, loop):
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid4()))
|
||||
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
|
||||
project.add_vm(vm)
|
||||
directory = project.vm_working_directory(vm)
|
||||
@@ -144,7 +142,7 @@ def test_commit(manager, loop):
|
||||
|
||||
|
||||
def test_commit_permission_issue(manager, loop):
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid4()))
|
||||
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
|
||||
project.add_vm(vm)
|
||||
directory = project.vm_working_directory(vm)
|
||||
@@ -158,7 +156,7 @@ def test_commit_permission_issue(manager, loop):
|
||||
|
||||
|
||||
def test_project_delete(loop):
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid4()))
|
||||
directory = project.path
|
||||
assert os.path.exists(directory)
|
||||
loop.run_until_complete(asyncio.async(project.delete()))
|
||||
@@ -166,7 +164,7 @@ def test_project_delete(loop):
|
||||
|
||||
|
||||
def test_project_delete_permission_issue(loop):
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid4()))
|
||||
directory = project.path
|
||||
assert os.path.exists(directory)
|
||||
os.chmod(directory, 0)
|
||||
@@ -176,7 +174,7 @@ def test_project_delete_permission_issue(loop):
|
||||
|
||||
|
||||
def test_project_add_vm(manager):
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid4()))
|
||||
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
|
||||
project.add_vm(vm)
|
||||
assert len(project.vms) == 1
|
||||
@@ -193,7 +191,7 @@ def test_project_close(loop, vm, project):
|
||||
def test_project_close_temporary_project(loop, manager):
|
||||
"""A temporary project is deleted when closed"""
|
||||
|
||||
project = Project(temporary=True)
|
||||
project = Project(temporary=True, project_id=str(uuid4()))
|
||||
directory = project.path
|
||||
assert os.path.exists(directory)
|
||||
loop.run_until_complete(asyncio.async(project.close()))
|
||||
@@ -203,7 +201,7 @@ def test_project_close_temporary_project(loop, manager):
|
||||
def test_get_default_project_directory(monkeypatch):
|
||||
|
||||
monkeypatch.undo()
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid4()))
|
||||
path = os.path.normpath(os.path.expanduser("~/GNS3/projects"))
|
||||
assert project._get_default_project_directory() == path
|
||||
assert os.path.exists(path)
|
||||
@@ -237,7 +235,7 @@ def test_clean_project_directory(tmpdir):
|
||||
def test_list_files(tmpdir, loop):
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
||||
project = Project()
|
||||
project = Project(project_id=str(uuid4()))
|
||||
path = project.path
|
||||
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
|
||||
with open(os.path.join(path, "vm-1", "dynamips", "test.bin"), "w+") as f:
|
||||
|
||||
@@ -51,8 +51,8 @@ def test_get_mac_id_multiple_project(loop, port_manager):
|
||||
vm1_id = str(uuid.uuid4())
|
||||
vm2_id = str(uuid.uuid4())
|
||||
vm3_id = str(uuid.uuid4())
|
||||
project1 = ProjectManager.instance().create_project()
|
||||
project2 = ProjectManager.instance().create_project()
|
||||
project1 = ProjectManager.instance().create_project(project_id=str(uuid.uuid4()))
|
||||
project2 = ProjectManager.instance().create_project(project_id=str(uuid.uuid4()))
|
||||
loop.run_until_complete(vpcs.create_vm("PC 1", project1.id, vm1_id))
|
||||
loop.run_until_complete(vpcs.create_vm("PC 2", project1.id, vm2_id))
|
||||
loop.run_until_complete(vpcs.create_vm("PC 2", project2.id, vm3_id))
|
||||
|
||||
Reference in new issue
Block a user