2015-02-20 13:39:13 +00:00
|
|
|
# -*- 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 os
|
|
|
|
import stat
|
2015-04-27 21:12:13 +00:00
|
|
|
import sys
|
2015-07-27 17:18:36 +00:00
|
|
|
import pytest
|
2016-01-04 15:30:06 +00:00
|
|
|
import platform
|
2015-02-20 13:39:13 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
from gns3server.compute.qemu import Qemu
|
|
|
|
from gns3server.compute.qemu.qemu_error import QemuError
|
2015-02-20 13:39:13 +00:00
|
|
|
from tests.utils import asyncio_patch
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2015-07-27 17:18:36 +00:00
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_qemu_img_binary(tmpdir):
|
|
|
|
|
2023-01-10 02:07:26 +00:00
|
|
|
if sys.platform.startswith("win"):
|
2023-01-10 03:09:27 +00:00
|
|
|
bin_path = str(tmpdir / "qemu-img.EXE")
|
2023-01-10 02:07:26 +00:00
|
|
|
else:
|
|
|
|
bin_path = str(tmpdir / "qemu-img")
|
2015-07-27 17:18:36 +00:00
|
|
|
with open(bin_path, "w+") as f:
|
|
|
|
f.write("1")
|
|
|
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
|
|
|
return bin_path
|
2015-02-20 13:39:13 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_get_qemu_version():
|
2015-02-20 13:39:13 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.qemu.subprocess_check_output", return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard"):
|
|
|
|
version = await Qemu.get_qemu_version("/tmp/qemu-test")
|
2015-04-27 21:12:13 +00:00
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
assert version == ""
|
|
|
|
else:
|
|
|
|
assert version == "2.2.0"
|
2015-02-20 13:39:13 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_binary_list(monkeypatch, tmpdir):
|
2015-02-20 13:39:13 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
monkeypatch.setenv("PATH", str(tmpdir))
|
2016-02-03 15:38:46 +00:00
|
|
|
files_to_create = ["qemu-system-x86", "qemu-system-x42", "qemu-kvm", "hello", "qemu-system-x86_64-spice"]
|
2015-02-20 13:39:13 +00:00
|
|
|
|
|
|
|
for file_to_create in files_to_create:
|
|
|
|
path = os.path.join(os.environ["PATH"], file_to_create)
|
|
|
|
with open(path, "w+") as f:
|
|
|
|
f.write("1")
|
|
|
|
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.qemu.subprocess_check_output", return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard") as mock:
|
2015-04-27 21:12:13 +00:00
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
version = ""
|
|
|
|
else:
|
|
|
|
version = "2.2.0"
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
qemus = await Qemu.binary_list()
|
2016-01-01 00:40:12 +00:00
|
|
|
|
2015-04-27 21:12:13 +00:00
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x86"), "version": version} in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-kvm"), "version": version} in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x42"), "version": version} in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "hello"), "version": version} not in qemus
|
2015-02-20 13:39:13 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
qemus = await Qemu.binary_list(["x86"])
|
2016-01-01 00:40:12 +00:00
|
|
|
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x86"), "version": version} in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-kvm"), "version": version} not in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x42"), "version": version} not in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "hello"), "version": version} not in qemus
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
qemus = await Qemu.binary_list(["x86", "x42"])
|
2016-01-01 00:40:12 +00:00
|
|
|
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x86"), "version": version} in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-kvm"), "version": version} not in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x42"), "version": version} in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "hello"), "version": version} not in qemus
|
|
|
|
|
2015-07-27 17:18:36 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_img_binary_list(monkeypatch, tmpdir):
|
2015-05-10 17:46:51 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
monkeypatch.setenv("PATH", str(tmpdir))
|
2015-05-10 17:46:51 +00:00
|
|
|
files_to_create = ["qemu-img", "qemu-io", "qemu-system-x86", "qemu-system-x42", "qemu-kvm", "hello"]
|
|
|
|
|
|
|
|
for file_to_create in files_to_create:
|
|
|
|
path = os.path.join(os.environ["PATH"], file_to_create)
|
|
|
|
with open(path, "w+") as f:
|
|
|
|
f.write("1")
|
|
|
|
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.qemu.subprocess_check_output", return_value="qemu-img version 2.2.0, Copyright (c) 2004-2008 Fabrice Bellard") as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
qemus = await Qemu.img_binary_list()
|
2015-05-10 17:46:51 +00:00
|
|
|
|
|
|
|
version = "2.2.0"
|
|
|
|
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-img"), "version": version} in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-io"), "version": version} not in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x86"), "version": version} not in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-kvm"), "version": version} not in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x42"), "version": version} not in qemus
|
|
|
|
assert {"path": os.path.join(os.environ["PATH"], "hello"), "version": version} not in qemus
|
|
|
|
|
2015-02-20 13:39:13 +00:00
|
|
|
|
2015-02-26 01:55:35 +00:00
|
|
|
def test_get_legacy_vm_workdir():
|
2015-02-20 13:39:13 +00:00
|
|
|
|
2015-04-27 21:12:13 +00:00
|
|
|
assert Qemu.get_legacy_vm_workdir(42, "bla") == os.path.join("qemu", "vm-42")
|
2015-07-27 17:18:36 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_image_abs_path(tmpdir, fake_qemu_img_binary):
|
|
|
|
|
2015-07-27 17:18:36 +00:00
|
|
|
options = {
|
|
|
|
"format": "qcow2",
|
|
|
|
"preallocation": "metadata",
|
|
|
|
"cluster_size": 64,
|
|
|
|
"refcount_bits": 12,
|
|
|
|
"lazy_refcounts": "off",
|
|
|
|
"size": 100
|
|
|
|
}
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
|
2020-06-16 04:29:03 +00:00
|
|
|
await Qemu.instance().create_disk(fake_qemu_img_binary, str(tmpdir / "hda.qcow2"), options)
|
2015-07-27 17:18:36 +00:00
|
|
|
args, kwargs = process.call_args
|
|
|
|
assert args == (
|
|
|
|
fake_qemu_img_binary,
|
|
|
|
"create",
|
|
|
|
"-f",
|
|
|
|
"qcow2",
|
|
|
|
"-o",
|
|
|
|
"cluster_size=64",
|
|
|
|
"-o",
|
|
|
|
"lazy_refcounts=off",
|
|
|
|
"-o",
|
|
|
|
"preallocation=metadata",
|
|
|
|
"-o",
|
|
|
|
"refcount_bits=12",
|
|
|
|
str(tmpdir / "hda.qcow2"),
|
|
|
|
"100M"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_image_relative_path(tmpdir, fake_qemu_img_binary):
|
|
|
|
|
2015-07-27 17:18:36 +00:00
|
|
|
options = {
|
|
|
|
"format": "raw",
|
|
|
|
"size": 100
|
|
|
|
}
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
|
2016-04-15 15:57:06 +00:00
|
|
|
with patch("gns3server.compute.qemu.Qemu.get_images_directory", return_value=str(tmpdir)):
|
2020-06-16 04:29:03 +00:00
|
|
|
await Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options)
|
2015-07-27 17:18:36 +00:00
|
|
|
args, kwargs = process.call_args
|
|
|
|
assert args == (
|
|
|
|
fake_qemu_img_binary,
|
|
|
|
"create",
|
|
|
|
"-f",
|
|
|
|
"raw",
|
|
|
|
str(tmpdir / "hda.qcow2"),
|
|
|
|
"100M"
|
|
|
|
)
|
2015-07-28 14:15:01 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_image_exist(tmpdir, fake_qemu_img_binary):
|
2015-07-28 14:15:01 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
open(str(tmpdir / "hda.qcow2"), "w+").close()
|
2015-07-28 14:15:01 +00:00
|
|
|
options = {
|
|
|
|
"format": "raw",
|
|
|
|
"size": 100
|
|
|
|
}
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
|
2016-04-15 15:57:06 +00:00
|
|
|
with patch("gns3server.compute.qemu.Qemu.get_images_directory", return_value=str(tmpdir)):
|
2015-07-28 14:15:01 +00:00
|
|
|
with pytest.raises(QemuError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options)
|
2015-07-28 14:15:01 +00:00
|
|
|
assert not process.called
|
2016-01-04 15:30:06 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_image_with_not_supported_characters_by_filesystem(tmpdir, fake_qemu_img_binary):
|
|
|
|
|
2017-06-07 10:35:41 +00:00
|
|
|
open(str(tmpdir / "hda.qcow2"), "w+").close()
|
|
|
|
|
|
|
|
options = {
|
|
|
|
"format": "raw",
|
|
|
|
"size": 100
|
|
|
|
}
|
|
|
|
|
|
|
|
# patching os.makedirs is necessary as it depends on already mocked os.path.exists
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process, \
|
2024-05-09 09:01:55 +00:00
|
|
|
patch("gns3server.compute.qemu.Qemu._init_config_disk", return_value=MagicMock()), \
|
2017-06-19 08:44:52 +00:00
|
|
|
patch("gns3server.compute.qemu.Qemu.get_images_directory", return_value=str(tmpdir)), \
|
|
|
|
patch("os.path.exists", side_effect=UnicodeEncodeError('error', u"", 1, 2, 'Emulated Unicode Err')),\
|
|
|
|
patch("os.makedirs"):
|
2017-06-07 10:35:41 +00:00
|
|
|
|
|
|
|
with pytest.raises(QemuError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options)
|
2017-06-07 10:35:41 +00:00
|
|
|
assert not process.called
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_get_kvm_archs_kvm_ok():
|
2016-01-04 15:30:06 +00:00
|
|
|
|
2016-01-26 12:57:55 +00:00
|
|
|
with patch("os.path.exists", return_value=True):
|
2020-06-16 04:29:03 +00:00
|
|
|
archs = await Qemu.get_kvm_archs()
|
2016-01-04 15:30:06 +00:00
|
|
|
if platform.machine() == 'x86_64':
|
|
|
|
assert archs == ['x86_64', 'i386']
|
|
|
|
else:
|
2017-06-19 08:44:52 +00:00
|
|
|
assert archs == [platform.machine()]
|
2016-01-26 12:57:55 +00:00
|
|
|
|
|
|
|
with patch("os.path.exists", return_value=False):
|
2020-06-16 04:29:03 +00:00
|
|
|
archs = await Qemu.get_kvm_archs()
|
2016-01-26 12:57:55 +00:00
|
|
|
assert archs == []
|