""" test framework utilities """
-import abc
import ipaddress
+import logging
import socket
from socket import AF_INET6
-import six
-import sys
import os.path
+from copy import deepcopy
import scapy.compat
from scapy.layers.l2 import Ether
from io import BytesIO
from vpp_papi import mac_pton
+# Set up an empty logger for the testcase that can be overridden as necessary
+null_logger = logging.getLogger('VppTestCase.util')
+null_logger.addHandler(logging.NullHandler())
+
def ppp(headline, packet):
""" Return string containing the output of scapy packet.show() call. """
" current core pattern is: %s" % corefmt)
-class NumericConstant(object):
+class NumericConstant:
desc_dict = {}
return ""
-class Host(object):
+class Host:
""" Generic test host "connected" to VPPs interface. """
@property
self._ip6_ll = ip6_ll
-class ForeignAddressFactory(object):
- count = 0
- prefix_len = 24
- net_template = '10.10.10.{}'
- net = net_template.format(0) + '/' + str(prefix_len)
-
- def get_ip4(self):
- if self.count > 255:
- raise Exception("Network host address exhaustion")
- self.count += 1
- return self.net_template.format(self.count)
-
-
class L4_Conn():
""" L4 'connection' tied to two VPP interfaces """
L4_CONN_SIDE_ONE = 1
-class LoggerWrapper(object):
- def __init__(self, logger=None):
- self._logger = logger
-
- def debug(self, *args, **kwargs):
- if self._logger:
- self._logger.debug(*args, **kwargs)
-
- def error(self, *args, **kwargs):
- if self._logger:
- self._logger.error(*args, **kwargs)
-
-
-def fragment_rfc791(packet, fragsize, _logger=None):
+def fragment_rfc791(packet, fragsize, logger=null_logger):
"""
Fragment an IPv4 packet per RFC 791
:param packet: packet to fragment
:note: IP options are not supported
:returns: list of fragments
"""
- logger = LoggerWrapper(_logger)
logger.debug(ppp("Fragmenting packet:", packet))
packet = packet.__class__(scapy.compat.raw(packet)) # recalc. all values
if len(packet[IP].options) > 0:
p[IP].frag = fo + nfb
del p[IP].chksum
- more_fragments = fragment_rfc791(p, fragsize, _logger)
+ more_fragments = fragment_rfc791(p, fragsize, logger)
pkts.extend(more_fragments)
return pkts
-def fragment_rfc8200(packet, identification, fragsize, _logger=None):
+def fragment_rfc8200(packet, identification, fragsize, logger=null_logger):
"""
Fragment an IPv6 packet per RFC 8200
:param packet: packet to fragment
:note: IP options are not supported
:returns: list of fragments
"""
- logger = LoggerWrapper(_logger)
packet = packet.__class__(scapy.compat.raw(packet)) # recalc. all values
if len(packet) <= fragsize:
return [packet]
def reassemble4(listoffragments):
return reassemble4_core(listoffragments, True)
+
+
+def recursive_dict_merge(dict_base, dict_update):
+ """Recursively merge base dict with update dict, return merged dict"""
+ for key in dict_update:
+ if key in dict_base:
+ if type(dict_update[key]) is dict:
+ dict_base[key] = recursive_dict_merge(dict_base[key],
+ dict_update[key])
+ else:
+ dict_base[key] = dict_update[key]
+ else:
+ dict_base[key] = dict_update[key]
+ return dict_base
+
+
+class StatsDiff:
+ """
+ Diff dictionary is a dictionary of dictionaries of interesting stats:
+
+ diff_dictionary =
+ {
+ "err" : { '/error/counter1' : 4, },
+ sw_if_index1 : { '/stat/segment/counter1' : 5,
+ '/stat/segment/counter2' : 6,
+ },
+ sw_if_index2 : { '/stat/segment/counter1' : 7,
+ },
+ }
+
+ It describes a per sw-if-index diffset, where each key is stat segment
+ path and value is the expected change for that counter for sw-if-index.
+ Special case string "err" is used for error counters, which are not per
+ sw-if-index.
+ """
+
+ def __init__(self, stats_diff={}):
+ self.stats_diff = stats_diff
+
+ 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 __or__(self, other):
+ return recursive_dict_merge(deepcopy(self.stats_diff), other)