def test_name(compute, project): """ If node use a name template generate names """ node = Node(project, compute, "PC", node_id=str(uuid.uuid4()), node_type="vpcs", console_type="vnc", properties={"startup_script": "echo test"}) assert node.name == "PC" node = Node(project, compute, "PC{0}", node_id=str(uuid.uuid4()), node_type="vpcs", console_type="vnc", properties={"startup_script": "echo test"}) assert node.name == "PC1" node = Node(project, compute, "PC{0}", node_id=str(uuid.uuid4()), node_type="vpcs", console_type="vnc", properties={"startup_script": "echo test"}) assert node.name == "PC2" # If we change the name to a name already used we patch the name to a free node.name == "PC1" assert node.name == "PC2"
def test_list_ports_atm_switch(project, compute): """ List port for atm switch """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="atm_switch") node.properties["mappings"] = {"1:0:100": "10:0:200"} assert node.__json__()["ports"] == [{ "name": "1", "short_name": "1", "data_link_types": { "ATM": "DLT_ATM_RFC1483" }, "port_number": 1, "adapter_number": 0, "link_type": "serial" }, { "name": "10", "short_name": "10", "data_link_types": { "ATM": "DLT_ATM_RFC1483" }, "port_number": 10, "adapter_number": 0, "link_type": "serial" }]
def test_list_ports_atm_switch(project, compute): """ List port for atm switch """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="atm_switch") node.properties["mappings"] = { "1:0:100": "10:0:200" } assert node.__json__()["ports"] == [ { "name": "1", "short_name": "1", "data_link_types": {"ATM": "DLT_ATM_RFC1483"}, "port_number": 1, "adapter_number": 0, "link_type": "serial" }, { "name": "10", "short_name": "10", "data_link_types": {"ATM": "DLT_ATM_RFC1483"}, "port_number": 10, "adapter_number": 0, "link_type": "serial" } ]
def test_update_filters(async_run, project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.emit_notification = MagicMock() project.dump = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 0, 4)] async_run(link.add_node(node2, 0, 4)) link.update = AsyncioMagicMock() assert link._created async_run(link.update_filters({ "packet_loss": [10], "delay": [50, 10], "frequency_drop": [0], "bpf": [" \n "] })) assert link.filters == { "packet_loss": [10], "delay": [50, 10] } assert link.update.called
def test_list_ports_adapters_cloud(project, compute): """ List port using adapters properties """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="cloud") node.properties["ports_mapping"] = [ { "interface": "eth0", "name": "eth0", "port_number": 0, "type": "ethernet" } ] assert node.__json__()["ports"] == [ { "name": "eth0", "short_name": "eth0", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 0, "adapter_number": 0, "link_type": "ethernet" } ]
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_list_ports_adapters_cloud(project, compute): """ List port using adapters properties """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="cloud") node.properties["ports_mapping"] = [{ "interface": "eth0", "name": "eth0", "port_number": 0, "type": "ethernet" }] assert node.__json__()["ports"] == [{ "name": "eth0", "short_name": "eth0", "data_link_types": { "Ethernet": "DLT_EN10MB" }, "port_number": 0, "adapter_number": 0, "link_type": "ethernet" }]
def test_create(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)] @asyncio.coroutine def subnet_callback(compute2): """ Fake subnet callback """ return ("192.168.1.1", "192.168.1.2") compute1.get_ip_on_same_subnet.side_effect = subnet_callback link = UDPLink(project) async_run(link.add_node(node1, 0, 4)) @asyncio.coroutine def compute1_callback(path, data={}, **kwargs): """ Fake server """ if "/ports/udp" in path: response = MagicMock() response.json = {"udp_port": 1024} return response @asyncio.coroutine def compute2_callback(path, data={}, **kwargs): """ Fake server """ if "/ports/udp" in path: response = MagicMock() response.json = {"udp_port": 2048} return response compute1.post.side_effect = compute1_callback compute1.host = "example.com" compute2.post.side_effect = compute2_callback compute2.host = "example.org" async_run(link.add_node(node2, 3, 1)) compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={ "lport": 1024, "rhost": "192.168.1.2", "rport": 2048, "type": "nio_udp" }, timeout=120) compute2.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), data={ "lport": 2048, "rhost": "192.168.1.1", "rport": 1024, "type": "nio_udp" }, timeout=120)
def test_upload_missing_image(compute, controller, async_run, images_dir): project = Project(str(uuid.uuid4()), controller=controller) node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="qemu", properties={"hda_disk_image": "linux.img"}) open(os.path.join(images_dir, "linux.img"), 'w+').close() assert async_run(node._upload_missing_image("qemu", "linux.img")) is True compute.post.assert_called_with("/qemu/images/linux.img", data=ANY, timeout=None)
def test_default_capture_file_name(project, compute, async_run): node1 = Node(project, compute, "Hello@", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "w0.rld", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 1, 3)] link = Link(project) link.create = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) async_run(link.add_node(node2, 1, 3)) assert link.default_capture_file_name() == "Hello_0-4_to_w0rld_1-3.pcap"
def test_json_serial_link(async_run, project, compute, link): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [SerialPort("S0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [SerialPort("S0", 0, 1, 3)] link = Link(project) link.create = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) async_run(link.add_node(node2, 1, 3)) assert link.__json__()["link_type"] == "serial"
def link(async_run, project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 1, 3)] link = Link(project) link.create = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) async_run(link.add_node(node2, 1, 3)) return link
def test_add_node_cloud(async_run, project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="cloud") node2._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.controller.notification.emit = MagicMock() async_run(link.add_node(node1, 0, 4)) async_run(link.add_node(node2, 0, 4))
def test_start_iou(compute, project, async_run, controller): node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="iou") compute.post = AsyncioMagicMock() # Without licence configured it should raise an error #with pytest.raises(aiohttp.web.HTTPConflict): # async_run(node.start()) controller._iou_license_settings = {"license_check": True, "iourc_content": "aa"} async_run(node.start()) compute.post.assert_called_with("/projects/{}/iou/nodes/{}/start".format(node.project.id, node.id), timeout=240, data={"license_check": True, "iourc_content": "aa"})
def test_start_iou(compute, project, async_run, controller): node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="iou") compute.post = AsyncioMagicMock() # Without licence configured it should raise an error with pytest.raises(aiohttp.web.HTTPConflict): async_run(node.start()) controller.settings["IOU"] = {"iourc_content": "aa"} async_run(node.start()) compute.post.assert_called_with("/projects/{}/iou/nodes/{}/start".format(node.project.id, node.id), timeout=240, data={"iourc_content": "aa"})
def test_add_node_same_node(async_run, project, compute): """ Connection to the same node is not allowed """ node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4), EthernetPort("E1", 0, 0, 5)] link = Link(project) link.create = AsyncioMagicMock() link._project.controller.notification.emit = MagicMock() async_run(link.add_node(node1, 0, 4)) with pytest.raises(aiohttp.web.HTTPConflict): async_run(link.add_node(node1, 0, 5))
def test_eq(compute, project, node, controller): assert node == Node(project, compute, "demo1", node_id=node.id, node_type="qemu") assert node != "a" assert node != Node( project, compute, "demo2", node_id=str(uuid.uuid4()), node_type="qemu") assert node != Node(Project(str(uuid.uuid4()), controller=controller), compute, "demo3", node_id=node.id, node_type="qemu")
def test_list_ports_frame_relay_switch(project, compute): """ List port for frame relay switch """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="frame_relay_switch") node.properties["mappings"] = {"1:101": "10:202", "2:102": "11:203"} assert node.__json__()["ports"] == [{ "name": "1", "short_name": "1", "data_link_types": { "Frame Relay": "DLT_FRELAY" }, "port_number": 1, "adapter_number": 0, "link_type": "serial" }, { "name": "2", "short_name": "2", "data_link_types": { "Frame Relay": "DLT_FRELAY" }, "port_number": 2, "adapter_number": 0, "link_type": "serial" }, { "name": "10", "short_name": "10", "data_link_types": { "Frame Relay": "DLT_FRELAY" }, "port_number": 10, "adapter_number": 0, "link_type": "serial" }, { "name": "11", "short_name": "11", "data_link_types": { "Frame Relay": "DLT_FRELAY" }, "port_number": 11, "adapter_number": 0, "link_type": "serial" }]
def test_add_node_cloud_to_cloud(async_run, project, compute): """ Cloud to cloud connection is not allowed """ node1 = Node(project, compute, "node1", node_type="cloud") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="cloud") node2._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.emit_notification = MagicMock() async_run(link.add_node(node1, 0, 4)) with pytest.raises(aiohttp.web.HTTPConflict): async_run(link.add_node(node2, 0, 4))
def test_available_filters(async_run, project, compute): node1 = Node(project, compute, "node1", node_type="ethernet_switch") node1._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() assert link.available_filters() == [] # Ethernet switch is not supported should return 0 filters async_run(link.add_node(node1, 0, 4)) assert link.available_filters() == [] node2 = Node(project, compute, "node2", node_type="vpcs") node2._ports = [EthernetPort("E0", 0, 0, 4)] async_run(link.add_node(node2, 0, 4)) assert len(link.available_filters()) > 0
def test_add_node_serial_to_ethernet(async_run, project, compute): """ Serial to ethernet connection is not allowed """ node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [SerialPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.controller.notification.emit = MagicMock() async_run(link.add_node(node1, 0, 4)) with pytest.raises(aiohttp.web.HTTPConflict): async_run(link.add_node(node2, 0, 4))
def node(compute, project): node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="vpcs", console_type="vnc", properties={"startup_script": "echo test"}) return node
def test_list_ports_frame_relay_switch(project, compute): """ List port for frame relay switch """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="frame_relay_switch") node.properties["mappings"] = { "1:101": "10:202", "2:102": "11:203" } assert node.__json__()["ports"] == [ { "name": "1", "short_name": "1", "data_link_types": {"Frame Relay": "DLT_FRELAY"}, "port_number": 1, "adapter_number": 0, "link_type": "serial" }, { "name": "2", "short_name": "2", "data_link_types": {"Frame Relay": "DLT_FRELAY"}, "port_number": 2, "adapter_number": 0, "link_type": "serial" }, { "name": "10", "short_name": "10", "data_link_types": {"Frame Relay": "DLT_FRELAY"}, "port_number": 10, "adapter_number": 0, "link_type": "serial" }, { "name": "11", "short_name": "11", "data_link_types": {"Frame Relay": "DLT_FRELAY"}, "port_number": 11, "adapter_number": 0, "link_type": "serial" } ]
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_node_updated(project, async_run): """ If a node stop when capturing we stop the capture """ compute1 = MagicMock() node_vpcs = Node(project, compute1, "V1", node_type="vpcs") node_vpcs._status = "started" link = UDPLink(project) link._capture_node = {"node": node_vpcs} link.stop_capture = AsyncioMagicMock() async_run(link.node_updated(node_vpcs)) assert not link.stop_capture.called node_vpcs._status = "stopped" async_run(link.node_updated(node_vpcs)) assert link.stop_capture.called
def test_delete(async_run, project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.controller.notification.emit = MagicMock() project.dump = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 0, 4)] async_run(link.add_node(node2, 0, 4)) assert link in node2.link async_run(link.delete()) assert link not in node2.link
def test_properties_filter(project, compute): """ Some properties are private and should not be exposed """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="vpcs", console_type="vnc", properties={ "startup_script": "echo test", "iourc_content": "test" }) assert node._properties == { "startup_script": "echo test", "iourc_content": "test" } assert node._filter_properties() == {"startup_script": "echo test"}
def test_vmname(compute, project): """ Additionnal properties should be add to the properties field """ node = Node(project, compute, "PC", node_id=str(uuid.uuid4()), node_type="virtualbox", vmname="test") assert node.properties["vmname"] == "test"
def test_update_filters(async_run, project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.emit_notification = MagicMock() project.dump = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 0, 4)] async_run(link.add_node(node2, 0, 4)) link.update = AsyncioMagicMock() assert link._created async_run( link.update_filters({ "packet_loss": [10], "delay": [50, 10], "frequency_drop": [0], "bpf": [" \n "] })) assert link.filters == {"packet_loss": [10], "delay": [50, 10]} assert link.update.called
def test_add_node(async_run, project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.emit_notification = MagicMock() project.dump = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) assert link._nodes == [ { "node": node1, "port": node1._ports[0], "adapter_number": 0, "port_number": 4, 'label': { 'text': '0/4', 'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;' } } ] assert project.dump.called assert not link._project.emit_notification.called assert not link.create.called # We call link.created only when both side are created node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 0, 4)] async_run(link.add_node(node2, 0, 4)) assert link.create.called link._project.emit_notification.assert_called_with("link.created", link.__json__()) assert link in node2.links
def test_add_node_already_connected(async_run, project, compute): """ Raise an error if we try to use an already connected port """ project.dump = AsyncioMagicMock() node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.controller.notification.emit = MagicMock() async_run(link.add_node(node1, 0, 4)) node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 0, 4)] async_run(link.add_node(node2, 0, 4)) assert link.create.called link2 = Link(project) link2.create = AsyncioMagicMock() with pytest.raises(aiohttp.web.HTTPConflict): async_run(link2.add_node(node1, 0, 4))
def test_add_node(async_run, project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.controller.notification.emit = MagicMock() project.dump = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) assert link._nodes == [ { "node": node1, "port": node1._ports[0], "adapter_number": 0, "port_number": 4, 'label': { 'y': -10, 'text': '0/4', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } } ] assert project.dump.called assert not link._project.controller.notification.emit.called assert not link.create.called # We call link.created only when both side are created node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 0, 4)] async_run(link.add_node(node2, 0, 4)) assert link.create.called link._project.controller.notification.emit.assert_called_with("link.created", link.__json__()) assert link in node2.link
def test_list_ports_ethernet_hub(project, compute): """ List port for atm switch """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="ethernet_hub") node.properties["ports_mapping"] = [{ "name": "Ethernet0", "port_number": 0 }, { "name": "Ethernet1", "port_number": 1 }] assert node.__json__()["ports"] == [{ "name": "Ethernet0", "short_name": "e0", "data_link_types": { "Ethernet": "DLT_EN10MB" }, "port_number": 0, "adapter_number": 0, "link_type": "ethernet" }, { "name": "Ethernet1", "short_name": "e1", "data_link_types": { "Ethernet": "DLT_EN10MB" }, "port_number": 1, "adapter_number": 0, "link_type": "ethernet" }]
def test_empty_properties(compute, project): """ Empty properties need to be ignored """ node = Node(project, compute, "PC", node_id=str(uuid.uuid4()), node_type="virtualbox", aa="", bb=None, category=2, cc="xx") assert "aa" not in node.properties assert "bb" not in node.properties assert "cc" in node.properties assert "category" not in node.properties # Controller only
def test_list_ports_ethernet_hub(project, compute): """ List port for atm switch """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="ethernet_hub") node.properties["ports_mapping"] = [ { "name": "Ethernet0", "port_number": 0 }, { "name": "Ethernet1", "port_number": 1 } ] assert node.__json__()["ports"] == [ { "name": "Ethernet0", "short_name": "e0", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 0, "adapter_number": 0, "link_type": "ethernet" }, { "name": "Ethernet1", "short_name": "e1", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 1, "adapter_number": 0, "link_type": "ethernet" } ]
async def test_add_node_cloud(project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="cloud") node2._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.emit_notification = MagicMock() await link.add_node(node1, 0, 4) await link.add_node(node2, 0, 4)
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)
def test_add_node_iou_no_id_available(async_run, controller): """ Test if an application ID is allocated for IOU nodes """ compute = MagicMock() compute.id = "local" project = async_run( controller.add_project(project_id=str(uuid.uuid4()), name="test")) project.emit_notification = MagicMock() response = MagicMock() compute.post = AsyncioMagicMock(return_value=response) with pytest.raises(aiohttp.web.HTTPConflict): for i in range(1, 513): prop = {"properties": {"application_id": i}} project._nodes[i] = Node(project, compute, "Node{}".format(i), node_id=i, node_type="iou", **prop) async_run(project.add_node(compute, "test1", None, node_type="iou"))
async def test_delete(project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] link = Link(project) link.create = AsyncioMagicMock() link._project.emit_notification = MagicMock() project.dump = AsyncioMagicMock() await link.add_node(node1, 0, 4) node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 0, 4)] await link.add_node(node2, 0, 4) assert link in node2.links await link.delete() assert link not in node2.links
def test_json(async_run, project, compute, link): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 1, 3)] link = Link(project) link.create = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) async_run(link.add_node(node2, 1, 3)) assert link.__json__() == { "link_id": link.id, "project_id": project.id, "nodes": [ { "node_id": node1.id, "adapter_number": 0, "port_number": 4, 'label': { 'y': -10, 'text': '0/4', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } }, { "node_id": node2.id, "adapter_number": 1, "port_number": 3, 'label': { 'y': -10, 'text': '1/3', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } } ], "link_type": "ethernet", "capturing": False, "capture_file_name": None, "capture_file_path": None } assert link.__json__(topology_dump=True) == { "link_id": link.id, "nodes": [ { "node_id": node1.id, "adapter_number": 0, "port_number": 4, 'label': { 'y': -10, 'text': '0/4', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } }, { "node_id": node2.id, "adapter_number": 1, "port_number": 3, 'label': { 'y': -10, 'text': '1/3', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } } ] }
def test_create_one_side_failure(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)] async def subnet_callback(compute2): """ Fake subnet callback """ return ("192.168.1.1", "192.168.1.2") compute1.get_ip_on_same_subnet.side_effect = subnet_callback link = UDPLink(project) async_run(link.add_node(node1, 0, 4)) async def compute1_callback(path, data={}, **kwargs): """ Fake server """ if "/ports/udp" in path: response = MagicMock() response.json = {"udp_port": 1024} return response async def compute2_callback(path, data={}, **kwargs): """ Fake server """ if "/ports/udp" in path: response = MagicMock() response.json = {"udp_port": 2048} return response elif "/adapters" in path: raise aiohttp.web.HTTPConflict(text="Error when creating the NIO") compute1.post.side_effect = compute1_callback compute1.host = "example.com" compute2.post.side_effect = compute2_callback compute2.host = "example.org" with pytest.raises(aiohttp.web.HTTPConflict): async_run(link.add_node(node2, 3, 1)) compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={ "lport": 1024, "rhost": "192.168.1.2", "rport": 2048, "type": "nio_udp", "filters": {}, "suspend": False, }, timeout=120) compute2.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), data={ "lport": 2048, "rhost": "192.168.1.1", "rport": 1024, "type": "nio_udp", "filters": {}, "suspend": False, }, timeout=120) # The link creation has failed we rollback the nio compute1.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), timeout=120)
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_update(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)] async def subnet_callback(compute2): """ Fake subnet callback """ return ("192.168.1.1", "192.168.1.2") compute1.get_ip_on_same_subnet.side_effect = subnet_callback link = UDPLink(project) async_run(link.add_node(node1, 0, 4)) async_run(link.update_filters({"latency": [10]})) async def compute1_callback(path, data={}, **kwargs): """ Fake server """ if "/ports/udp" in path: response = MagicMock() response.json = {"udp_port": 1024} return response async def compute2_callback(path, data={}, **kwargs): """ Fake server """ if "/ports/udp" in path: response = MagicMock() response.json = {"udp_port": 2048} return response compute1.post.side_effect = compute1_callback compute1.host = "example.com" compute2.post.side_effect = compute2_callback compute2.host = "example.org" async_run(link.add_node(node2, 3, 1)) compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={ "lport": 1024, "rhost": "192.168.1.2", "rport": 2048, "type": "nio_udp", "suspend": False, "filters": {"latency": [10]} }, timeout=120) compute2.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), data={ "lport": 2048, "rhost": "192.168.1.1", "rport": 1024, "type": "nio_udp", "suspend": False, "filters": {} }, timeout=120) assert link.created async_run(link.update_filters({"drop": [5], "bpf": ["icmp[icmptype] == 8"]})) compute1.put.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={ "lport": 1024, "rhost": "192.168.1.2", "rport": 2048, "type": "nio_udp", "suspend": False, "filters": { "drop": [5], "bpf": ["icmp[icmptype] == 8"] } }, timeout=120)
def test_list_ports_iou(compute, project): """ IOU has a special behavior 4 port by adapters """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="iou") node.properties["serial_adapters"] = 2 node.properties["ethernet_adapters"] = 3 assert node.__json__()["ports"] == [ { "name": "Ethernet0/0", "short_name": "e0/0", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 0, "adapter_number": 0, "link_type": "ethernet" }, { "name": "Ethernet0/1", "short_name": "e0/1", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 1, "adapter_number": 0, "link_type": "ethernet" }, { "name": "Ethernet0/2", "short_name": "e0/2", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 2, "adapter_number": 0, "link_type": "ethernet" }, { "name": "Ethernet0/3", "short_name": "e0/3", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 3, "adapter_number": 0, "link_type": "ethernet" }, { "name": "Ethernet1/0", "short_name": "e1/0", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 0, "adapter_number": 1, "link_type": "ethernet" }, { "name": "Ethernet1/1", "short_name": "e1/1", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 1, "adapter_number": 1, "link_type": "ethernet" }, { "name": "Ethernet1/2", "short_name": "e1/2", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 2, "adapter_number": 1, "link_type": "ethernet" }, { "name": "Ethernet1/3", "short_name": "e1/3", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 3, "adapter_number": 1, "link_type": "ethernet" }, { "name": "Ethernet2/0", "short_name": "e2/0", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 0, "adapter_number": 2, "link_type": "ethernet" }, { "name": "Ethernet2/1", "short_name": "e2/1", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 1, "adapter_number": 2, "link_type": "ethernet" }, { "name": "Ethernet2/2", "short_name": "e2/2", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 2, "adapter_number": 2, "link_type": "ethernet" }, { "name": "Ethernet2/3", "short_name": "e2/3", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 3, "adapter_number": 2, "link_type": "ethernet" }, { "name": "Serial3/0", "short_name": "s3/0", "data_link_types": { "Frame Relay": "DLT_FRELAY", "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL" }, "port_number": 0, "adapter_number": 3, "link_type": "serial" }, { "name": "Serial3/1", "short_name": "s3/1", "data_link_types": { "Frame Relay": "DLT_FRELAY", "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL" }, "port_number": 1, "adapter_number": 3, "link_type": "serial" }, { "name": "Serial3/2", "short_name": "s3/2", "data_link_types": { "Frame Relay": "DLT_FRELAY", "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL" }, "port_number": 2, "adapter_number": 3, "link_type": "serial" }, { "name": "Serial3/3", "short_name": "s3/3", "data_link_types": { "Frame Relay": "DLT_FRELAY", "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL" }, "port_number": 3, "adapter_number": 3, "link_type": "serial" }, { "name": "Serial4/0", "short_name": "s4/0", "data_link_types": { "Frame Relay": "DLT_FRELAY", "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL" }, "port_number": 0, "adapter_number": 4, "link_type": "serial" }, { "name": "Serial4/1", "short_name": "s4/1", "data_link_types": { "Frame Relay": "DLT_FRELAY", "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL" }, "port_number": 1, "adapter_number": 4, "link_type": "serial" }, { "name": "Serial4/2", "short_name": "s4/2", "data_link_types": { "Frame Relay": "DLT_FRELAY", "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL" }, "port_number": 2, "adapter_number": 4, "link_type": "serial" }, { "name": "Serial4/3", "short_name": "s4/3", "data_link_types": { "Frame Relay": "DLT_FRELAY", "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL" }, "port_number": 3, "adapter_number": 4, "link_type": "serial" } ]
def test_list_ports_dynamips(project, compute): """ List port for dynamips """ node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="dynamips") node.properties["slot0"] = "C7200-IO-FE" node.properties["slot1"] = "GT96100-FE" node.properties["wic0"] = "WIC-2T" node.properties["wic1"] = "WIC-2T" assert node.__json__()["ports"] == [ { "name": "FastEthernet0/0", "short_name": "f0/0", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 0, "adapter_number": 0, "link_type": "ethernet" }, { "name": "FastEthernet1/0", "short_name": "f1/0", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 0, "adapter_number": 1, "link_type": "ethernet" }, { "name": "FastEthernet1/1", "short_name": "f1/1", "data_link_types": {"Ethernet": "DLT_EN10MB"}, "port_number": 1, "adapter_number": 1, "link_type": "ethernet" }, { "name": "Serial0/0", "short_name": "s0/0", "data_link_types": { "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL", "Frame Relay": "DLT_FRELAY"}, "port_number": 16, "adapter_number": 0, "link_type": "serial" }, { "name": "Serial0/1", "short_name": "s0/1", "data_link_types": { "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL", "Frame Relay": "DLT_FRELAY"}, "port_number": 17, "adapter_number": 0, "link_type": "serial" }, { "name": "Serial0/2", "short_name": "s0/2", "data_link_types": { "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL", "Frame Relay": "DLT_FRELAY"}, "port_number": 32, "adapter_number": 0, "link_type": "serial" }, { "name": "Serial0/3", "short_name": "s0/3", "data_link_types": { "Cisco HDLC": "DLT_C_HDLC", "Cisco PPP": "DLT_PPP_SERIAL", "Frame Relay": "DLT_FRELAY"}, "port_number": 33, "adapter_number": 0, "link_type": "serial" } ]