From 1b68a54234d4f9fd75188f394e9833aedb41cff0 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Sat, 7 Mar 2015 11:56:07 +0100 Subject: [PATCH] Look for qemu images in ~/GNS3/images --- gns3server/modules/qemu/qemu_vm.py | 9 +++++++++ tests/handlers/api/test_qemu.py | 8 ++++---- tests/modules/qemu/test_qemu_vm.py | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/gns3server/modules/qemu/qemu_vm.py b/gns3server/modules/qemu/qemu_vm.py index dc29ced6..89ac9f45 100644 --- a/gns3server/modules/qemu/qemu_vm.py +++ b/gns3server/modules/qemu/qemu_vm.py @@ -32,6 +32,7 @@ from ..adapters.ethernet_adapter import EthernetAdapter from ..nios.nio_udp import NIOUDP from ..base_vm import BaseVM from ...schemas.qemu import QEMU_OBJECT_SCHEMA +from ...config import Config import logging log = logging.getLogger(__name__) @@ -182,6 +183,10 @@ class QemuVM(BaseVM): :param hda_disk_image: QEMU hda disk image path """ + if hda_disk_image[0] != "/": + server_config = Config.instance().get_section_config("Server") + hda_disk_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), hda_disk_image) + log.info("QEMU VM {name} [id={id}] has set the QEMU hda disk image path to {disk_image}".format(name=self._name, id=self._id, disk_image=hda_disk_image)) @@ -205,6 +210,10 @@ class QemuVM(BaseVM): :param hdb_disk_image: QEMU hdb disk image path """ + if hdb_disk_image[0] != "/": + server_config = Config.instance().get_section_config("Server") + hdb_disk_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), hdb_disk_image) + log.info("QEMU VM {name} [id={id}] has set the QEMU hdb disk image path to {disk_image}".format(name=self._name, id=self._id, disk_image=hdb_disk_image)) diff --git a/tests/handlers/api/test_qemu.py b/tests/handlers/api/test_qemu.py index 12a72e87..b06189f5 100644 --- a/tests/handlers/api/test_qemu.py +++ b/tests/handlers/api/test_qemu.py @@ -56,7 +56,7 @@ def test_qemu_create(server, project, base_params): def test_qemu_create_with_params(server, project, base_params): params = base_params params["ram"] = 1024 - params["hda_disk_image"] = "hda" + params["hda_disk_image"] = "/tmp/hda" response = server.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), params, example=True) assert response.status == 201 @@ -64,7 +64,7 @@ def test_qemu_create_with_params(server, project, base_params): assert response.json["name"] == "PC TEST 1" assert response.json["project_id"] == project.id assert response.json["ram"] == 1024 - assert response.json["hda_disk_image"] == "hda" + assert response.json["hda_disk_image"] == "/tmp/hda" def test_qemu_get(server, project, vm): @@ -122,13 +122,13 @@ def test_qemu_update(server, vm, tmpdir, free_console_port, project): "name": "test", "console": free_console_port, "ram": 1024, - "hdb_disk_image": "hdb" + "hdb_disk_image": "/tmp/hdb" } response = server.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True) assert response.status == 200 assert response.json["name"] == "test" assert response.json["console"] == free_console_port - assert response.json["hdb_disk_image"] == "hdb" + assert response.json["hdb_disk_image"] == "/tmp/hdb" assert response.json["ram"] == 1024 diff --git a/tests/modules/qemu/test_qemu_vm.py b/tests/modules/qemu/test_qemu_vm.py index e13bdc27..71c5d70b 100644 --- a/tests/modules/qemu/test_qemu_vm.py +++ b/tests/modules/qemu/test_qemu_vm.py @@ -284,3 +284,21 @@ def test_build_command_without_display(vm, loop, fake_qemu_binary): with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio.async(vm._build_command())) assert "-nographic" in cmd + + +def test_hda_disk_image(vm, tmpdir): + + with patch("gns3server.config.Config.get_section_config", return_value={"images_path": str(tmpdir)}): + vm.hda_disk_image = "/tmp/test" + assert vm.hda_disk_image == "/tmp/test" + vm.hda_disk_image = "test" + assert vm.hda_disk_image == str(tmpdir / "test") + + +def test_hdb_disk_image(vm, tmpdir): + + with patch("gns3server.config.Config.get_section_config", return_value={"images_path": str(tmpdir)}): + vm.hdb_disk_image = "/tmp/test" + assert vm.hdb_disk_image == "/tmp/test" + vm.hdb_disk_image = "test" + assert vm.hdb_disk_image == str(tmpdir / "test")