async def test_build_command_two_adapters_mac_address(vm): """ Should support multiple base vmac address """ vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.5.0") vm.adapters = 2 vm.mac_address = "00:00:ab:0e:0f:09" mac_0 = vm._mac_address mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 1) assert mac_0[:8] == "00:00:ab" with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()): cmd = await vm._build_command() assert "e1000,mac={},netdev=gns3-0".format(mac_0) in cmd assert "e1000,mac={},netdev=gns3-1".format(mac_1) in cmd vm.mac_address = "00:42:ab:0e:0f:0a" mac_0 = vm._mac_address mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 1) assert mac_0[:8] == "00:42:ab" with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()): cmd = await vm._build_command() assert "e1000,mac={},netdev=gns3-0".format(mac_0) in cmd assert "e1000,mac={},netdev=gns3-1".format(mac_1) in cmd
def test_build_command_two_adapters_mac_address(vm, loop, fake_qemu_binary, port_manager): """ Should support multiple base vmac address """ vm.adapters = 2 vm.mac_address = "00:00:ab:0e:0f:09" mac_0 = vm._mac_address mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address)) assert mac_0[:8] == "00:00:ab" with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio. async (vm._build_command())) assert "e1000,mac={}".format(mac_0) in cmd assert "e1000,mac={}".format(mac_1) in cmd vm.mac_address = "00:42:ab:0e:0f:0a" mac_0 = vm._mac_address mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address)) assert mac_0[:8] == "00:42:ab" with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio. async (vm._build_command())) assert "e1000,mac={}".format(mac_0) in cmd assert "e1000,mac={}".format(mac_1) in cmd
def test_build_command_large_number_of_adapters(vm, loop, fake_qemu_binary, port_manager): """ When we have more than 28 interface we need to add a pci bridge for additionnal interfaces """ # It's supported only with Qemu 2.4 and later vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.4.0") vm.adapters = 100 vm.mac_address = "00:00:ab:0e:0f:09" mac_0 = vm._mac_address mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 1) assert mac_0[:8] == "00:00:ab" with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete( asyncio.ensure_future(vm._build_command())) # Count if we have 100 e1000 adapters in the command assert len([l for l in cmd if "e1000" in l]) == 100 assert len(vm._ethernet_adapters) == 100 assert "e1000,mac={},netdev=gns3-0".format(mac_0) in cmd assert "e1000,mac={},netdev=gns3-1".format(mac_1) in cmd assert "pci-bridge,id=pci-bridge0,bus=dmi_pci_bridge0,chassis_nr=0x1,addr=0x0,shpc=off" not in cmd assert "pci-bridge,id=pci-bridge1,bus=dmi_pci_bridge1,chassis_nr=0x1,addr=0x1,shpc=off" in cmd assert "pci-bridge,id=pci-bridge2,bus=dmi_pci_bridge2,chassis_nr=0x1,addr=0x2,shpc=off" in cmd assert "i82801b11-bridge,id=dmi_pci_bridge1" in cmd mac_29 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 29) assert "e1000,mac={},bus=pci-bridge1,addr=0x04,netdev=gns3-29".format( mac_29) in cmd mac_30 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 30) assert "e1000,mac={},bus=pci-bridge1,addr=0x05,netdev=gns3-30".format( mac_30) in cmd mac_74 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 74) assert "e1000,mac={},bus=pci-bridge2,addr=0x11,netdev=gns3-74".format( mac_74) in cmd # Qemu < 2.4 doesn't support large number of adapters vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.0.0") with pytest.raises(QemuError): with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete( asyncio.ensure_future(vm._build_command())) vm.adapters = 5 with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete( asyncio.ensure_future(vm._build_command()))
def test_build_command_two_adapters(vm, loop, fake_qemu_binary, port_manager): os.environ["DISPLAY"] = "0:0" vm.adapters = 2 with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio.async(vm._build_command())) assert cmd == [ fake_qemu_binary, "-name", "test", "-m", "256M", "-smp", "cpus=1", "-boot", "order=c", "-serial", "telnet:127.0.0.1:{},server,nowait".format(vm.console), "-net", "none", "-device", "e1000,mac={}".format(vm.mac_address), "-device", "e1000,mac={}".format(int_to_macaddress(macaddress_to_int(vm._mac_address) + 1)) ]
def test_build_command_two_adapters(vm, loop, fake_qemu_binary, port_manager): os.environ["DISPLAY"] = "0:0" vm.adapters = 2 with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio.async(vm._build_command())) nio1 = vm._local_udp_tunnels[0][0] nio2 = vm._local_udp_tunnels[1][0] assert cmd == [ fake_qemu_binary, "-name", "test", "-m", "256M", "-smp", "cpus=1", "-boot", "order=c", "-uuid", vm.id, "-serial", "telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port), "-net", "none", "-device", "e1000,mac={},netdev=gns3-0".format(vm._mac_address), "-netdev", "socket,id=gns3-0,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format(nio1.rport, nio1.lport), "-device", "e1000,mac={},netdev=gns3-1".format(int_to_macaddress(macaddress_to_int(vm._mac_address) + 1)), "-netdev", "socket,id=gns3-1,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format(nio2.rport, nio2.lport) ]
def test_build_command_two_adapters(vm, loop, fake_qemu_binary, port_manager): vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.5.0") os.environ["DISPLAY"] = "0:0" vm.adapters = 2 with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete( asyncio.ensure_future(vm._build_command())) nio1 = vm._local_udp_tunnels[0][0] nio2 = vm._local_udp_tunnels[1][0] assert cmd == [ fake_qemu_binary, "-name", "test", "-m", "256M", "-smp", "cpus=1", "-boot", "order=c", "-uuid", vm.id, "-serial", "telnet:127.0.0.1:{},server,nowait".format( vm._internal_console_port), "-net", "none", "-device", "e1000,mac={},netdev=gns3-0".format(vm._mac_address), "-netdev", "socket,id=gns3-0,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format( nio1.rport, nio1.lport), "-device", "e1000,mac={},netdev=gns3-1".format( int_to_macaddress(macaddress_to_int(vm._mac_address) + 1)), "-netdev", "socket,id=gns3-1,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format( nio2.rport, nio2.lport), "-nographic" ]
def __new__(cls, properties, port_by_adapter, first_port_name, port_name_format, port_segment_size, custom_adapters): ports = [] adapter_number = interface_number = segment_number = 0 if "ethernet_adapters" in properties: ethernet_adapters = properties["ethernet_adapters"] else: ethernet_adapters = properties.get("adapters", 1) for adapter_number in range(adapter_number, ethernet_adapters + adapter_number): custom_adapter_settings = {} for custom_adapter in custom_adapters: if custom_adapter["adapter_number"] == adapter_number: custom_adapter_settings = custom_adapter break for port_number in range(0, port_by_adapter): if first_port_name and adapter_number == 0: port_name = custom_adapter_settings.get("port_name", first_port_name) port = PortFactory(port_name, segment_number, adapter_number, port_number, "ethernet", short_name=port_name) else: try: port_name = port_name_format.format( interface_number, segment_number, adapter=adapter_number, **cls._generate_replacement(interface_number, segment_number)) except (IndexError, ValueError, KeyError) as e: raise aiohttp.web.HTTPConflict(text="Invalid port name format {}: {}".format(port_name_format, str(e))) port_name = custom_adapter_settings.get("port_name", port_name) port = PortFactory(port_name, segment_number, adapter_number, port_number, "ethernet") interface_number += 1 if port_segment_size: if interface_number % port_segment_size == 0: segment_number += 1 interface_number = 0 else: segment_number += 1 port.adapter_type = custom_adapter_settings.get("adapter_type", properties.get("adapter_type", None)) mac_address = custom_adapter_settings.get("mac_address") if not mac_address and "mac_address" in properties: mac_address = int_to_macaddress(macaddress_to_int(properties["mac_address"]) + adapter_number) port.mac_address = mac_address ports.append(port) if len(ports): adapter_number += 1 if "serial_adapters" in properties: for adapter_number in range(adapter_number, properties["serial_adapters"] + adapter_number): for port_number in range(0, port_by_adapter): ports.append(PortFactory("Serial{}/{}".format(segment_number, port_number), segment_number, adapter_number, port_number, "serial")) segment_number += 1 return ports
def test_build_command_large_number_of_adapters(vm, loop, fake_qemu_binary, port_manager): """ When we have more than 28 interface we need to add a pci bridge for additionnal interfaces """ # It's supported only with Qemu 2.4 and later vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.4.0") vm.adapters = 100 vm.mac_address = "00:00:ab:0e:0f:09" mac_0 = vm._mac_address mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 1) assert mac_0[:8] == "00:00:ab" with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio.async(vm._build_command())) # Count if we have 100 e1000 adapters in the command assert len([l for l in cmd if "e1000" in l ]) == 100 assert len(vm._ethernet_adapters) == 100 assert "e1000,mac={},netdev=gns3-0".format(mac_0) in cmd assert "e1000,mac={},netdev=gns3-1".format(mac_1) in cmd assert "pci-bridge,id=pci-bridge0,bus=dmi_pci_bridge0,chassis_nr=0x1,addr=0x0,shpc=off" not in cmd assert "pci-bridge,id=pci-bridge1,bus=dmi_pci_bridge1,chassis_nr=0x1,addr=0x1,shpc=off" in cmd assert "pci-bridge,id=pci-bridge2,bus=dmi_pci_bridge2,chassis_nr=0x1,addr=0x2,shpc=off" in cmd assert "i82801b11-bridge,id=dmi_pci_bridge1" in cmd mac_29 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 29) assert "e1000,mac={},bus=pci-bridge1,addr=0x04,netdev=gns3-29".format(mac_29) in cmd mac_30 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 30) assert "e1000,mac={},bus=pci-bridge1,addr=0x05,netdev=gns3-30".format(mac_30) in cmd mac_74 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 74) assert "e1000,mac={},bus=pci-bridge2,addr=0x11,netdev=gns3-74".format(mac_74) in cmd # Qemu < 2.4 doesn't support large number of adapters vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.0.0") with pytest.raises(QemuError): with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio.async(vm._build_command())) vm.adapters = 5 with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio.async(vm._build_command()))
def test_build_command_two_adapters_mac_address(vm, loop, fake_qemu_binary, port_manager): """ Should support multiple base vmac address """ vm.adapters = 2 vm.mac_address = "00:00:ab:0e:0f:09" mac_0 = vm._mac_address mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address)) assert mac_0[:8] == "00:00:ab" with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio.async(vm._build_command())) assert "e1000,mac={}".format(mac_0) in cmd assert "e1000,mac={}".format(mac_1) in cmd vm.mac_address = "00:42:ab:0e:0f:0a" mac_0 = vm._mac_address mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address)) assert mac_0[:8] == "00:42:ab" with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio.async(vm._build_command())) assert "e1000,mac={}".format(mac_0) in cmd assert "e1000,mac={}".format(mac_1) in cmd
def test_build_command_two_adapters(vm, loop, fake_qemu_binary, port_manager): os.environ["DISPLAY"] = "0:0" vm.adapters = 2 with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: cmd = loop.run_until_complete(asyncio. async (vm._build_command())) assert cmd == [ fake_qemu_binary, "-name", "test", "-m", "256M", "-smp", "cpus=1", "-boot", "order=c", "-serial", "telnet:127.0.0.1:{},server,nowait".format(vm.console), "-net", "none", "-device", "e1000,mac={}".format(vm.mac_address), "-device", "e1000,mac={}".format( int_to_macaddress(macaddress_to_int(vm._mac_address) + 1)) ]
def __new__(cls, properties, port_by_adapter, first_port_name, port_name_format, port_segment_size, custom_adapters): ports = [] adapter_number = interface_number = segment_number = 0 if "ethernet_adapters" in properties: ethernet_adapters = properties["ethernet_adapters"] else: ethernet_adapters = properties.get("adapters", 1) for adapter_number in range(adapter_number, ethernet_adapters + adapter_number): custom_adapter_settings = {} for custom_adapter in custom_adapters: if custom_adapter["adapter_number"] == adapter_number: custom_adapter_settings = custom_adapter break for port_number in range(0, port_by_adapter): if first_port_name and adapter_number == 0: port_name = custom_adapter_settings.get( "port_name", first_port_name) port = PortFactory(port_name, segment_number, adapter_number, port_number, "ethernet", short_name=port_name) else: try: port_name = port_name_format.format( interface_number, segment_number, adapter=adapter_number, **cls._generate_replacement( interface_number, segment_number)) except (IndexError, ValueError, KeyError) as e: raise aiohttp.web.HTTPConflict( text="Invalid port name format {}: {}".format( port_name_format, str(e))) port_name = custom_adapter_settings.get( "port_name", port_name) port = PortFactory(port_name, segment_number, adapter_number, port_number, "ethernet") interface_number += 1 if port_segment_size: if interface_number % port_segment_size == 0: segment_number += 1 interface_number = 0 else: segment_number += 1 port.adapter_type = custom_adapter_settings.get( "adapter_type", properties.get("adapter_type", None)) mac_address = custom_adapter_settings.get("mac_address") if not mac_address and "mac_address" in properties: mac_address = int_to_macaddress( macaddress_to_int(properties["mac_address"]) + adapter_number) port.mac_address = mac_address ports.append(port) if len(ports): adapter_number += 1 if "serial_adapters" in properties: for adapter_number in range( adapter_number, properties["serial_adapters"] + adapter_number): for port_number in range(0, port_by_adapter): ports.append( PortFactory( "Serial{}/{}".format(segment_number, port_number), segment_number, adapter_number, port_number, "serial")) segment_number += 1 return ports
def test_int_to_macaddress(): assert int_to_macaddress(52228632586) == "00:0c:29:11:b0:0a"