From f12d3f07f7844f7d94b014365bd8ba2128d44466 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 25 Feb 2015 18:23:41 +0100 Subject: [PATCH] Drop the old -files in the project --- gns3server/modules/base_manager.py | 16 ++++++++++++++-- tests/modules/test_manager.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index acf9c1ab..4bafe78c 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -158,7 +158,7 @@ class BaseManager: project = ProjectManager.instance().get_project(project_id) - # If it's not an UUID + # If it's not an UUID, old topology if vm_id and (isinstance(vm_id, int) or len(vm_id) != 36): legacy_id = int(vm_id) vm_id = str(uuid4()) @@ -173,7 +173,19 @@ class BaseManager: 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)) + raise aiohttp.web.HTTPInternalServerError(text="Could not move VM working directory: {} to {} {}".format(vm_working_dir, new_vm_working_dir, e)) + + if os.listdir(module_path) == []: + try: + os.rmdir(module_path) + except OSError as e: + raise aiohttp.web.HTTPInternalServerError(text="Could not delete {}: {}".format(module_path, e)) + + if os.listdir(project_files_dir) == []: + try: + os.rmdir(project_files_dir) + except OSError as e: + raise aiohttp.web.HTTPInternalServerError(text="Could not delete {}: {}".format(project_files_dir, e)) if not vm_id: vm_id = str(uuid4()) diff --git a/tests/modules/test_manager.py b/tests/modules/test_manager.py index b38eb804..54996169 100644 --- a/tests/modules/test_manager.py +++ b/tests/modules/test_manager.py @@ -62,6 +62,35 @@ def test_create_vm_old_topology(loop, project, tmpdir, port_manager): vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id)) assert len(vm.id) == 36 + assert os.path.exists(os.path.join(project_dir, "testold-files")) is False + + 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" + + +def test_create_vm_old_topology_with_garbage_in_project_dir(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") + with open(os.path.join(os.path.join(project_dir, "testold-files"), "crash.log"), "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 + + assert os.path.exists(os.path.join(project_dir, "testold-files")) is True + 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"