From 0bd0b7ff61ba56799aa84cee917c54bcdc0d054a Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 5 Dec 2016 16:22:41 +0100 Subject: test: l2xc instances multi-context test (CSIT-491) - add/delete L2XC instances and verify results by traffic Change-Id: Ib538a98e499ae3c9c9601a22afcabb1afb84c881 Signed-off-by: Jan Gelety --- test/test_l2xc_multi_instance.py | 351 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 351 insertions(+) create mode 100644 test/test_l2xc_multi_instance.py (limited to 'test/test_l2xc_multi_instance.py') diff --git a/test/test_l2xc_multi_instance.py b/test/test_l2xc_multi_instance.py new file mode 100644 index 00000000..6f5ab6fe --- /dev/null +++ b/test/test_l2xc_multi_instance.py @@ -0,0 +1,351 @@ +#!/usr/bin/env python +"""L2XC Multi-instance Test Case HLD: + +**NOTES:** + - higher number (more than 15) of pg-l2 interfaces causes problems => only \ + 14 pg-l2 interfaces and 10 cross-connects are tested + - jumbo packets in configuration with 14 l2-pg interfaces leads to \ + problems too + +**config 1** + - add 14 pg-l2 interfaces + - add 10 cross-connects (two cross-connects per pair of l2-pg interfaces) + +**test 1** + - send L2 MAC frames between all pairs of pg-l2 interfaces + +**verify 1** + - all packets received correctly in case of cross-connected l2-pg interfaces + - no packet received in case of not cross-connected l2-pg interfaces + +**config 2** + - delete 4 cross-connects + +**test 2** + - send L2 MAC frames between all pairs of pg-l2 interfaces + +**verify 2** + - all packets received correctly in case of cross-connected l2-pg interfaces + - no packet received in case of not cross-connected l2-pg interfaces + +**config 3** + - add new 4 cross-connects + +**test 3** + - send L2 MAC frames between all pairs of pg-l2 interfaces + +**verify 3** + - all packets received correctly in case of cross-connected l2-pg interfaces + - no packet received in case of not cross-connected l2-pg interfaces + +**config 4** + - delete 10 cross-connects + +**test 4** + - send L2 MAC frames between all pairs of pg-l2 interfaces + +**verify 4** + - no packet received on all of l2-pg interfaces (no cross-connect created) +""" + +import unittest +import random + +from scapy.packet import Raw +from scapy.layers.l2 import Ether +from scapy.layers.inet import IP, UDP + +from framework import VppTestCase, VppTestRunner +from util import Host + + +class TestL2xcMultiInst(VppTestCase): + """ L2XC Multi-instance Test Case """ + + @classmethod + def setUpClass(cls): + """ + Perform standard class setup (defined by class method setUpClass in + class VppTestCase) before running the test case, set test case related + variables and configure VPP. + """ + super(TestL2xcMultiInst, cls).setUpClass() + + try: + # Create pg interfaces + cls.create_pg_interfaces(range(14)) + + # Packet flows mapping pg0 -> pg1 etc. + cls.flows = dict() + for i in range(len(cls.pg_interfaces)): + delta = 1 if i % 2 == 0 else -1 + cls.flows[cls.pg_interfaces[i]] = [cls.pg_interfaces[i+delta]] + + # Mapping between packet-generator index and lists of test hosts + cls.hosts_by_pg_idx = dict() + for pg_if in cls.pg_interfaces: + cls.hosts_by_pg_idx[pg_if.sw_if_index] = [] + + # Create test host entries + cls.create_hosts(70) + + # Packet sizes - jumbo packet (9018 bytes) skipped + cls.pg_if_packet_sizes = [64, 512, 1518] + + # Set up all interfaces + for i in cls.pg_interfaces: + i.admin_up() + + # Create list of x-connected pg_interfaces + cls.pg_in_xc = list() + + # Create list of not x-connected pg_interfaces + cls.pg_not_in_xc = list() + for pg_if in cls.pg_interfaces: + cls.pg_not_in_xc.append(pg_if) + + except Exception: + super(TestL2xcMultiInst, cls).tearDownClass() + raise + + def setUp(self): + """ + Clear trace and packet infos before running each test. + """ + super(TestL2xcMultiInst, self).setUp() + self.packet_infos = {} + + def tearDown(self): + """ + Show various debug prints after each test. + """ + super(TestL2xcMultiInst, self).tearDown() + if not self.vpp_dead: + self.logger.info(self.vapi.ppcli("show l2patch")) + + @classmethod + def create_hosts(cls, count): + """ + Create required number of host MAC addresses and distribute them among + interfaces. Create host IPv4 address for every host MAC address. + + :param int count: Number of hosts to create MAC/IPv4 addresses for. + """ + n_int = len(cls.pg_interfaces) + macs_per_if = count / n_int + i = -1 + for pg_if in cls.pg_interfaces: + i += 1 + start_nr = macs_per_if * i + end_nr = count if i == (n_int - 1) else macs_per_if * (i + 1) + hosts = cls.hosts_by_pg_idx[pg_if.sw_if_index] + for j in range(start_nr, end_nr): + host = Host( + "00:00:00:ff:%02x:%02x" % (pg_if.sw_if_index, j), + "172.17.1%02u.%u" % (pg_if.sw_if_index, j)) + hosts.append(host) + + def create_xconnects(self, count, start=0): + """ + Create required number of cross-connects (always two cross-connects per + pair of packet-generator interfaces). + + :param int count: Number of cross-connects to be created. + :param int start: Starting index of packet-generator interfaces. \ + (Default value = 0) + """ + for i in range(count): + rx_if = self.pg_interfaces[i+start] + delta = 1 if i % 2 == 0 else -1 + tx_if = self.pg_interfaces[i+start+delta] + self.vapi.sw_interface_set_l2_xconnect(rx_if.sw_if_index, + tx_if.sw_if_index, 1) + self.logger.info("Cross-connect from %s to %s created" + % (tx_if.name, rx_if.name)) + if self.pg_in_xc.count(rx_if) == 0: + self.pg_in_xc.append(rx_if) + if self.pg_not_in_xc.count(rx_if) == 1: + self.pg_not_in_xc.remove(rx_if) + + def delete_xconnects(self, count, start=0): + """ + Delete required number of cross-connects (always two cross-connects per + pair of packet-generator interfaces). + + :param int count: Number of cross-connects to be deleted. + :param int start: Starting index of packet-generator interfaces. \ + (Default value = 0) + """ + for i in range(count): + rx_if = self.pg_interfaces[i+start] + delta = 1 if i % 2 == 0 else -1 + tx_if = self.pg_interfaces[i+start+delta] + self.vapi.sw_interface_set_l2_xconnect(rx_if.sw_if_index, + tx_if.sw_if_index, 0) + self.logger.info("Cross-connect from %s to %s deleted" + % (tx_if.name, rx_if.name)) + if self.pg_not_in_xc.count(rx_if) == 0: + self.pg_not_in_xc.append(rx_if) + if self.pg_in_xc.count(rx_if) == 1: + self.pg_in_xc.remove(rx_if) + + def create_stream(self, src_if, packet_sizes): + """ + Create input packet stream for defined interface using hosts list. + + :param object src_if: Interface to create packet stream for. + :param list packet_sizes: List of required packet sizes. + :return: Stream of packets. + """ + pkts = [] + src_hosts = self.hosts_by_pg_idx[src_if.sw_if_index] + for dst_if in self.flows[src_if]: + dst_hosts = self.hosts_by_pg_idx[dst_if.sw_if_index] + n_int = len(dst_hosts) + for i in range(0, n_int): + dst_host = dst_hosts[i] + src_host = random.choice(src_hosts) + pkt_info = self.create_packet_info( + src_if.sw_if_index, dst_if.sw_if_index) + payload = self.info_to_payload(pkt_info) + p = (Ether(dst=dst_host.mac, src=src_host.mac) / + IP(src=src_host.ip4, dst=dst_host.ip4) / + UDP(sport=1234, dport=1234) / + Raw(payload)) + pkt_info.data = p.copy() + size = random.choice(packet_sizes) + self.extend_packet(p, size) + pkts.append(p) + self.logger.debug("Input stream created for port %s. Length: %u pkt(s)" + % (src_if.name, len(pkts))) + return pkts + + def verify_capture(self, pg_if, capture): + """ + Verify captured input packet stream for defined interface. + + :param object pg_if: Interface to verify captured packet stream for. + :param list capture: Captured packet stream. + """ + last_info = dict() + for i in self.pg_interfaces: + last_info[i.sw_if_index] = None + dst_sw_if_index = pg_if.sw_if_index + for packet in capture: + payload_info = self.payload_to_info(str(packet[Raw])) + try: + ip = packet[IP] + udp = packet[UDP] + packet_index = payload_info.index + self.assertEqual(payload_info.dst, dst_sw_if_index) + self.logger.debug("Got packet on port %s: src=%u (id=%u)" % + (pg_if.name, payload_info.src, packet_index)) + next_info = self.get_next_packet_info_for_interface2( + payload_info.src, dst_sw_if_index, + last_info[payload_info.src]) + last_info[payload_info.src] = next_info + self.assertTrue(next_info is not None) + self.assertEqual(packet_index, next_info.index) + saved_packet = next_info.data + # Check standard fields + self.assertEqual(ip.src, saved_packet[IP].src) + self.assertEqual(ip.dst, saved_packet[IP].dst) + self.assertEqual(udp.sport, saved_packet[UDP].sport) + self.assertEqual(udp.dport, saved_packet[UDP].dport) + except: + self.logger.error("Unexpected or invalid packet:") + self.logger.error(packet.show()) + raise + for i in self.pg_interfaces: + remaining_packet = self.get_next_packet_info_for_interface2( + i, dst_sw_if_index, last_info[i.sw_if_index]) + self.assertTrue( + remaining_packet is None, + "Port %u: Packet expected from source %u didn't arrive" % + (dst_sw_if_index, i.sw_if_index)) + + def run_verify_test(self): + """ + Create packet streams for all configured l2-pg interfaces, send all + prepared packet streams and verify that: + - all packets received correctly on all pg-l2 interfaces assigned \ + to cross-connects + - no packet received on all pg-l2 interfaces not assigned to \ + cross-connects + + :raise: RuntimeError if no packet captured on l2-pg interface assigned \ + to the cross-connect or if any packet is captured on l2-pg interface \ + not assigned to the cross-connect. + """ + # Test + # Create incoming packet streams for packet-generator interfaces + for pg_if in self.pg_interfaces: + pkts = self.create_stream(pg_if, self.pg_if_packet_sizes) + pg_if.add_stream(pkts) + + # Enable packet capture and start packet sending + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + + # Verify + # Verify outgoing packet streams per packet-generator interface + for pg_if in self.pg_interfaces: + capture = pg_if.get_capture() + if pg_if in self.pg_in_xc: + if len(capture) == 0: + raise RuntimeError("Interface %s is cross-connect sink but " + "the capture is empty!" % pg_if.name) + self.verify_capture(pg_if, capture) + elif pg_if in self.pg_not_in_xc: + try: + self.assertEqual(len(capture), 0) + except AssertionError: + raise RuntimeError("Interface %s is not cross-connect sink " + "but the capture is not empty!" + % pg_if.name) + else: + self.logger.error("Unknown interface: %s" % pg_if.name) + + def test_l2xc_inst_01(self): + """ L2XC Multi-instance test 1 - create 10 cross-connects + """ + # Config 1 + # Create 10 cross-connects + self.create_xconnects(10) + + # Test 1 + self.run_verify_test() + + def test_l2xc_inst_02(self): + """ L2XC Multi-instance test 2 - delete 4 cross-connects + """ + # Config 2 + # Delete 4 cross-connects + self.delete_xconnects(4) + + # Test 2 + self.run_verify_test() + + def test_l2xc_inst_03(self): + """ L2BD Multi-instance 3 - add new 4 cross-connects + """ + # Config 3 + # Add new 4 cross-connects + self.create_xconnects(4, start=10) + + # Test 3 + self.run_verify_test() + + def test_l2xc_inst_04(self): + """ L2XC Multi-instance test 4 - delete 10 cross-connects + """ + # Config 4 + # Delete 10 cross-connects + self.delete_xconnects(10, start=4) + + # Test 4 + self.run_verify_test() + + +if __name__ == '__main__': + unittest.main(testRunner=VppTestRunner) -- cgit 1.2.3-korg From deb698447172d8cae94df9871bb32a9d21705dbb Mon Sep 17 00:00:00 2001 From: Matej Klotton Date: Fri, 9 Dec 2016 15:05:46 +0100 Subject: make test: Use VXLAN built in scapy 2.3.3 - fix documentation issues. - fix mpls test. Change-Id: Ieef6b4b5e4aca99e89bd03e45a991be89d42adba Signed-off-by: Matej Klotton --- test/scapy_handlers/vxlan.py | 17 ----------- test/test_l2bd_multi_instance.py | 19 +++++++------ test/test_l2xc_multi_instance.py | 19 +++++++------ test/test_mpls.py | 61 ++++++++++++++++++++++------------------ test/test_vxlan.py | 8 ++++-- test/vpp_papi_provider.py | 3 +- 6 files changed, 60 insertions(+), 67 deletions(-) delete mode 100644 test/scapy_handlers/vxlan.py (limited to 'test/test_l2xc_multi_instance.py') diff --git a/test/scapy_handlers/vxlan.py b/test/scapy_handlers/vxlan.py deleted file mode 100644 index bf86f179..00000000 --- a/test/scapy_handlers/vxlan.py +++ /dev/null @@ -1,17 +0,0 @@ -from scapy.fields import BitField, XByteField, X3BytesField -from scapy.packet import Packet, bind_layers -from scapy.layers.l2 import Ether -from scapy.layers.inet import UDP - - -class VXLAN(Packet): - name = "VXLAN" - fields_desc = [BitField("flags", 0x08000000, 32), - X3BytesField("vni", 0), - XByteField("reserved", 0x00)] - - def mysummary(self): - return self.sprintf("VXLAN (vni=%VXLAN.vni%)") - -bind_layers(UDP, VXLAN, dport=4789) -bind_layers(VXLAN, Ether) diff --git a/test/test_l2bd_multi_instance.py b/test/test_l2bd_multi_instance.py index e0b4b266..417df9e1 100644 --- a/test/test_l2bd_multi_instance.py +++ b/test/test_l2bd_multi_instance.py @@ -366,16 +366,17 @@ class TestL2bdMultiInst(VppTestCase): def run_verify_test(self): """ - Create packet streams for all configured l2-pg interfaces, send all + Create packet streams for all configured l2-pg interfaces, send all \ prepared packet streams and verify that: - - all packets received correctly on all pg-l2 interfaces assigned \ - to bridge domains - - no packet received on all pg-l2 interfaces not assigned to \ - bridge domains - - :raise: RuntimeError if no packet captured on l2-pg interface assigned \ - to the bridge domain or if any packet is captured on l2-pg interface \ - not assigned to the bridge domain. + - all packets received correctly on all pg-l2 interfaces assigned + to bridge domains + - no packet received on all pg-l2 interfaces not assigned to + bridge domains + + :raise RuntimeError: if no packet captured on l2-pg interface assigned + to the bridge domain or if any packet is captured + on l2-pg interface not assigned to the bridge + domain. """ # Test # Create incoming packet streams for packet-generator interfaces diff --git a/test/test_l2xc_multi_instance.py b/test/test_l2xc_multi_instance.py index 6f5ab6fe..4de76917 100644 --- a/test/test_l2xc_multi_instance.py +++ b/test/test_l2xc_multi_instance.py @@ -266,16 +266,17 @@ class TestL2xcMultiInst(VppTestCase): def run_verify_test(self): """ - Create packet streams for all configured l2-pg interfaces, send all + Create packet streams for all configured l2-pg interfaces, send all \ prepared packet streams and verify that: - - all packets received correctly on all pg-l2 interfaces assigned \ - to cross-connects - - no packet received on all pg-l2 interfaces not assigned to \ - cross-connects - - :raise: RuntimeError if no packet captured on l2-pg interface assigned \ - to the cross-connect or if any packet is captured on l2-pg interface \ - not assigned to the cross-connect. + - all packets received correctly on all pg-l2 interfaces assigned + to cross-connects + - no packet received on all pg-l2 interfaces not assigned to + cross-connects + + :raise RuntimeError: if no packet captured on l2-pg interface assigned + to the cross-connect or if any packet is captured + on l2-pg interface not assigned to the + cross-connect. """ # Test # Create incoming packet streams for packet-generator interfaces diff --git a/test/test_mpls.py b/test/test_mpls.py index 92a579f2..e6a1e8c5 100644 --- a/test/test_mpls.py +++ b/test/test_mpls.py @@ -4,7 +4,6 @@ import unittest import socket from framework import VppTestCase, VppTestRunner -from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint from vpp_ip_route import IpRoute, RoutePath, MplsRoute, MplsIpBind from scapy.packet import Raw @@ -15,7 +14,6 @@ from scapy.contrib.mpls import MPLS from util import ppp - class TestMPLS(VppTestCase): """ MPLS Test Case """ @@ -97,11 +95,12 @@ class TestMPLS(VppTestCase): pkts.append(p) return pkts - def verify_filter(self, capture, sent): + @staticmethod + def verify_filter(capture, sent): if not len(capture) == len(sent): - # filter out any IPv6 RAs from the captur + # filter out any IPv6 RAs from the capture for p in capture: - if (p.haslayer(IPv6)): + if p.haslayer(IPv6): capture.remove(p) return capture @@ -327,8 +326,8 @@ class TestMPLS(VppTestCase): try: self.assertEqual(0, len(rx)) except: - error("MPLS non-EOS packets popped and forwarded") - error(packet.show()) + self.logger.error("MPLS non-EOS packets popped and forwarded") + self.logger.error(ppp("", rx)) raise # @@ -352,7 +351,8 @@ class TestMPLS(VppTestCase): self.verify_capture_labelled_ip4(self.pg0, rx, tx, [33, 44, 45]) # - # A recursive non-EOS x-connect, which resolves through another x-connect + # A recursive non-EOS x-connect, which resolves through another + # x-connect # route_34_neos = MplsRoute(self, 34, 0, [RoutePath("0.0.0.0", @@ -369,11 +369,12 @@ class TestMPLS(VppTestCase): self.pg_start() rx = self.pg0.get_capture() - # it's the 2nd (counting from 0) lael in the stack that is swapped + # it's the 2nd (counting from 0) label in the stack that is swapped self.verify_capture_labelled(self.pg0, rx, tx, [33, 44, 46, 99], num=2) # - # an recursive IP route that resolves through the recursive non-eos x-connect + # an recursive IP route that resolves through the recursive non-eos + # x-connect # ip_10_0_0_1 = IpRoute(self, "10.0.0.1", 32, [RoutePath("0.0.0.0", @@ -505,7 +506,7 @@ class TestMPLS(VppTestCase): self.verify_capture_labelled_ip4(self.pg0, rx, tx, [32, 33, 34]) # - # add a recursive path, with ouput label, via the 1 label route + # add a recursive path, with output label, via the 1 label route # route_11_0_0_1 = IpRoute(self, "11.0.0.1", 32, [RoutePath("10.0.0.1", @@ -567,14 +568,16 @@ class TestMPLS(VppTestCase): # nh_addr = socket.inet_pton(socket.AF_INET, self.pg0.remote_ip4) - reply = self.vapi.mpls_tunnel_add_del(0xffffffff, # don't know the if index yet - 1, # IPv4 next-hop - nh_addr, - self.pg0.sw_if_index, - 0, # next-hop-table-id - 1, # next-hop-weight - 2, # num-out-labels, - [44, 46]) + reply = self.vapi.mpls_tunnel_add_del( + 0xffffffff, # don't know the if index yet + 1, # IPv4 next-hop + nh_addr, + self.pg0.sw_if_index, + 0, # next-hop-table-id + 1, # next-hop-weight + 2, # num-out-labels, + [44, 46] + ) self.vapi.sw_interface_set_flags(reply.sw_if_index, admin_up_down=1) # @@ -584,15 +587,17 @@ class TestMPLS(VppTestCase): nh_addr = socket.inet_pton(socket.AF_INET, "0.0.0.0") dest_addr_len = 32 - self.vapi.ip_add_del_route(dest_addr, - dest_addr_len, - nh_addr, # all zeros next-hop - tunnel is p2p - reply.sw_if_index, # sw_if_index of the new tunnel - 0, # table-id - 0, # next-hop-table-id - 1, # next-hop-weight - 0, # num-out-labels, - []) # out-label + self.vapi.ip_add_del_route( + dest_addr, + dest_addr_len, + nh_addr, # all zeros next-hop - tunnel is p2p + reply.sw_if_index, # sw_if_index of the new tunnel + 0, # table-id + 0, # next-hop-table-id + 1, # next-hop-weight + 0, # num-out-labels, + [] # out-label + ) self.vapi.cli("clear trace") tx = self.create_stream_ip4(self.pg0, "10.0.0.3") diff --git a/test/test_vxlan.py b/test/test_vxlan.py index ac435852..1978cf0c 100644 --- a/test/test_vxlan.py +++ b/test/test_vxlan.py @@ -6,7 +6,7 @@ from template_bd import BridgeDomain from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP -from scapy_handlers.vxlan import VXLAN +from scapy.layers.vxlan import VXLAN class TestVxlan(BridgeDomain, VppTestCase): @@ -24,13 +24,15 @@ class TestVxlan(BridgeDomain, VppTestCase): return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / UDP(sport=self.dport, dport=self.dport, chksum=0) / - VXLAN(vni=self.vni) / + VXLAN(vni=self.vni, flags=self.flags) / pkt) def decapsulate(self, pkt): """ Decapsulate the original payload frame by removing VXLAN header """ + # check if is set I flag + self.assertEqual(pkt[VXLAN].flags, int('0x8', 16)) return pkt[VXLAN].payload # Method for checking VXLAN encapsulation. @@ -62,6 +64,7 @@ class TestVxlan(BridgeDomain, VppTestCase): try: cls.dport = 4789 + cls.flags = 0x8 cls.vni = 1 # Create 2 pg interfaces. @@ -95,5 +98,6 @@ class TestVxlan(BridgeDomain, VppTestCase): if not self.vpp_dead: self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) + if __name__ == '__main__': unittest.main(testRunner=VppTestRunner) diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py index 3fdb428e..9200eeaa 100644 --- a/test/vpp_papi_provider.py +++ b/test/vpp_papi_provider.py @@ -581,8 +581,7 @@ class VppPapiProvider(object): :param sw_if_index_from: :param sw_if_index_to: - :param enable - + :param state: """ return self.api(self.papi.sw_interface_span_enable_disable, {'sw_if_index_from': sw_if_index_from, -- cgit 1.2.3-korg From 9225dee9655ce607130f9bab5472441b72e25858 Mon Sep 17 00:00:00 2001 From: Klement Sekera Date: Mon, 12 Dec 2016 08:36:58 +0100 Subject: make test: improve robustness and performance Introduce an API which asserts empty capture for interface. Throw exception in old API if the capture does not exist, thus making it clear if the test expects packets to arrive or not. Improve performance by not doing sleeps after starting the packet generator, rather lazily deleting captures when needed. Fix wrong usage of packet.show() in various tests. Change-Id: I456cb23316eef99b3f35f80344fe595c4db9a21c Signed-off-by: Klement Sekera --- Makefile | 4 +- test/framework.py | 41 ++++++++++++++------ test/test_bfd.py | 2 +- test/test_gre.py | 62 +++++++++++------------------- test/test_l2_fib.py | 14 ++----- test/test_l2bd_multi_instance.py | 22 +++++------ test/test_l2xc_multi_instance.py | 31 ++++++--------- test/test_lb.py | 16 +++++--- test/test_mpls.py | 29 +++----------- test/test_snat.py | 24 +++++------- test/util.py | 22 +++++++++++ test/vpp_pg_interface.py | 82 +++++++++++++++++++++++++++++----------- 12 files changed, 189 insertions(+), 160 deletions(-) (limited to 'test/test_l2xc_multi_instance.py') diff --git a/Makefile b/Makefile index 50155664..1c7534cf 100644 --- a/Makefile +++ b/Makefile @@ -217,8 +217,6 @@ build-vpp-api: $(BR)/.bootstrap.ok VPP_PYTHON_PREFIX=$(BR)/python - - #$(if $(filter-out $(3),retest),make -C $(BR) PLATFORM=$(1) TAG=$(2) vpp-install ,) define test $(if $(filter-out $(3),retest),make -C $(BR) PLATFORM=$(1) TAG=$(2) vpp-api-install plugins-install vpp-install,) make -C test \ @@ -250,7 +248,7 @@ test-doc: test-wipe-doc: @make -C test wipe-doc BR=$(BR) -test-cov: +test-cov: bootstrap $(call test,vpp_lite,vpp_lite_gcov,cov) test-wipe-cov: diff --git a/test/framework.py b/test/framework.py index 2618b267..1c3e56cc 100644 --- a/test/framework.py +++ b/test/framework.py @@ -5,7 +5,6 @@ import unittest import tempfile import time import resource -from time import sleep from collections import deque from threading import Thread from inspect import getdoc @@ -181,7 +180,8 @@ class VppTestCase(unittest.TestCase): cls.logger.info("Temporary dir is %s, shm prefix is %s", cls.tempdir, cls.shm_prefix) cls.setUpConstants() - cls.pg_streams = [] + cls._captures = [] + cls._zombie_captures = [] cls.packet_infos = {} cls.verbose = 0 cls.vpp_dead = False @@ -312,17 +312,36 @@ class VppTestCase(unittest.TestCase): i.enable_capture() @classmethod - def pg_start(cls, sleep_time=1): - """ - Enable the packet-generator and send all prepared packet streams - Remove the packet streams afterwards - """ + def register_capture(cls, cap_name): + """ Register a capture in the testclass """ + # add to the list of captures with current timestamp + cls._captures.append((time.time(), cap_name)) + # filter out from zombies + cls._zombie_captures = [(stamp, name) + for (stamp, name) in cls._zombie_captures + if name != cap_name] + + @classmethod + def pg_start(cls): + """ Remove any zombie captures and enable the packet generator """ + # how long before capture is allowed to be deleted - otherwise vpp + # crashes - 100ms seems enough (this shouldn't be needed at all) + capture_ttl = 0.1 + now = time.time() + for stamp, cap_name in cls._zombie_captures: + wait = stamp + capture_ttl - now + if wait > 0: + cls.logger.debug("Waiting for %ss before deleting capture %s", + wait, cap_name) + time.sleep(wait) + now = time.time() + cls.logger.debug("Removing zombie capture %s" % cap_name) + cls.vapi.cli('packet-generator delete %s' % cap_name) + cls.vapi.cli("trace add pg-input 50") # 50 is maximum cls.vapi.cli('packet-generator enable') - sleep(sleep_time) # give VPP some time to process the packets - for stream in cls.pg_streams: - cls.vapi.cli('packet-generator delete %s' % stream) - cls.pg_streams = [] + cls._zombie_captures = cls._captures + cls._captures = [] @classmethod def create_pg_interfaces(cls, interfaces): diff --git a/test/test_bfd.py b/test/test_bfd.py index bf0e88dd..c1095d22 100644 --- a/test/test_bfd.py +++ b/test/test_bfd.py @@ -98,7 +98,7 @@ class BFDTestSession(object): p = self.create_packet() self.test.logger.debug(ppp("Sending packet:", p)) self.test.pg0.add_stream([p]) - self.test.pg_start(sleep_time=0) + self.test.pg_start() def verify_packet(self, packet): """ Verify correctness of BFD layer. """ diff --git a/test/test_gre.py b/test/test_gre.py index 0b508285..59d03e93 100644 --- a/test/test_gre.py +++ b/test/test_gre.py @@ -1,22 +1,22 @@ #!/usr/bin/env python import unittest -import socket from logging import * from framework import VppTestCase, VppTestRunner -from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint +from vpp_sub_interface import VppDot1QSubint from vpp_gre_interface import VppGreInterface from vpp_ip_route import IpRoute, RoutePath from vpp_papi_provider import L2_VTR_OP from scapy.packet import Raw -from scapy.layers.l2 import Ether, Dot1Q, ARP, GRE +from scapy.layers.l2 import Ether, Dot1Q, GRE from scapy.layers.inet import IP, UDP -from scapy.layers.inet6 import ICMPv6ND_NS, ICMPv6ND_RA, IPv6, UDP -from scapy.contrib.mpls import MPLS +from scapy.layers.inet6 import ICMPv6ND_RA, IPv6 from scapy.volatile import RandMAC, RandIP +from util import ppp, ppc + class TestGRE(VppTestCase): """ GRE Test Case """ @@ -131,7 +131,7 @@ class TestGRE(VppTestCase): def verify_filter(self, capture, sent): if not len(capture) == len(sent): - # filter out any IPv6 RAs from the captur + # filter out any IPv6 RAs from the capture for p in capture: if (p.haslayer(ICMPv6ND_RA)): capture.remove(p) @@ -163,8 +163,8 @@ class TestGRE(VppTestCase): self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl) except: - rx.show() - tx.show() + self.logger.error(ppp("Rx:", rx)) + self.logger.error(ppp("Tx:", tx)) raise def verify_tunneled_l2o4(self, src_if, capture, sent, @@ -196,8 +196,8 @@ class TestGRE(VppTestCase): self.assertEqual(rx_ip.ttl, tx_ip.ttl) except: - rx.show() - tx.show() + self.logger.error(ppp("Rx:", rx)) + self.logger.error(ppp("Tx:", tx)) raise def verify_tunneled_vlano4(self, src_if, capture, sent, @@ -206,7 +206,7 @@ class TestGRE(VppTestCase): capture = self.verify_filter(capture, sent) self.assertEqual(len(capture), len(sent)) except: - capture.show() + ppc("Unexpected packets captured:", capture) raise for i in range(len(capture)): @@ -237,8 +237,8 @@ class TestGRE(VppTestCase): self.assertEqual(rx_ip.ttl, tx_ip.ttl) except: - rx.show() - tx.show() + self.logger.error(ppp("Rx:", rx)) + self.logger.error(ppp("Tx:", tx)) raise def verify_decapped_4o4(self, src_if, capture, sent): @@ -261,8 +261,8 @@ class TestGRE(VppTestCase): self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl) except: - rx.show() - tx.show() + self.logger.error(ppp("Rx:", rx)) + self.logger.error(ppp("Tx:", tx)) raise def verify_decapped_6o4(self, src_if, capture, sent): @@ -284,8 +284,8 @@ class TestGRE(VppTestCase): self.assertEqual(rx_ip.hlim + 1, tx_ip.hlim) except: - rx.show() - tx.show() + self.logger.error(ppp("Rx:", rx)) + self.logger.error(ppp("Tx:", tx)) raise def test_gre(self): @@ -333,14 +333,8 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() - - try: - self.assertEqual(0, len(rx)) - except: - error("GRE packets forwarded without DIP resolved") - error(rx.show()) - raise + self.pg0.assert_nothing_captured( + remark="GRE packets forwarded without DIP resolved") # # Add a route that resolves the tunnel's destination @@ -397,13 +391,8 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() - try: - self.assertEqual(0, len(rx)) - except: - error("GRE packets forwarded despite no SRC address match") - error(rx.show()) - raise + self.pg0.assert_nothing_captured( + remark="GRE packets forwarded despite no SRC address match") # # Configure IPv6 on the PG interface so we can route IPv6 @@ -427,13 +416,8 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() - try: - self.assertEqual(0, len(rx)) - except: - error("IPv6 GRE packets forwarded despite IPv6 not enabled on tunnel") - error(rx.show()) - raise + self.pg0.assert_nothing_captured(remark="IPv6 GRE packets forwarded " + "despite IPv6 not enabled on tunnel") # # Enable IPv6 on the tunnel diff --git a/test/test_l2_fib.py b/test/test_l2_fib.py index eb4f4e32..4855a3ea 100644 --- a/test/test_l2_fib.py +++ b/test/test_l2_fib.py @@ -68,7 +68,7 @@ from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP from framework import VppTestCase, VppTestRunner -from util import Host +from util import Host, ppp class TestL2fib(VppTestCase): @@ -282,8 +282,7 @@ class TestL2fib(VppTestCase): self.assertEqual(udp.sport, saved_packet[UDP].sport) self.assertEqual(udp.dport, saved_packet[UDP].dport) except: - self.logger.error("Unexpected or invalid packet:") - self.logger.error(packet.show()) + self.logger.error(ppp("Unexpected or invalid packet:", packet)) raise for i in self.pg_interfaces: remaining_packet = self.get_next_packet_info_for_interface2( @@ -327,14 +326,7 @@ class TestL2fib(VppTestCase): # Verify # Verify outgoing packet streams per packet-generator interface for i in self.pg_interfaces: - capture = i.get_capture() - self.logger.info("Verifying capture on interface %s" % i.name) - try: - self.assertEqual(len(capture), 0) - except AssertionError: - self.logger.error("The capture on interface %s is not empty!" - % i.name) - raise AssertionError("%d != 0" % len(capture)) + i.assert_nothing_captured(remark="outgoing interface") def test_l2_fib_01(self): """ L2 FIB test 1 - program 100 MAC addresses diff --git a/test/test_l2bd_multi_instance.py b/test/test_l2bd_multi_instance.py index 417df9e1..1272d765 100644 --- a/test/test_l2bd_multi_instance.py +++ b/test/test_l2bd_multi_instance.py @@ -70,7 +70,8 @@ from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP from framework import VppTestCase, VppTestRunner -from util import Host +from util import Host, ppp + @unittest.skip("Crashes VPP") class TestL2bdMultiInst(VppTestCase): @@ -92,12 +93,12 @@ class TestL2bdMultiInst(VppTestCase): # Packet flows mapping pg0 -> pg1, pg2 etc. cls.flows = dict() for i in range(0, len(cls.pg_interfaces), 3): - cls.flows[cls.pg_interfaces[i]] = [cls.pg_interfaces[i+1], - cls.pg_interfaces[i+2]] - cls.flows[cls.pg_interfaces[i+1]] = [cls.pg_interfaces[i], - cls.pg_interfaces[i+2]] - cls.flows[cls.pg_interfaces[i+2]] = [cls.pg_interfaces[i], - cls.pg_interfaces[i+1]] + cls.flows[cls.pg_interfaces[i]] = [cls.pg_interfaces[i + 1], + cls.pg_interfaces[i + 2]] + cls.flows[cls.pg_interfaces[i + 1]] = [cls.pg_interfaces[i], + cls.pg_interfaces[i + 2]] + cls.flows[cls.pg_interfaces[i + 2]] = [cls.pg_interfaces[i], + cls.pg_interfaces[i + 1]] # Mapping between packet-generator index and lists of test hosts cls.hosts_by_pg_idx = dict() @@ -188,7 +189,7 @@ class TestL2bdMultiInst(VppTestCase): if self.bd_deleted_list.count(bd_id) == 1: self.bd_deleted_list.remove(bd_id) for j in range(3): - pg_if = self.pg_interfaces[(i+start-1)*3+j] + pg_if = self.pg_interfaces[(i + start - 1) * 3 + j] self.vapi.sw_interface_set_l2_bridge(pg_if.sw_if_index, bd_id=bd_id) self.logger.info("pg-interface %s added to bridge domain ID %d" @@ -221,7 +222,7 @@ class TestL2bdMultiInst(VppTestCase): if self.bd_deleted_list.count(bd_id) == 0: self.bd_deleted_list.append(bd_id) for j in range(3): - pg_if = self.pg_interfaces[(i+start-1)*3+j] + pg_if = self.pg_interfaces[(i + start - 1) * 3 + j] self.pg_in_bd.remove(pg_if) self.pg_not_in_bd.append(pg_if) self.logger.info("Bridge domain ID %d deleted" % bd_id) @@ -290,8 +291,7 @@ class TestL2bdMultiInst(VppTestCase): self.assertEqual(udp.sport, saved_packet[UDP].sport) self.assertEqual(udp.dport, saved_packet[UDP].dport) except: - self.logger.error("Unexpected or invalid packet:") - self.logger.error(packet.show()) + self.logger.error(ppp("Unexpected or invalid packet:", packet)) raise for i in self.pg_interfaces: remaining_packet = self.get_next_packet_info_for_interface2( diff --git a/test/test_l2xc_multi_instance.py b/test/test_l2xc_multi_instance.py index 4de76917..2e55674e 100644 --- a/test/test_l2xc_multi_instance.py +++ b/test/test_l2xc_multi_instance.py @@ -56,7 +56,7 @@ from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP from framework import VppTestCase, VppTestRunner -from util import Host +from util import Host, ppp class TestL2xcMultiInst(VppTestCase): @@ -79,7 +79,7 @@ class TestL2xcMultiInst(VppTestCase): cls.flows = dict() for i in range(len(cls.pg_interfaces)): delta = 1 if i % 2 == 0 else -1 - cls.flows[cls.pg_interfaces[i]] = [cls.pg_interfaces[i+delta]] + cls.flows[cls.pg_interfaces[i]] = [cls.pg_interfaces[i + delta]] # Mapping between packet-generator index and lists of test hosts cls.hosts_by_pg_idx = dict() @@ -155,9 +155,9 @@ class TestL2xcMultiInst(VppTestCase): (Default value = 0) """ for i in range(count): - rx_if = self.pg_interfaces[i+start] + rx_if = self.pg_interfaces[i + start] delta = 1 if i % 2 == 0 else -1 - tx_if = self.pg_interfaces[i+start+delta] + tx_if = self.pg_interfaces[i + start + delta] self.vapi.sw_interface_set_l2_xconnect(rx_if.sw_if_index, tx_if.sw_if_index, 1) self.logger.info("Cross-connect from %s to %s created" @@ -177,9 +177,9 @@ class TestL2xcMultiInst(VppTestCase): (Default value = 0) """ for i in range(count): - rx_if = self.pg_interfaces[i+start] + rx_if = self.pg_interfaces[i + start] delta = 1 if i % 2 == 0 else -1 - tx_if = self.pg_interfaces[i+start+delta] + tx_if = self.pg_interfaces[i + start + delta] self.vapi.sw_interface_set_l2_xconnect(rx_if.sw_if_index, tx_if.sw_if_index, 0) self.logger.info("Cross-connect from %s to %s deleted" @@ -253,8 +253,7 @@ class TestL2xcMultiInst(VppTestCase): self.assertEqual(udp.sport, saved_packet[UDP].sport) self.assertEqual(udp.dport, saved_packet[UDP].dport) except: - self.logger.error("Unexpected or invalid packet:") - self.logger.error(packet.show()) + self.logger.error(ppp("Unexpected or invalid packet:", packet)) raise for i in self.pg_interfaces: remaining_packet = self.get_next_packet_info_for_interface2( @@ -291,21 +290,15 @@ class TestL2xcMultiInst(VppTestCase): # Verify # Verify outgoing packet streams per packet-generator interface for pg_if in self.pg_interfaces: - capture = pg_if.get_capture() if pg_if in self.pg_in_xc: - if len(capture) == 0: - raise RuntimeError("Interface %s is cross-connect sink but " - "the capture is empty!" % pg_if.name) + capture = pg_if.get_capture( + remark="interface is a cross-connect sink") self.verify_capture(pg_if, capture) elif pg_if in self.pg_not_in_xc: - try: - self.assertEqual(len(capture), 0) - except AssertionError: - raise RuntimeError("Interface %s is not cross-connect sink " - "but the capture is not empty!" - % pg_if.name) + pg_if.assert_nothing_captured( + remark="interface is not a cross-connect sink") else: - self.logger.error("Unknown interface: %s" % pg_if.name) + raise Exception("Unexpected interface: %s" % pg_if.name) def test_l2xc_inst_01(self): """ L2XC Multi-instance test 1 - create 10 cross-connects diff --git a/test/test_lb.py b/test/test_lb.py index 3e7f5e13..7037d80c 100644 --- a/test/test_lb.py +++ b/test/test_lb.py @@ -1,7 +1,7 @@ import socket from scapy.layers.inet import IP, UDP -from scapy.layers.inet6 import IPv6 +from scapy.layers.inet6 import ICMPv6ND_RA, IPv6 from scapy.layers.l2 import Ether, GRE from scapy.packet import Raw @@ -95,10 +95,16 @@ class TestLB(VppTestCase): self.assertEqual(str(inner), str(self.info.data[IPver])) def checkCapture(self, gre4, isv4): - out = self.pg0.get_capture() - # This check is edited because RA appears in output, maybe disable RA? - # self.assertEqual(len(out), 0) - self.assertLess(len(out), 20) + # RA might appear in capture + try: + out = self.pg0.get_capture() + # filter out any IPv6 RAs from the capture + for p in out: + if (p.haslayer(ICMPv6ND_RA)): + out.remove(p) + self.assertEqual(len(out), 0) + except: + pass out = self.pg1.get_capture() self.assertEqual(len(out), len(self.packets)) diff --git a/test/test_mpls.py b/test/test_mpls.py index 24fc4129..6d5eeb2b 100644 --- a/test/test_mpls.py +++ b/test/test_mpls.py @@ -11,8 +11,6 @@ from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP, ICMP from scapy.layers.inet6 import IPv6 from scapy.contrib.mpls import MPLS -from util import ppp - class TestMPLS(VppTestCase): """ MPLS Test Case """ @@ -60,7 +58,7 @@ class TestMPLS(VppTestCase): else: p = p / MPLS(label=mpls_labels[ii], ttl=mpls_ttl, s=0) if not ping: - p = (p / IP(src=src_if.local_ip4, dst=src_if.remote_ip4) / + p = (p / IP(src=src_if.local_ip4, dst=src_if.remote_ip4) / UDP(sport=1234, dport=1234) / Raw(payload)) else: @@ -331,14 +329,8 @@ class TestMPLS(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - - rx = self.pg0.get_capture() - try: - self.assertEqual(0, len(rx)) - except: - self.logger.error("MPLS non-EOS packets popped and forwarded") - self.logger.error(ppp("", rx)) - raise + self.pg0.assert_nothing_captured( + remark="MPLS non-EOS packets popped and forwarded") # # A recursive EOS x-connect, which resolves through another x-connect @@ -586,8 +578,7 @@ class TestMPLS(VppTestCase): 0, # next-hop-table-id 1, # next-hop-weight 2, # num-out-labels, - [44, 46] - ) + [44, 46]) self.vapi.sw_interface_set_flags(reply.sw_if_index, admin_up_down=1) # @@ -606,8 +597,7 @@ class TestMPLS(VppTestCase): 0, # next-hop-table-id 1, # next-hop-weight 0, # num-out-labels, - [] # out-label - ) + []) # out-label self.vapi.cli("clear trace") tx = self.create_stream_ip4(self.pg0, "10.0.0.3") @@ -632,14 +622,7 @@ class TestMPLS(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() - - try: - self.assertEqual(0, len(rx)) - except: - self.logger.error("MPLS TTL=0 packets forwarded") - self.logger.error(ppp("", rx)) - raise + self.pg0.assert_nothing_captured(remark="MPLS TTL=0 packets forwarded") # # a stream with a non-zero MPLS TTL diff --git a/test/test_snat.py b/test/test_snat.py index 5cc76f6c..fdd81f02 100644 --- a/test/test_snat.py +++ b/test/test_snat.py @@ -2,12 +2,12 @@ import socket import unittest -from logging import * from framework import VppTestCase, VppTestRunner from scapy.layers.inet import IP, TCP, UDP, ICMP from scapy.layers.l2 import Ether +from util import ppp class TestSNAT(VppTestCase): @@ -88,7 +88,7 @@ class TestSNAT(VppTestCase): :param dst_ip: Destination IP address (Default use global SNAT address) """ if dst_ip is None: - dst_ip=self.snat_addr + dst_ip = self.snat_addr pkts = [] # TCP p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / @@ -145,8 +145,8 @@ class TestSNAT(VppTestCase): self.assertNotEqual(packet[ICMP].id, self.icmp_id_in) self.icmp_id_out = packet[ICMP].id except: - error("Unexpected or invalid packet (outside network):") - error(packet.show()) + self.logger.error(ppp("Unexpected or invalid packet " + "(outside network):", packet)) raise def verify_capture_in(self, capture, in_if, packet_num=3): @@ -168,8 +168,8 @@ class TestSNAT(VppTestCase): else: self.assertEqual(packet[ICMP].id, self.icmp_id_in) except: - error("Unexpected or invalid packet (inside network):") - error(packet.show()) + self.logger.error(ppp("Unexpected or invalid packet " + "(inside network):", packet)) raise def clear_snat(self): @@ -410,11 +410,10 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg3.get_capture() - self.verify_capture_out(capture, packet_num=0) + self.pg3.assert_nothing_captured() def test_multiple_inside_interfaces(self): - """ SNAT multiple inside interfaces with non-overlapping address space """ + """SNAT multiple inside interfaces with non-overlapping address space""" self.snat_add_address(self.snat_addr) self.vapi.snat_interface_add_del_feature(self.pg0.sw_if_index) @@ -566,8 +565,7 @@ class TestSNAT(VppTestCase): self.assertEqual(tcp.dport, server_in_port) host_out_port = tcp.sport except: - error("Unexpected or invalid packet:") - error(p.show()) + self.logger.error(ppp("Unexpected or invalid packet:", p)) raise # send reply from server to host @@ -588,11 +586,9 @@ class TestSNAT(VppTestCase): self.assertEqual(tcp.sport, server_out_port) self.assertEqual(tcp.dport, host_in_port) except: - error("Unexpected or invalid packet:") - error(p.show()) + self.logger.error(ppp("Unexpected or invalid packet:"), p) raise - def tearDown(self): super(TestSNAT, self).tearDown() if not self.vpp_dead: diff --git a/test/util.py b/test/util.py index f6c6acd4..0ac23760 100644 --- a/test/util.py +++ b/test/util.py @@ -15,6 +15,28 @@ def ppp(headline, packet): return o.getvalue() +def ppc(headline, capture, limit=10): + """ Return string containing ppp() printout for a capture. + + :param headline: printed as first line of output + :param capture: packets to print + :param limit: limit the print to # of packets + """ + if not capture: + return headline + result = headline + "\n" + count = 1 + for p in capture: + result.append(ppp("Packet #%s:" % count, p)) + count += 1 + if count >= limit: + break + if limit < len(capture): + result.append( + "Capture contains %s packets in total, of which %s were printed" % + (len(capture), limit)) + + class NumericConstant(object): __metaclass__ = ABCMeta diff --git a/test/vpp_pg_interface.py b/test/vpp_pg_interface.py index 2ebcbb57..44bd1a2d 100644 --- a/test/vpp_pg_interface.py +++ b/test/vpp_pg_interface.py @@ -6,7 +6,7 @@ from vpp_interface import VppInterface from scapy.layers.l2 import Ether, ARP from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\ ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr -from util import ppp +from util import ppp, ppc class VppPGInterface(VppInterface): @@ -114,25 +114,71 @@ class VppPGInterface(VppInterface): except: pass wrpcap(self.in_path, pkts) + self.test.register_capture(self.cap_name) # FIXME this should be an API, but no such exists atm self.test.vapi.cli(self.input_cli) - self.test.pg_streams.append(self.cap_name) - self.test.vapi.cli("trace add pg-input %d" % len(pkts)) - def get_capture(self): + def get_capture(self, remark=None): """ Get captured packets :returns: iterable packets """ try: + self.wait_for_capture_file() output = rdpcap(self.out_path) except IOError: # TODO - self.test.logger.error("File %s does not exist, probably because no" + self.test.logger.debug("File %s does not exist, probably because no" " packets arrived" % self.out_path) - return [] + if remark: + raise Exception("No packets captured on %s(%s)" % + (self.name, remark)) + else: + raise Exception("No packets captured on %s" % self.name) return output + def assert_nothing_captured(self, remark=None): + if os.path.isfile(self.out_path): + try: + capture = self.get_capture() + self.test.logger.error( + ppc("Unexpected packets captured:", capture)) + except: + pass + if remark: + raise AssertionError( + "Capture file present for interface %s(%s)" % + (self.name, remark)) + else: + raise AssertionError("Capture file present for interface %s" % + self.name) + + def wait_for_capture_file(self, timeout=1): + """ + Wait until pcap capture file appears + + :param timeout: How long to wait for the packet (default 1s) + + :raises Exception: if the capture file does not appear within timeout + """ + limit = time.time() + timeout + if not os.path.isfile(self.out_path): + self.test.logger.debug( + "Waiting for capture file to appear, timeout is %ss", timeout) + else: + self.test.logger.debug("Capture file already exists") + return + while time.time() < limit: + if os.path.isfile(self.out_path): + break + time.sleep(0) # yield + if os.path.isfile(self.out_path): + self.test.logger.debug("Capture file appeared after %fs" % + (time.time() - (limit - timeout))) + else: + self.test.logger.debug("Timeout - capture file still nowhere") + raise Exception("Capture file did not appear within timeout") + def wait_for_packet(self, timeout): """ Wait for next packet captured with a timeout @@ -144,18 +190,8 @@ class VppPGInterface(VppInterface): """ limit = time.time() + timeout if self._pcap_reader is None: - self.test.logger.debug("Waiting for the capture file to appear") - while time.time() < limit: - if os.path.isfile(self.out_path): - break - time.sleep(0) # yield - if os.path.isfile(self.out_path): - self.test.logger.debug("Capture file appeared after %fs" % - (time.time() - (limit - timeout))) - self._pcap_reader = PcapReader(self.out_path) - else: - self.test.logger.debug("Timeout - capture file still nowhere") - raise Exception("Packet didn't arrive within timeout") + self.wait_for_capture_file(timeout) + self._pcap_reader = PcapReader(self.out_path) self.test.logger.debug("Waiting for packet") while time.time() < limit: @@ -197,11 +233,11 @@ class VppPGInterface(VppInterface): pg_interface.enable_capture() self.test.pg_start() self.test.logger.info(self.test.vapi.cli("show trace")) - arp_reply = pg_interface.get_capture() - if arp_reply is None or len(arp_reply) == 0: - self.test.logger.info( - "No ARP received on port %s" % - pg_interface.name) + try: + arp_reply = pg_interface.get_capture() + except: + self.test.logger.info("No ARP received on port %s" % + pg_interface.name) return arp_reply = arp_reply[0] # Make Dot1AD packet content recognizable to scapy -- cgit 1.2.3-korg From dab231a11ec96e829b22ff80c612333edc5a93e6 Mon Sep 17 00:00:00 2001 From: Klement Sekera Date: Wed, 21 Dec 2016 08:50:14 +0100 Subject: make test: improve handling of packet captures Perform accounting of expected packets based on created packet infos. Use this accounting info to automatically expect (and verify) the correct number of packets to be captured. Automatically retry the read of the capture file if scapy raises an exception while doing so to handle rare cases when capture file is read while only partially written during busy wait. Don't fail assert_nothing_captured if only junk packets arrived. Change-Id: I16ec2e9410ef510d313ec16b7e13c57d0b2a63f5 Signed-off-by: Klement Sekera --- test/framework.py | 53 ++++++----- test/template_bd.py | 10 +-- test/test_bfd.py | 8 +- test/test_classifier.py | 85 ++++++++++-------- test/test_gre.py | 33 +++---- test/test_ip4.py | 19 ++-- test/test_ip4_irb.py | 17 ++-- test/test_ip6.py | 3 +- test/test_l2_fib.py | 10 +-- test/test_l2bd.py | 5 +- test/test_l2bd_multi_instance.py | 15 +--- test/test_l2xc.py | 8 +- test/test_l2xc_multi_instance.py | 5 +- test/test_lb.py | 11 ++- test/test_mpls.py | 41 +++++---- test/test_snat.py | 55 ++++++------ test/test_span.py | 11 +-- test/util.py | 17 ++-- test/vpp_pg_interface.py | 186 +++++++++++++++++++++++++++------------ 19 files changed, 321 insertions(+), 271 deletions(-) (limited to 'test/test_l2xc_multi_instance.py') diff --git a/test/framework.py b/test/framework.py index 1b745ff3..324a64ce 100644 --- a/test/framework.py +++ b/test/framework.py @@ -10,6 +10,7 @@ from threading import Thread from inspect import getdoc from hook import StepHook, PollHook from vpp_pg_interface import VppPGInterface +from vpp_sub_interface import VppSubInterface from vpp_lo_interface import VppLoInterface from vpp_papi_provider import VppPapiProvider from scapy.packet import Raw @@ -63,9 +64,13 @@ class VppTestCase(unittest.TestCase): """List of packet infos""" return self._packet_infos - @packet_infos.setter - def packet_infos(self, value): - self._packet_infos = value + @classmethod + def get_packet_count_for_if_idx(cls, dst_if_index): + """Get the number of packet info for specified destination if index""" + if dst_if_index in cls._packet_count_for_dst_if_idx: + return cls._packet_count_for_dst_if_idx[dst_if_index] + else: + return 0 @classmethod def instance(cls): @@ -184,9 +189,9 @@ class VppTestCase(unittest.TestCase): cls.logger.info("Temporary dir is %s, shm prefix is %s", cls.tempdir, cls.shm_prefix) cls.setUpConstants() + cls.reset_packet_infos() cls._captures = [] cls._zombie_captures = [] - cls.packet_infos = {} cls.verbose = 0 cls.vpp_dead = False print(double_line_delim) @@ -394,31 +399,37 @@ class VppTestCase(unittest.TestCase): if extend > 0: packet[Raw].load += ' ' * extend - def add_packet_info_to_list(self, info): - """ - Add packet info to the testcase's packet info list - - :param info: packet info - - """ - info.index = len(self.packet_infos) - self.packet_infos[info.index] = info + @classmethod + def reset_packet_infos(cls): + """ Reset the list of packet info objects and packet counts to zero """ + cls._packet_infos = {} + cls._packet_count_for_dst_if_idx = {} - def create_packet_info(self, src_pg_index, dst_pg_index): + @classmethod + def create_packet_info(cls, src_if, dst_if): """ Create packet info object containing the source and destination indexes and add it to the testcase's packet info list - :param src_pg_index: source packet-generator index - :param dst_pg_index: destination packet-generator index + :param VppInterface src_if: source interface + :param VppInterface dst_if: destination interface :returns: _PacketInfo object """ info = _PacketInfo() - self.add_packet_info_to_list(info) - info.src = src_pg_index - info.dst = dst_pg_index + info.index = len(cls._packet_infos) + info.src = src_if.sw_if_index + info.dst = dst_if.sw_if_index + if isinstance(dst_if, VppSubInterface): + dst_idx = dst_if.parent.sw_if_index + else: + dst_idx = dst_if.sw_if_index + if dst_idx in cls._packet_count_for_dst_if_idx: + cls._packet_count_for_dst_if_idx[dst_idx] += 1 + else: + cls._packet_count_for_dst_if_idx[dst_idx] = 1 + cls._packet_infos[info.index] = info return info @staticmethod @@ -462,10 +473,10 @@ class VppTestCase(unittest.TestCase): next_index = 0 else: next_index = info.index + 1 - if next_index == len(self.packet_infos): + if next_index == len(self._packet_infos): return None else: - return self.packet_infos[next_index] + return self._packet_infos[next_index] def get_next_packet_info_for_interface(self, src_index, info): """ diff --git a/test/template_bd.py b/test/template_bd.py index 01e8b855..d70648b4 100644 --- a/test/template_bd.py +++ b/test/template_bd.py @@ -56,10 +56,7 @@ class BridgeDomain(object): self.pg_start() # Pick first received frame and check if it's the non-encapsulated frame - out = self.pg1.get_capture() - self.assertEqual(len(out), 1, - 'Invalid number of packets on ' - 'output: {}'.format(len(out))) + out = self.pg1.get_capture(1) pkt = out[0] # TODO: add error messages @@ -83,10 +80,7 @@ class BridgeDomain(object): self.pg_start() # Pick first received frame and check if it's corectly encapsulated. - out = self.pg0.get_capture() - self.assertEqual(len(out), 1, - 'Invalid number of packets on ' - 'output: {}'.format(len(out))) + out = self.pg0.get_capture(1) pkt = out[0] self.check_encapsulation(pkt) diff --git a/test/test_bfd.py b/test/test_bfd.py index 87a5ea4b..1ea69f55 100644 --- a/test/test_bfd.py +++ b/test/test_bfd.py @@ -184,10 +184,10 @@ class BFDTestCase(VppTestCase): self.pg_enable_capture([self.pg0]) expected_packets = 3 self.logger.info("BFD: Waiting for %d BFD packets" % expected_packets) - self.wait_for_bfd_packet() + self.wait_for_bfd_packet(2) for i in range(expected_packets): before = time.time() - self.wait_for_bfd_packet() + self.wait_for_bfd_packet(2) after = time.time() # spec says the range should be <0.75, 1>, allow extra 0.05 margin # to work around timing issues @@ -198,7 +198,7 @@ class BFDTestCase(VppTestCase): def test_zero_remote_min_rx(self): """ no packets when zero BFD RemoteMinRxInterval """ self.pg_enable_capture([self.pg0]) - p = self.wait_for_bfd_packet() + p = self.wait_for_bfd_packet(2) self.test_session.update(my_discriminator=randint(0, 40000000), your_discriminator=p[BFD].my_discriminator, state=BFDState.init, @@ -216,7 +216,7 @@ class BFDTestCase(VppTestCase): def bfd_session_up(self): self.pg_enable_capture([self.pg0]) self.logger.info("BFD: Waiting for slow hello") - p = self.wait_for_bfd_packet() + p = self.wait_for_bfd_packet(2) self.logger.info("BFD: Sending Init") self.test_session.update(my_discriminator=randint(0, 40000000), your_discriminator=p[BFD].my_discriminator, diff --git a/test/test_classifier.py b/test/test_classifier.py index 0923387c..302430f8 100644 --- a/test/test_classifier.py +++ b/test/test_classifier.py @@ -11,6 +11,7 @@ from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP from util import ppp + class TestClassifier(VppTestCase): """ Classifier Test Case """ @@ -84,8 +85,7 @@ class TestClassifier(VppTestCase): """ pkts = [] for size in packet_sizes: - info = self.create_packet_info(src_if.sw_if_index, - dst_if.sw_if_index) + info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4) / @@ -150,8 +150,8 @@ class TestClassifier(VppTestCase): :param str dst_port: destination port number <0-ffff> """ - return ('{:0>20}{:0>12}{:0>8}{:0>12}{:0>4}'.format(proto, src_ip, - dst_ip, src_port, dst_port)).rstrip('0') + return ('{:0>20}{:0>12}{:0>8}{:0>12}{:0>4}'.format( + proto, src_ip, dst_ip, src_port, dst_port)).rstrip('0') @staticmethod def build_ip_match(proto='', src_ip='', dst_ip='', @@ -164,11 +164,13 @@ class TestClassifier(VppTestCase): :param str src_port: source port number <0-ffff> :param str dst_port: destination port number <0-ffff> """ - if src_ip: src_ip = socket.inet_aton(src_ip).encode('hex') - if dst_ip: dst_ip = socket.inet_aton(dst_ip).encode('hex') + if src_ip: + src_ip = socket.inet_aton(src_ip).encode('hex') + if dst_ip: + dst_ip = socket.inet_aton(dst_ip).encode('hex') - return ('{:0>20}{:0>12}{:0>8}{:0>12}{:0>4}'.format(proto, src_ip, - dst_ip, src_port, dst_port)).rstrip('0') + return ('{:0>20}{:0>12}{:0>8}{:0>12}{:0>4}'.format( + proto, src_ip, dst_ip, src_port, dst_port)).rstrip('0') @staticmethod def build_mac_mask(dst_mac='', src_mac='', ether_type=''): @@ -180,7 +182,7 @@ class TestClassifier(VppTestCase): """ return ('{:0>12}{:0>12}{:0>4}'.format(dst_mac, src_mac, - ether_type)).rstrip('0') + ether_type)).rstrip('0') @staticmethod def build_mac_match(dst_mac='', src_mac='', ether_type=''): @@ -190,11 +192,13 @@ class TestClassifier(VppTestCase): :param str src_mac: destination MAC address :param str ether_type: ethernet type <0-ffff> """ - if dst_mac: dst_mac = dst_mac.replace(':', '') - if src_mac: src_mac = src_mac.replace(':', '') + if dst_mac: + dst_mac = dst_mac.replace(':', '') + if src_mac: + src_mac = src_mac.replace(':', '') return ('{:0>12}{:0>12}{:0>4}'.format(dst_mac, src_mac, - ether_type)).rstrip('0') + ether_type)).rstrip('0') def create_classify_table(self, key, mask, data_offset=0, is_add=1): """Create Classify Table @@ -206,12 +210,12 @@ class TestClassifier(VppTestCase): - create(1) or delete(0) """ r = self.vapi.classify_add_del_table( - is_add, - binascii.unhexlify(mask), - match_n_vectors=(len(mask)-1)//32 + 1, - miss_next_index=0, - current_data_flag=1, - current_data_offset=data_offset) + is_add, + binascii.unhexlify(mask), + match_n_vectors=(len(mask) - 1) // 32 + 1, + miss_next_index=0, + current_data_flag=1, + current_data_offset=data_offset) self.assertIsNotNone(r, msg='No response msg for add_del_table') self.acl_tbl_idx[key] = r.new_table_index @@ -228,12 +232,12 @@ class TestClassifier(VppTestCase): - create(1) or delete(0) """ r = self.vapi.classify_add_del_session( - is_add, - table_index, - binascii.unhexlify(match), - opaque_index=0, - action=pbr_option, - metadata=vrfid) + is_add, + table_index, + binascii.unhexlify(match), + opaque_index=0, + action=pbr_option, + metadata=vrfid) self.assertIsNotNone(r, msg='No response msg for add_del_session') def input_acl_set_interface(self, intf, table_index, is_add=1): @@ -245,9 +249,9 @@ class TestClassifier(VppTestCase): - enable(1) or disable(0) """ r = self.vapi.input_acl_set_interface( - is_add, - intf.sw_if_index, - ip4_table_index=table_index) + is_add, + intf.sw_if_index, + ip4_table_index=table_index) self.assertIsNotNone(r, msg='No response msg for acl_set_interface') def test_acl_ip(self): @@ -264,14 +268,15 @@ class TestClassifier(VppTestCase): self.pg0.add_stream(pkts) self.create_classify_table('ip', self.build_ip_mask(src_ip='ffffffff')) - self.create_classify_session(self.pg0, self.acl_tbl_idx.get('ip'), - self.build_ip_match(src_ip=self.pg0.remote_ip4)) + self.create_classify_session( + self.pg0, self.acl_tbl_idx.get('ip'), + self.build_ip_match(src_ip=self.pg0.remote_ip4)) self.input_acl_set_interface(self.pg0, self.acl_tbl_idx.get('ip')) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - pkts = self.pg1.get_capture() + pkts = self.pg1.get_capture(len(pkts)) self.verify_capture(self.pg1, pkts) self.input_acl_set_interface(self.pg0, self.acl_tbl_idx.get('ip'), 0) self.pg0.assert_nothing_captured(remark="packets forwarded") @@ -291,16 +296,17 @@ class TestClassifier(VppTestCase): pkts = self.create_stream(self.pg0, self.pg2, self.pg_if_packet_sizes) self.pg0.add_stream(pkts) - self.create_classify_table('mac', - self.build_mac_mask(src_mac='ffffffffffff'), data_offset=-14) - self.create_classify_session(self.pg0, self.acl_tbl_idx.get('mac'), - self.build_mac_match(src_mac=self.pg0.remote_mac)) + self.create_classify_table( + 'mac', self.build_mac_mask(src_mac='ffffffffffff'), data_offset=-14) + self.create_classify_session( + self.pg0, self.acl_tbl_idx.get('mac'), + self.build_mac_match(src_mac=self.pg0.remote_mac)) self.input_acl_set_interface(self.pg0, self.acl_tbl_idx.get('mac')) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - pkts = self.pg2.get_capture() + pkts = self.pg2.get_capture(len(pkts)) self.verify_capture(self.pg2, pkts) self.input_acl_set_interface(self.pg0, self.acl_tbl_idx.get('mac'), 0) self.pg0.assert_nothing_captured(remark="packets forwarded") @@ -322,16 +328,17 @@ class TestClassifier(VppTestCase): self.create_classify_table('pbr', self.build_ip_mask(src_ip='ffffffff')) pbr_option = 1 - self.create_classify_session(self.pg0, self.acl_tbl_idx.get('pbr'), - self.build_ip_match(src_ip=self.pg0.remote_ip4), - pbr_option, self.pbr_vrfid) + self.create_classify_session( + self.pg0, self.acl_tbl_idx.get('pbr'), + self.build_ip_match(src_ip=self.pg0.remote_ip4), + pbr_option, self.pbr_vrfid) self.config_pbr_fib_entry(self.pg3) self.input_acl_set_interface(self.pg0, self.acl_tbl_idx.get('pbr')) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - pkts = self.pg3.get_capture() + pkts = self.pg3.get_capture(len(pkts)) self.verify_capture(self.pg3, pkts) self.input_acl_set_interface(self.pg0, self.acl_tbl_idx.get('pbr'), 0) self.pg0.assert_nothing_captured(remark="packets forwarded") diff --git a/test/test_gre.py b/test/test_gre.py index f00e4467..b1313044 100644 --- a/test/test_gre.py +++ b/test/test_gre.py @@ -43,8 +43,7 @@ class TestGRE(VppTestCase): def create_stream_ip4(self, src_if, src_ip, dst_ip): pkts = [] for i in range(0, 257): - info = self.create_packet_info(src_if.sw_if_index, - src_if.sw_if_index) + info = self.create_packet_info(src_if, src_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=src_ip, dst=dst_ip) / @@ -59,8 +58,7 @@ class TestGRE(VppTestCase): src_ip, dst_ip): pkts = [] for i in range(0, 257): - info = self.create_packet_info(src_if.sw_if_index, - src_if.sw_if_index) + info = self.create_packet_info(src_if, src_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=tunnel_src, dst=tunnel_dst) / @@ -77,8 +75,7 @@ class TestGRE(VppTestCase): src_ip, dst_ip): pkts = [] for i in range(0, 257): - info = self.create_packet_info(src_if.sw_if_index, - src_if.sw_if_index) + info = self.create_packet_info(src_if, src_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=tunnel_src, dst=tunnel_dst) / @@ -94,8 +91,7 @@ class TestGRE(VppTestCase): tunnel_src, tunnel_dst): pkts = [] for i in range(0, 257): - info = self.create_packet_info(src_if.sw_if_index, - src_if.sw_if_index) + info = self.create_packet_info(src_if, src_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=tunnel_src, dst=tunnel_dst) / @@ -113,8 +109,7 @@ class TestGRE(VppTestCase): tunnel_src, tunnel_dst, vlan): pkts = [] for i in range(0, 257): - info = self.create_packet_info(src_if.sw_if_index, - src_if.sw_if_index) + info = self.create_packet_info(src_if, src_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=tunnel_src, dst=tunnel_dst) / @@ -342,7 +337,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() + rx = self.pg0.get_capture(len(tx)) self.verify_tunneled_4o4(self.pg0, rx, tx, self.pg0.local_ip4, "1.1.1.2") @@ -361,7 +356,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() + rx = self.pg0.get_capture(len(tx)) self.verify_decapped_4o4(self.pg0, rx, tx) # @@ -426,7 +421,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() + rx = self.pg0.get_capture(len(tx)) self.verify_decapped_6o4(self.pg0, rx, tx) # @@ -483,7 +478,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg1.get_capture() + rx = self.pg1.get_capture(len(tx)) self.verify_tunneled_4o4(self.pg1, rx, tx, self.pg1.local_ip4, "2.2.2.2") @@ -503,7 +498,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() + rx = self.pg0.get_capture(len(tx)) self.verify_decapped_4o4(self.pg0, rx, tx) # @@ -564,7 +559,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() + rx = self.pg0.get_capture(len(tx)) self.verify_tunneled_l2o4(self.pg0, rx, tx, self.pg0.local_ip4, "2.2.2.3") @@ -578,7 +573,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() + rx = self.pg0.get_capture(len(tx)) self.verify_tunneled_l2o4(self.pg0, rx, tx, self.pg0.local_ip4, "2.2.2.2") @@ -635,7 +630,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() + rx = self.pg0.get_capture(len(tx)) self.verify_tunneled_vlano4(self.pg0, rx, tx, self.pg0.local_ip4, "2.2.2.3", @@ -651,7 +646,7 @@ class TestGRE(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg0.get_capture() + rx = self.pg0.get_capture(len(tx)) self.verify_tunneled_vlano4(self.pg0, rx, tx, self.pg0.local_ip4, "2.2.2.2", diff --git a/test/test_ip4.py b/test/test_ip4.py index f67c3b9c..df93533d 100644 --- a/test/test_ip4.py +++ b/test/test_ip4.py @@ -108,8 +108,7 @@ class TestIPv4(VppTestCase): pkts = [] for i in range(0, 257): dst_if = self.flows[src_if][i % 2] - info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4) / @@ -254,15 +253,13 @@ class TestIPv4FibCrud(VppTestCase): for _ in range(count): dst_addr = random.choice(dst_ips) - info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=src_if.remote_ip4, dst=dst_addr) / UDP(sport=1234, dport=1234) / Raw(payload)) info.data = p.copy() - size = random.choice(self.pg_if_packet_sizes) self.extend_packet(p, random.choice(self.pg_if_packet_sizes)) pkts.append(p) @@ -270,7 +267,8 @@ class TestIPv4FibCrud(VppTestCase): def _find_ip_match(self, find_in, pkt): for p in find_in: - if self.payload_to_info(str(p[Raw])) == self.payload_to_info(str(pkt[Raw])): + if self.payload_to_info(str(p[Raw])) == \ + self.payload_to_info(str(pkt[Raw])): if p[IP].src != pkt[IP].src: break if p[IP].dst != pkt[IP].dst: @@ -357,7 +355,7 @@ class TestIPv4FibCrud(VppTestCase): def setUp(self): super(TestIPv4FibCrud, self).setUp() - self.packet_infos = {} + self.reset_packet_infos() def test_1_add_routes(self): """ Add 1k routes @@ -381,10 +379,9 @@ class TestIPv4FibCrud(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - pkts = self.pg0.get_capture() + pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2)) self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2) - def test_2_del_routes(self): """ Delete 100 routes @@ -411,7 +408,7 @@ class TestIPv4FibCrud(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - pkts = self.pg0.get_capture() + pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2)) self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2) def test_3_add_new_routes(self): @@ -446,7 +443,7 @@ class TestIPv4FibCrud(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - pkts = self.pg0.get_capture() + pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2)) self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2) def test_4_del_routes(self): diff --git a/test/test_ip4_irb.py b/test/test_ip4_irb.py index cf2bb150..bbec7ca7 100644 --- a/test/test_ip4_irb.py +++ b/test/test_ip4_irb.py @@ -103,8 +103,7 @@ class TestIpIrb(VppTestCase): pkts = [] for i in range(0, 257): remote_dst_host = choice(dst_ip_if.remote_hosts) - info = self.create_packet_info( - src_ip_if.sw_if_index, dst_ip_if.sw_if_index) + info = self.create_packet_info(src_ip_if, dst_ip_if) payload = self.info_to_payload(info) p = (Ether(dst=src_ip_if.local_mac, src=src_ip_if.remote_mac) / IP(src=src_ip_if.remote_ip4, @@ -121,14 +120,13 @@ class TestIpIrb(VppTestCase): packet_sizes): pkts = [] for i in range(0, 257): - info = self.create_packet_info( - src_ip_if.sw_if_index, dst_ip_if.sw_if_index) + info = self.create_packet_info(src_ip_if, dst_ip_if) payload = self.info_to_payload(info) host = choice(src_l2_if.remote_hosts) p = (Ether(src=host.mac, - dst = src_ip_if.local_mac) / + dst=src_ip_if.local_mac) / IP(src=host.ip4, dst=dst_ip_if.remote_ip4) / UDP(sport=1234, dport=1234) / @@ -152,7 +150,6 @@ class TestIpIrb(VppTestCase): ip = packet[IP] udp = packet[IP][UDP] payload_info = self.payload_to_info(str(packet[IP][UDP][Raw])) - packet_index = payload_info.index self.assertEqual(payload_info.dst, dst_ip_sw_if_index) @@ -231,8 +228,10 @@ class TestIpIrb(VppTestCase): self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rcvd1 = self.pg0.get_capture() - rcvd2 = self.pg1.get_capture() + packet_count = self.get_packet_count_for_if_idx(self.loop0.sw_if_index) + + rcvd1 = self.pg0.get_capture(packet_count) + rcvd2 = self.pg1.get_capture(packet_count) self.verify_capture(self.loop0, self.pg2, rcvd1) self.verify_capture(self.loop0, self.pg2, rcvd2) @@ -259,8 +258,6 @@ class TestIpIrb(VppTestCase): rcvd = self.pg2.get_capture() self.verify_capture_l2_to_ip(self.pg2, self.loop0, rcvd) - self.assertEqual(len(stream1) + len(stream2), len(rcvd.res)) - if __name__ == '__main__': unittest.main(testRunner=VppTestRunner) diff --git a/test/test_ip6.py b/test/test_ip6.py index 06b15f94..e8b12f68 100644 --- a/test/test_ip6.py +++ b/test/test_ip6.py @@ -116,8 +116,7 @@ class TestIPv6(VppTestCase): pkts = [] for i in range(0, 257): dst_if = self.flows[src_if][i % 2] - info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6) / diff --git a/test/test_l2_fib.py b/test/test_l2_fib.py index 4855a3ea..d4ef3f4a 100644 --- a/test/test_l2_fib.py +++ b/test/test_l2_fib.py @@ -130,11 +130,8 @@ class TestL2fib(VppTestCase): raise def setUp(self): - """ - Clear trace and packet infos before running each test. - """ super(TestL2fib, self).setUp() - self.packet_infos = {} + self.reset_packet_infos() def tearDown(self): """ @@ -236,8 +233,7 @@ class TestL2fib(VppTestCase): for i in range(0, n_int): dst_host = dst_hosts[i] src_host = random.choice(src_hosts) - pkt_info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + pkt_info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(pkt_info) p = (Ether(dst=dst_host.mac, src=src_host.mac) / IP(src=src_host.ip4, dst=dst_host.ip4) / @@ -314,7 +310,7 @@ class TestL2fib(VppTestCase): # Test # Create incoming packet streams for packet-generator interfaces for # deleted MAC addresses - self.packet_infos = {} + self.reset_packet_infos() for i in self.pg_interfaces: pkts = self.create_stream(i, self.pg_if_packet_sizes, deleted=True) i.add_stream(pkts) diff --git a/test/test_l2bd.py b/test/test_l2bd.py index 50720e64..30708a46 100644 --- a/test/test_l2bd.py +++ b/test/test_l2bd.py @@ -99,7 +99,7 @@ class TestL2bd(VppTestCase): Clear trace and packet infos before running each test. """ super(TestL2bd, self).setUp() - self.packet_infos = {} + self.reset_packet_infos() def tearDown(self): """ @@ -158,8 +158,7 @@ class TestL2bd(VppTestCase): dst_if = self.flows[src_if][i % 2] dst_host = random.choice(self.hosts_by_pg_idx[dst_if.sw_if_index]) src_host = random.choice(self.hosts_by_pg_idx[src_if.sw_if_index]) - pkt_info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + pkt_info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(pkt_info) p = (Ether(dst=dst_host.mac, src=src_host.mac) / IP(src=src_host.ip4, dst=dst_host.ip4) / diff --git a/test/test_l2bd_multi_instance.py b/test/test_l2bd_multi_instance.py index 1272d765..a1226222 100644 --- a/test/test_l2bd_multi_instance.py +++ b/test/test_l2bd_multi_instance.py @@ -138,7 +138,6 @@ class TestL2bdMultiInst(VppTestCase): Clear trace and packet infos before running each test. """ super(TestL2bdMultiInst, self).setUp() - self.packet_infos = {} def tearDown(self): """ @@ -243,8 +242,7 @@ class TestL2bdMultiInst(VppTestCase): for i in range(0, n_int): dst_host = dst_hosts[i] src_host = random.choice(src_hosts) - pkt_info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + pkt_info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(pkt_info) p = (Ether(dst=dst_host.mac, src=src_host.mac) / IP(src=src_host.ip4, dst=dst_host.ip4) / @@ -393,17 +391,8 @@ class TestL2bdMultiInst(VppTestCase): for pg_if in self.pg_interfaces: capture = pg_if.get_capture() if pg_if in self.pg_in_bd: - if len(capture) == 0: - raise RuntimeError("Interface %s is in BD but the capture " - "is empty!" % pg_if.name) self.verify_capture(pg_if, capture) - elif pg_if in self.pg_not_in_bd: - try: - self.assertEqual(len(capture), 0) - except AssertionError: - raise RuntimeError("Interface %s is not in BD but " - "the capture is not empty!" % pg_if.name) - else: + elif pg_if not in self.pg_not_in_bd: self.logger.error("Unknown interface: %s" % pg_if.name) def test_l2bd_inst_01(self): diff --git a/test/test_l2xc.py b/test/test_l2xc.py index 37893042..2ec4af92 100644 --- a/test/test_l2xc.py +++ b/test/test_l2xc.py @@ -77,11 +77,8 @@ class TestL2xc(VppTestCase): raise def setUp(self): - """ - Clear trace and packet infos before running each test. - """ super(TestL2xc, self).setUp() - self.packet_infos = {} + self.reset_packet_infos() def tearDown(self): """ @@ -123,8 +120,7 @@ class TestL2xc(VppTestCase): dst_if = self.flows[src_if][0] dst_host = random.choice(self.hosts_by_pg_idx[dst_if.sw_if_index]) src_host = random.choice(self.hosts_by_pg_idx[src_if.sw_if_index]) - pkt_info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + pkt_info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(pkt_info) p = (Ether(dst=dst_host.mac, src=src_host.mac) / IP(src=src_host.ip4, dst=dst_host.ip4) / diff --git a/test/test_l2xc_multi_instance.py b/test/test_l2xc_multi_instance.py index 2e55674e..6c28cebb 100644 --- a/test/test_l2xc_multi_instance.py +++ b/test/test_l2xc_multi_instance.py @@ -113,7 +113,7 @@ class TestL2xcMultiInst(VppTestCase): Clear trace and packet infos before running each test. """ super(TestL2xcMultiInst, self).setUp() - self.packet_infos = {} + self.reset_packet_infos() def tearDown(self): """ @@ -205,8 +205,7 @@ class TestL2xcMultiInst(VppTestCase): for i in range(0, n_int): dst_host = dst_hosts[i] src_host = random.choice(src_hosts) - pkt_info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + pkt_info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(pkt_info) p = (Ether(dst=dst_host.mac, src=src_host.mac) / IP(src=src_host.ip4, dst=dst_host.ip4) / diff --git a/test/test_lb.py b/test/test_lb.py index 9b5baaea..e5802d99 100644 --- a/test/test_lb.py +++ b/test/test_lb.py @@ -69,10 +69,10 @@ class TestLB(VppTestCase): UDP(sport=10000 + id, dport=20000 + id)) def generatePackets(self, src_if, isv4): - self.packet_infos = {} + self.reset_packet_infos() pkts = [] for pktid in self.packets: - info = self.create_packet_info(src_if.sw_if_index, pktid) + info = self.create_packet_info(src_if, self.pg1) payload = self.info_to_payload(info) ip = self.getIPv4Flow(pktid) if isv4 else self.getIPv6Flow(pktid) packet = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / @@ -90,14 +90,13 @@ class TestLB(VppTestCase): self.assertEqual(gre.version, 0) inner = IPver(str(gre.payload)) payload_info = self.payload_to_info(str(inner[Raw])) - self.info = self.get_next_packet_info_for_interface2( - self.pg0.sw_if_index, payload_info.dst, self.info) + self.info = self.packet_infos[payload_info.index] + self.assertEqual(payload_info.src, self.pg0.sw_if_index) self.assertEqual(str(inner), str(self.info.data[IPver])) def checkCapture(self, gre4, isv4): self.pg0.assert_nothing_captured() - out = self.pg1.get_capture() - self.assertEqual(len(out), len(self.packets)) + out = self.pg1.get_capture(len(self.packets)) load = [0] * len(self.ass) self.info = None diff --git a/test/test_mpls.py b/test/test_mpls.py index 6d5eeb2b..806e69dc 100644 --- a/test/test_mpls.py +++ b/test/test_mpls.py @@ -12,6 +12,7 @@ from scapy.layers.inet import IP, UDP, ICMP from scapy.layers.inet6 import IPv6 from scapy.contrib.mpls import MPLS + class TestMPLS(VppTestCase): """ MPLS Test Case """ @@ -44,11 +45,17 @@ class TestMPLS(VppTestCase): super(TestMPLS, self).tearDown() # the default of 64 matches the IP packet TTL default - def create_stream_labelled_ip4(self, src_if, mpls_labels, mpls_ttl=255, ping=0, ip_itf=None): + def create_stream_labelled_ip4( + self, + src_if, + mpls_labels, + mpls_ttl=255, + ping=0, + ip_itf=None): + self.reset_packet_infos() pkts = [] for i in range(0, 257): - info = self.create_packet_info(src_if.sw_if_index, - src_if.sw_if_index) + info = self.create_packet_info(src_if, src_if) payload = self.info_to_payload(info) p = Ether(dst=src_if.local_mac, src=src_if.remote_mac) @@ -71,10 +78,10 @@ class TestMPLS(VppTestCase): return pkts def create_stream_ip4(self, src_if, dst_ip): + self.reset_packet_infos() pkts = [] for i in range(0, 257): - info = self.create_packet_info(src_if.sw_if_index, - src_if.sw_if_index) + info = self.create_packet_info(src_if, src_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=src_if.remote_ip4, dst=dst_ip) / @@ -85,10 +92,10 @@ class TestMPLS(VppTestCase): return pkts def create_stream_labelled_ip6(self, src_if, mpls_label, mpls_ttl): + self.reset_packet_infos() pkts = [] for i in range(0, 257): - info = self.create_packet_info(src_if.sw_if_index, - src_if.sw_if_index) + info = self.create_packet_info(src_if, src_if) payload = self.info_to_payload(info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / MPLS(label=mpls_label, ttl=mpls_ttl) / @@ -342,7 +349,6 @@ class TestMPLS(VppTestCase): labels=[44, 45])]) route_34_eos.add_vpp_config() - self.vapi.cli("clear trace") tx = self.create_stream_labelled_ip4(self.pg0, [34]) self.pg0.add_stream(tx) @@ -628,7 +634,6 @@ class TestMPLS(VppTestCase): # a stream with a non-zero MPLS TTL # PG0 is in the default table # - self.vapi.cli("clear trace") tx = self.create_stream_labelled_ip4(self.pg0, [0]) self.pg0.add_stream(tx) @@ -692,9 +697,9 @@ class TestMPLS(VppTestCase): # A de-agg route - next-hop lookup in default table # route_34_eos = MplsRoute(self, 34, 1, - [RoutePath("0.0.0.0", - 0xffffffff, - nh_table_id=0)]) + [RoutePath("0.0.0.0", + 0xffffffff, + nh_table_id=0)]) route_34_eos.add_vpp_config() # @@ -716,9 +721,9 @@ class TestMPLS(VppTestCase): # A de-agg route - next-hop lookup in non-default table # route_35_eos = MplsRoute(self, 35, 1, - [RoutePath("0.0.0.0", - 0xffffffff, - nh_table_id=1)]) + [RoutePath("0.0.0.0", + 0xffffffff, + nh_table_id=1)]) route_35_eos.add_vpp_config() # @@ -727,13 +732,15 @@ class TestMPLS(VppTestCase): # default table and egress unlabelled in the non-default # self.vapi.cli("clear trace") - tx = self.create_stream_labelled_ip4(self.pg0, [35], ping=1, ip_itf=self.pg1) + tx = self.create_stream_labelled_ip4( + self.pg0, [35], ping=1, ip_itf=self.pg1) self.pg0.add_stream(tx) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - rx = self.pg1.get_capture() + packet_count = self.get_packet_count_for_if_idx(self.pg0.sw_if_index) + rx = self.pg1.get_capture(packet_count) self.verify_capture_ip4(self.pg1, rx, tx, ping_resp=1) route_35_eos.remove_vpp_config() diff --git a/test/test_snat.py b/test/test_snat.py index 8985c3e4..ca2e52a7 100644 --- a/test/test_snat.py +++ b/test/test_snat.py @@ -241,7 +241,7 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg1.get_capture() + capture = self.pg1.get_capture(len(pkts)) self.verify_capture_out(capture) # out2in @@ -249,7 +249,7 @@ class TestSNAT(VppTestCase): self.pg1.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg0.get_capture() + capture = self.pg0.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg0) def test_static_in(self): @@ -270,7 +270,7 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg1.get_capture() + capture = self.pg1.get_capture(len(pkts)) self.verify_capture_out(capture, nat_ip, True) # out2in @@ -278,7 +278,7 @@ class TestSNAT(VppTestCase): self.pg1.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg0.get_capture() + capture = self.pg0.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg0) def test_static_out(self): @@ -299,7 +299,7 @@ class TestSNAT(VppTestCase): self.pg1.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg0.get_capture() + capture = self.pg0.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg0) # in2out @@ -307,7 +307,7 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg1.get_capture() + capture = self.pg1.get_capture(len(pkts)) self.verify_capture_out(capture, nat_ip, True) def test_static_with_port_in(self): @@ -333,7 +333,7 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg1.get_capture() + capture = self.pg1.get_capture(len(pkts)) self.verify_capture_out(capture) # out2in @@ -341,7 +341,7 @@ class TestSNAT(VppTestCase): self.pg1.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg0.get_capture() + capture = self.pg0.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg0) def test_static_with_port_out(self): @@ -367,7 +367,7 @@ class TestSNAT(VppTestCase): self.pg1.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg0.get_capture() + capture = self.pg0.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg0) # in2out @@ -375,7 +375,7 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg1.get_capture() + capture = self.pg1.get_capture(len(pkts)) self.verify_capture_out(capture) def test_static_vrf_aware(self): @@ -401,7 +401,7 @@ class TestSNAT(VppTestCase): self.pg4.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg3.get_capture() + capture = self.pg3.get_capture(len(pkts)) self.verify_capture_out(capture, nat_ip1, True) # inside interface VRF don't match SNAT static mapping VRF (packets @@ -427,7 +427,7 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg3.get_capture() + capture = self.pg3.get_capture(len(pkts)) self.verify_capture_out(capture) # out2in 1st interface @@ -435,7 +435,7 @@ class TestSNAT(VppTestCase): self.pg3.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg0.get_capture() + capture = self.pg0.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg0) # in2out 2nd interface @@ -443,7 +443,7 @@ class TestSNAT(VppTestCase): self.pg1.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg3.get_capture() + capture = self.pg3.get_capture(len(pkts)) self.verify_capture_out(capture) # out2in 2nd interface @@ -451,7 +451,7 @@ class TestSNAT(VppTestCase): self.pg3.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg1.get_capture() + capture = self.pg1.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg1) # in2out 3rd interface @@ -459,7 +459,7 @@ class TestSNAT(VppTestCase): self.pg2.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg3.get_capture() + capture = self.pg3.get_capture(len(pkts)) self.verify_capture_out(capture) # out2in 3rd interface @@ -467,7 +467,7 @@ class TestSNAT(VppTestCase): self.pg3.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg2.get_capture() + capture = self.pg2.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg2) def test_inside_overlapping_interfaces(self): @@ -485,7 +485,7 @@ class TestSNAT(VppTestCase): self.pg4.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg3.get_capture() + capture = self.pg3.get_capture(len(pkts)) self.verify_capture_out(capture) # out2in 1st interface @@ -493,7 +493,7 @@ class TestSNAT(VppTestCase): self.pg3.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg4.get_capture() + capture = self.pg4.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg4) # in2out 2nd interface @@ -501,7 +501,7 @@ class TestSNAT(VppTestCase): self.pg5.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg3.get_capture() + capture = self.pg3.get_capture(len(pkts)) self.verify_capture_out(capture) # out2in 2nd interface @@ -509,7 +509,7 @@ class TestSNAT(VppTestCase): self.pg3.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg5.get_capture() + capture = self.pg5.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg5) # in2out 3rd interface @@ -517,7 +517,7 @@ class TestSNAT(VppTestCase): self.pg6.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg3.get_capture() + capture = self.pg3.get_capture(len(pkts)) self.verify_capture_out(capture) # out2in 3rd interface @@ -525,7 +525,7 @@ class TestSNAT(VppTestCase): self.pg3.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg6.get_capture() + capture = self.pg6.get_capture(len(pkts)) self.verify_capture_in(capture, self.pg6) def test_hairpinning(self): @@ -553,8 +553,7 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(p) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg0.get_capture() - self.assertEqual(1, len(capture)) + capture = self.pg0.get_capture(1) p = capture[0] try: ip = p[IP] @@ -575,8 +574,7 @@ class TestSNAT(VppTestCase): self.pg0.add_stream(p) self.pg_enable_capture(self.pg_interfaces) self.pg_start() - capture = self.pg0.get_capture() - self.assertEqual(1, len(capture)) + capture = self.pg0.get_capture(1) p = capture[0] try: ip = p[IP] @@ -613,8 +611,7 @@ class TestSNAT(VppTestCase): self.pg_start() # verify number of translated packet - capture = self.pg1.get_capture() - self.assertEqual(pkts_num, len(capture)) + self.pg1.get_capture(pkts_num) def tearDown(self): super(TestSNAT, self).tearDown() diff --git a/test/test_span.py b/test/test_span.py index e42fbd77..41507092 100644 --- a/test/test_span.py +++ b/test/test_span.py @@ -85,8 +85,7 @@ class TestSpan(VppTestCase): pkts = [] for i in range(0, TestSpan.pkts_per_burst): dst_if = self.flows[src_if][0] - pkt_info = self.create_packet_info( - src_if.sw_if_index, dst_if.sw_if_index) + pkt_info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(pkt_info) p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4) / @@ -184,9 +183,11 @@ class TestSpan(VppTestCase): # Verify packets outgoing packet streams on mirrored interface (pg2) self.logger.info("Verifying capture on interfaces %s and %s" % (self.pg1.name, self.pg2.name)) - self.verify_capture(self.pg1, - self.pg1.get_capture(), - self.pg2.get_capture()) + pg2_expected = self.get_packet_count_for_if_idx(self.pg1.sw_if_index) + self.verify_capture( + self.pg1, + self.pg1.get_capture(), + self.pg2.get_capture(pg2_expected)) if __name__ == '__main__': diff --git a/test/util.py b/test/util.py index 0ac23760..6658febf 100644 --- a/test/util.py +++ b/test/util.py @@ -24,17 +24,14 @@ def ppc(headline, capture, limit=10): """ if not capture: return headline - result = headline + "\n" - count = 1 - for p in capture: - result.append(ppp("Packet #%s:" % count, p)) - count += 1 - if count >= limit: - break + tail = "" if limit < len(capture): - result.append( - "Capture contains %s packets in total, of which %s were printed" % - (len(capture), limit)) + tail = "\nPrint limit reached, %s out of %s packets printed" % ( + len(capture), limit) + limit = len(capture) + body = "".join([ppp("Packet #%s:" % count, p) + for count, p in zip(range(0, limit), capture)]) + return "%s\n%s%s" % (headline, body, tail) class NumericConstant(object): diff --git a/test/vpp_pg_interface.py b/test/vpp_pg_interface.py index 634d7d3e..f4305275 100644 --- a/test/vpp_pg_interface.py +++ b/test/vpp_pg_interface.py @@ -1,5 +1,6 @@ import os import time +from traceback import format_exc from scapy.utils import wrpcap, rdpcap, PcapReader from vpp_interface import VppInterface @@ -130,30 +131,20 @@ class VppPGInterface(VppInterface): # FIXME this should be an API, but no such exists atm self.test.vapi.cli(self.input_cli) - def get_capture(self, remark=None, filter_fn=is_ipv6_misc): - """ - Get captured packets - - :param remark: remark printed into debug logs - :param filter_fn: filter applied to each packet, packets for which - the filter returns True are removed from capture - :returns: iterable packets - """ + def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc): + """ Helper method to get capture and filter it """ try: - self.wait_for_capture_file() + if not self.wait_for_capture_file(timeout): + return None output = rdpcap(self.out_path) self.test.logger.debug("Capture has %s packets" % len(output.res)) - except IOError: # TODO - self.test.logger.debug("File %s does not exist, probably because no" - " packets arrived" % self.out_path) - if remark: - raise Exception("No packets captured on %s(%s)" % - (self.name, remark)) - else: - raise Exception("No packets captured on %s" % self.name) + except: + self.test.logger.debug("Exception in scapy.rdpcap(%s): %s" % + (self.out_path, format_exc())) + return None before = len(output.res) - if filter_fn: - output.res = [p for p in output.res if not filter_fn(p)] + if filter_out_fn: + output.res = [p for p in output.res if not filter_out_fn(p)] removed = len(output.res) - before if removed: self.test.logger.debug( @@ -161,21 +152,75 @@ class VppPGInterface(VppInterface): (removed, len(output.res))) return output - def assert_nothing_captured(self, remark=None): + def get_capture(self, expected_count=None, remark=None, timeout=1, + filter_out_fn=is_ipv6_misc): + """ Get captured packets + + :param expected_count: expected number of packets to capture, if None, + then self.test.packet_count_for_dst_pg_idx is + used to lookup the expected count + :param remark: remark printed into debug logs + :param timeout: how long to wait for packets + :param filter_out_fn: filter applied to each packet, packets for which + the filter returns True are removed from capture + :returns: iterable packets + """ + remaining_time = timeout + capture = None + name = self.name if remark is None else "%s (%s)" % (self.name, remark) + based_on = "based on provided argument" + if expected_count is None: + expected_count = \ + self.test.get_packet_count_for_if_idx(self.sw_if_index) + based_on = "based on stored packet_infos" + self.test.logger.debug("Expecting to capture %s(%s) packets on %s" % ( + expected_count, based_on, name)) + if expected_count == 0: + raise Exception( + "Internal error, expected packet count for %s is 0!" % name) + while remaining_time > 0: + before = time.time() + capture = self._get_capture(remaining_time, filter_out_fn) + elapsed_time = time.time() - before + if capture: + if len(capture.res) == expected_count: + # bingo, got the packets we expected + return capture + remaining_time -= elapsed_time + if capture: + raise Exception("Captured packets mismatch, captured %s packets, " + "expected %s packets on %s" % + (len(capture.res), expected_count, name)) + else: + raise Exception("No packets captured on %s" % name) + + def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc): + """ Assert that nothing unfiltered was captured on interface + + :param remark: remark printed into debug logs + :param filter_out_fn: filter applied to each packet, packets for which + the filter returns True are removed from capture + """ if os.path.isfile(self.out_path): try: - capture = self.get_capture(remark=remark) + capture = self.get_capture( + 0, remark=remark, filter_out_fn=filter_out_fn) + if capture: + if len(capture.res) == 0: + # junk filtered out, we're good + return self.test.logger.error( ppc("Unexpected packets captured:", capture)) except: pass if remark: raise AssertionError( - "Capture file present for interface %s(%s)" % + "Non-empty capture file present for interface %s(%s)" % (self.name, remark)) else: - raise AssertionError("Capture file present for interface %s" % - self.name) + raise AssertionError( + "Non-empty capture file present for interface %s" % + self.name) def wait_for_capture_file(self, timeout=1): """ @@ -183,15 +228,17 @@ class VppPGInterface(VppInterface): :param timeout: How long to wait for the packet (default 1s) - :raises Exception: if the capture file does not appear within timeout + :returns: True/False if the file is present or appears within timeout """ limit = time.time() + timeout if not os.path.isfile(self.out_path): - self.test.logger.debug( - "Waiting for capture file to appear, timeout is %ss", timeout) + self.test.logger.debug("Waiting for capture file %s to appear, " + "timeout is %ss" % (self.out_path, timeout)) else: - self.test.logger.debug("Capture file already exists") - return + self.test.logger.debug( + "Capture file %s already exists" % + self.out_path) + return True while time.time() < limit: if os.path.isfile(self.out_path): break @@ -201,9 +248,10 @@ class VppPGInterface(VppInterface): (time.time() - (limit - timeout))) else: self.test.logger.debug("Timeout - capture file still nowhere") - raise Exception("Capture file did not appear within timeout") + return False + return True - def wait_for_packet(self, timeout): + def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc): """ Wait for next packet captured with a timeout @@ -212,18 +260,34 @@ class VppPGInterface(VppInterface): :returns: Captured packet if no packet arrived within timeout :raises Exception: if no packet arrives within timeout """ - limit = time.time() + timeout + deadline = time.time() + timeout if self._pcap_reader is None: - self.wait_for_capture_file(timeout) - self._pcap_reader = PcapReader(self.out_path) + if not self.wait_for_capture_file(timeout): + raise Exception("Capture file %s did not appear within " + "timeout" % self.out_path) + while time.time() < deadline: + try: + self._pcap_reader = PcapReader(self.out_path) + break + except: + self.test.logger.debug("Exception in scapy.PcapReader(%s): " + "%s" % (self.out_path, format_exc())) + if not self._pcap_reader: + raise Exception("Capture file %s did not appear within " + "timeout" % self.out_path) self.test.logger.debug("Waiting for packet") - while time.time() < limit: + while time.time() < deadline: p = self._pcap_reader.recv() if p is not None: - self.test.logger.debug("Packet received after %fs", - (time.time() - (limit - timeout))) - return p + if filter_out_fn is not None and filter_out_fn(p): + self.test.logger.debug( + "Packet received after %ss was filtered out" % + (time.time() - (deadline - timeout))) + else: + self.test.logger.debug("Packet received after %fs" % + (time.time() - (deadline - timeout))) + return p time.sleep(0) # yield self.test.logger.debug("Timeout - no packets received") raise Exception("Packet didn't arrive within timeout") @@ -258,12 +322,12 @@ class VppPGInterface(VppInterface): self.test.pg_start() self.test.logger.info(self.test.vapi.cli("show trace")) try: - arp_reply = pg_interface.get_capture(filter_fn=None) + captured_packet = pg_interface.wait_for_packet(1) except: self.test.logger.info("No ARP received on port %s" % pg_interface.name) return - arp_reply = arp_reply[0] + arp_reply = captured_packet.copy() # keep original for exception # Make Dot1AD packet content recognizable to scapy if arp_reply.type == 0x88a8: arp_reply.type = 0x8100 @@ -274,19 +338,19 @@ class VppPGInterface(VppInterface): (self.name, arp_reply[ARP].hwsrc)) self._local_mac = arp_reply[ARP].hwsrc else: - self.test.logger.info( - "No ARP received on port %s" % - pg_interface.name) + self.test.logger.info("No ARP received on port %s" % + pg_interface.name) except: self.test.logger.error( - ppp("Unexpected response to ARP request:", arp_reply)) + ppp("Unexpected response to ARP request:", captured_packet)) raise - def resolve_ndp(self, pg_interface=None): + def resolve_ndp(self, pg_interface=None, timeout=1): """Resolve NDP using provided packet-generator interface :param pg_interface: interface used to resolve, if None then this interface is used + :param timeout: how long to wait for response before giving up """ if pg_interface is None: @@ -297,17 +361,19 @@ class VppPGInterface(VppInterface): pg_interface.add_stream(ndp_req) pg_interface.enable_capture() self.test.pg_start() - self.test.logger.info(self.test.vapi.cli("show trace")) - replies = pg_interface.get_capture(filter_fn=None) - if replies is None or len(replies) == 0: - self.test.logger.info( - "No NDP received on port %s" % - pg_interface.name) - return + now = time.time() + deadline = now + timeout # Enabling IPv6 on an interface can generate more than the # ND reply we are looking for (namely MLD). So loop through # the replies to look for want we want. - for ndp_reply in replies: + while now < deadline: + try: + captured_packet = pg_interface.wait_for_packet( + deadline - now, filter_out_fn=None) + except: + self.test.logger.error("Timeout while waiting for NDP response") + raise + ndp_reply = captured_packet.copy() # keep original for exception # Make Dot1AD packet content recognizable to scapy if ndp_reply.type == 0x88a8: ndp_reply.type = 0x8100 @@ -318,9 +384,13 @@ class VppPGInterface(VppInterface): self.test.logger.info("VPP %s MAC address is %s " % (self.name, opt.lladdr)) self._local_mac = opt.lladdr + self.test.logger.debug(self.test.vapi.cli("show trace")) + # we now have the MAC we've been after + return except: self.test.logger.info( - ppp("Unexpected response to NDP request:", ndp_reply)) - # if no packets above provided the local MAC, then this failed. - if not hasattr(self, '_local_mac'): - raise + ppp("Unexpected response to NDP request:", captured_packet)) + now = time.time() + + self.test.logger.debug(self.test.vapi.cli("show trace")) + raise Exception("Timeout while waiting for NDP response") -- cgit 1.2.3-korg From da505f608e0919c45089dc80f9e3e16330a6551a Mon Sep 17 00:00:00 2001 From: Klement Sekera Date: Wed, 4 Jan 2017 12:58:53 +0100 Subject: make test: improve documentation and PEP8 compliance Change-Id: Ib4f0353aab6112fcc3c3d8f0bcbed5bc4b567b9b Signed-off-by: Klement Sekera --- test/Makefile | 4 +- test/doc/index.rst | 285 ++++++++++++++++++++++++++++++++++-- test/framework.py | 23 +-- test/hook.py | 4 +- test/template_bd.py | 4 +- test/test_classifier.py | 14 +- test/test_flowperpkt.py | 4 +- test/test_ip4.py | 17 ++- test/test_ip4_vrf_multi_instance.py | 11 +- test/test_ip6.py | 57 ++++---- test/test_l2_fib.py | 11 +- test/test_l2bd_multi_instance.py | 13 +- test/test_l2xc_multi_instance.py | 20 ++- test/test_snat.py | 20 ++- test/test_span.py | 11 +- test/vpp_interface.py | 14 +- test/vpp_ip_route.py | 52 ++++--- test/vpp_papi_provider.py | 12 +- test/vpp_pg_interface.py | 48 +++--- 19 files changed, 468 insertions(+), 156 deletions(-) (limited to 'test/test_l2xc_multi_instance.py') diff --git a/test/Makefile b/test/Makefile index 204afd38..543fe913 100644 --- a/test/Makefile +++ b/test/Makefile @@ -17,7 +17,7 @@ PAPI_INSTALL_DONE=$(VPP_PYTHON_PREFIX)/papi-install.done PAPI_INSTALL_FLAGS=$(PIP_INSTALL_DONE) $(PIP_PATCH_DONE) $(PAPI_INSTALL_DONE) $(PIP_INSTALL_DONE): - @virtualenv $(PYTHON_VENV_PATH) + @virtualenv $(PYTHON_VENV_PATH) -p python2.7 @bash -c "source $(PYTHON_VENV_PATH)/bin/activate && pip install $(PYTHON_DEPENDS)" @touch $@ @@ -54,7 +54,7 @@ wipe: reset @rm -f $(PAPI_INSTALL_FLAGS) doc: verify-python-path - @virtualenv $(PYTHON_VENV_PATH) + @virtualenv $(PYTHON_VENV_PATH) -p python2.7 @bash -c "source $(PYTHON_VENV_PATH)/bin/activate && pip install $(PYTHON_DEPENDS) sphinx" @bash -c "source $(PYTHON_VENV_PATH)/bin/activate && make -C doc WS_ROOT=$(WS_ROOT) BR=$(BR) NO_VPP_PAPI=1 html" diff --git a/test/doc/index.rst b/test/doc/index.rst index 8cbe961f..f51d5058 100644 --- a/test/doc/index.rst +++ b/test/doc/index.rst @@ -4,6 +4,7 @@ .. _SkipTest: https://docs.python.org/2/library/unittest.html#unittest.SkipTest .. _virtualenv: http://docs.python-guide.org/en/latest/dev/virtualenvs/ .. _scapy: http://www.secdev.org/projects/scapy/ +.. _logging: https://docs.python.org/2/library/logging.html .. |vtf| replace:: VPP Test Framework @@ -39,7 +40,7 @@ Function flow when running a test case is: 3. *test_*: This is the guts of the test case. It should execute the test scenario and use the various assert functions from the unittest framework to check - necessary. + necessary. Multiple test_ methods can exist in a test case. 4. `tearDown `: The tearDown function is called after each test function with the purpose of doing partial cleanup. @@ -47,16 +48,56 @@ Function flow when running a test case is: Method called once after running all of the test functions to perform the final cleanup. +Logging +####### + +Each test case has a logger automatically created for it, stored in +'logger' property, based on logging_. Use the logger's standard methods +debug(), info(), error(), ... to emit log messages to the logger. + +All the log messages go always into a log file in temporary directory +(see below). + +To control the messages printed to console, specify the V= parameter. + +.. code-block:: shell + + make test # minimum verbosity + make test V=1 # moderate verbosity + make test V=2 # maximum verbosity + Test temporary directory and VPP life cycle ########################################### Test separation is achieved by separating the test files and vpp instances. Each test creates a temporary directory and it's name is used to create a shared memory prefix which is used to run a VPP instance. +The temporary directory name contains the testcase class name for easy +reference, so for testcase named 'TestVxlan' the directory could be named +e.g. vpp-unittest-TestVxlan-UNUP3j. This way, there is no conflict between any other VPP instances running on the box and the test VPP. Any temporary files created by the test case are stored in this temporary test directory. +The test temporary directory holds the following interesting files: + +* log.txt - this contains the logger output on max verbosity +* pg*_in.pcap - last injected packet stream into VPP, named after the interface, + so for pg0, the file will be named pg0_in.pcap +* pg*_out.pcap - last capture file created by VPP for interface, similarly, + named after the interface, so for e.g. pg1, the file will be named + pg1_out.pcap +* history files - whenever the capture is restarted or a new stream is added, + the existing files are rotated and renamed, soo all the pcap files + are always saved for later debugging if needed +* core - if vpp dumps a core, it'll be stored in the temporary directory +* vpp_stdout.txt - file containing output which vpp printed to stdout +* vpp_stderr.txt - file containing output which vpp printed to stderr + +*NOTE*: existing temporary directories named vpp-unittest-* are automatically +removed when invoking 'make test*' or 'make retest*' to keep the temporary +directory clean. + Virtual environment ################### @@ -82,6 +123,37 @@ thus: So e.g. `remote_mac ` address is the MAC address assigned to the virtual host connected to the VPP. +Automatically generated addresses +################################# + +To send packets, one needs to typically provide some addresses, otherwise +the packets will be dropped. The interface objects in |vtf| automatically +provide addresses based on (typically) their indexes, which ensures +there are no conflicts and eases debugging by making the addressing scheme +consistent. + +The developer of a test case typically doesn't need to work with the actual +numbers, rather using the properties of the objects. The addresses typically +come in two flavors: '
' and '
n' - note the 'n' suffix. +The former address is a Python string, while the latter is translated using +socket.inet_pton to raw format in network byte order - this format is suitable +for passing as an argument to VPP APIs. + +e.g. for the IPv4 address assigned to the VPP interface: + +* local_ip4 - Local IPv4 address on VPP interface (string) +* local_ip4n - Local IPv4 address - raw, suitable as API parameter. + +These addresses need to be configured in VPP to be usable using e.g. +`config_ip4` API. Please see the documentation to `VppInterface` for more +details. + +By default, there is one remote address of each kind created for L3: +remote_ip4 and remote_ip6. If the test needs more addresses, because it's +simulating more remote hosts, they can be generated using +`generate_remote_hosts` API and the entries for them inserted into the ARP +table using `configure_ipv4_neighbors` API. + Packet flow in the |vtf| ######################## @@ -93,6 +165,10 @@ using packet-generator interfaces, represented by the `VppPGInterface` class. Packets are written into a temporary .pcap file, which is then read by the VPP and the packets are injected into the VPP world. +To add a list of packets to an interface, call the `add_stream` method on that +interface. Once everything is prepared, call `pg_start` method to start +the packet generator on the VPP side. + VPP -> test framework ~~~~~~~~~~~~~~~~~~~~~ @@ -100,6 +176,72 @@ Similarly, VPP doesn't send any packets to |vtf| directly. Instead, packet capture feature is used to capture and write traffic to a temporary .pcap file, which is then read and analyzed by the |vtf|. +The following APIs are available to the test case for reading pcap files. + +* `get_capture`: this API is suitable for bulk & batch style of test, where + a list of packets is prepared & sent, then the received packets are read + and verified. The API needs the number of packets which are expected to + be captured (ignoring filtered packets - see below) to know when the pcap + file is completely written by the VPP. If using packet infos for verifying + packets, then the counts of the packet infos can be automatically used + by `get_capture` to get the proper count (in this case the default value + None can be supplied as expected_count or ommitted altogether). +* `wait_for_packet`: this API is suitable for interactive style of test, + e.g. when doing session management, three-way handsakes, etc. This API waits + for and returns a single packet, keeping the capture file in place + and remembering context. Repeated invocations return following packets + (or raise Exception if timeout is reached) from the same capture file + (= packets arriving on the same interface). + +*NOTE*: it is not recommended to mix these APIs unless you understand how they +work internally. None of these APIs rotate the pcap capture file, so calling +e.g. `get_capture` after `wait_for_packet` will return already read packets. +It is safe to switch from one API to another after calling `enable_capture` +as that API rotates the capture file. + +Automatic filtering of packets: +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Both APIs (`get_capture` and `wait_for_packet`) by default filter the packet +capture, removing known uninteresting packets from it - these are IPv6 Router +Advertisments and IPv6 Router Alerts. These packets are unsolicitated +and from the point of |vtf| are random. If a test wants to receive these +packets, it should specify either None or a custom filtering function +as the value to the 'filter_out_fn' argument. + +Common API flow for sending/receiving packets: +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +We will describe a simple scenario, where packets are sent from pg0 to pg1 +interface, assuming that the interfaces were created using +`create_pg_interfaces` API. + +1. Create a list of packets for pg0:: + + packet_count = 10 + packets = create_packets(src=self.pg0, dst=self.pg1, + count=packet_count) + +2. Add that list of packets to the source interface:: + + self.pg0.add_stream(packets) + +3. Enable capture on the destination interface:: + + self.pg1.enable_capture() + +4. Start the packet generator:: + + self.pg_start() + +5. Wait for capture file to appear and read it:: + + capture = self.pg1.get_capture(expected_count=packet_count) + +6. Verify packets match sent packets:: + + self.verify_capture(send=packets, captured=capture) + Test framework objects ###################### @@ -113,8 +255,8 @@ common tasks easily in the test cases. * `VppSubInterface`: VPP sub-interface abstract class, containing common functionality for e.g. `VppDot1QSubint` and `VppDot1ADSubint` classes -How VPP API/CLI is called -######################### +How VPP APIs/CLIs are called +############################ Vpp provides python bindings in a python module called vpp-papi, which the test framework installs in the virtual environment. A shim layer represented by @@ -137,19 +279,144 @@ purposes: more readable. E.g. ip_add_del_route API takes ~25 parameters, of which in the common case, only 3 are needed. +Utility methods +############### + +Some interesting utility methods are: + +* `ppp`: 'Pretty Print Packet' - returns a string containing the same output + as Scapy's packet.show() would print +* `ppc`: 'Pretty Print Capture' - returns a string containing printout of + a capture (with configurable limit on the number of packets printed from it) + using `ppp` + +*NOTE*: Do not use Scapy's packet.show() in the tests, because it prints +the output to stdout. All output should go to the logger associated with +the test case. + Example: how to add a new test ############################## -In this example, we will describe how to add a new test case which tests VPP... +In this example, we will describe how to add a new test case which tests +basic IPv4 forwarding. + +1. Add a new file called test_ip4_fwd.py in the test directory, starting + with a few imports:: + + from framework import VppTestCase + from scapy.layers.l2 import Ether + from scapy.packet import Raw + from scapy.layers.inet import IP, UDP + from random import randint + +2. Create a class inherited from the VppTestCase:: + + class IP4FwdTestCase(VppTestCase): + """ IPv4 simple forwarding test case """ + +2. Add a setUpClass function containing the setup needed for our test to run:: + + @classmethod + def setUpClass(self): + super(IP4FwdTestCase, self).setUpClass() + self.create_pg_interfaces(range(2)) # create pg0 and pg1 + for i in self.pg_interfaces: + i.admin_up() # put the interface up + i.config_ip4() # configure IPv4 address on the interface + i.resolve_arp() # resolve ARP, so that we know VPP MAC + +3. Create a helper method to create the packets to send:: + + def create_stream(self, src_if, dst_if, count): + packets = [] + for i in range(count): + # create packet info stored in the test case instance + info = self.create_packet_info(src_if, dst_if) + # convert the info into packet payload + payload = self.info_to_payload(info) + # create the packet itself + p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / + IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4) / + UDP(sport=randint(1000, 2000), dport=5678) / + Raw(payload)) + # store a copy of the packet in the packet info + info.data = p.copy() + # append the packet to the list + packets.append(p) + + # return the created packet list + return packets + +4. Create a helper method to verify the capture:: + + def verify_capture(self, src_if, dst_if, capture): + packet_info = None + for packet in capture: + try: + ip = packet[IP] + udp = packet[UDP] + # convert the payload to packet info object + payload_info = self.payload_to_info(str(packet[Raw])) + # make sure the indexes match + self.assert_equal(payload_info.src, src_if.sw_if_index, + "source sw_if_index") + self.assert_equal(payload_info.dst, dst_if.sw_if_index, + "destination sw_if_index") + packet_info = self.get_next_packet_info_for_interface2( + src_if.sw_if_index, + dst_if.sw_if_index, + packet_info) + # make sure we didn't run out of saved packets + self.assertIsNotNone(packet_info) + self.assert_equal(payload_info.index, packet_info.index, + "packet info index") + saved_packet = packet_info.data # fetch the saved packet + # assert the values match + self.assert_equal(ip.src, saved_packet[IP].src, + "IP source address") + # ... more assertions here + self.assert_equal(udp.sport, saved_packet[UDP].sport, + "UDP source port") + except: + self.logger.error(ppp("Unexpected or invalid packet:", + packet)) + raise + remaining_packet = self.get_next_packet_info_for_interface2( + src_if.sw_if_index, + dst_if.sw_if_index, + packet_info) + self.assertIsNone(remaining_packet, + "Interface %s: Packet expected from interface " + "%s didn't arrive" % (dst_if.name, src_if.name)) + +5. Add the test code to test_basic function:: + + def test_basic(self): + count = 10 + # create the packet stream + packets = self.create_stream(self.pg0, self.pg1, count) + # add the stream to the source interface + self.pg0.add_stream(packets) + # enable capture on both interfaces + self.pg0.enable_capture() + self.pg1.enable_capture() + # start the packet generator + self.pg_start() + # get capture - the proper count of packets was saved by + # create_packet_info() based on dst_if parameter + capture = self.pg1.get_capture() + # assert nothing captured on pg0 (always do this last, so that + # some time has already passed since pg_start()) + self.pg0.assert_nothing_captured() + # verify capture + self.verify_capture(self.pg0, self.pg1, capture) + +6. Run the test by issuing 'make test'. -1. Add a new file called... -2. Add a setUpClass function containing... -3. Add the test code in test... -4. Run the test... |vtf| module documentation ########################## - + .. toctree:: :maxdepth: 2 :glob: diff --git a/test/framework.py b/test/framework.py index 896a1e0d..b2c6b9e4 100644 --- a/test/framework.py +++ b/test/framework.py @@ -117,7 +117,8 @@ class VppTestCase(unittest.TestCase): debug_cli = "" if cls.step or cls.debug_gdb or cls.debug_gdbserver: debug_cli = "cli-listen localhost:5002" - cls.vpp_cmdline = [cls.vpp_bin, "unix", "{", "nodaemon", debug_cli, "}", + cls.vpp_cmdline = [cls.vpp_bin, + "unix", "{", "nodaemon", debug_cli, "}", "api-segment", "{", "prefix", cls.shm_prefix, "}"] if cls.plugin_path is not None: cls.vpp_cmdline.extend(["plugin_path", cls.plugin_path]) @@ -246,8 +247,8 @@ class VppTestCase(unittest.TestCase): print(double_line_delim) print("VPP or GDB server is still running") print(single_line_delim) - raw_input("When done debugging, press ENTER to kill the process" - " and finish running the testcase...") + raw_input("When done debugging, press ENTER to kill the " + "process and finish running the testcase...") if hasattr(cls, 'vpp'): if hasattr(cls, 'vapi'): @@ -563,10 +564,10 @@ class VppTestResult(unittest.TestResult): def __init__(self, stream, descriptions, verbosity): """ - :param stream File descriptor to store where to report test results. Set - to the standard error stream by default. - :param descriptions Boolean variable to store information if to use test - case descriptions. + :param stream File descriptor to store where to report test results. + Set to the standard error stream by default. + :param descriptions Boolean variable to store information if to use + test case descriptions. :param verbosity Integer variable to store required verbosity level. """ unittest.TestResult.__init__(self, stream, descriptions, verbosity) @@ -664,12 +665,12 @@ class VppTestResult(unittest.TestResult): unittest.TestResult.stopTest(self, test) if self.verbosity > 0: self.stream.writeln(single_line_delim) - self.stream.writeln("%-60s%s" % - (self.getDescription(test), self.result_string)) + self.stream.writeln("%-60s%s" % (self.getDescription(test), + self.result_string)) self.stream.writeln(single_line_delim) else: - self.stream.writeln("%-60s%s" % - (self.getDescription(test), self.result_string)) + self.stream.writeln("%-60s%s" % (self.getDescription(test), + self.result_string)) def printErrors(self): """ diff --git a/test/hook.py b/test/hook.py index f3e5f880..247704ec 100644 --- a/test/hook.py +++ b/test/hook.py @@ -105,8 +105,8 @@ class PollHook(Hook): s = signaldict[abs(self.testcase.vpp.returncode)] else: s = "unknown" - msg = "VPP subprocess died unexpectedly with returncode %d [%s]" % ( - self.testcase.vpp.returncode, s) + msg = "VPP subprocess died unexpectedly with returncode %d [%s]" %\ + (self.testcase.vpp.returncode, s) self.logger.critical(msg) core_path = self.testcase.tempdir + '/core' if os.path.isfile(core_path): diff --git a/test/template_bd.py b/test/template_bd.py index 91d5dd71..ae171351 100644 --- a/test/template_bd.py +++ b/test/template_bd.py @@ -75,8 +75,8 @@ class BridgeDomain(object): self.pg_start() - # Pick first received frame and check if it's the - # non-encapsulated frame + # Pick first received frame and check if it's the non-encapsulated + # frame out = self.pg1.get_capture(1) pkt = out[0] self.assert_eq_pkts(pkt, self.frame_request) diff --git a/test/test_classifier.py b/test/test_classifier.py index 302430f8..faa107dc 100644 --- a/test/test_classifier.py +++ b/test/test_classifier.py @@ -114,8 +114,9 @@ class TestClassifier(VppTestCase): payload_info = self.payload_to_info(str(packet[Raw])) packet_index = payload_info.index self.assertEqual(payload_info.dst, dst_sw_if_index) - self.logger.debug("Got packet on port %s: src=%u (id=%u)" % - (dst_if.name, payload_info.src, packet_index)) + self.logger.debug( + "Got packet on port %s: src=%u (id=%u)" % + (dst_if.name, payload_info.src, packet_index)) next_info = self.get_next_packet_info_for_interface2( payload_info.src, dst_sw_if_index, last_info[payload_info.src]) @@ -296,8 +297,9 @@ class TestClassifier(VppTestCase): pkts = self.create_stream(self.pg0, self.pg2, self.pg_if_packet_sizes) self.pg0.add_stream(pkts) - self.create_classify_table( - 'mac', self.build_mac_mask(src_mac='ffffffffffff'), data_offset=-14) + self.create_classify_table('mac', + self.build_mac_mask(src_mac='ffffffffffff'), + data_offset=-14) self.create_classify_session( self.pg0, self.acl_tbl_idx.get('mac'), self.build_mac_match(src_mac=self.pg0.remote_mac)) @@ -326,7 +328,9 @@ class TestClassifier(VppTestCase): pkts = self.create_stream(self.pg0, self.pg3, self.pg_if_packet_sizes) self.pg0.add_stream(pkts) - self.create_classify_table('pbr', self.build_ip_mask(src_ip='ffffffff')) + self.create_classify_table( + 'pbr', self.build_ip_mask( + src_ip='ffffffff')) pbr_option = 1 self.create_classify_session( self.pg0, self.acl_tbl_idx.get('pbr'), diff --git a/test/test_flowperpkt.py b/test/test_flowperpkt.py index 29b3353b..f16bfb7e 100644 --- a/test/test_flowperpkt.py +++ b/test/test_flowperpkt.py @@ -57,8 +57,8 @@ class TestFlowperpkt(VppTestCase): if len(payload) * 2 != len(masked_expected_data): return False - # iterate over pairs: raw byte from payload and ASCII code for that byte - # from masked payload (or XX if masked) + # iterate over pairs: raw byte from payload and ASCII code for that + # byte from masked payload (or XX if masked) for i in range(len(payload)): p = payload[i] m = masked_expected_data[2 * i:2 * i + 2] diff --git a/test/test_ip4.py b/test/test_ip4.py index df93533d..9bd9a458 100644 --- a/test/test_ip4.py +++ b/test/test_ip4.py @@ -148,8 +148,9 @@ class TestIPv4(VppTestCase): payload_info = self.payload_to_info(str(packet[Raw])) packet_index = payload_info.index self.assertEqual(payload_info.dst, dst_sw_if_index) - self.logger.debug("Got packet on port %s: src=%u (id=%u)" % - (dst_if.name, payload_info.src, packet_index)) + self.logger.debug( + "Got packet on port %s: src=%u (id=%u)" % + (dst_if.name, payload_info.src, packet_index)) next_info = self.get_next_packet_info_for_interface2( payload_info.src, dst_sw_if_index, last_info[payload_info.src]) @@ -209,7 +210,7 @@ class TestIPv4FibCrud(VppTestCase): - add new 1k, - del 1.5k - ..note:: Python API is to slow to add many routes, needs C code replacement. + ..note:: Python API is too slow to add many routes, needs replacement. """ def config_fib_many_to_one(self, start_dest_addr, next_hop_addr, count): @@ -221,8 +222,9 @@ class TestIPv4FibCrud(VppTestCase): :return list: added ips with 32 prefix """ added_ips = [] - dest_addr = int( - socket.inet_pton(socket.AF_INET, start_dest_addr).encode('hex'), 16) + dest_addr = int(socket.inet_pton(socket.AF_INET, + start_dest_addr).encode('hex'), + 16) dest_addr_len = 32 n_next_hop_addr = socket.inet_pton(socket.AF_INET, next_hop_addr) for _ in range(count): @@ -236,8 +238,9 @@ class TestIPv4FibCrud(VppTestCase): def unconfig_fib_many_to_one(self, start_dest_addr, next_hop_addr, count): removed_ips = [] - dest_addr = int( - socket.inet_pton(socket.AF_INET, start_dest_addr).encode('hex'), 16) + dest_addr = int(socket.inet_pton(socket.AF_INET, + start_dest_addr).encode('hex'), + 16) dest_addr_len = 32 n_next_hop_addr = socket.inet_pton(socket.AF_INET, next_hop_addr) for _ in range(count): diff --git a/test/test_ip4_vrf_multi_instance.py b/test/test_ip4_vrf_multi_instance.py index 1449ef7d..b4279194 100644 --- a/test/test_ip4_vrf_multi_instance.py +++ b/test/test_ip4_vrf_multi_instance.py @@ -95,7 +95,8 @@ class TestIp4VrfMultiInst(VppTestCase): try: # Create pg interfaces - cls.create_pg_interfaces(range(cls.nr_of_vrfs * cls.pg_ifs_per_vrf)) + cls.create_pg_interfaces( + range(cls.nr_of_vrfs * cls.pg_ifs_per_vrf)) # Packet flows mapping pg0 -> pg1, pg2 etc. cls.flows = dict() @@ -308,14 +309,14 @@ class TestIp4VrfMultiInst(VppTestCase): """ Create packet streams for all configured l2-pg interfaces, send all prepared packet streams and verify that: - - all packets received correctly on all pg-l2 interfaces assigned to - bridge domains + - all packets received correctly on all pg-l2 interfaces assigned + to bridge domains - no packet received on all pg-l2 interfaces not assigned to bridge domains :raise RuntimeError: If no packet captured on l2-pg interface assigned - to the bridge domain or if any packet is captured on l2-pg interface - not assigned to the bridge domain. + to the bridge domain or if any packet is captured on l2-pg + interface not assigned to the bridge domain. """ # Test # Create incoming packet streams for packet-generator interfaces diff --git a/test/test_ip6.py b/test/test_ip6.py index cd9c4b95..ea669b70 100644 --- a/test/test_ip6.py +++ b/test/test_ip6.py @@ -8,8 +8,8 @@ from vpp_sub_interface import VppSubInterface, VppDot1QSubint from scapy.packet import Raw from scapy.layers.l2 import Ether, Dot1Q -from scapy.layers.inet6 import IPv6, UDP, ICMPv6ND_NS, ICMPv6ND_RS, ICMPv6ND_RA, \ - ICMPv6NDOptSrcLLAddr, getmacbyip6, ICMPv6MRD_Solicitation +from scapy.layers.inet6 import IPv6, UDP, ICMPv6ND_NS, ICMPv6ND_RS, \ + ICMPv6ND_RA, ICMPv6NDOptSrcLLAddr, getmacbyip6, ICMPv6MRD_Solicitation from util import ppp from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ptop, in6_islladdr, \ in6_mactoifaceid @@ -172,8 +172,9 @@ class TestIPv6(VppTestCase): payload_info = self.payload_to_info(str(packet[Raw])) packet_index = payload_info.index self.assertEqual(payload_info.dst, dst_sw_if_index) - self.logger.debug("Got packet on port %s: src=%u (id=%u)" % - (dst_if.name, payload_info.src, packet_index)) + self.logger.debug( + "Got packet on port %s: src=%u (id=%u)" % + (dst_if.name, payload_info.src, packet_index)) next_info = self.get_next_packet_info_for_interface2( payload_info.src, dst_sw_if_index, last_info[payload_info.src]) @@ -229,9 +230,9 @@ class TestIPv6(VppTestCase): intf.assert_nothing_captured(remark=remark) def test_ns(self): - """ IPv6 Neighbour Soliciatation Exceptions + """ IPv6 Neighbour Solicitation Exceptions - Test sceanrio: + Test scenario: - Send an NS Sourced from an address not covered by the link sub-net - Send an NS to an mcast address the router has not joined - Send NS for a target address the router does not onn. @@ -249,12 +250,13 @@ class TestIPv6(VppTestCase): ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac)) pkts = [p] - self.send_and_assert_no_replies(self.pg0, pkts, - "No response to NS source by address not on sub-net") + self.send_and_assert_no_replies( + self.pg0, pkts, + "No response to NS source by address not on sub-net") # - # An NS for sent to a solicited mcast group the router is not a member of - # FAILS + # An NS for sent to a solicited mcast group the router is + # not a member of FAILS # if 0: nsma = in6_getnsma(inet_pton(socket.AF_INET6, "fd::ffff")) @@ -266,8 +268,9 @@ class TestIPv6(VppTestCase): ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac)) pkts = [p] - self.send_and_assert_no_replies(self.pg0, pkts, - "No response to NS sent to unjoined mcast address") + self.send_and_assert_no_replies( + self.pg0, pkts, + "No response to NS sent to unjoined mcast address") # # An NS whose target address is one the router does not own @@ -307,15 +310,15 @@ class TestIPv6(VppTestCase): in6_ptop(mk_ll_addr(intf.local_mac))) def test_rs(self): - """ IPv6 Router Soliciatation Exceptions + """ IPv6 Router Solicitation Exceptions - Test sceanrio: + Test scenario: """ # - # Before we begin change the IPv6 RA responses to use the unicast address - # that way we will not confuse them with the periodic Ras which go to the Mcast - # address + # Before we begin change the IPv6 RA responses to use the unicast + # address - that way we will not confuse them with the periodic + # RAs which go to the mcast address # self.pg0.ip6_ra_config(send_unicast=1) @@ -336,8 +339,8 @@ class TestIPv6(VppTestCase): # # When we reconfiure the IPv6 RA config, we reset the RA rate limiting, - # so we need to do this before each test below so as not to drop packets for - # rate limiting reasons. Test this works here. + # so we need to do this before each test below so as not to drop + # packets for rate limiting reasons. Test this works here. # self.pg0.ip6_ra_config(send_unicast=1) self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS") @@ -366,12 +369,12 @@ class TestIPv6(VppTestCase): self.pg0, pkts, "RS sourced from link-local", src_ip=ll) # - # Source from the unspecified address ::. This happens when the RS is sent before - # the host has a configured address/sub-net, i.e. auto-config. - # Since the sender has no IP address, the reply comes back mcast - so the - # capture needs to not filter this. - # If we happen to pick up the periodic RA at this point then so be it, it's not - # an error. + # Source from the unspecified address ::. This happens when the RS + # is sent before the host has a configured address/sub-net, + # i.e. auto-config. Since the sender has no IP address, the reply + # comes back mcast - so the capture needs to not filter this. + # If we happen to pick up the periodic RA at this point then so be it, + # it's not an error. # self.pg0.ip6_ra_config(send_unicast=1) p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / @@ -399,9 +402,9 @@ class TestIPv6(VppTestCase): @unittest.skip("Unsupported") def test_mrs(self): - """ IPv6 Multicast Router Soliciatation Exceptions + """ IPv6 Multicast Router Solicitation Exceptions - Test sceanrio: + Test scenario: """ # diff --git a/test/test_l2_fib.py b/test/test_l2_fib.py index d4ef3f4a..3d5d2129 100644 --- a/test/test_l2_fib.py +++ b/test/test_l2_fib.py @@ -106,7 +106,8 @@ class TestL2fib(VppTestCase): # Create BD with MAC learning and unknown unicast flooding disabled # and put interfaces to this BD - cls.vapi.bridge_domain_add_del(bd_id=cls.bd_id, uu_flood=0, learn=0) + cls.vapi.bridge_domain_add_del( + bd_id=cls.bd_id, uu_flood=0, learn=0) for pg_if in cls.pg_interfaces: cls.vapi.sw_interface_set_l2_bridge(pg_if.sw_if_index, bd_id=cls.bd_id) @@ -180,8 +181,8 @@ class TestL2fib(VppTestCase): end_nr = start + count / n_int for j in range(start, end_nr): host = self.hosts_by_pg_idx[pg_if.sw_if_index][j] - self.vapi.l2fib_add_del(host.mac, self.bd_id, pg_if.sw_if_index, - static_mac=1) + self.vapi.l2fib_add_del( + host.mac, self.bd_id, pg_if.sw_if_index, static_mac=1) counter += 1 percentage = counter / count * 100 if percentage > percent: @@ -202,8 +203,8 @@ class TestL2fib(VppTestCase): for pg_if in self.pg_interfaces: for j in range(count / n_int): host = self.hosts_by_pg_idx[pg_if.sw_if_index][0] - self.vapi.l2fib_add_del(host.mac, self.bd_id, pg_if.sw_if_index, - is_add=0) + self.vapi.l2fib_add_del( + host.mac, self.bd_id, pg_if.sw_if_index, is_add=0) self.deleted_hosts_by_pg_idx[pg_if.sw_if_index].append(host) del self.hosts_by_pg_idx[pg_if.sw_if_index][0] counter += 1 diff --git a/test/test_l2bd_multi_instance.py b/test/test_l2bd_multi_instance.py index a1226222..e24a8613 100644 --- a/test/test_l2bd_multi_instance.py +++ b/test/test_l2bd_multi_instance.py @@ -95,10 +95,10 @@ class TestL2bdMultiInst(VppTestCase): for i in range(0, len(cls.pg_interfaces), 3): cls.flows[cls.pg_interfaces[i]] = [cls.pg_interfaces[i + 1], cls.pg_interfaces[i + 2]] - cls.flows[cls.pg_interfaces[i + 1]] = [cls.pg_interfaces[i], - cls.pg_interfaces[i + 2]] - cls.flows[cls.pg_interfaces[i + 2]] = [cls.pg_interfaces[i], - cls.pg_interfaces[i + 1]] + cls.flows[cls.pg_interfaces[i + 1]] = \ + [cls.pg_interfaces[i], cls.pg_interfaces[i + 2]] + cls.flows[cls.pg_interfaces[i + 2]] = \ + [cls.pg_interfaces[i], cls.pg_interfaces[i + 1]] # Mapping between packet-generator index and lists of test hosts cls.hosts_by_pg_idx = dict() @@ -172,8 +172,9 @@ class TestL2bdMultiInst(VppTestCase): def create_bd_and_mac_learn(self, count, start=1): """ - Create required number of bridge domains with MAC learning enabled, put - 3 l2-pg interfaces to every bridge domain and send MAC learning packets. + Create required number of bridge domains with MAC learning enabled, + put 3 l2-pg interfaces to every bridge domain and send MAC learning + packets. :param int count: Number of bridge domains to be created. :param int start: Starting number of the bridge domain ID. diff --git a/test/test_l2xc_multi_instance.py b/test/test_l2xc_multi_instance.py index 6c28cebb..bb26f959 100644 --- a/test/test_l2xc_multi_instance.py +++ b/test/test_l2xc_multi_instance.py @@ -2,10 +2,10 @@ """L2XC Multi-instance Test Case HLD: **NOTES:** - - higher number (more than 15) of pg-l2 interfaces causes problems => only \ - 14 pg-l2 interfaces and 10 cross-connects are tested - - jumbo packets in configuration with 14 l2-pg interfaces leads to \ - problems too + - higher number (more than 15) of pg-l2 interfaces causes problems => only + 14 pg-l2 interfaces and 10 cross-connects are tested + - jumbo packets in configuration with 14 l2-pg interfaces leads to + problems too **config 1** - add 14 pg-l2 interfaces @@ -15,7 +15,8 @@ - send L2 MAC frames between all pairs of pg-l2 interfaces **verify 1** - - all packets received correctly in case of cross-connected l2-pg interfaces + - all packets received correctly in case of cross-connected l2-pg + interfaces - no packet received in case of not cross-connected l2-pg interfaces **config 2** @@ -25,7 +26,8 @@ - send L2 MAC frames between all pairs of pg-l2 interfaces **verify 2** - - all packets received correctly in case of cross-connected l2-pg interfaces + - all packets received correctly in case of cross-connected l2-pg + interfaces - no packet received in case of not cross-connected l2-pg interfaces **config 3** @@ -35,7 +37,8 @@ - send L2 MAC frames between all pairs of pg-l2 interfaces **verify 3** - - all packets received correctly in case of cross-connected l2-pg interfaces + - all packets received correctly in case of cross-connected l2-pg + interfaces - no packet received in case of not cross-connected l2-pg interfaces **config 4** @@ -79,7 +82,8 @@ class TestL2xcMultiInst(VppTestCase): cls.flows = dict() for i in range(len(cls.pg_interfaces)): delta = 1 if i % 2 == 0 else -1 - cls.flows[cls.pg_interfaces[i]] = [cls.pg_interfaces[i + delta]] + cls.flows[cls.pg_interfaces[i]] =\ + [cls.pg_interfaces[i + delta]] # Mapping between packet-generator index and lists of test hosts cls.hosts_by_pg_idx = dict() diff --git a/test/test_snat.py b/test/test_snat.py index ca2e52a7..d23becf5 100644 --- a/test/test_snat.py +++ b/test/test_snat.py @@ -130,13 +130,15 @@ class TestSNAT(VppTestCase): if same_port: self.assertEqual(packet[TCP].sport, self.tcp_port_in) else: - self.assertNotEqual(packet[TCP].sport, self.tcp_port_in) + self.assertNotEqual( + packet[TCP].sport, self.tcp_port_in) self.tcp_port_out = packet[TCP].sport elif packet.haslayer(UDP): if same_port: self.assertEqual(packet[UDP].sport, self.udp_port_in) else: - self.assertNotEqual(packet[UDP].sport, self.udp_port_in) + self.assertNotEqual( + packet[UDP].sport, self.udp_port_in) self.udp_port_out = packet[UDP].sport else: if same_port: @@ -215,8 +217,14 @@ class TestSNAT(VppTestCase): addr_only = 0 l_ip = socket.inet_pton(socket.AF_INET, local_ip) e_ip = socket.inet_pton(socket.AF_INET, external_ip) - self.vapi.snat_add_static_mapping(l_ip, e_ip, local_port, external_port, - addr_only, vrf_id, is_add) + self.vapi.snat_add_static_mapping( + l_ip, + e_ip, + local_port, + external_port, + addr_only, + vrf_id, + is_add) def snat_add_address(self, ip, is_add=1): """ @@ -413,7 +421,9 @@ class TestSNAT(VppTestCase): self.pg3.assert_nothing_captured() def test_multiple_inside_interfaces(self): - """SNAT multiple inside interfaces with non-overlapping address space""" + """ + SNAT multiple inside interfaces with non-overlapping address space + """ self.snat_add_address(self.snat_addr) self.vapi.snat_interface_add_del_feature(self.pg0.sw_if_index) diff --git a/test/test_span.py b/test/test_span.py index 41507092..dc0110db 100644 --- a/test/test_span.py +++ b/test/test_span.py @@ -102,11 +102,12 @@ class TestSpan(VppTestCase): for i in self.interfaces: last_info[i.sw_if_index] = None dst_sw_if_index = dst_if.sw_if_index - if len(capture_pg1) != len(capture_pg2): - self.logger.error( - "Different number of outgoing and mirrored packets : %u != %u" % - (len(capture_pg1), len(capture_pg2))) - raise + self.AssertEqual( + len(capture_pg1), + len(capture_pg2), + "Different number of outgoing and mirrored packets : %u != %u" % + (len(capture_pg1), + len(capture_pg2))) for pkt_pg1, pkt_pg2 in zip(capture_pg1, capture_pg2): try: ip1 = pkt_pg1[IP] diff --git a/test/vpp_interface.py b/test/vpp_interface.py index 9d9712eb..e0a29f94 100644 --- a/test/vpp_interface.py +++ b/test/vpp_interface.py @@ -15,7 +15,7 @@ class VppInterface(object): @property def remote_mac(self): - """MAC-address of the remote interface "connected" to this interface.""" + """MAC-address of the remote interface "connected" to this interface""" return self._remote_hosts[0].mac @property @@ -261,19 +261,23 @@ class VppInterface(object): def admin_up(self): """Put interface ADMIN-UP.""" - self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1) + self.test.vapi.sw_interface_set_flags(self.sw_if_index, + admin_up_down=1) def admin_down(self): """Put interface ADMIN-down.""" - self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=0) + self.test.vapi.sw_interface_set_flags(self.sw_if_index, + admin_up_down=0) def ip6_enable(self): """IPv6 Enable interface""" - self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index, enable=1) + self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index, + enable=1) def ip6_disable(self): """Put interface ADMIN-DOWN.""" - self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index, enable=0) + self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index, + enable=0) def add_sub_if(self, sub_if): """Register a sub-interface with this interface. diff --git a/test/vpp_ip_route.py b/test/vpp_ip_route.py index 75f400f1..975e3934 100644 --- a/test/vpp_ip_route.py +++ b/test/vpp_ip_route.py @@ -13,7 +13,13 @@ MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1 class RoutePath: - def __init__(self, nh_addr, nh_sw_if_index, nh_table_id=0, labels=[], nh_via_label=MPLS_LABEL_INVALID): + def __init__( + self, + nh_addr, + nh_sw_if_index, + nh_table_id=0, + labels=[], + nh_via_label=MPLS_LABEL_INVALID): self.nh_addr = socket.inet_pton(socket.AF_INET, nh_addr) self.nh_itf = nh_sw_if_index self.nh_table_id = nh_table_id @@ -36,15 +42,16 @@ class IpRoute: def add_vpp_config(self): for path in self.paths: - self._test.vapi.ip_add_del_route(self.dest_addr, - self.dest_addr_len, - path.nh_addr, - path.nh_itf, - table_id=self.table_id, - next_hop_out_label_stack=path.nh_labels, - next_hop_n_out_labels=len( - path.nh_labels), - next_hop_via_label=path.nh_via_label) + self._test.vapi.ip_add_del_route( + self.dest_addr, + self.dest_addr_len, + path.nh_addr, + path.nh_itf, + table_id=self.table_id, + next_hop_out_label_stack=path.nh_labels, + next_hop_n_out_labels=len( + path.nh_labels), + next_hop_via_label=path.nh_via_label) def remove_vpp_config(self): for path in self.paths: @@ -61,7 +68,7 @@ class MplsIpBind: MPLS to IP Binding """ - def __init__(self, test, local_label, dest_addr, dest_addr_len): + def __init__(self, test, local_label, dest_addr, dest_addr_len): self._test = test self.dest_addr = socket.inet_pton(socket.AF_INET, dest_addr) self.dest_addr_len = dest_addr_len @@ -93,17 +100,18 @@ class MplsRoute: def add_vpp_config(self): for path in self.paths: - self._test.vapi.mpls_route_add_del(self.local_label, - self.eos_bit, - 1, - path.nh_addr, - path.nh_itf, - table_id=self.table_id, - next_hop_out_label_stack=path.nh_labels, - next_hop_n_out_labels=len( - path.nh_labels), - next_hop_via_label=path.nh_via_label, - next_hop_table_id=path.nh_table_id) + self._test.vapi.mpls_route_add_del( + self.local_label, + self.eos_bit, + 1, + path.nh_addr, + path.nh_itf, + table_id=self.table_id, + next_hop_out_label_stack=path.nh_labels, + next_hop_n_out_labels=len( + path.nh_labels), + next_hop_via_label=path.nh_via_label, + next_hop_table_id=path.nh_table_id) def remove_vpp_config(self): for path in self.paths: diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py index 26edd4f2..b78e8613 100644 --- a/test/vpp_papi_provider.py +++ b/test/vpp_papi_provider.py @@ -5,9 +5,9 @@ from hook import Hook from collections import deque # Sphinx creates auto-generated documentation by importing the python source -# files and collecting the docstrings from them. The NO_VPP_PAPI flag allows the -# vpp_papi_provider.py file to be importable without having to build the whole -# vpp api if the user only wishes to generate the test documentation. +# files and collecting the docstrings from them. The NO_VPP_PAPI flag allows +# the vpp_papi_provider.py file to be importable without having to build +# the whole vpp api if the user only wishes to generate the test documentation. do_import = True try: no_vpp_papi = os.getenv("NO_VPP_PAPI") @@ -224,8 +224,8 @@ class VppPapiProvider(object): send_unicast,): return self.api(self.papi.sw_interface_ip6nd_ra_config, {'sw_if_index': sw_if_index, - 'suppress' : suppress, - 'send_unicast' : send_unicast}) + 'suppress': suppress, + 'send_unicast': send_unicast}) def ip6_sw_interface_enable_disable(self, sw_if_index, enable): """ @@ -972,7 +972,7 @@ class VppPapiProvider(object): """ :param is_add: :param mask: - :param match_n_vectors: (Default value = 1): + :param match_n_vectors: (Default value = 1) :param table_index: (Default value = 0xFFFFFFFF) :param nbuckets: (Default value = 2) :param memory_size: (Default value = 2097152) diff --git a/test/vpp_pg_interface.py b/test/vpp_pg_interface.py index eeb9c1a5..756f79b5 100644 --- a/test/vpp_pg_interface.py +++ b/test/vpp_pg_interface.py @@ -13,6 +13,7 @@ from util import ppp, ppc from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ismaddr from scapy.utils import inet_pton, inet_ntop + def is_ipv6_misc(p): """ Is packet one of uninteresting IPv6 broadcasts? """ if p.haslayer(ICMPv6ND_RA): @@ -91,10 +92,12 @@ class VppPGInterface(VppInterface): self._capture_cli = "packet-generator capture pg%u pcap %s" % ( self.pg_index, self.out_path) self._cap_name = "pcap%u" % self.sw_if_index - self._input_cli = "packet-generator new pcap %s source pg%u name %s" % ( - self.in_path, self.pg_index, self.cap_name) + self._input_cli = \ + "packet-generator new pcap %s source pg%u name %s" % ( + self.in_path, self.pg_index, self.cap_name) - def rotate_out_file(self): + def enable_capture(self): + """ Enable capture on this packet-generator interface""" try: if os.path.isfile(self.out_path): os.rename(self.out_path, @@ -106,10 +109,6 @@ class VppPGInterface(VppInterface): self._out_file)) except: pass - - def enable_capture(self): - """ Enable capture on this packet-generator interface""" - self.rotate_out_file() # FIXME this should be an API, but no such exists atm self.test.vapi.cli(self.capture_cli) self._pcap_reader = None @@ -151,7 +150,7 @@ class VppPGInterface(VppInterface): before = len(output.res) if filter_out_fn: output.res = [p for p in output.res if not filter_out_fn(p)] - removed = before - len(output.res) + removed = len(output.res) - before if removed: self.test.logger.debug( "Filtered out %s packets from capture (returning %s)" % @@ -181,9 +180,13 @@ class VppPGInterface(VppInterface): based_on = "based on stored packet_infos" if expected_count == 0: raise Exception( - "Internal error, expected packet count for %s is 0!" % name) + "Internal error, expected packet count for %s is 0!" % + name) self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % ( expected_count, based_on, name)) + if expected_count == 0: + raise Exception( + "Internal error, expected packet count for %s is 0!" % name) while remaining_time > 0: before = time.time() capture = self._get_capture(remaining_time, filter_out_fn) @@ -192,8 +195,6 @@ class VppPGInterface(VppInterface): if len(capture.res) == expected_count: # bingo, got the packets we expected return capture - elif expected_count == 0: - return None remaining_time -= elapsed_time if capture: raise Exception("Captured packets mismatch, captured %s packets, " @@ -241,8 +242,9 @@ class VppPGInterface(VppInterface): self.test.logger.debug("Waiting for capture file %s to appear, " "timeout is %ss" % (self.out_path, timeout)) else: - self.test.logger.debug("Capture file %s already exists" % - self.out_path) + self.test.logger.debug( + "Capture file %s already exists" % + self.out_path) return True while time.time() < limit: if os.path.isfile(self.out_path): @@ -275,8 +277,10 @@ class VppPGInterface(VppInterface): self._pcap_reader = PcapReader(self.out_path) break except: - self.test.logger.debug("Exception in scapy.PcapReader(%s): " - "%s" % (self.out_path, format_exc())) + self.test.logger.debug( + "Exception in scapy.PcapReader(%s): " + "%s" % + (self.out_path, format_exc())) if not self._pcap_reader: raise Exception("Capture file %s did not appear within " "timeout" % self.out_path) @@ -290,8 +294,9 @@ class VppPGInterface(VppInterface): "Packet received after %ss was filtered out" % (time.time() - (deadline - timeout))) else: - self.test.logger.debug("Packet received after %fs" % - (time.time() - (deadline - timeout))) + self.test.logger.debug( + "Packet received after %fs" % + (time.time() - (deadline - timeout))) return p time.sleep(0) # yield self.test.logger.debug("Timeout - no packets received") @@ -335,7 +340,6 @@ class VppPGInterface(VppInterface): self.test.logger.info("No ARP received on port %s" % pg_interface.name) return - self.rotate_out_file() arp_reply = captured_packet.copy() # keep original for exception # Make Dot1AD packet content recognizable to scapy if arp_reply.type == 0x88a8: @@ -380,7 +384,8 @@ class VppPGInterface(VppInterface): captured_packet = pg_interface.wait_for_packet( deadline - now, filter_out_fn=None) except: - self.test.logger.error("Timeout while waiting for NDP response") + self.test.logger.error( + "Timeout while waiting for NDP response") raise ndp_reply = captured_packet.copy() # keep original for exception # Make Dot1AD packet content recognizable to scapy @@ -395,13 +400,12 @@ class VppPGInterface(VppInterface): self._local_mac = opt.lladdr self.test.logger.debug(self.test.vapi.cli("show trace")) # we now have the MAC we've been after - self.rotate_out_file() return except: self.test.logger.info( - ppp("Unexpected response to NDP request:", captured_packet)) + ppp("Unexpected response to NDP request:", + captured_packet)) now = time.time() self.test.logger.debug(self.test.vapi.cli("show trace")) - self.rotate_out_file() raise Exception("Timeout while waiting for NDP response") -- cgit 1.2.3-korg