From e92d9ca5ffec50519645393ed1d6f8e2e986f040 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 28 Jul 2015 16:15:01 +0200 Subject: [PATCH] Raise error if qemu image already exist when creating disk --- gns3server/modules/qemu/__init__.py | 4 +++- tests/modules/qemu/test_qemu_manager.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/gns3server/modules/qemu/__init__.py b/gns3server/modules/qemu/__init__.py index dbd64ec1..608dd15a 100644 --- a/gns3server/modules/qemu/__init__.py +++ b/gns3server/modules/qemu/__init__.py @@ -205,6 +205,8 @@ class Qemu(BaseManager): directory = self.get_images_directory() os.makedirs(directory, exist_ok=True) path = os.path.join(directory, os.path.basename(path)) + if os.path.exists(path): + raise QemuError("Could not create disk image {} already exist".format(path)) command = [qemu_img, "create", "-f", img_format] for option in sorted(options.keys()): @@ -215,4 +217,4 @@ class Qemu(BaseManager): process = yield from asyncio.create_subprocess_exec(*command) yield from process.wait() except (OSError, subprocess.SubprocessError) as e: - raise QemuError("Could create disk image {}:{}".format(path, e)) + raise QemuError("Could not create disk image {}:{}".format(path, e)) diff --git a/tests/modules/qemu/test_qemu_manager.py b/tests/modules/qemu/test_qemu_manager.py index c4ba7e18..fef5f9ee 100644 --- a/tests/modules/qemu/test_qemu_manager.py +++ b/tests/modules/qemu/test_qemu_manager.py @@ -22,6 +22,7 @@ import sys import pytest from gns3server.modules.qemu import Qemu +from gns3server.modules.qemu.qemu_error import QemuError from tests.utils import asyncio_patch from unittest.mock import patch, MagicMock @@ -145,3 +146,17 @@ def test_create_image_relative_path(loop, tmpdir, fake_qemu_img_binary): str(tmpdir / "hda.qcow2"), "100M" ) + + +def test_create_image_exist(loop, tmpdir, fake_qemu_img_binary): + open(str(tmpdir / "hda.qcow2"), "w+").close() + + options = { + "format": "raw", + "size": 100 + } + with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: + with patch("gns3server.modules.qemu.Qemu.get_images_directory", return_value=str(tmpdir)): + with pytest.raises(QemuError): + loop.run_until_complete(asyncio.async(Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options))) + assert not process.called