tests: clean up logging
Tests currently expect the logger to be poked from run_tests.py.
The tests should run without any magic values. This change sets a default
null logger and removes the hasattr checks for the logger.
For reference, see: https://docs.python-guide.org/writing/logging/
Type: test
Change-Id: I98f953d73d12d00e74b59c94a0fb8c7a625b9c44
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
diff --git a/test/util.py b/test/util.py
index c86d602..c7e4693 100644
--- a/test/util.py
+++ b/test/util.py
@@ -2,6 +2,7 @@
import abc
import ipaddress
+import logging
import socket
from socket import AF_INET6
import six
@@ -20,6 +21,10 @@
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. """
@@ -272,20 +277,7 @@
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
@@ -293,7 +285,6 @@
: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:
@@ -325,13 +316,13 @@
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
@@ -339,7 +330,6 @@
: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]