diff options
author | Klement Sekera <klement@graphiant.com> | 2022-02-18 10:35:08 +0000 |
---|---|---|
committer | Klement Sekera <klement.sekera@gmail.com> | 2022-02-24 12:51:33 +0000 |
commit | 26cd0242c95025e0d644db3a80dfe8dee83b6d7a (patch) | |
tree | 40051a3d6efd96a57745f371910116bd9f9f5497 /test/util.py | |
parent | 40cfc1560ee6fa11e4d6c74e9730541a8a45b68a (diff) |
tests: better reporting for unexpected packets
Raise a new UnexpectedPacketErrror, when a packet is captured
unexpectedly. This pretty-prints a terse description of said packet.
Type: improvement
Signed-off-by: Klement Sekera <klement.sekera@gmail.com>
Change-Id: Ibac19fc5bbd82a150fec3c90940a37af6344fd4f
Diffstat (limited to 'test/util.py')
-rw-r--r-- | test/util.py | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/test/util.py b/test/util.py index 653b667eb6c..2c24571c350 100644 --- a/test/util.py +++ b/test/util.py @@ -6,6 +6,7 @@ import socket from socket import AF_INET6 import os.path from copy import deepcopy +from collections import UserDict import scapy.compat from scapy.layers.l2 import Ether @@ -24,8 +25,12 @@ null_logger = logging.getLogger('VppTestCase.util') null_logger.addHandler(logging.NullHandler()) +def pr(packet): + return packet.__repr__() + + def ppp(headline, packet): - """ Return string containing the output of scapy packet.show() call. """ + """ Return string containing headline and output of scapy packet.show() """ return '%s\n%s\n\n%s\n' % (headline, hexdump(packet, dump=True), packet.show(dump=True)) @@ -453,6 +458,15 @@ def reassemble4(listoffragments): return reassemble4_core(listoffragments, True) +class UnexpectedPacketError(Exception): + def __init__(self, packet, msg=""): + self.packet = packet + self.msg = msg + + def __str__(self): + return f"\nUnexpected packet:\n{pr(self.packet)}{self.msg}" + + def recursive_dict_merge(dict_base, dict_update): """Recursively merge base dict with update dict, return merged dict""" for key in dict_update: @@ -467,7 +481,7 @@ def recursive_dict_merge(dict_base, dict_update): return dict_base -class StatsDiff: +class StatsDiff(UserDict): """ Diff dictionary is a dictionary of dictionaries of interesting stats: @@ -487,14 +501,10 @@ class StatsDiff: sw-if-index. """ - def __init__(self, stats_diff={}): - self.stats_diff = stats_diff + __slots__ = () # prevent setting properties to act like a dictionary - def update(self, sw_if_index, key, value): - if sw_if_index in self.stats_diff: - self.stats_diff[sw_if_index][key] = value - else: - self.stats_diff[sw_if_index] = {key: value} + def __init__(self, data): + super().__init__(data) def __or__(self, other): - return recursive_dict_merge(deepcopy(self.stats_diff), other) + return recursive_dict_merge(deepcopy(self.data), other) |