tests: better reporting for unexpected packets
[vpp.git] / test / vpp_pg_interface.py
index 01f10fc..434a300 100644 (file)
@@ -16,7 +16,7 @@ from scapy.layers.l2 import Ether, ARP
 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\
     ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr, ICMPv6ND_RA, RouterAlert, \
     IPv6ExtHdrHopByHop
-from util import ppp, ppc
+from util import ppp, ppc, UnexpectedPacketError
 from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ismaddr
 
 
@@ -287,13 +287,19 @@ class VppPGInterface(VppInterface):
             remaining_time -= elapsed_time
         if capture:
             self.generate_debug_aid("count-mismatch")
+            if len(capture) > 0 and 0 == expected_count:
+                rem = f"\n{remark}" if remark else ""
+                raise UnexpectedPacketError(
+                    capture[0],
+                    f"\n({len(capture)} packets captured in total){rem}")
             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):
+    def assert_nothing_captured(self, timeout=1, remark=None,
+                                filter_out_fn=is_ipv6_misc):
         """ Assert that nothing unfiltered was captured on interface
 
         :param remark: remark printed into debug logs
@@ -303,7 +309,8 @@ class VppPGInterface(VppInterface):
         if os.path.isfile(self.out_path):
             try:
                 capture = self.get_capture(
-                    0, remark=remark, filter_out_fn=filter_out_fn)
+                    0, timeout=timeout, remark=remark,
+                    filter_out_fn=filter_out_fn)
                 if not capture or len(capture.res) == 0:
                     # junk filtered out, we're good
                     return
@@ -311,12 +318,12 @@ class VppPGInterface(VppInterface):
                 pass
             self.generate_debug_aid("empty-assert")
             if remark:
-                raise AssertionError(
-                    "Non-empty capture file present for interface %s (%s)" %
-                    (self.name, remark))
+                raise UnexpectedPacketError(
+                    capture[0],
+                    f" ({len(capture)} packets captured in total) ({remark})")
             else:
-                raise AssertionError("Capture file present for interface %s" %
-                                     self.name)
+                raise UnexpectedPacketError(
+                    capture[0], f" ({len(capture)} packets captured in total)")
 
     def wait_for_pg_stop(self):
         # wait till packet-generator is stopped