1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-24 17:28:08 +00:00
* Add more test
* Check to kvmi-ok is asynchronous
This commit is contained in:
Julien Duponchelle 2016-01-04 16:30:06 +01:00
parent fba049767a
commit d184b65800
5 changed files with 58 additions and 9 deletions

View File

@ -306,7 +306,7 @@ class QEMUHandler:
output=QEMU_BINARY_LIST_SCHEMA) output=QEMU_BINARY_LIST_SCHEMA)
def list_binaries(request, response): def list_binaries(request, response):
binaries = yield from Qemu.binary_list(request.json["archs"] if "archs" in request.json else None) binaries = yield from Qemu.binary_list(request.json.get("archs", None))
response.json(binaries) response.json(binaries)
@classmethod @classmethod
@ -333,8 +333,8 @@ class QEMUHandler:
output=QEMU_CAPABILITY_LIST_SCHEMA output=QEMU_CAPABILITY_LIST_SCHEMA
) )
def get_capabilities(request, response): def get_capabilities(request, response):
capabilities = {} capabilities = {"kvm": []}
kvms = Qemu.get_kvm_archs() kvms = yield from Qemu.get_kvm_archs()
if kvms: if kvms:
capabilities["kvm"] = kvms capabilities["kvm"] = kvms
response.json(capabilities) response.json(capabilities)

View File

@ -40,6 +40,7 @@ class Qemu(BaseManager):
_VM_CLASS = QemuVM _VM_CLASS = QemuVM
@staticmethod @staticmethod
@asyncio.coroutine
def get_kvm_archs(): def get_kvm_archs():
""" """
Gets a list of architectures for which KVM is available on this server. Gets a list of architectures for which KVM is available on this server.
@ -47,14 +48,19 @@ class Qemu(BaseManager):
:returns: List of architectures for which KVM is available on this server. :returns: List of architectures for which KVM is available on this server.
""" """
kvm = [] kvm = []
x86_64_aliases = ["x86_64", "x86-64", "x64", "AMD64", "amd64", "Intel 64", "EM64T"]
i386_aliases = ["i386", "x86", "x32"] try:
if sys.platform.startswith("linux") and subprocess.call("kvm-ok") == 0: process = yield from asyncio.create_subprocess_exec("kvm-ok")
yield from process.wait()
except OSError:
return kvm
if process.returncode == 0:
arch = platform.machine() arch = platform.machine()
if arch in x86_64_aliases: if arch == "x86_64":
kvm.append("x86_64") kvm.append("x86_64")
kvm.append("i386") kvm.append("i386")
elif arch in i386_aliases: elif arch == "i386":
kvm.append("i386") kvm.append("i386")
else: else:
kvm.append(platform.machine()) kvm.append(platform.machine())

View File

@ -200,11 +200,24 @@ def test_qemu_list_binaries(server, vm):
{"path": "/tmp/2", "version": "2.1.0"}] {"path": "/tmp/2", "version": "2.1.0"}]
with asyncio_patch("gns3server.modules.qemu.Qemu.binary_list", return_value=ret) as mock: with asyncio_patch("gns3server.modules.qemu.Qemu.binary_list", return_value=ret) as mock:
response = server.get("/qemu/binaries".format(project_id=vm["project_id"]), example=True) response = server.get("/qemu/binaries".format(project_id=vm["project_id"]), example=True)
assert mock.called assert mock.called_with(None)
assert response.status == 200 assert response.status == 200
assert response.json == ret assert response.json == ret
def test_qemu_list_binaries_filter(server, vm):
ret = [
{"path": "/tmp/x86_64", "version": "2.2.0"},
{"path": "/tmp/alpha", "version": "2.1.0"},
{"path": "/tmp/i386", "version": "2.1.0"}
]
with asyncio_patch("gns3server.modules.qemu.Qemu.binary_list", return_value=ret) as mock:
response = server.get("/qemu/binaries".format(project_id=vm["project_id"]), body={"archs": ["i386"]}, example=True)
assert response.status == 200
assert mock.called_with(["i386"])
assert response.json == ret
def test_vms(server, tmpdir, fake_qemu_vm): def test_vms(server, tmpdir, fake_qemu_vm):
with patch("gns3server.modules.Qemu.get_images_directory", return_value=str(tmpdir), example=True): with patch("gns3server.modules.Qemu.get_images_directory", return_value=str(tmpdir), example=True):
@ -312,3 +325,9 @@ def test_create_img_absolute_local(server):
response = server.post("/qemu/img", body=body, example=True) response = server.post("/qemu/img", body=body, example=True)
assert response.status == 201 assert response.status == 201
def test_capabilities(server):
with asyncio_patch("gns3server.modules.Qemu.get_kvm_archs", return_value=["x86_64"]):
response = server.get("/qemu/capabilities", example=True)
assert response.json["kvm"] == ["x86_64"]

View File

@ -20,6 +20,7 @@ import stat
import asyncio import asyncio
import sys import sys
import pytest import pytest
import platform
from gns3server.modules.qemu import Qemu from gns3server.modules.qemu import Qemu
from gns3server.modules.qemu.qemu_error import QemuError from gns3server.modules.qemu.qemu_error import QemuError
@ -174,3 +175,21 @@ def test_create_image_exist(loop, tmpdir, fake_qemu_img_binary):
with pytest.raises(QemuError): with pytest.raises(QemuError):
loop.run_until_complete(asyncio.async(Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options))) loop.run_until_complete(asyncio.async(Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options)))
assert not process.called assert not process.called
def test_get_kvm_archs_no_kvm(loop):
with asyncio_patch("asyncio.create_subprocess_exec", side_effect=FileNotFoundError('kvm-ok')):
archs = loop.run_until_complete(asyncio.async(Qemu.get_kvm_archs()))
assert archs == []
def test_get_kvm_archs_kvm_ok(loop):
process = MagicMock()
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
process.returncode = 0
archs = loop.run_until_complete(asyncio.async(Qemu.get_kvm_archs()))
if platform.machine() == 'x86_64':
assert archs == ['x86_64', 'i386']
else:
assert archs == platform.machine()

View File

@ -50,6 +50,11 @@ class _asyncio_patch:
future = asyncio.Future() future = asyncio.Future()
if "return_value" in self.kwargs: if "return_value" in self.kwargs:
future.set_result(self.kwargs["return_value"]) future.set_result(self.kwargs["return_value"])
elif "side_effect" in self.kwargs:
if isinstance(self.kwargs["side_effect"], Exception):
future.set_exception(self.kwargs["side_effect"])
else:
raise NotImplementedError
else: else:
future.set_result(True) future.set_result(True)
return future return future