mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-24 15:58:08 +00:00
Fix old project directories renames
This commit is contained in:
parent
3cb7213427
commit
d65617657c
@ -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())
|
||||
|
67
tests/modules/test_manager.py
Normal file
67
tests/modules/test_manager.py
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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"
|
Loading…
Reference in New Issue
Block a user