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" )
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
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
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
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")
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
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
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()
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" )
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")
def manager(port_manager): m = Qemu.instance() m.port_manager = port_manager return m
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
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
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
def qemu(port_manager): Qemu._instance = None qemu = Qemu.instance() qemu.port_manager = port_manager return qemu