From e108a9e136cff67fe24f3aa60d8f39c77f8372a0 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 10 May 2016 17:58:11 +0200 Subject: [PATCH 01/12] Prepare alpha 1 --- CHANGELOG | 6 ++++++ gns3server/version.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index d99410fb..64feb186 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,11 @@ # Change Log +## 1.5.0a1 10/05/2016 + +* Rebase the qcow2 when starting the VM if needed +* Docker support +* Import / Export topologies + ## 1.4.6 28/04/2016 * More robust save/restore for VirtualBox linked clone VM hard disks. diff --git a/gns3server/version.py b/gns3server/version.py index 8ed6cebe..f4cac907 100644 --- a/gns3server/version.py +++ b/gns3server/version.py @@ -23,5 +23,5 @@ # or negative for a release candidate or beta (after the base version # number has been incremented) -__version__ = "1.5.0dev2" +__version__ = "1.5.0a1" __version_info__ = (1, 5, 0, -99) From ab13f628f744dd780e2ec0025515532f5dbe560b Mon Sep 17 00:00:00 2001 From: grossmj Date: Tue, 10 May 2016 10:10:56 -0600 Subject: [PATCH 02/12] Minor changes on CHANGELOG --- CHANGELOG | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 64feb186..a821d33b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,9 +2,9 @@ ## 1.5.0a1 10/05/2016 -* Rebase the qcow2 when starting the VM if needed +* Rebase Qcow2 disks when starting a VM if needed * Docker support -* Import / Export topologies +* import / export portable projects (.gns3project) ## 1.4.6 28/04/2016 @@ -63,7 +63,7 @@ * Fix error when setting Qemu VM boot to 'cd' (HDD or CD/DVD-ROM) * Fixed the VMware default VM location on Windows, so that it doesn't assume the "Documents" folder is within the %USERPROFILE% folder, and also support Windows Server's folder (which is "My Virtual Machines" instead of "Virtual Machines"). * Improve dynamips startup_config dump -* Dump environnement to server debug log +* Dump environment to server debug log * Fix usage of qemu 0.10 on Windows * Show hostname when the hostname is missing in the iourc.txt From 366e9620dcaac61a16eac8f8f33ce4e0747c08ea Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 10 May 2016 19:09:15 +0200 Subject: [PATCH 03/12] Prepare 1.5.0 alpha 2 --- CHANGELOG | 4 ++++ MANIFEST.in | 1 + gns3server/version.py | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index a821d33b..b68a6d48 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,9 @@ # Change Log +## 1.5.0a2 10/05/2016 + +* Fix distribution on PyPi + ## 1.5.0a1 10/05/2016 * Rebase Qcow2 disks when starting a VM if needed diff --git a/MANIFEST.in b/MANIFEST.in index ff327eea..61bdd940 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,6 +4,7 @@ include INSTALL include LICENSE include MANIFEST.in include tox.ini +include requirements.txt recursive-include tests * recursive-exclude docs * recursive-include gns3server * diff --git a/gns3server/version.py b/gns3server/version.py index f4cac907..e0297eea 100644 --- a/gns3server/version.py +++ b/gns3server/version.py @@ -23,5 +23,5 @@ # or negative for a release candidate or beta (after the base version # number has been incremented) -__version__ = "1.5.0a1" +__version__ = "1.5.0a2" __version_info__ = (1, 5, 0, -99) From f414ce459a1091fac7d6ff7c98d7f16b70318aa8 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 10:03:03 +0200 Subject: [PATCH 04/12] 1.5.0dev3 --- gns3server/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gns3server/version.py b/gns3server/version.py index e0297eea..21ff02ff 100644 --- a/gns3server/version.py +++ b/gns3server/version.py @@ -23,5 +23,5 @@ # or negative for a release candidate or beta (after the base version # number has been incremented) -__version__ = "1.5.0a2" +__version__ = "1.5.0dev3" __version_info__ = (1, 5, 0, -99) From a1bf9cdfd35712d5be8d7632e01c9e17715d3bc0 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 10:06:21 +0200 Subject: [PATCH 05/12] Keep the version.py untouched when merging --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..953bd7d6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +gns3server/version.py merge=ours + From cd836f146eb8fb81f707faa1945a120c497d8127 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 10:56:43 +0200 Subject: [PATCH 06/12] Create the project on compute only when needed --- gns3server/controller/project.py | 7 ++++++- tests/controller/test_project.py | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index f273b8e1..a421d103 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -60,6 +60,9 @@ class Project: self._links = {} self._listeners = set() + # Create the project on demand on the compute node + self._project_created_on_compute = set() + @property def name(self): return self._name @@ -103,7 +106,6 @@ class Project: @asyncio.coroutine def addCompute(self, compute): self._computes.add(compute) - yield from compute.post("/projects", self) @asyncio.coroutine def addVM(self, compute, vm_id, **kwargs): @@ -114,6 +116,9 @@ class Project: """ if vm_id not in self._vms: vm = VM(self, compute, vm_id=vm_id, **kwargs) + if compute not in self._project_created_on_compute: + yield from compute.post("/projects", self) + self._project_created_on_compute.add(compute) yield from vm.create() self._vms[vm.id] = vm return vm diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index c6aaf64b..cb74d959 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -69,6 +69,13 @@ def test_captures_directory(tmpdir): assert os.path.exists(p.captures_directory) +def test_addCompute(async_run): + compute = MagicMock() + project = Project() + async_run(project.addCompute(compute)) + assert compute in project._computes + + def test_addVM(async_run): compute = MagicMock() project = Project() @@ -79,11 +86,12 @@ def test_addVM(async_run): vm = async_run(project.addVM(compute, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"})) - compute.post.assert_called_with('/projects/{}/vpcs/vms'.format(project.id), - data={'vm_id': vm.id, - 'console_type': 'telnet', - 'startup_config': 'test.cfg', - 'name': 'test'}) + compute.post.assert_any_call('/projects/{}/vpcs/vms'.format(project.id), + data={'vm_id': vm.id, + 'console_type': 'telnet', + 'startup_config': 'test.cfg', + 'name': 'test'}) + assert compute in project._project_created_on_compute def test_getVM(async_run): From 4342b4346efb59ac98802fc9bc77850684b3aad1 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 11:22:59 +0200 Subject: [PATCH 07/12] Fix tests --- tests/controller/test_controller.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 8c651834..57f7b52e 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -138,7 +138,6 @@ def test_addProject_with_compute(controller, async_run): controller._computes = {"test1": compute} project1 = async_run(controller.addProject(project_id=uuid1)) - compute.post.assert_called_with("/projects", project1) def test_getProject(controller, async_run): From 390401000fc787f464ed62d12b73f93d8dac52b8 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 15:01:57 +0200 Subject: [PATCH 08/12] A client could not erase the local compute node config in the controller Fix #499 --- gns3server/controller/__init__.py | 23 +++++++++++++++++++++++ tests/controller/test_controller.py | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 48b6ebac..53518b26 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -91,14 +91,35 @@ class Controller: """ Add a server to the dictionnary of computes controlled by GNS3 + :param compute_id: Id of the compute node :param kwargs: See the documentation of Compute """ + + # We dissallow to create from the outside the + if compute_id == 'local': + return self._createLocalCompute() + if compute_id not in self._computes: compute = Compute(compute_id=compute_id, controller=self, **kwargs) self._computes[compute_id] = compute self.save() return self._computes[compute_id] + def _createLocalCompute(self): + """ + Create the local compute node. It's the controller itself + """ + server_config = Config.instance().get_section_config("Server") + self._computes["local"] = Compute( + compute_id="local", + controller=self, + protocol=server_config.get("protocol", "http"), + host=server_config.get("host", "localhost"), + port=server_config.get("port", 3080), + user=server_config.get("user", ""), + password=server_config.get("password", "")) + return self._computes["local"] + @property def computes(self): """ @@ -113,6 +134,8 @@ class Controller: try: return self._computes[compute_id] except KeyError: + if compute_id == "local": + return self._createLocalCompute() raise aiohttp.web.HTTPNotFound(text="Compute ID {} doesn't exist".format(compute_id)) @asyncio.coroutine diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 57f7b52e..dd9adc02 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -108,6 +108,15 @@ def test_getCompute(controller, async_run): assert controller.getCompute("dsdssd") +def test_addComputeLocal(controller, controller_config_path, async_run): + """ + The local node is the controller itself you can not change the informations + """ + Config.instance().set("Server", "local", True) + async_run(controller.addCompute("local", host="example.org")) + assert controller.getCompute("local").host == "localhost" + + def test_addProject(controller, async_run): uuid1 = str(uuid.uuid4()) uuid2 = str(uuid.uuid4()) From c2da568543f0dbb4e892af17a7ddbf71d255452b Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 15:59:32 +0200 Subject: [PATCH 09/12] Protect controller and compute about path outside project directory Fix #503 --- gns3server/compute/project.py | 2 ++ gns3server/controller/project.py | 3 ++- gns3server/utils/path.py | 39 ++++++++++++++++++++++++++++++++ tests/utils/test_path.py | 32 ++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 gns3server/utils/path.py create mode 100644 tests/utils/test_path.py diff --git a/gns3server/compute/project.py b/gns3server/compute/project.py index 1e5276cf..b9567c94 100644 --- a/gns3server/compute/project.py +++ b/gns3server/compute/project.py @@ -29,6 +29,7 @@ from .port_manager import PortManager from .notification_manager import NotificationManager from ..config import Config from ..utils.asyncio import wait_run_in_executor +from ..utils.path import check_path_allowed import logging @@ -122,6 +123,7 @@ class Project: @path.setter def path(self, path): + check_path_allowed(path) if hasattr(self, "_path"): if path != self._path and self.is_local() is False: diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index a421d103..26fe6cb3 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -25,6 +25,7 @@ from .vm import VM from .udp_link import UDPLink from ..notification_queue import NotificationQueue from ..config import Config +from ..utils.path import check_path_allowed class Project: @@ -48,7 +49,6 @@ class Project: raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(project_id)) self._id = project_id - #TODO: Security check if not locale if path is None: location = self._config().get("project_directory", self._get_default_project_directory()) path = os.path.join(location, self._id) @@ -81,6 +81,7 @@ class Project: @path.setter def path(self, path): + check_path_allowed(path) try: os.makedirs(path, exist_ok=True) except OSError as e: diff --git a/gns3server/utils/path.py b/gns3server/utils/path.py new file mode 100644 index 00000000..ebafe9fe --- /dev/null +++ b/gns3server/utils/path.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# +# Copyright (C) 2016 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 . + +import os +import aiohttp + +from ..config import Config + + +def check_path_allowed(path): + """ + If the server is non local raise an error if + the path is outside project directories + + Raise a 403 in case of error + """ + + config = Config.instance().get_section_config("Server") + project_directory = config.get("project_directory") + + if len(os.path.commonprefix([project_directory, path])) == len(project_directory): + return + + if config.getboolean("local") is False: + raise aiohttp.web.HTTPForbidden(text="The path is not allowed") diff --git a/tests/utils/test_path.py b/tests/utils/test_path.py new file mode 100644 index 00000000..20bed40c --- /dev/null +++ b/tests/utils/test_path.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# +# Copyright (C) 2016 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 . + +import pytest +import aiohttp + +from gns3server.utils.path import check_path_allowed + + +def test_check_path_allowed(config, tmpdir): + config.set("Server", "local", False) + config.set("Server", "project_directory", str(tmpdir)) + with pytest.raises(aiohttp.web.HTTPForbidden): + check_path_allowed("/private") + + config.set("Server", "local", True) + check_path_allowed(str(tmpdir / "hello" / "world")) + check_path_allowed("/private") From f4b79af45fd8a9d1311728456bc7307dc9dc052d Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 16:13:32 +0200 Subject: [PATCH 10/12] Move tools for creating self signed certificate to clean repo --- {gns3server/cert_utils => scripts}/create_cert.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {gns3server/cert_utils => scripts}/create_cert.sh (100%) diff --git a/gns3server/cert_utils/create_cert.sh b/scripts/create_cert.sh similarity index 100% rename from gns3server/cert_utils/create_cert.sh rename to scripts/create_cert.sh From a0344576ba606b5f9c88413c88f68ec3de7bc8e0 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 16:31:16 +0200 Subject: [PATCH 11/12] Show version for debug --- gns3server/controller/__init__.py | 2 +- gns3server/controller/compute.py | 16 ++++++++++++++++ gns3server/templates/controller.html | 12 +++++++++++- tests/controller/test_compute.py | 1 + 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 53518b26..d1d2f2eb 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -115,7 +115,7 @@ class Controller: controller=self, protocol=server_config.get("protocol", "http"), host=server_config.get("host", "localhost"), - port=server_config.get("port", 3080), + port=server_config.getint("port", 3080), user=server_config.get("user", ""), password=server_config.get("password", "")) return self._computes["local"] diff --git a/gns3server/controller/compute.py b/gns3server/controller/compute.py index 97cafa41..d64929ab 100644 --- a/gns3server/controller/compute.py +++ b/gns3server/controller/compute.py @@ -51,6 +51,7 @@ class Compute: self._controller = controller self._setAuth(user, password) self._session = aiohttp.ClientSession() + self._version = None # If the compute is local but the compute id is local # it's a configuration issue @@ -71,6 +72,20 @@ class Compute: else: self._auth = None + @property + def version(self): + """ + :returns: Version of compute node (string or None if not connected) + """ + return self._version + + @property + def connected(self): + """ + :returns: True if compute node is connected + """ + return self._connected + @property def id(self): """ @@ -160,6 +175,7 @@ class Compute: if "version" not in response.json: raise aiohttp.web.HTTPConflict(text="The server {} is not a GNS3 server".format(self._id)) + self._version = response.json["version"] if parse_version(__version__)[:2] != parse_version(response.json["version"])[:2]: raise aiohttp.web.HTTPConflict(text="The server {} versions are not compatible {} != {}".format(self._id, __version__, response.json["version"])) diff --git a/gns3server/templates/controller.html b/gns3server/templates/controller.html index a9064f16..871e72ce 100644 --- a/gns3server/templates/controller.html +++ b/gns3server/templates/controller.html @@ -28,11 +28,21 @@ in futur GNS3 versions.

Computes

- + + + + + {% for compute in controller.computes.values() %} + + + + + {% endfor %}
ID + IDVersionConnectedProtocolHostPort
{{compute.id}}{{compute.version}}{{compute.connected}}{{compute.protocol}}{{compute.host}}{{compute.port}}
diff --git a/tests/controller/test_compute.py b/tests/controller/test_compute.py index 63aa8fc7..2a6d7293 100644 --- a/tests/controller/test_compute.py +++ b/tests/controller/test_compute.py @@ -86,6 +86,7 @@ def test_compute_httpQueryNotConnected(compute, async_run): mock.assert_any_call("GET", "https://example.com:84/v2/compute/version", headers={'content-type': 'application/json'}, data=None, auth=None) mock.assert_any_call("POST", "https://example.com:84/v2/compute/projects", data='{"a": "b"}', headers={'content-type': 'application/json'}, auth=None) assert compute._connected + assert compute.version == __version__ def test_compute_httpQueryNotConnectedInvalidVersion(compute, async_run): From 9ccb55206ded9838f9bd04eb1e617797ae39195e Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 18:42:55 +0200 Subject: [PATCH 12/12] Cleanup code for getting the default project directory --- gns3server/compute/project.py | 23 +++------------------- gns3server/controller/project.py | 21 ++------------------ gns3server/utils/path.py | 20 +++++++++++++++++-- tests/compute/test_project.py | 19 +++++------------- tests/conftest.py | 4 ++-- tests/controller/test_project.py | 4 ++-- tests/handlers/api/compute/test_project.py | 4 ++-- tests/utils/test_path.py | 14 +++++++++++-- 8 files changed, 46 insertions(+), 63 deletions(-) diff --git a/gns3server/compute/project.py b/gns3server/compute/project.py index b9567c94..b8da58e6 100644 --- a/gns3server/compute/project.py +++ b/gns3server/compute/project.py @@ -29,7 +29,7 @@ from .port_manager import PortManager from .notification_manager import NotificationManager from ..config import Config from ..utils.asyncio import wait_run_in_executor -from ..utils.path import check_path_allowed +from ..utils.path import check_path_allowed, get_default_project_directory import logging @@ -63,7 +63,7 @@ class Project: self._used_udp_ports = set() if path is None: - location = self._config().get("project_directory", self._get_default_project_directory()) + location = get_default_project_directory() path = os.path.join(location, self._id) try: os.makedirs(path, exist_ok=True) @@ -95,22 +95,6 @@ class Project: return self._config().getboolean("local", False) - @classmethod - def _get_default_project_directory(cls): - """ - Return the default location for the project directory - depending of the operating system - """ - - server_config = Config.instance().get_section_config("Server") - path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects")) - path = os.path.normpath(path) - try: - os.makedirs(path, exist_ok=True) - except OSError as e: - raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) - return path - @property def id(self): @@ -418,8 +402,7 @@ class Project: At startup drop old temporary project. After a crash for example """ - config = Config.instance().get_section_config("Server") - directory = config.get("project_directory", cls._get_default_project_directory()) + directory = get_default_project_directory() if os.path.exists(directory): for project in os.listdir(directory): path = os.path.join(directory, project) diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index 26fe6cb3..92ed2189 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -25,7 +25,7 @@ from .vm import VM from .udp_link import UDPLink from ..notification_queue import NotificationQueue from ..config import Config -from ..utils.path import check_path_allowed +from ..utils.path import check_path_allowed, get_default_project_directory class Project: @@ -50,8 +50,7 @@ class Project: self._id = project_id if path is None: - location = self._config().get("project_directory", self._get_default_project_directory()) - path = os.path.join(location, self._id) + path = os.path.join(get_default_project_directory(), self._id) self.path = path self._temporary = temporary @@ -205,22 +204,6 @@ class Project: for listener in self._listeners: listener.put_nowait((action, event, kwargs)) - @classmethod - def _get_default_project_directory(cls): - """ - Return the default location for the project directory - depending of the operating system - """ - - server_config = Config.instance().get_section_config("Server") - path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects")) - path = os.path.normpath(path) - try: - os.makedirs(path, exist_ok=True) - except OSError as e: - raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) - return path - def __json__(self): return { diff --git a/gns3server/utils/path.py b/gns3server/utils/path.py index ebafe9fe..94a1ee64 100644 --- a/gns3server/utils/path.py +++ b/gns3server/utils/path.py @@ -21,6 +21,22 @@ import aiohttp from ..config import Config +def get_default_project_directory(): + """ + Return the default location for the project directory + depending of the operating system + """ + + server_config = Config.instance().get_section_config("Server") + path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects")) + path = os.path.normpath(path) + try: + os.makedirs(path, exist_ok=True) + except OSError as e: + raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) + return path + + def check_path_allowed(path): """ If the server is non local raise an error if @@ -30,10 +46,10 @@ def check_path_allowed(path): """ config = Config.instance().get_section_config("Server") - project_directory = config.get("project_directory") + project_directory = get_default_project_directory() if len(os.path.commonprefix([project_directory, path])) == len(project_directory): return - if config.getboolean("local") is False: + if "local" in config and config.getboolean("local") is False: raise aiohttp.web.HTTPForbidden(text="The path is not allowed") diff --git a/tests/compute/test_project.py b/tests/compute/test_project.py index 8f171308..638c5196 100644 --- a/tests/compute/test_project.py +++ b/tests/compute/test_project.py @@ -69,10 +69,10 @@ def test_clean_tmp_directory(async_run): def test_path(tmpdir): - directory = Config.instance().get_section_config("Server").get("project_directory") + directory = Config.instance().get_section_config("Server").get("projects_path") with patch("gns3server.compute.project.Project.is_local", return_value=True): - with patch("gns3server.compute.project.Project._get_default_project_directory", return_value=directory): + with patch("gns3server.utils.path.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)) @@ -124,7 +124,7 @@ def test_json(tmpdir): def test_vm_working_directory(tmpdir, vm): - directory = Config.instance().get_section_config("Server").get("project_directory") + directory = Config.instance().get_section_config("Server").get("projects_path") with patch("gns3server.compute.project.Project.is_local", return_value=True): p = Project(project_id=str(uuid4())) @@ -211,15 +211,6 @@ def test_project_close_temporary_project(loop, manager): assert os.path.exists(directory) is False -def test_get_default_project_directory(monkeypatch): - - monkeypatch.undo() - 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) - - def test_clean_project_directory(tmpdir): # A non anonymous project with uuid. @@ -237,7 +228,7 @@ def test_clean_project_directory(tmpdir): with open(str(tmp), 'w+') as f: f.write("1") - with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}): + with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}): Project.clean_project_directory() assert os.path.exists(str(project1)) @@ -247,7 +238,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)}): + with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}): project = Project(project_id=str(uuid4())) path = project.path os.makedirs(os.path.join(path, "vm-1", "dynamips")) diff --git a/tests/conftest.py b/tests/conftest.py index 85f2c194..7c0de0bd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -203,7 +203,7 @@ def run_around_tests(monkeypatch, port_manager, controller, config): port_manager._instance = port_manager os.makedirs(os.path.join(tmppath, 'projects')) - config.set("Server", "project_directory", os.path.join(tmppath, 'projects')) + config.set("Server", "projects_path", os.path.join(tmppath, 'projects')) config.set("Server", "images_path", os.path.join(tmppath, 'images')) config.set("Server", "auth", False) config.set("Server", "controller", True) @@ -216,7 +216,7 @@ def run_around_tests(monkeypatch, port_manager, controller, config): # Force turn off KVM because it's not available on CI config.set("Qemu", "enable_kvm", False) - monkeypatch.setattr("gns3server.compute.project.Project._get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects')) + monkeypatch.setattr("gns3server.utils.path.get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects')) # Force sys.platform to the original value. Because it seem not be restore correctly at each tests sys.platform = sys.original_platform diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index cb74d959..c2d524ec 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -43,9 +43,9 @@ def test_json(tmpdir): def test_path(tmpdir): - directory = Config.instance().get_section_config("Server").get("project_directory") + directory = Config.instance().get_section_config("Server").get("projects_path") - with patch("gns3server.compute.project.Project._get_default_project_directory", return_value=directory): + with patch("gns3server.utils.path.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)) diff --git a/tests/handlers/api/compute/test_project.py b/tests/handlers/api/compute/test_project.py index 4e7a8e14..1b8ea231 100644 --- a/tests/handlers/api/compute/test_project.py +++ b/tests/handlers/api/compute/test_project.py @@ -201,7 +201,7 @@ def test_close_project_invalid_uuid(http_compute): def test_get_file(http_compute, tmpdir): - with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}): + with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}): project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b") with open(os.path.join(project.path, "hello"), "w+") as f: @@ -220,7 +220,7 @@ def test_get_file(http_compute, tmpdir): def test_stream_file(http_compute, tmpdir): - with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}): + with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}): project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b") with open(os.path.join(project.path, "hello"), "w+") as f: diff --git a/tests/utils/test_path.py b/tests/utils/test_path.py index 20bed40c..08c7042f 100644 --- a/tests/utils/test_path.py +++ b/tests/utils/test_path.py @@ -15,18 +15,28 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import os import pytest import aiohttp -from gns3server.utils.path import check_path_allowed +from gns3server.utils.path import check_path_allowed, get_default_project_directory def test_check_path_allowed(config, tmpdir): config.set("Server", "local", False) - config.set("Server", "project_directory", str(tmpdir)) + config.set("Server", "projects_path", str(tmpdir)) with pytest.raises(aiohttp.web.HTTPForbidden): check_path_allowed("/private") config.set("Server", "local", True) check_path_allowed(str(tmpdir / "hello" / "world")) check_path_allowed("/private") + + +def test_get_default_project_directory(config): + + config.clear() + + path = os.path.normpath(os.path.expanduser("~/GNS3/projects")) + assert get_default_project_directory() == path + assert os.path.exists(path)