From d65617657c03dc6243fd793906f80fc31907b246 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 18 Feb 2015 16:13:09 +0100 Subject: [PATCH] Fix old project directories renames --- gns3server/modules/base_manager.py | 19 ++++----- tests/modules/test_manager.py | 67 ++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 tests/modules/test_manager.py diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index 4013dad3..d379a59b 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -158,23 +158,22 @@ class BaseManager: project = ProjectManager.instance().get_project(project_id) - try: - if vm_id and hasattr(self, "get_legacy_vm_workdir_name"): + # If it's not an UUID + if vm_id and (isinstance(vm_id, int) or len(vm_id) != 36): + legacy_id = int(vm_id) + vm_id = str(uuid4()) + if hasattr(self, "get_legacy_vm_workdir_name"): # move old project VM files to a new location - legacy_id = int(vm_id) - project_dir = os.path.dirname(project.path) - project_name = os.path.basename(project_dir) - project_files_dir = os.path.join(project_dir, "{}-files".format(project_name)) + + project_name = os.path.basename(project.path) + project_files_dir = os.path.join(project.path, "{}-files".format(project_name)) module_path = os.path.join(project_files_dir, self.module_name.lower()) vm_working_dir = os.path.join(module_path, self.get_legacy_vm_workdir_name(legacy_id)) - vm_id = str(uuid4()) - new_vm_working_dir = os.path.join(project.path, self.module_name.lower(), vm_id) + new_vm_working_dir = os.path.join(project.path, "project-files", self.module_name.lower(), vm_id) try: yield from wait_run_in_executor(shutil.move, vm_working_dir, new_vm_working_dir) except OSError as e: raise aiohttp.web.HTTPInternalServerError(text="Could not move VM working directory: {}".format(e)) - except ValueError: - pass if not vm_id: vm_id = str(uuid4()) diff --git a/tests/modules/test_manager.py b/tests/modules/test_manager.py new file mode 100644 index 00000000..b38eb804 --- /dev/null +++ b/tests/modules/test_manager.py @@ -0,0 +1,67 @@ +# -*- 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 . + +import uuid +import os +import pytest +from unittest.mock import patch + + +from gns3server.modules.vpcs import VPCS + + +def test_create_vm_new_topology(loop, project, port_manager): + + VPCS._instance = None + vpcs = VPCS.instance() + vpcs.port_manager = port_manager + vm_id = str(uuid.uuid4()) + vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id)) + assert vm in project.vms + + +def test_create_vm_new_topology_without_uuid(loop, project, port_manager): + + VPCS._instance = None + vpcs = VPCS.instance() + vpcs.port_manager = port_manager + vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, None)) + assert vm in project.vms + assert len(vm.id) == 36 + + +def test_create_vm_old_topology(loop, project, tmpdir, port_manager): + + with patch("gns3server.config.Config.get_section_config", return_value={"local": True}): + # Create an old topology directory + project_dir = str(tmpdir / "testold") + vm_dir = os.path.join(project_dir, "testold-files", "vpcs", "pc-1") + project.path = project_dir + os.makedirs(vm_dir, exist_ok=True) + with open(os.path.join(vm_dir, "startup.vpc"), "w+") as f: + f.write("1") + + VPCS._instance = None + vpcs = VPCS.instance() + vpcs.port_manager = port_manager + vm_id = 1 + vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id)) + assert len(vm.id) == 36 + + vm_dir = os.path.join(project_dir, "project-files", "vpcs", vm.id) + with open(os.path.join(vm_dir, "startup.vpc")) as f: + assert f.read() == "1"