def update(self, new_settings): """ Updates the settings for this cloud. :param new_settings: settings dictionary """ updated = False if "nios" in new_settings: nios = new_settings["nios"] # add ports for nio in nios: if nio in self._settings["nios"]: # port already created for this NIO continue nio_object = None if nio.lower().startswith("nio_udp"): nio_object = self._createNIOUDP(nio) if nio.lower().startswith("nio_gen_eth"): nio_object = self._createNIOGenericEthernet(nio) if nio.lower().startswith("nio_gen_linux"): nio_object = self._createNIOLinuxEthernet(nio) if nio.lower().startswith("nio_tap"): nio_object = self._createNIOTAP(nio) if nio.lower().startswith("nio_unix"): nio_object = self._createNIOUNIX(nio) if nio.lower().startswith("nio_vde"): nio_object = self._createNIOVDE(nio) if nio.lower().startswith("nio_null"): nio_object = self._createNIONull(nio) if nio_object is None: log.error( "Could not create NIO object from {}".format(nio)) continue port = Port(nio, nio_object, stub=True) port.setStatus(Port.started) self._ports.append(port) updated = True log.debug("port {} has been added".format(nio)) # delete ports for nio in self._settings["nios"]: if nio not in nios: for port in self._ports.copy(): if port.name() == nio: self._ports.remove(port) updated = True log.debug("port {} has been deleted".format(nio)) break self._settings["nios"] = new_settings["nios"].copy() if "name" in new_settings and new_settings["name"] != self.name(): self._settings["name"] = new_settings["name"] updated = True if updated: log.info("cloud {} has been updated".format(self.name())) self.updated_signal.emit()
def update(self, new_settings): """ Updates the settings for this cloud. :param new_settings: settings dictionary """ updated = False if "nios" in new_settings: nios = new_settings["nios"] # add ports for nio in nios: if nio in self._settings["nios"]: # port already created for this NIO continue nio_object = None if nio.lower().startswith("nio_udp"): nio_object = self._createNIOUDP(nio) if nio.lower().startswith("nio_gen_eth"): nio_object = self._createNIOGenericEthernet(nio) if nio.lower().startswith("nio_gen_linux"): nio_object = self._createNIOLinuxEthernet(nio) if nio.lower().startswith("nio_tap"): nio_object = self._createNIOTAP(nio) if nio.lower().startswith("nio_unix"): nio_object = self._createNIOUNIX(nio) if nio.lower().startswith("nio_vde"): nio_object = self._createNIOVDE(nio) if nio.lower().startswith("nio_null"): nio_object = self._createNIONull(nio) if nio_object is None: log.error("Could not create NIO object from {}".format(nio)) continue port = Port(nio, nio_object, stub=True) port.setStatus(Port.started) self._ports.append(port) updated = True log.debug("port {} has been added".format(nio)) # delete ports for nio in self._settings["nios"]: if nio not in nios: for port in self._ports.copy(): if port.name() == nio: self._ports.remove(port) updated = True log.debug("port {} has been deleted".format(nio)) break self._settings["nios"] = new_settings["nios"].copy() if "name" in new_settings and new_settings["name"] != self.name(): self._settings["name"] = new_settings["name"] updated = True if updated: log.info("cloud {} has been updated".format(self.name())) self.updated_signal.emit()
def reset_modules(): """ Reset modules (VPCS, VirtualBox...) internal variables. """ from gns3.http_client import HTTPClient from gns3.ports.port import Port from gns3.modules.vpcs.vpcs_device import VPCSDevice from gns3.modules.virtualbox.virtualbox_vm import VirtualBoxVM from gns3.modules.iou.iou_device import IOUDevice Port.reset() VPCSDevice.reset() VirtualBoxVM.reset() HTTPClient.reset() IOUDevice.reset()
def test_addNIO(vpcs_device): with patch('gns3.node.Node.httpPost') as mock: port = Port("Port 1") port.setPortNumber(0) port.setAdapterNumber(0) nio = NIOUDP(4242, "127.0.0.1", 4243) vpcs_device.addNIO(port, nio) assert mock.called args, kwargs = mock.call_args assert args[0] == "/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format( vm_id=vpcs_device.vm_id()) # Connect the signal signal_mock = Mock() vpcs_device.nio_signal.connect(signal_mock) # Callback args[1]({}, context=kwargs["context"]) # Check the signal assert signal_mock.called args, kwargs = signal_mock.call_args assert args[0] == vpcs_device.id() assert args[1] == port.id()
def test_stopPacketCapture(iou_device): port = Port("test") port.setAdapterNumber(2) port.setPortNumber(1) with patch("gns3.node.Node.httpPost") as mock: iou_device.stopPacketCapture(port) assert mock.called args, kwargs = mock.call_args assert args[0] == "/iou/vms/{vm_id}/adapters/2/ports/1/stop_capture".format(vm_id=iou_device.vm_id()) with patch("gns3.ports.port.Port.stopPacketCapture") as port_mock: # Callback args[1]({}, context=kwargs["context"]) assert port_mock.called
def update(self, new_settings): """ Updates the settings for this cloud. :param new_settings: settings dictionary """ updated = False if "nios" in new_settings: nios = new_settings["nios"] # add ports for nio in nios: if nio in self._settings["nios"]: # port already created for this NIO continue nio_object = self._allocateNIO(nio) if nio_object is None: continue port = Port(nio, nio_object, stub=True) port.setStatus(Port.started) self._ports.append(port) updated = True log.debug("port {} has been added".format(nio)) # delete ports for nio in self._settings["nios"]: if nio not in nios: for port in self._ports.copy(): if port.name() == nio: self._ports.remove(port) updated = True log.debug("port {} has been deleted".format(nio)) break self._settings["nios"] = new_settings["nios"].copy() if "name" in new_settings and new_settings["name"] != self.name(): self._settings["name"] = new_settings["name"] updated = True if updated: log.info("cloud {} has been updated".format(self.name())) self.updated_signal.emit()
def _addPorts(self, nios, ignore_existing_nio=False): """ Adds adapters. :param adapters: number of adapters """ # add ports for nio in nios: if ignore_existing_nio and nio in self._settings["nios"]: # port already created for this NIO continue nio_object = self._allocateNIO(nio) if nio_object is None: continue port = Port(nio, nio_object, stub=True) port.setStatus(Port.started) self._ports.append(port) log.debug("port {} has been added".format(nio))
def test_startPacketCapture(iou_device): port = Port("test") port.setAdapterNumber(2) port.setPortNumber(1) with patch("gns3.node.Node.httpPost") as mock: iou_device.startPacketCapture(port, "test.pcap", "DLT_EN10MB") assert mock.called args, kwargs = mock.call_args assert args[0] == "/iou/vms/{vm_id}/adapters/2/ports/1/start_capture".format(vm_id=iou_device.vm_id()) assert kwargs["body"] == {"data_link_type": "DLT_EN10MB", "capture_file_name": "test.pcap"} with patch("gns3.ports.port.Port.startPacketCapture") as port_mock: # Callback args[1]({"pcap_file_path": "/tmp/test.pcap"}, context=kwargs["context"]) assert port_mock.called_with("/tmp/test.pcap")
def test_startPacketCapture(iou_device): port = Port("test") port.setAdapterNumber(2) port.setPortNumber(1) with patch("gns3.node.Node.httpPost") as mock: iou_device.startPacketCapture(port, "test.pcap", "DLT_EN10MB") assert mock.called args, kwargs = mock.call_args assert args[ 0] == "/iou/vms/{vm_id}/adapters/2/ports/1/start_capture".format( vm_id=iou_device.vm_id()) assert kwargs["body"] == { "data_link_type": "DLT_EN10MB", "capture_file_name": "test.pcap" } with patch("gns3.ports.port.Port.startPacketCapture") as port_mock: # Callback args[1]({ "pcap_file_path": "/tmp/test.pcap" }, context=kwargs["context"]) assert port_mock.called_with("/tmp/test.pcap")
def test_deleteNIO(vpcs_device): with patch('gns3.node.Node.httpPost') as mock_post: with patch('gns3.node.Node.httpDelete') as mock_delete: port = Port("Port 1") port.setPortNumber(0) port.setAdapterNumber(0) nio = NIOUDP(4242, "127.0.0.1", 4243) vpcs_device.addNIO(port, nio) vpcs_device.deleteNIO(port) assert mock_delete.called args, kwargs = mock_delete.call_args assert args[0] == "/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(vm_id=vpcs_device.vm_id())
def test_addNIO(vpcs_device): with patch('gns3.node.Node.httpPost') as mock: port = Port("Port 1") port.setPortNumber(0) port.setAdapterNumber(0) nio = NIOUDP(4242, "127.0.0.1", 4243) vpcs_device.addNIO(port, nio) assert mock.called args, kwargs = mock.call_args assert args[0] == "/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(vm_id=vpcs_device.vm_id()) # Connect the signal signal_mock = Mock() vpcs_device.nio_signal.connect(signal_mock) # Callback args[1]({}, context=kwargs["context"]) # Check the signal assert signal_mock.called args, kwargs = signal_mock.call_args assert args[0] == vpcs_device.id() assert args[1] == port.id()