From 84db3b999671b0f15ef84e93393b277487d8f1e0 Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 22 Feb 2024 12:59:44 +0800 Subject: [PATCH 1/2] Fix not all Docker resources are copied to a writable location --- gns3server/controller/__init__.py | 12 ++++++------ tests/compute/docker/test_docker_vm.py | 26 +++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 34ddf273..adaefe67 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -297,12 +297,12 @@ class Controller: else: for entry in importlib_resources.files('gns3server').joinpath(resource_name).iterdir(): full_path = os.path.join(dst_path, entry.name) - if not os.path.exists(full_path): - if entry.is_file(): - log.debug(f'Installing {resource_name} resource file "{entry.name}" to "{full_path}"') - shutil.copy(str(entry), os.path.join(dst_path, entry.name)) - elif entry.is_dir(): - os.makedirs(full_path, exist_ok=True) + if entry.is_file() and not os.path.exists(full_path): + log.debug(f'Installing {resource_name} resource file "{entry.name}" to "{full_path}"') + shutil.copy(str(entry), os.path.join(dst_path, entry.name)) + elif entry.is_dir(): + os.makedirs(full_path, exist_ok=True) + Controller.install_resource_files(full_path, os.path.join(resource_name, entry.name)) def _install_base_configs(self): """ diff --git a/tests/compute/docker/test_docker_vm.py b/tests/compute/docker/test_docker_vm.py index 097130bf..28839219 100644 --- a/tests/compute/docker/test_docker_vm.py +++ b/tests/compute/docker/test_docker_vm.py @@ -852,7 +852,7 @@ async def test_unpause(vm): mock.assert_called_with("POST", "containers/e90e34656842/unpause") -async def test_start(vm, manager, free_console_port): +async def test_start(vm, manager, free_console_port, tmpdir): assert vm.status != "started" vm.adapters = 1 @@ -880,6 +880,30 @@ async def test_start(vm, manager, free_console_port): assert vm.status == "started" +async def test_resources_installed(vm, manager, tmpdir): + + assert vm.status != "started" + vm.adapters = 1 + + docker_resources_path = os.path.join(tmpdir, "docker", "resources") + os.makedirs(docker_resources_path, exist_ok=True) + manager.resources_path = MagicMock(return_value=docker_resources_path) + + with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"): + with asyncio_patch("gns3server.compute.docker.Docker.query"): + with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge"): + with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42): + with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection"): + with asyncio_patch("gns3server.compute.docker.DockerVM._start_console"): + await vm.start() + + assert vm.status == "started" + assert os.path.exists(os.path.join(docker_resources_path, "init.sh")) + assert os.path.exists(os.path.join(docker_resources_path, "run-cmd.sh")) + assert os.path.exists(os.path.join(docker_resources_path, "bin", "udhcpc")) + assert os.path.exists(os.path.join(docker_resources_path, "etc", "udhcpc", "default.script")) + + async def test_start_namespace_failed(vm, manager, free_console_port): assert vm.status != "started" From 0137688ba75e6c4b26960f66a5f943e066da89d9 Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 22 Feb 2024 13:05:48 +0800 Subject: [PATCH 2/2] Test busybox is installed --- tests/compute/docker/test_docker_vm.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/compute/docker/test_docker_vm.py b/tests/compute/docker/test_docker_vm.py index 28839219..e5271edd 100644 --- a/tests/compute/docker/test_docker_vm.py +++ b/tests/compute/docker/test_docker_vm.py @@ -900,6 +900,7 @@ async def test_resources_installed(vm, manager, tmpdir): assert vm.status == "started" assert os.path.exists(os.path.join(docker_resources_path, "init.sh")) assert os.path.exists(os.path.join(docker_resources_path, "run-cmd.sh")) + assert os.path.exists(os.path.join(docker_resources_path, "bin", "busybox")) assert os.path.exists(os.path.join(docker_resources_path, "bin", "udhcpc")) assert os.path.exists(os.path.join(docker_resources_path, "etc", "udhcpc", "default.script"))