make test: improve handling of packet captures
[vpp.git] / test / util.py
index f6c6acd..6658feb 100644 (file)
@@ -15,6 +15,25 @@ 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
+    tail = ""
+    if limit < len(capture):
+        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):
     __metaclass__ = ABCMeta