def test_get_kvm_archs_kvm_ok(loop):

    with patch("os.path.exists", return_value=True):
        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()

    with patch("os.path.exists", return_value=False):
        archs = loop.run_until_complete(asyncio. async (Qemu.get_kvm_archs()))
        assert archs == []
Пример #2
0
def test_get_kvm_archs_kvm_ok(loop):

    with patch("os.path.exists", return_value=True):
        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()

    with patch("os.path.exists", return_value=False):
        archs = loop.run_until_complete(asyncio.async(Qemu.get_kvm_archs()))
        assert archs == []
Пример #3
0
def test_create_image_abs_path(loop, tmpdir, fake_qemu_img_binary):
    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:
        loop.run_until_complete(asyncio.async(Qemu.instance().create_disk(fake_qemu_img_binary, str(tmpdir / "hda.qcow2"), options)))
        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"
        )
Пример #4
0
def test_binary_list(loop):

    files_to_create = ["qemu-system-x86", "qemu-system-x42", "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)

    with asyncio_patch(
            "gns3server.modules.qemu.subprocess_check_output",
            return_value=
            "QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard"
    ) as mock:
        qemus = loop.run_until_complete(asyncio. async (Qemu.binary_list()))

        assert {
            "path": os.path.join(os.environ["PATH"], "qemu-system-x86"),
            "version": "2.2.0"
        } in qemus
        assert {
            "path": os.path.join(os.environ["PATH"], "qemu-system-x42"),
            "version": "2.2.0"
        } in qemus
        assert {
            "path": os.path.join(os.environ["PATH"], "hello"),
            "version": "2.2.0"
        } not in qemus
Пример #5
0
def test_binary_list(loop):

    files_to_create = ["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)

    with asyncio_patch(
        "gns3server.modules.qemu.subprocess_check_output",
        return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard",
    ) as mock:
        qemus = loop.run_until_complete(asyncio.async(Qemu.binary_list()))

        if sys.platform.startswith("win"):
            version = ""
        else:
            version = "2.2.0"

        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
Пример #6
0
def test_get_qemu_version(loop):

    with asyncio_patch("gns3server.modules.qemu.subprocess_check_output", return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard") as mock:
        version = loop.run_until_complete(asyncio.async(Qemu.get_qemu_version("/tmp/qemu-test")))
        if sys.platform.startswith("win"):
            assert version == ""
        else:
            assert version == "2.2.0"
Пример #7
0
def test_get_qemu_version(loop):

    with asyncio_patch("gns3server.modules.qemu.subprocess_check_output", return_value="QEMU emulator version 2.2.0, Copyright (c) 2003-2008 Fabrice Bellard") as mock:
        version = loop.run_until_complete(asyncio.async(Qemu._get_qemu_version("/tmp/qemu-test")))
        if sys.platform.startswith("win"):
            assert version == ""
        else:
            assert version == "2.2.0"
Пример #8
0
def test_add_nio_binding_udp(vm, loop):
    nio = Qemu.instance().create_nio(vm.qemu_path, {
        "type": "nio_udp",
        "lport": 4242,
        "rport": 4243,
        "rhost": "127.0.0.1"
    })
    loop.run_until_complete(asyncio. async (vm.adapter_add_nio_binding(0,
                                                                       nio)))
    assert nio.lport == 4242
Пример #9
0
def test_add_nio_binding_ethernet(vm, loop, ethernet_device):
    with patch(
            "gns3server.modules.base_manager.BaseManager.has_privileged_access",
            return_value=True):
        nio = Qemu.instance().create_nio(vm.qemu_path, {
            "type": "nio_generic_ethernet",
            "ethernet_device": ethernet_device
        })
        loop.run_until_complete(asyncio. async (vm.adapter_add_nio_binding(
            0, nio)))
        assert nio.ethernet_device == ethernet_device
Пример #10
0
def test_port_remove_nio_binding(vm, loop):
    nio = Qemu.instance().create_nio(vm.qemu_path, {
        "type": "nio_udp",
        "lport": 4242,
        "rport": 4243,
        "rhost": "127.0.0.1"
    })
    loop.run_until_complete(asyncio. async (vm.adapter_add_nio_binding(0,
                                                                       nio)))
    loop.run_until_complete(asyncio. async (vm.adapter_remove_nio_binding(0)))
    assert vm._ethernet_adapters[0].ports[0] is None
Пример #11
0
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
Пример #12
0
def test_create_image_relative_path(loop, tmpdir, fake_qemu_img_binary):
    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)):
            loop.run_until_complete(asyncio. async (
                Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2",
                                            options)))
            args, kwargs = process.call_args
            assert args == (fake_qemu_img_binary, "create", "-f", "raw",
                            str(tmpdir / "hda.qcow2"), "100M")
Пример #13
0
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
Пример #14
0
def test_stop(loop, vm, running_subprocess_mock):
    process = running_subprocess_mock

    # Wait process kill success
    future = asyncio.Future()
    future.set_result(True)
    process.wait.return_value = future

    with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
        nio = Qemu.instance().create_nio(vm.qemu_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
        vm.adapter_add_nio_binding(0, nio)
        loop.run_until_complete(asyncio.async(vm.start()))
        assert vm.is_running()
        loop.run_until_complete(asyncio.async(vm.stop()))
        assert vm.is_running() is False
        process.terminate.assert_called_with()
Пример #15
0
def test_stop(loop, vm, running_subprocess_mock):
    process = running_subprocess_mock

    # Wait process kill success
    future = asyncio.Future()
    future.set_result(True)
    process.wait.return_value = future

    with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
        nio = Qemu.instance().create_nio(vm.qemu_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
        vm.adapter_add_nio_binding(0, nio)
        loop.run_until_complete(asyncio.async(vm.start()))
        assert vm.is_running()
        loop.run_until_complete(asyncio.async(vm.stop()))
        assert vm.is_running() is False
        process.terminate.assert_called_with()
Пример #16
0
def test_create_image_relative_path(loop, tmpdir, fake_qemu_img_binary):
    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)):
            loop.run_until_complete(asyncio.async(Qemu.instance().create_disk(fake_qemu_img_binary, "hda.qcow2", options)))
            args, kwargs = process.call_args
            assert args == (
                fake_qemu_img_binary,
                "create",
                "-f",
                "raw",
                str(tmpdir / "hda.qcow2"),
                "100M"
            )
Пример #17
0
def test_create_image_abs_path(loop, tmpdir, fake_qemu_img_binary):
    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:
        loop.run_until_complete(asyncio. async (Qemu.instance().create_disk(
            fake_qemu_img_binary, str(tmpdir / "hda.qcow2"), options)))
        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")
Пример #18
0
def manager(port_manager):
    m = Qemu.instance()
    m.port_manager = port_manager
    return m
Пример #19
0
def test_port_remove_nio_binding(vm, loop):
    nio = Qemu.instance().create_nio(vm.qemu_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
    loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
    loop.run_until_complete(asyncio.async(vm.adapter_remove_nio_binding(0)))
    assert vm._ethernet_adapters[0].ports[0] is None
Пример #20
0
def test_add_nio_binding_ethernet(vm, loop, ethernet_device):
    with patch("gns3server.modules.base_manager.BaseManager.has_privileged_access", return_value=True):
        nio = Qemu.instance().create_nio(vm.qemu_path, {"type": "nio_generic_ethernet", "ethernet_device": ethernet_device})
        loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
        assert nio.ethernet_device == ethernet_device
Пример #21
0
def test_add_nio_binding_udp(vm, loop):
    nio = Qemu.instance().create_nio(vm.qemu_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
    loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
    assert nio.lport == 4242
Пример #22
0
def test_get_legacy_vm_workdir():

    assert Qemu.get_legacy_vm_workdir(42, "bla") == "qemu/vm-42"
Пример #23
0
def manager(port_manager):
    m = Qemu.instance()
    m.port_manager = port_manager
    return m
Пример #24
0
def test_get_legacy_vm_workdir():

    assert Qemu.get_legacy_vm_workdir(42, "bla") == os.path.join("qemu", "vm-42")
Пример #25
0
def test_get_legacy_vm_workdir():

    assert Qemu.get_legacy_vm_workdir(42, "bla") == "qemu/vm-42"
Пример #26
0
def qemu(port_manager):
    Qemu._instance = None
    qemu = Qemu.instance()
    qemu.port_manager = port_manager
    return qemu
Пример #27
0
def qemu(port_manager):
    Qemu._instance = None
    qemu = Qemu.instance()
    qemu.port_manager = port_manager
    return qemu
Пример #28
0
def test_get_legacy_vm_workdir():

    assert Qemu.get_legacy_vm_workdir(42,
                                      "bla") == os.path.join("qemu", "vm-42")