async def test_choose_capture_side(project): """ The link capture should run on the optimal node """ compute1 = MagicMock() compute2 = MagicMock() compute2.id = "local" # Capture should run on the local node node_vpcs = Node(project, compute1, "node1", node_type="vpcs") node_vpcs._status = "started" node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)] node_iou = Node(project, compute2, "node2", node_type="iou") node_iou._status = "started" node_iou._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() await link.add_node(node_vpcs, 0, 4) await link.add_node(node_iou, 3, 1) assert link._choose_capture_side()["node"] == node_iou # Capture should choose always running node node_iou = Node(project, compute1, "node5", node_type="iou") node_iou._status = "started" node_iou._ports = [EthernetPort("E0", 0, 0, 4)] node_switch = Node(project, compute1, "node6", node_type="ethernet_switch") node_switch._status = "started" node_switch._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() await link.add_node(node_iou, 0, 4) await link.add_node(node_switch, 3, 1) assert link._choose_capture_side()["node"] == node_switch # Capture should raise error if node are not started node_vpcs = Node(project, compute1, "node1", node_type="vpcs") node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)] node_iou = Node(project, compute2, "node2", node_type="iou") node_iou._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() await link.add_node(node_vpcs, 0, 4) await link.add_node(node_iou, 3, 1) with pytest.raises(aiohttp.web.HTTPConflict): link._choose_capture_side() # If you start a node you can capture on it node_vpcs._status = "started" assert link._choose_capture_side()["node"] == node_vpcs
def test_choose_capture_side(async_run, project): """ The link capture should run on the optimal node """ compute1 = MagicMock() compute2 = MagicMock() compute2.id = "local" # Capture should run on the local node node_vpcs = Node(project, compute1, "node1", node_type="vpcs") node_vpcs._status = "started" node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)] node_iou = Node(project, compute2, "node2", node_type="iou") node_iou._status = "started" node_iou._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() async_run(link.add_node(node_vpcs, 0, 4)) async_run(link.add_node(node_iou, 3, 1)) assert link._choose_capture_side()["node"] == node_iou # Capture should choose always running node node_iou = Node(project, compute1, "node5", node_type="iou") node_iou._status = "started" node_iou._ports = [EthernetPort("E0", 0, 0, 4)] node_switch = Node(project, compute1, "node6", node_type="ethernet_switch") node_switch._status = "started" node_switch._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() async_run(link.add_node(node_iou, 0, 4)) async_run(link.add_node(node_switch, 3, 1)) assert link._choose_capture_side()["node"] == node_switch # Capture should raise error if node are not started node_vpcs = Node(project, compute1, "node1", node_type="vpcs") node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)] node_iou = Node(project, compute2, "node2", node_type="iou") node_iou._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() async_run(link.add_node(node_vpcs, 0, 4)) async_run(link.add_node(node_iou, 3, 1)) with pytest.raises(aiohttp.web.HTTPConflict): link._choose_capture_side() # If you start a node you can capture on it node_vpcs._status = "started" assert link._choose_capture_side()["node"] == node_vpcs
def test_capture(async_run, project): compute1 = MagicMock() node_vpcs = Node(project, compute1, "V1", node_type="vpcs") node_vpcs._status = "started" node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)] node_iou = Node(project, compute1, "I1", node_type="iou") node_iou._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() async_run(link.add_node(node_vpcs, 0, 4)) async_run(link.add_node(node_iou, 3, 1)) capture = async_run(link.start_capture()) assert link.capturing compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/start_capture".format(project.id, node_vpcs.id), data={ "capture_file_name": link.default_capture_file_name(), "data_link_type": "DLT_EN10MB" }) capture = async_run(link.stop_capture()) assert link.capturing is False compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/stop_capture".format(project.id, node_vpcs.id))
def test_capture(async_run, project): compute1 = MagicMock() node_vpcs = Node(project, compute1, "V1", node_type="vpcs") node_vpcs._status = "started" node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)] node_iou = Node(project, compute1, "I1", node_type="iou") node_iou._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() async_run(link.add_node(node_vpcs, 0, 4)) async_run(link.add_node(node_iou, 3, 1)) capture = async_run(link.start_capture()) assert link.capturing compute1.post.assert_any_call( "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/start_capture".format( project.id, node_vpcs.id), data={ "capture_file_name": link.default_capture_file_name(), "data_link_type": "DLT_EN10MB" }) capture = async_run(link.stop_capture()) assert link.capturing is False compute1.post.assert_any_call( "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/stop_capture".format( project.id, node_vpcs.id))
def test_delete(async_run, project): compute1 = MagicMock() compute2 = MagicMock() node1 = Node(project, compute1, "node1", node_type="vpcs") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute2, "node2", node_type="vpcs") node2._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) async_run(link.add_node(node2, 3, 1)) async_run(link.delete()) compute1.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), timeout=120) compute2.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), timeout=120)
def test_read_pcap_from_source(project, async_run): compute1 = MagicMock() node_vpcs = Node(project, compute1, "V1", node_type="vpcs") node_vpcs._status = "started" node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)] node_iou = Node(project, compute1, "I1", node_type="iou") node_iou._ports = [EthernetPort("E0", 0, 3, 1)] link = UDPLink(project) link.create = AsyncioMagicMock() async_run(link.add_node(node_vpcs, 0, 4)) async_run(link.add_node(node_iou, 3, 1)) capture = async_run(link.start_capture()) assert link._capture_node is not None async_run(link.read_pcap_from_source()) link._capture_node["node"].compute.stream_file.assert_called_with(project, "tmp/captures/" + link._capture_file_name)