2016-10-03 19:44:57 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2017-02-14 02:55:31 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
import gc
|
Revert "VPP-1508 python3 tests: raw_input"
This reverts commit 72f0004ac5f6e201cbe042593d76df6f2491d743.
Reason for revert: Traceback (most recent call last):
File "/home/ksekera/vpp/test/test_ipsec_ah.py", line 36, in
setUpClass
super(TemplateIpsecAh, cls).setUpClass()
File "/home/ksekera/vpp/test/template_ipsec.py", line 105, in
setUpClass
super(TemplateIpsec, cls).setUpClass()
File "/home/ksekera/vpp/test/framework.py", line 459, in setUpClass
cls.quit()
File "/home/ksekera/vpp/test/framework.py", line 475, in quit
six.input("When done debugging, press ENTER to kill the "
AttributeError: 'module' object has no attribute 'input'
Change-Id: Idf0bbfea231730b37bae5dcb4557a0f82ab1b810
Signed-off-by: Ole Troan <ot@cisco.com>
2018-11-26 10:27:50 +00:00
|
|
|
import sys
|
2017-02-14 02:55:31 +01:00
|
|
|
import os
|
|
|
|
import select
|
Revert "VPP-1508 python3 tests: raw_input"
This reverts commit 72f0004ac5f6e201cbe042593d76df6f2491d743.
Reason for revert: Traceback (most recent call last):
File "/home/ksekera/vpp/test/test_ipsec_ah.py", line 36, in
setUpClass
super(TemplateIpsecAh, cls).setUpClass()
File "/home/ksekera/vpp/test/template_ipsec.py", line 105, in
setUpClass
super(TemplateIpsec, cls).setUpClass()
File "/home/ksekera/vpp/test/framework.py", line 459, in setUpClass
cls.quit()
File "/home/ksekera/vpp/test/framework.py", line 475, in quit
six.input("When done debugging, press ENTER to kill the "
AttributeError: 'module' object has no attribute 'input'
Change-Id: Idf0bbfea231730b37bae5dcb4557a0f82ab1b810
Signed-off-by: Ole Troan <ot@cisco.com>
2018-11-26 10:27:50 +00:00
|
|
|
import unittest
|
2016-10-11 11:47:09 +02:00
|
|
|
import tempfile
|
2016-10-28 13:20:27 +02:00
|
|
|
import time
|
Revert "VPP-1508 python3 tests: raw_input"
This reverts commit 72f0004ac5f6e201cbe042593d76df6f2491d743.
Reason for revert: Traceback (most recent call last):
File "/home/ksekera/vpp/test/test_ipsec_ah.py", line 36, in
setUpClass
super(TemplateIpsecAh, cls).setUpClass()
File "/home/ksekera/vpp/test/template_ipsec.py", line 105, in
setUpClass
super(TemplateIpsec, cls).setUpClass()
File "/home/ksekera/vpp/test/framework.py", line 459, in setUpClass
cls.quit()
File "/home/ksekera/vpp/test/framework.py", line 475, in quit
six.input("When done debugging, press ENTER to kill the "
AttributeError: 'module' object has no attribute 'input'
Change-Id: Idf0bbfea231730b37bae5dcb4557a0f82ab1b810
Signed-off-by: Ole Troan <ot@cisco.com>
2018-11-26 10:27:50 +00:00
|
|
|
import faulthandler
|
|
|
|
import random
|
|
|
|
import copy
|
|
|
|
import psutil
|
2018-11-29 09:37:08 +01:00
|
|
|
import platform
|
2016-12-08 10:16:41 +01:00
|
|
|
from collections import deque
|
2018-11-25 11:05:13 -08:00
|
|
|
from threading import Thread, Event
|
Revert "VPP-1508 python3 tests: raw_input"
This reverts commit 72f0004ac5f6e201cbe042593d76df6f2491d743.
Reason for revert: Traceback (most recent call last):
File "/home/ksekera/vpp/test/test_ipsec_ah.py", line 36, in
setUpClass
super(TemplateIpsecAh, cls).setUpClass()
File "/home/ksekera/vpp/test/template_ipsec.py", line 105, in
setUpClass
super(TemplateIpsec, cls).setUpClass()
File "/home/ksekera/vpp/test/framework.py", line 459, in setUpClass
cls.quit()
File "/home/ksekera/vpp/test/framework.py", line 475, in quit
six.input("When done debugging, press ENTER to kill the "
AttributeError: 'module' object has no attribute 'input'
Change-Id: Idf0bbfea231730b37bae5dcb4557a0f82ab1b810
Signed-off-by: Ole Troan <ot@cisco.com>
2018-11-26 10:27:50 +00:00
|
|
|
from inspect import getdoc, isclass
|
2018-11-25 11:05:13 -08:00
|
|
|
from traceback import format_exception
|
Revert "VPP-1508 python3 tests: raw_input"
This reverts commit 72f0004ac5f6e201cbe042593d76df6f2491d743.
Reason for revert: Traceback (most recent call last):
File "/home/ksekera/vpp/test/test_ipsec_ah.py", line 36, in
setUpClass
super(TemplateIpsecAh, cls).setUpClass()
File "/home/ksekera/vpp/test/template_ipsec.py", line 105, in
setUpClass
super(TemplateIpsec, cls).setUpClass()
File "/home/ksekera/vpp/test/framework.py", line 459, in setUpClass
cls.quit()
File "/home/ksekera/vpp/test/framework.py", line 475, in quit
six.input("When done debugging, press ENTER to kill the "
AttributeError: 'module' object has no attribute 'input'
Change-Id: Idf0bbfea231730b37bae5dcb4557a0f82ab1b810
Signed-off-by: Ole Troan <ot@cisco.com>
2018-11-26 10:27:50 +00:00
|
|
|
from logging import FileHandler, DEBUG, Formatter
|
2019-03-10 10:04:23 -07:00
|
|
|
|
|
|
|
import scapy.compat
|
2017-02-14 02:55:31 +01:00
|
|
|
from scapy.packet import Raw
|
2018-11-25 11:05:13 -08:00
|
|
|
from hook import StepHook, PollHook, VppDiedError
|
2018-12-17 21:43:43 -08:00
|
|
|
from vpp_pg_interface import VppPGInterface
|
2018-12-21 16:04:22 +01:00
|
|
|
from vpp_sub_interface import VppSubInterface
|
Revert "VPP-1508 python3 tests: raw_input"
This reverts commit 72f0004ac5f6e201cbe042593d76df6f2491d743.
Reason for revert: Traceback (most recent call last):
File "/home/ksekera/vpp/test/test_ipsec_ah.py", line 36, in
setUpClass
super(TemplateIpsecAh, cls).setUpClass()
File "/home/ksekera/vpp/test/template_ipsec.py", line 105, in
setUpClass
super(TemplateIpsec, cls).setUpClass()
File "/home/ksekera/vpp/test/framework.py", line 459, in setUpClass
cls.quit()
File "/home/ksekera/vpp/test/framework.py", line 475, in quit
six.input("When done debugging, press ENTER to kill the "
AttributeError: 'module' object has no attribute 'input'
Change-Id: Idf0bbfea231730b37bae5dcb4557a0f82ab1b810
Signed-off-by: Ole Troan <ot@cisco.com>
2018-11-26 10:27:50 +00:00
|
|
|
from vpp_lo_interface import VppLoInterface
|
|
|
|
from vpp_papi_provider import VppPapiProvider
|
|
|
|
from vpp_papi.vpp_stats import VPPStats
|
2018-03-14 18:14:49 +01:00
|
|
|
from log import RED, GREEN, YELLOW, double_line_delim, single_line_delim, \
|
2018-11-09 11:58:54 +01:00
|
|
|
get_logger, colorize
|
2018-11-25 11:05:13 -08:00
|
|
|
from vpp_object import VppObjectRegistry
|
Revert "VPP-1508 python3 tests: raw_input"
This reverts commit 72f0004ac5f6e201cbe042593d76df6f2491d743.
Reason for revert: Traceback (most recent call last):
File "/home/ksekera/vpp/test/test_ipsec_ah.py", line 36, in
setUpClass
super(TemplateIpsecAh, cls).setUpClass()
File "/home/ksekera/vpp/test/template_ipsec.py", line 105, in
setUpClass
super(TemplateIpsec, cls).setUpClass()
File "/home/ksekera/vpp/test/framework.py", line 459, in setUpClass
cls.quit()
File "/home/ksekera/vpp/test/framework.py", line 475, in quit
six.input("When done debugging, press ENTER to kill the "
AttributeError: 'module' object has no attribute 'input'
Change-Id: Idf0bbfea231730b37bae5dcb4557a0f82ab1b810
Signed-off-by: Ole Troan <ot@cisco.com>
2018-11-26 10:27:50 +00:00
|
|
|
from util import ppp, is_core_present
|
|
|
|
from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror
|
|
|
|
from scapy.layers.inet6 import ICMPv6DestUnreach, ICMPv6EchoRequest
|
|
|
|
from scapy.layers.inet6 import ICMPv6EchoReply
|
2018-12-09 15:37:04 -08:00
|
|
|
|
2017-02-14 02:55:31 +01:00
|
|
|
if os.name == 'posix' and sys.version_info[0] < 3:
|
|
|
|
# using subprocess32 is recommended by python official documentation
|
|
|
|
# @ https://docs.python.org/2/library/subprocess.html
|
|
|
|
import subprocess32 as subprocess
|
|
|
|
else:
|
|
|
|
import subprocess
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2018-12-15 10:16:35 -08:00
|
|
|
# Python2/3 compatible
|
|
|
|
try:
|
|
|
|
input = raw_input
|
|
|
|
except NameError:
|
|
|
|
pass
|
|
|
|
|
2018-09-19 15:01:47 +02:00
|
|
|
PASS = 0
|
|
|
|
FAIL = 1
|
|
|
|
ERROR = 2
|
|
|
|
SKIP = 3
|
|
|
|
TEST_RUN = 4
|
|
|
|
|
2018-02-17 13:41:33 +01:00
|
|
|
debug_framework = False
|
|
|
|
if os.getenv('TEST_DEBUG', "0") == "1":
|
|
|
|
debug_framework = True
|
|
|
|
import debug_internal
|
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Test framework module.
|
|
|
|
|
|
|
|
The module provides a set of tools for constructing and running tests and
|
|
|
|
representing the results.
|
|
|
|
"""
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
class _PacketInfo(object):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""Private class to create packet info object.
|
|
|
|
|
|
|
|
Help process information about the next packet.
|
|
|
|
Set variables to default values.
|
|
|
|
"""
|
2016-11-11 11:38:55 +01:00
|
|
|
#: Store the index of the packet.
|
2016-10-03 19:44:57 +02:00
|
|
|
index = -1
|
2016-11-11 11:38:55 +01:00
|
|
|
#: Store the index of the source packet generator interface of the packet.
|
2016-10-03 19:44:57 +02:00
|
|
|
src = -1
|
2016-11-11 11:38:55 +01:00
|
|
|
#: Store the index of the destination packet generator interface
|
|
|
|
#: of the packet.
|
2016-10-03 19:44:57 +02:00
|
|
|
dst = -1
|
2017-03-02 15:22:47 +01:00
|
|
|
#: Store expected ip version
|
|
|
|
ip = -1
|
|
|
|
#: Store expected upper protocol
|
|
|
|
proto = -1
|
2016-11-11 11:38:55 +01:00
|
|
|
#: Store the copy of the former packet.
|
2016-10-03 19:44:57 +02:00
|
|
|
data = None
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-12-07 15:09:13 +01:00
|
|
|
def __eq__(self, other):
|
|
|
|
index = self.index == other.index
|
|
|
|
src = self.src == other.src
|
|
|
|
dst = self.dst == other.dst
|
|
|
|
data = self.data == other.data
|
|
|
|
return index and src and dst and data
|
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2017-02-14 02:55:31 +01:00
|
|
|
def pump_output(testclass):
|
|
|
|
""" pump output from vpp stdout/stderr to proper queues """
|
2017-11-09 09:16:39 +01:00
|
|
|
stdout_fragment = ""
|
|
|
|
stderr_fragment = ""
|
2018-07-23 05:35:56 -04:00
|
|
|
while not testclass.pump_thread_stop_flag.is_set():
|
2017-02-14 02:55:31 +01:00
|
|
|
readable = select.select([testclass.vpp.stdout.fileno(),
|
|
|
|
testclass.vpp.stderr.fileno(),
|
|
|
|
testclass.pump_thread_wakeup_pipe[0]],
|
|
|
|
[], [])[0]
|
|
|
|
if testclass.vpp.stdout.fileno() in readable:
|
2017-11-09 09:16:39 +01:00
|
|
|
read = os.read(testclass.vpp.stdout.fileno(), 102400)
|
|
|
|
if len(read) > 0:
|
|
|
|
split = read.splitlines(True)
|
|
|
|
if len(stdout_fragment) > 0:
|
|
|
|
split[0] = "%s%s" % (stdout_fragment, split[0])
|
|
|
|
if len(split) > 0 and split[-1].endswith("\n"):
|
|
|
|
limit = None
|
|
|
|
else:
|
|
|
|
limit = -1
|
|
|
|
stdout_fragment = split[-1]
|
|
|
|
testclass.vpp_stdout_deque.extend(split[:limit])
|
|
|
|
if not testclass.cache_vpp_output:
|
|
|
|
for line in split[:limit]:
|
|
|
|
testclass.logger.debug(
|
|
|
|
"VPP STDOUT: %s" % line.rstrip("\n"))
|
2017-02-14 02:55:31 +01:00
|
|
|
if testclass.vpp.stderr.fileno() in readable:
|
2017-11-09 09:16:39 +01:00
|
|
|
read = os.read(testclass.vpp.stderr.fileno(), 102400)
|
|
|
|
if len(read) > 0:
|
|
|
|
split = read.splitlines(True)
|
|
|
|
if len(stderr_fragment) > 0:
|
|
|
|
split[0] = "%s%s" % (stderr_fragment, split[0])
|
2018-12-06 17:35:12 +01:00
|
|
|
if len(split) > 0 and split[-1].endswith(b"\n"):
|
2017-11-09 09:16:39 +01:00
|
|
|
limit = None
|
|
|
|
else:
|
|
|
|
limit = -1
|
|
|
|
stderr_fragment = split[-1]
|
|
|
|
testclass.vpp_stderr_deque.extend(split[:limit])
|
|
|
|
if not testclass.cache_vpp_output:
|
|
|
|
for line in split[:limit]:
|
|
|
|
testclass.logger.debug(
|
|
|
|
"VPP STDERR: %s" % line.rstrip("\n"))
|
2018-12-09 15:37:04 -08:00
|
|
|
# ignoring the dummy pipe here intentionally - the
|
|
|
|
# flag will take care of properly terminating the loop
|
2016-10-28 13:20:27 +02:00
|
|
|
|
|
|
|
|
2018-12-06 07:46:13 -08:00
|
|
|
def _is_skip_aarch64_set():
|
2018-11-29 09:37:08 +01:00
|
|
|
return os.getenv('SKIP_AARCH64', 'n').lower() in ('yes', 'y', '1')
|
|
|
|
|
2018-12-06 07:46:13 -08:00
|
|
|
is_skip_aarch64_set = _is_skip_aarch64_set()
|
2018-11-29 09:37:08 +01:00
|
|
|
|
2018-12-06 07:46:13 -08:00
|
|
|
|
|
|
|
def _is_platform_aarch64():
|
2018-11-29 09:37:08 +01:00
|
|
|
return platform.machine() == 'aarch64'
|
|
|
|
|
2018-12-06 07:46:13 -08:00
|
|
|
is_platform_aarch64 = _is_platform_aarch64()
|
|
|
|
|
2018-11-29 09:37:08 +01:00
|
|
|
|
2018-12-06 07:46:13 -08:00
|
|
|
def _running_extended_tests():
|
2018-03-21 12:35:51 +01:00
|
|
|
s = os.getenv("EXTENDED_TESTS", "n")
|
|
|
|
return True if s.lower() in ("y", "yes", "1") else False
|
2017-03-07 11:39:27 +01:00
|
|
|
|
2018-12-06 07:46:13 -08:00
|
|
|
running_extended_tests = _running_extended_tests()
|
2017-03-07 11:39:27 +01:00
|
|
|
|
2018-12-06 07:46:13 -08:00
|
|
|
|
|
|
|
def _running_on_centos():
|
2018-03-21 12:35:51 +01:00
|
|
|
os_id = os.getenv("OS_ID", "")
|
|
|
|
return True if "centos" in os_id.lower() else False
|
2017-09-29 12:36:37 +02:00
|
|
|
|
2018-12-06 07:46:13 -08:00
|
|
|
running_on_centos = _running_on_centos
|
|
|
|
|
2017-09-29 12:36:37 +02:00
|
|
|
|
2017-08-08 04:33:53 +02:00
|
|
|
class KeepAliveReporter(object):
|
|
|
|
"""
|
|
|
|
Singleton object which reports test start to parent process
|
|
|
|
"""
|
|
|
|
_shared_state = {}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.__dict__ = self._shared_state
|
VPP-1498: test/framework.py: AttributeError
Traceback if .send_keep_alive is called before pipe is configured.
Traceback (most recent call last):
File "/vpp/test/test_span.py", line 27, in setUpClass
super(TestSpan, cls).setUpClass()
File "/vpp/test/framework.py", line 411, in setUpClass
cls.reporter.send_keep_alive(cls, 'setUpClass')
File "/vpp/test/framework.py", line 172, in send_keep_alive
if self.pipe is None:
File "/vpp/test/framework.py", line 160, in pipe
return self._pipe
AttributeError: 'KeepAliveReporter' object has no attribute '_pipe'
Change-Id: I561d2748441702478a84fbb4580a4d2667d70ffd
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
2018-11-18 08:17:34 -08:00
|
|
|
self._pipe = None
|
2017-08-08 04:33:53 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def pipe(self):
|
|
|
|
return self._pipe
|
|
|
|
|
|
|
|
@pipe.setter
|
|
|
|
def pipe(self, pipe):
|
VPP-1498: test/framework.py: AttributeError
Traceback if .send_keep_alive is called before pipe is configured.
Traceback (most recent call last):
File "/vpp/test/test_span.py", line 27, in setUpClass
super(TestSpan, cls).setUpClass()
File "/vpp/test/framework.py", line 411, in setUpClass
cls.reporter.send_keep_alive(cls, 'setUpClass')
File "/vpp/test/framework.py", line 172, in send_keep_alive
if self.pipe is None:
File "/vpp/test/framework.py", line 160, in pipe
return self._pipe
AttributeError: 'KeepAliveReporter' object has no attribute '_pipe'
Change-Id: I561d2748441702478a84fbb4580a4d2667d70ffd
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
2018-11-18 08:17:34 -08:00
|
|
|
if self._pipe is not None:
|
2017-08-08 04:33:53 +02:00
|
|
|
raise Exception("Internal error - pipe should only be set once.")
|
|
|
|
self._pipe = pipe
|
|
|
|
|
2018-09-21 13:55:16 +02:00
|
|
|
def send_keep_alive(self, test, desc=None):
|
2017-08-08 04:33:53 +02:00
|
|
|
"""
|
|
|
|
Write current test tmpdir & desc to keep-alive pipe to signal liveness
|
|
|
|
"""
|
2017-08-11 06:56:05 +02:00
|
|
|
if self.pipe is None:
|
|
|
|
# if not running forked..
|
|
|
|
return
|
|
|
|
|
2017-08-08 04:33:53 +02:00
|
|
|
if isclass(test):
|
2018-09-21 13:55:16 +02:00
|
|
|
desc = '%s (%s)' % (desc, unittest.util.strclass(test))
|
2017-08-08 04:33:53 +02:00
|
|
|
else:
|
2018-09-21 13:55:16 +02:00
|
|
|
desc = test.id()
|
2017-08-08 04:33:53 +02:00
|
|
|
|
2017-09-30 22:04:21 -04:00
|
|
|
self.pipe.send((desc, test.vpp_bin, test.tempdir, test.vpp.pid))
|
2017-08-08 04:33:53 +02:00
|
|
|
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
class VppTestCase(unittest.TestCase):
|
2016-11-11 11:38:55 +01:00
|
|
|
"""This subclass is a base class for VPP test cases that are implemented as
|
|
|
|
classes. It provides methods to create and run test case.
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
|
2018-12-21 16:04:22 +01:00
|
|
|
extra_vpp_punt_config = []
|
|
|
|
extra_vpp_plugin_config = []
|
2018-11-28 07:42:11 +01:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
@property
|
|
|
|
def packet_infos(self):
|
|
|
|
"""List of packet infos"""
|
|
|
|
return self._packet_infos
|
|
|
|
|
2016-12-21 08:50:14 +01:00
|
|
|
@classmethod
|
|
|
|
def get_packet_count_for_if_idx(cls, dst_if_index):
|
|
|
|
"""Get the number of packet info for specified destination if index"""
|
|
|
|
if dst_if_index in cls._packet_count_for_dst_if_idx:
|
|
|
|
return cls._packet_count_for_dst_if_idx[dst_if_index]
|
|
|
|
else:
|
|
|
|
return 0
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def instance(cls):
|
|
|
|
"""Return the instance of this testcase"""
|
|
|
|
return cls.test_instance
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2016-10-28 13:20:27 +02:00
|
|
|
@classmethod
|
|
|
|
def set_debug_flags(cls, d):
|
|
|
|
cls.debug_core = False
|
|
|
|
cls.debug_gdb = False
|
|
|
|
cls.debug_gdbserver = False
|
|
|
|
if d is None:
|
|
|
|
return
|
|
|
|
dl = d.lower()
|
|
|
|
if dl == "core":
|
|
|
|
cls.debug_core = True
|
|
|
|
elif dl == "gdb":
|
|
|
|
cls.debug_gdb = True
|
|
|
|
elif dl == "gdbserver":
|
|
|
|
cls.debug_gdbserver = True
|
|
|
|
else:
|
|
|
|
raise Exception("Unrecognized DEBUG option: '%s'" % d)
|
|
|
|
|
2018-11-21 09:28:32 -08:00
|
|
|
@staticmethod
|
|
|
|
def get_least_used_cpu():
|
2018-07-16 14:22:01 +02:00
|
|
|
cpu_usage_list = [set(range(psutil.cpu_count()))]
|
|
|
|
vpp_processes = [p for p in psutil.process_iter(attrs=['pid', 'name'])
|
|
|
|
if 'vpp_main' == p.info['name']]
|
|
|
|
for vpp_process in vpp_processes:
|
|
|
|
for cpu_usage_set in cpu_usage_list:
|
|
|
|
try:
|
|
|
|
cpu_num = vpp_process.cpu_num()
|
|
|
|
if cpu_num in cpu_usage_set:
|
|
|
|
cpu_usage_set_index = cpu_usage_list.index(
|
|
|
|
cpu_usage_set)
|
|
|
|
if cpu_usage_set_index == len(cpu_usage_list) - 1:
|
|
|
|
cpu_usage_list.append({cpu_num})
|
|
|
|
else:
|
|
|
|
cpu_usage_list[cpu_usage_set_index + 1].add(
|
|
|
|
cpu_num)
|
|
|
|
cpu_usage_set.remove(cpu_num)
|
|
|
|
break
|
|
|
|
except psutil.NoSuchProcess:
|
|
|
|
pass
|
|
|
|
|
|
|
|
for cpu_usage_set in cpu_usage_list:
|
|
|
|
if len(cpu_usage_set) > 0:
|
|
|
|
min_usage_set = cpu_usage_set
|
|
|
|
break
|
|
|
|
|
|
|
|
return random.choice(tuple(min_usage_set))
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpConstants(cls):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Set-up the test case class based on environment variables """
|
2018-03-21 12:35:51 +01:00
|
|
|
s = os.getenv("STEP", "n")
|
|
|
|
cls.step = True if s.lower() in ("y", "yes", "1") else False
|
|
|
|
d = os.getenv("DEBUG", None)
|
|
|
|
c = os.getenv("CACHE_OUTPUT", "1")
|
|
|
|
cls.cache_vpp_output = False if c.lower() in ("n", "no", "0") else True
|
2016-10-28 13:20:27 +02:00
|
|
|
cls.set_debug_flags(d)
|
2018-11-08 11:21:39 +01:00
|
|
|
cls.vpp_bin = os.getenv('VPP_BIN', "vpp")
|
|
|
|
cls.plugin_path = os.getenv('VPP_PLUGIN_PATH')
|
2017-03-21 08:21:25 +01:00
|
|
|
cls.extern_plugin_path = os.getenv('EXTERN_PLUGINS')
|
|
|
|
plugin_path = None
|
|
|
|
if cls.plugin_path is not None:
|
|
|
|
if cls.extern_plugin_path is not None:
|
|
|
|
plugin_path = "%s:%s" % (
|
|
|
|
cls.plugin_path, cls.extern_plugin_path)
|
2017-03-24 05:47:15 +01:00
|
|
|
else:
|
|
|
|
plugin_path = cls.plugin_path
|
2017-03-21 08:21:25 +01:00
|
|
|
elif cls.extern_plugin_path is not None:
|
|
|
|
plugin_path = cls.extern_plugin_path
|
2018-12-21 16:04:22 +01:00
|
|
|
debug_cli = ""
|
2016-11-02 09:25:05 +01:00
|
|
|
if cls.step or cls.debug_gdb or cls.debug_gdbserver:
|
2018-12-21 16:04:22 +01:00
|
|
|
debug_cli = "cli-listen localhost:5002"
|
2017-03-02 11:27:11 +01:00
|
|
|
coredump_size = None
|
2018-03-21 12:35:51 +01:00
|
|
|
size = os.getenv("COREDUMP_SIZE")
|
2018-12-21 16:04:22 +01:00
|
|
|
if size is not None:
|
|
|
|
coredump_size = "coredump-size %s" % size
|
|
|
|
if coredump_size is None:
|
|
|
|
coredump_size = "coredump-size unlimited"
|
|
|
|
|
|
|
|
cpu_core_number = cls.get_least_used_cpu()
|
|
|
|
|
|
|
|
cls.vpp_cmdline = [cls.vpp_bin, "unix",
|
|
|
|
"{", "nodaemon", debug_cli, "full-coredump",
|
|
|
|
coredump_size, "runtime-dir", cls.tempdir, "}",
|
|
|
|
"api-trace", "{", "on", "}", "api-segment", "{",
|
|
|
|
"prefix", cls.shm_prefix, "}", "cpu", "{",
|
|
|
|
"main-core", str(cpu_core_number), "}", "statseg",
|
|
|
|
"{", "socket-name", cls.stats_sock, "}", "plugins",
|
|
|
|
"{", "plugin", "dpdk_plugin.so", "{", "disable",
|
|
|
|
"}", "plugin", "unittest_plugin.so", "{", "enable",
|
|
|
|
"}"] + cls.extra_vpp_plugin_config + ["}", ]
|
|
|
|
if cls.extra_vpp_punt_config is not None:
|
|
|
|
cls.vpp_cmdline.extend(cls.extra_vpp_punt_config)
|
2017-03-21 08:21:25 +01:00
|
|
|
if plugin_path is not None:
|
2018-12-21 16:04:22 +01:00
|
|
|
cls.vpp_cmdline.extend(["plugin_path", plugin_path])
|
2018-11-08 11:24:34 +01:00
|
|
|
cls.logger.info("vpp_cmdline args: %s" % cls.vpp_cmdline)
|
|
|
|
cls.logger.info("vpp_cmdline: %s" % " ".join(cls.vpp_cmdline))
|
2016-10-28 13:20:27 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def wait_for_enter(cls):
|
|
|
|
if cls.debug_gdbserver:
|
|
|
|
print(double_line_delim)
|
|
|
|
print("Spawned GDB server with PID: %d" % cls.vpp.pid)
|
|
|
|
elif cls.debug_gdb:
|
|
|
|
print(double_line_delim)
|
|
|
|
print("Spawned VPP with PID: %d" % cls.vpp.pid)
|
|
|
|
else:
|
|
|
|
cls.logger.debug("Spawned VPP with PID: %d" % cls.vpp.pid)
|
|
|
|
return
|
|
|
|
print(single_line_delim)
|
|
|
|
print("You can debug the VPP using e.g.:")
|
|
|
|
if cls.debug_gdbserver:
|
|
|
|
print("gdb " + cls.vpp_bin + " -ex 'target remote localhost:7777'")
|
|
|
|
print("Now is the time to attach a gdb by running the above "
|
|
|
|
"command, set up breakpoints etc. and then resume VPP from "
|
|
|
|
"within gdb by issuing the 'continue' command")
|
|
|
|
elif cls.debug_gdb:
|
|
|
|
print("gdb " + cls.vpp_bin + " -ex 'attach %s'" % cls.vpp.pid)
|
|
|
|
print("Now is the time to attach a gdb by running the above "
|
|
|
|
"command and set up breakpoints etc.")
|
|
|
|
print(single_line_delim)
|
2018-12-15 10:16:35 -08:00
|
|
|
input("Press ENTER to continue running the testcase...")
|
2016-10-28 13:20:27 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run_vpp(cls):
|
|
|
|
cmdline = cls.vpp_cmdline
|
|
|
|
|
|
|
|
if cls.debug_gdbserver:
|
2016-11-03 05:36:01 +01:00
|
|
|
gdbserver = '/usr/bin/gdbserver'
|
|
|
|
if not os.path.isfile(gdbserver) or \
|
|
|
|
not os.access(gdbserver, os.X_OK):
|
|
|
|
raise Exception("gdbserver binary '%s' does not exist or is "
|
|
|
|
"not executable" % gdbserver)
|
|
|
|
|
|
|
|
cmdline = [gdbserver, 'localhost:7777'] + cls.vpp_cmdline
|
2016-10-28 13:20:27 +02:00
|
|
|
cls.logger.info("Gdbserver cmdline is %s", " ".join(cmdline))
|
|
|
|
|
2016-11-03 05:36:01 +01:00
|
|
|
try:
|
|
|
|
cls.vpp = subprocess.Popen(cmdline,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
bufsize=1)
|
2018-11-24 21:19:38 -08:00
|
|
|
except subprocess.CalledProcessError as e:
|
2018-11-28 11:34:21 -08:00
|
|
|
cls.logger.critical("Subprocess returned with non-0 return code: ("
|
|
|
|
"%s)", e.returncode)
|
|
|
|
raise
|
|
|
|
except OSError as e:
|
|
|
|
cls.logger.critical("Subprocess returned with OS error: "
|
|
|
|
"(%s) %s", e.errno, e.strerror)
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
cls.logger.exception("Subprocess returned unexpected from "
|
|
|
|
"%s:", cmdline)
|
2016-11-03 05:36:01 +01:00
|
|
|
raise
|
|
|
|
|
2016-10-28 13:20:27 +02:00
|
|
|
cls.wait_for_enter()
|
2016-10-07 16:30:03 +01:00
|
|
|
|
2018-09-26 11:19:00 +02:00
|
|
|
@classmethod
|
|
|
|
def wait_for_stats_socket(cls):
|
|
|
|
deadline = time.time() + 3
|
2018-11-15 08:13:03 -08:00
|
|
|
ok = False
|
|
|
|
while time.time() < deadline or \
|
|
|
|
cls.debug_gdb or cls.debug_gdbserver:
|
2018-09-26 11:19:00 +02:00
|
|
|
if os.path.exists(cls.stats_sock):
|
2018-11-15 08:13:03 -08:00
|
|
|
ok = True
|
2018-09-26 11:19:00 +02:00
|
|
|
break
|
2019-03-10 09:10:54 -07:00
|
|
|
cls.sleep(0.8)
|
2018-11-15 08:13:03 -08:00
|
|
|
if not ok:
|
|
|
|
cls.logger.critical("Couldn't stat : {}".format(cls.stats_sock))
|
2018-09-26 11:19:00 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Perform class setup before running the testcase
|
|
|
|
Remove shared memory files, start vpp and connect the vpp-api
|
|
|
|
"""
|
2019-01-25 14:05:48 -08:00
|
|
|
super(VppTestCase, cls).setUpClass()
|
2017-02-14 02:55:31 +01:00
|
|
|
gc.collect() # run garbage collection first
|
2018-02-02 11:27:53 +01:00
|
|
|
random.seed()
|
2018-11-09 11:58:54 +01:00
|
|
|
cls.logger = get_logger(cls.__name__)
|
|
|
|
if hasattr(cls, 'parallel_handler'):
|
|
|
|
cls.logger.addHandler(cls.parallel_handler)
|
2018-11-21 13:13:39 +01:00
|
|
|
cls.logger.propagate = False
|
2016-10-11 11:47:09 +02:00
|
|
|
cls.tempdir = tempfile.mkdtemp(
|
2017-08-15 07:09:02 +02:00
|
|
|
prefix='vpp-unittest-%s-' % cls.__name__)
|
2018-09-26 11:19:00 +02:00
|
|
|
cls.stats_sock = "%s/stats.sock" % cls.tempdir
|
2017-04-11 06:01:53 +02:00
|
|
|
cls.file_handler = FileHandler("%s/log.txt" % cls.tempdir)
|
|
|
|
cls.file_handler.setFormatter(
|
2017-02-14 02:55:31 +01:00
|
|
|
Formatter(fmt='%(asctime)s,%(msecs)03d %(message)s',
|
|
|
|
datefmt="%H:%M:%S"))
|
2017-04-11 06:01:53 +02:00
|
|
|
cls.file_handler.setLevel(DEBUG)
|
|
|
|
cls.logger.addHandler(cls.file_handler)
|
2018-07-16 14:22:01 +02:00
|
|
|
cls.shm_prefix = os.path.basename(cls.tempdir)
|
2016-10-11 11:47:09 +02:00
|
|
|
os.chdir(cls.tempdir)
|
2016-10-28 13:20:27 +02:00
|
|
|
cls.logger.info("Temporary dir is %s, shm prefix is %s",
|
|
|
|
cls.tempdir, cls.shm_prefix)
|
2016-10-03 19:44:57 +02:00
|
|
|
cls.setUpConstants()
|
2016-12-21 08:50:14 +01:00
|
|
|
cls.reset_packet_infos()
|
2016-12-12 08:36:58 +01:00
|
|
|
cls._captures = []
|
|
|
|
cls._zombie_captures = []
|
2016-10-11 11:47:09 +02:00
|
|
|
cls.verbose = 0
|
2016-11-24 01:59:16 +01:00
|
|
|
cls.vpp_dead = False
|
2017-01-11 08:16:53 +01:00
|
|
|
cls.registry = VppObjectRegistry()
|
2017-04-10 06:30:17 +02:00
|
|
|
cls.vpp_startup_failed = False
|
2017-08-08 04:33:53 +02:00
|
|
|
cls.reporter = KeepAliveReporter()
|
2016-10-11 11:47:09 +02:00
|
|
|
# need to catch exceptions here because if we raise, then the cleanup
|
|
|
|
# doesn't get called and we might end with a zombie vpp
|
|
|
|
try:
|
2016-10-28 13:20:27 +02:00
|
|
|
cls.run_vpp()
|
2018-09-21 13:55:16 +02:00
|
|
|
cls.reporter.send_keep_alive(cls, 'setUpClass')
|
|
|
|
VppTestResult.current_test_case_info = TestCaseInfo(
|
|
|
|
cls.logger, cls.tempdir, cls.vpp.pid, cls.vpp_bin)
|
2016-12-08 10:16:41 +01:00
|
|
|
cls.vpp_stdout_deque = deque()
|
|
|
|
cls.vpp_stderr_deque = deque()
|
2017-02-14 02:55:31 +01:00
|
|
|
cls.pump_thread_stop_flag = Event()
|
|
|
|
cls.pump_thread_wakeup_pipe = os.pipe()
|
|
|
|
cls.pump_thread = Thread(target=pump_output, args=(cls,))
|
2017-02-14 07:11:52 +01:00
|
|
|
cls.pump_thread.daemon = True
|
2017-02-14 02:55:31 +01:00
|
|
|
cls.pump_thread.start()
|
2018-09-26 11:19:00 +02:00
|
|
|
if cls.debug_gdb or cls.debug_gdbserver:
|
|
|
|
read_timeout = 0
|
|
|
|
else:
|
|
|
|
read_timeout = 5
|
|
|
|
cls.vapi = VppPapiProvider(cls.shm_prefix, cls.shm_prefix, cls,
|
|
|
|
read_timeout)
|
2016-10-28 13:20:27 +02:00
|
|
|
if cls.step:
|
2016-11-24 01:59:16 +01:00
|
|
|
hook = StepHook(cls)
|
2016-10-28 13:20:27 +02:00
|
|
|
else:
|
2016-11-24 01:59:16 +01:00
|
|
|
hook = PollHook(cls)
|
|
|
|
cls.vapi.register_hook(hook)
|
2018-09-26 11:19:00 +02:00
|
|
|
cls.wait_for_stats_socket()
|
|
|
|
cls.statistics = VPPStats(socketname=cls.stats_sock)
|
2017-04-10 06:30:17 +02:00
|
|
|
try:
|
|
|
|
hook.poll_vpp()
|
2018-03-21 12:35:51 +01:00
|
|
|
except VppDiedError:
|
2017-04-10 06:30:17 +02:00
|
|
|
cls.vpp_startup_failed = True
|
|
|
|
cls.logger.critical(
|
|
|
|
"VPP died shortly after startup, check the"
|
|
|
|
" output to standard error for possible cause")
|
|
|
|
raise
|
2016-10-28 13:20:27 +02:00
|
|
|
try:
|
|
|
|
cls.vapi.connect()
|
2018-03-21 12:35:51 +01:00
|
|
|
except Exception:
|
2018-02-22 11:41:12 +01:00
|
|
|
try:
|
|
|
|
cls.vapi.disconnect()
|
2018-03-21 12:35:51 +01:00
|
|
|
except Exception:
|
2018-02-22 11:41:12 +01:00
|
|
|
pass
|
2016-10-28 13:20:27 +02:00
|
|
|
if cls.debug_gdbserver:
|
|
|
|
print(colorize("You're running VPP inside gdbserver but "
|
|
|
|
"VPP-API connection failed, did you forget "
|
|
|
|
"to 'continue' VPP from within gdb?", RED))
|
|
|
|
raise
|
2018-03-21 12:35:51 +01:00
|
|
|
except Exception:
|
2016-11-24 01:59:16 +01:00
|
|
|
try:
|
|
|
|
cls.quit()
|
2018-03-21 12:35:51 +01:00
|
|
|
except Exception:
|
2016-11-24 01:59:16 +01:00
|
|
|
pass
|
2018-03-21 12:35:51 +01:00
|
|
|
raise
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
@classmethod
|
|
|
|
def quit(cls):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Disconnect vpp-api, kill vpp and cleanup shared memory files
|
|
|
|
"""
|
2016-10-28 13:20:27 +02:00
|
|
|
if (cls.debug_gdbserver or cls.debug_gdb) and hasattr(cls, 'vpp'):
|
|
|
|
cls.vpp.poll()
|
|
|
|
if cls.vpp.returncode is None:
|
|
|
|
print(double_line_delim)
|
|
|
|
print("VPP or GDB server is still running")
|
|
|
|
print(single_line_delim)
|
2018-12-15 10:16:35 -08:00
|
|
|
input("When done debugging, press ENTER to kill the "
|
|
|
|
"process and finish running the testcase...")
|
2016-10-28 13:20:27 +02:00
|
|
|
|
2018-07-16 14:22:01 +02:00
|
|
|
# first signal that we want to stop the pump thread, then wake it up
|
|
|
|
if hasattr(cls, 'pump_thread_stop_flag'):
|
|
|
|
cls.pump_thread_stop_flag.set()
|
|
|
|
if hasattr(cls, 'pump_thread_wakeup_pipe'):
|
2018-12-06 17:35:12 +01:00
|
|
|
os.write(cls.pump_thread_wakeup_pipe[1], b'ding dong wake up')
|
2017-02-14 02:55:31 +01:00
|
|
|
if hasattr(cls, 'pump_thread'):
|
|
|
|
cls.logger.debug("Waiting for pump thread to stop")
|
|
|
|
cls.pump_thread.join()
|
|
|
|
if hasattr(cls, 'vpp_stderr_reader_thread'):
|
|
|
|
cls.logger.debug("Waiting for stdderr pump to stop")
|
|
|
|
cls.vpp_stderr_reader_thread.join()
|
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
if hasattr(cls, 'vpp'):
|
2016-12-02 07:05:24 +01:00
|
|
|
if hasattr(cls, 'vapi'):
|
2019-03-10 10:04:23 -07:00
|
|
|
cls.logger.debug("Disconnecting class vapi client on %s",
|
|
|
|
cls.__name__)
|
2016-12-02 07:05:24 +01:00
|
|
|
cls.vapi.disconnect()
|
2019-03-10 10:04:23 -07:00
|
|
|
cls.logger.debug("Deleting class vapi attribute on %s",
|
|
|
|
cls.__name__)
|
2017-02-14 02:55:31 +01:00
|
|
|
del cls.vapi
|
2016-10-11 11:47:09 +02:00
|
|
|
cls.vpp.poll()
|
|
|
|
if cls.vpp.returncode is None:
|
2017-02-14 02:55:31 +01:00
|
|
|
cls.logger.debug("Sending TERM to vpp")
|
2018-09-26 11:19:00 +02:00
|
|
|
cls.vpp.kill()
|
2017-02-14 02:55:31 +01:00
|
|
|
cls.logger.debug("Waiting for vpp to die")
|
|
|
|
cls.vpp.communicate()
|
2019-03-10 10:04:23 -07:00
|
|
|
cls.logger.debug("Deleting class vpp attribute on %s",
|
|
|
|
cls.__name__)
|
2016-10-11 11:47:09 +02:00
|
|
|
del cls.vpp
|
|
|
|
|
2017-04-10 06:30:17 +02:00
|
|
|
if cls.vpp_startup_failed:
|
|
|
|
stdout_log = cls.logger.info
|
|
|
|
stderr_log = cls.logger.critical
|
|
|
|
else:
|
|
|
|
stdout_log = cls.logger.info
|
|
|
|
stderr_log = cls.logger.info
|
|
|
|
|
2016-12-08 10:16:41 +01:00
|
|
|
if hasattr(cls, 'vpp_stdout_deque'):
|
2017-04-10 06:30:17 +02:00
|
|
|
stdout_log(single_line_delim)
|
|
|
|
stdout_log('VPP output to stdout while running %s:', cls.__name__)
|
|
|
|
stdout_log(single_line_delim)
|
2016-12-08 10:16:41 +01:00
|
|
|
vpp_output = "".join(cls.vpp_stdout_deque)
|
2017-04-11 06:01:53 +02:00
|
|
|
with open(cls.tempdir + '/vpp_stdout.txt', 'w') as f:
|
|
|
|
f.write(vpp_output)
|
2017-04-10 06:30:17 +02:00
|
|
|
stdout_log('\n%s', vpp_output)
|
|
|
|
stdout_log(single_line_delim)
|
2016-10-28 13:20:27 +02:00
|
|
|
|
2016-12-08 10:16:41 +01:00
|
|
|
if hasattr(cls, 'vpp_stderr_deque'):
|
2017-04-10 06:30:17 +02:00
|
|
|
stderr_log(single_line_delim)
|
|
|
|
stderr_log('VPP output to stderr while running %s:', cls.__name__)
|
|
|
|
stderr_log(single_line_delim)
|
2019-01-29 11:49:09 +01:00
|
|
|
vpp_output = "".join(cls.vpp_stderr_deque)
|
2017-04-11 06:01:53 +02:00
|
|
|
with open(cls.tempdir + '/vpp_stderr.txt', 'w') as f:
|
|
|
|
f.write(vpp_output)
|
2017-04-10 06:30:17 +02:00
|
|
|
stderr_log('\n%s', vpp_output)
|
|
|
|
stderr_log(single_line_delim)
|
2016-10-28 13:20:27 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Perform final cleanup after running all tests in this test-case """
|
2018-09-21 13:55:16 +02:00
|
|
|
cls.reporter.send_keep_alive(cls, 'tearDownClass')
|
2016-10-03 19:44:57 +02:00
|
|
|
cls.quit()
|
2017-04-11 06:01:53 +02:00
|
|
|
cls.file_handler.close()
|
2018-02-17 13:41:33 +01:00
|
|
|
cls.reset_packet_infos()
|
|
|
|
if debug_framework:
|
|
|
|
debug_internal.on_tear_down_class(cls)
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Show various debug prints after each test """
|
2017-02-09 06:04:36 +01:00
|
|
|
self.logger.debug("--- tearDown() for %s.%s(%s) called ---" %
|
|
|
|
(self.__class__.__name__, self._testMethodName,
|
|
|
|
self._testMethodDoc))
|
2016-10-11 11:47:09 +02:00
|
|
|
if not self.vpp_dead:
|
2019-01-15 13:25:09 +01:00
|
|
|
self.logger.debug(self.vapi.cli("show trace max 1000"))
|
2017-04-05 08:11:14 -07:00
|
|
|
self.logger.info(self.vapi.ppcli("show interface"))
|
2016-10-26 15:44:27 +02:00
|
|
|
self.logger.info(self.vapi.ppcli("show hardware"))
|
2018-08-31 00:29:48 +02:00
|
|
|
self.logger.info(self.statistics.set_errors_str())
|
2016-10-26 15:44:27 +02:00
|
|
|
self.logger.info(self.vapi.ppcli("show run"))
|
2018-01-21 06:44:18 -08:00
|
|
|
self.logger.info(self.vapi.ppcli("show log"))
|
2017-01-11 08:16:53 +01:00
|
|
|
self.registry.remove_vpp_config(self.logger)
|
2017-02-16 11:25:26 -05:00
|
|
|
# Save/Dump VPP api trace log
|
|
|
|
api_trace = "vpp_api_trace.%s.log" % self._testMethodName
|
|
|
|
tmp_api_trace = "/tmp/%s" % api_trace
|
|
|
|
vpp_api_trace_log = "%s/%s" % (self.tempdir, api_trace)
|
|
|
|
self.logger.info(self.vapi.ppcli("api trace save %s" % api_trace))
|
|
|
|
self.logger.info("Moving %s to %s\n" % (tmp_api_trace,
|
|
|
|
vpp_api_trace_log))
|
|
|
|
os.rename(tmp_api_trace, vpp_api_trace_log)
|
2018-02-13 16:14:06 -05:00
|
|
|
self.logger.info(self.vapi.ppcli("api trace custom-dump %s" %
|
2017-02-16 11:25:26 -05:00
|
|
|
vpp_api_trace_log))
|
2017-03-02 11:29:19 +01:00
|
|
|
else:
|
|
|
|
self.registry.unregister_all(self.logger)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def setUp(self):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Clear trace before running each test"""
|
2019-01-25 14:05:48 -08:00
|
|
|
super(VppTestCase, self).setUp()
|
2017-08-08 04:33:53 +02:00
|
|
|
self.reporter.send_keep_alive(self)
|
2017-02-09 06:04:36 +01:00
|
|
|
self.logger.debug("--- setUp() for %s.%s(%s) called ---" %
|
|
|
|
(self.__class__.__name__, self._testMethodName,
|
|
|
|
self._testMethodDoc))
|
2016-12-08 05:03:32 +01:00
|
|
|
if self.vpp_dead:
|
|
|
|
raise Exception("VPP is dead when setting up the test")
|
2017-02-14 02:55:31 +01:00
|
|
|
self.sleep(.1, "during setUp")
|
2016-12-08 10:16:41 +01:00
|
|
|
self.vpp_stdout_deque.append(
|
|
|
|
"--- test setUp() for %s.%s(%s) starts here ---\n" %
|
|
|
|
(self.__class__.__name__, self._testMethodName,
|
|
|
|
self._testMethodDoc))
|
|
|
|
self.vpp_stderr_deque.append(
|
|
|
|
"--- test setUp() for %s.%s(%s) starts here ---\n" %
|
|
|
|
(self.__class__.__name__, self._testMethodName,
|
|
|
|
self._testMethodDoc))
|
2016-10-11 11:47:09 +02:00
|
|
|
self.vapi.cli("clear trace")
|
|
|
|
# store the test instance inside the test class - so that objects
|
|
|
|
# holding the class can access instance methods (like assertEqual)
|
|
|
|
type(self).test_instance = self
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
@classmethod
|
2017-09-20 08:26:30 +02:00
|
|
|
def pg_enable_capture(cls, interfaces=None):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Enable capture on packet-generator interfaces
|
|
|
|
|
2017-09-20 08:26:30 +02:00
|
|
|
:param interfaces: iterable interface indexes (if None,
|
|
|
|
use self.pg_interfaces)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
"""
|
2017-09-20 08:26:30 +02:00
|
|
|
if interfaces is None:
|
|
|
|
interfaces = cls.pg_interfaces
|
2016-10-11 11:47:09 +02:00
|
|
|
for i in interfaces:
|
|
|
|
i.enable_capture()
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
@classmethod
|
2016-12-12 08:36:58 +01:00
|
|
|
def register_capture(cls, cap_name):
|
|
|
|
""" Register a capture in the testclass """
|
|
|
|
# add to the list of captures with current timestamp
|
|
|
|
cls._captures.append((time.time(), cap_name))
|
|
|
|
# filter out from zombies
|
|
|
|
cls._zombie_captures = [(stamp, name)
|
|
|
|
for (stamp, name) in cls._zombie_captures
|
|
|
|
if name != cap_name]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def pg_start(cls):
|
|
|
|
""" Remove any zombie captures and enable the packet generator """
|
|
|
|
# how long before capture is allowed to be deleted - otherwise vpp
|
|
|
|
# crashes - 100ms seems enough (this shouldn't be needed at all)
|
|
|
|
capture_ttl = 0.1
|
|
|
|
now = time.time()
|
|
|
|
for stamp, cap_name in cls._zombie_captures:
|
|
|
|
wait = stamp + capture_ttl - now
|
|
|
|
if wait > 0:
|
2017-02-14 02:55:31 +01:00
|
|
|
cls.sleep(wait, "before deleting capture %s" % cap_name)
|
2016-12-12 08:36:58 +01:00
|
|
|
now = time.time()
|
|
|
|
cls.logger.debug("Removing zombie capture %s" % cap_name)
|
|
|
|
cls.vapi.cli('packet-generator delete %s' % cap_name)
|
|
|
|
|
2019-01-15 13:25:09 +01:00
|
|
|
cls.vapi.cli("trace add pg-input 1000")
|
2016-10-11 11:47:09 +02:00
|
|
|
cls.vapi.cli('packet-generator enable')
|
2016-12-12 08:36:58 +01:00
|
|
|
cls._zombie_captures = cls._captures
|
|
|
|
cls._captures = []
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
@classmethod
|
2016-10-11 11:47:09 +02:00
|
|
|
def create_pg_interfaces(cls, interfaces):
|
|
|
|
"""
|
2016-12-22 11:06:56 +01:00
|
|
|
Create packet-generator interfaces.
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-12-22 11:06:56 +01:00
|
|
|
:param interfaces: iterable indexes of the interfaces.
|
|
|
|
:returns: List of created interfaces.
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
for i in interfaces:
|
|
|
|
intf = VppPGInterface(cls, i)
|
|
|
|
setattr(cls, intf.name, intf)
|
|
|
|
result.append(intf)
|
|
|
|
cls.pg_interfaces = result
|
|
|
|
return result
|
|
|
|
|
2016-11-04 11:11:44 +01:00
|
|
|
@classmethod
|
2018-06-24 22:49:33 +02:00
|
|
|
def create_loopback_interfaces(cls, count):
|
2016-11-04 11:11:44 +01:00
|
|
|
"""
|
2016-12-22 11:06:56 +01:00
|
|
|
Create loopback interfaces.
|
2016-11-04 11:11:44 +01:00
|
|
|
|
2018-06-24 22:49:33 +02:00
|
|
|
:param count: number of interfaces created.
|
2016-12-22 11:06:56 +01:00
|
|
|
:returns: List of created interfaces.
|
2016-11-04 11:11:44 +01:00
|
|
|
"""
|
2018-06-24 22:49:33 +02:00
|
|
|
result = [VppLoInterface(cls) for i in range(count)]
|
|
|
|
for intf in result:
|
2016-11-04 11:11:44 +01:00
|
|
|
setattr(cls, intf.name, intf)
|
|
|
|
cls.lo_interfaces = result
|
|
|
|
return result
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
@staticmethod
|
2017-09-20 08:26:30 +02:00
|
|
|
def extend_packet(packet, size, padding=' '):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
2017-09-20 08:26:30 +02:00
|
|
|
Extend packet to given size by padding with spaces or custom padding
|
2016-10-11 11:47:09 +02:00
|
|
|
NOTE: Currently works only when Raw layer is present.
|
|
|
|
|
|
|
|
:param packet: packet
|
|
|
|
:param size: target size
|
2017-09-20 08:26:30 +02:00
|
|
|
:param padding: padding used to extend the payload
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
"""
|
2016-10-03 19:44:57 +02:00
|
|
|
packet_len = len(packet) + 4
|
|
|
|
extend = size - packet_len
|
|
|
|
if extend > 0:
|
2017-09-20 08:26:30 +02:00
|
|
|
num = (extend / len(padding)) + 1
|
|
|
|
packet[Raw].load += (padding * num)[:extend]
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-12-21 08:50:14 +01:00
|
|
|
@classmethod
|
|
|
|
def reset_packet_infos(cls):
|
|
|
|
""" Reset the list of packet info objects and packet counts to zero """
|
|
|
|
cls._packet_infos = {}
|
|
|
|
cls._packet_count_for_dst_if_idx = {}
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-12-21 08:50:14 +01:00
|
|
|
@classmethod
|
|
|
|
def create_packet_info(cls, src_if, dst_if):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Create packet info object containing the source and destination indexes
|
|
|
|
and add it to the testcase's packet info list
|
|
|
|
|
2016-12-21 08:50:14 +01:00
|
|
|
:param VppInterface src_if: source interface
|
|
|
|
:param VppInterface dst_if: destination interface
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
:returns: _PacketInfo object
|
|
|
|
|
|
|
|
"""
|
2016-10-03 19:44:57 +02:00
|
|
|
info = _PacketInfo()
|
2016-12-21 08:50:14 +01:00
|
|
|
info.index = len(cls._packet_infos)
|
|
|
|
info.src = src_if.sw_if_index
|
|
|
|
info.dst = dst_if.sw_if_index
|
|
|
|
if isinstance(dst_if, VppSubInterface):
|
|
|
|
dst_idx = dst_if.parent.sw_if_index
|
|
|
|
else:
|
|
|
|
dst_idx = dst_if.sw_if_index
|
|
|
|
if dst_idx in cls._packet_count_for_dst_if_idx:
|
|
|
|
cls._packet_count_for_dst_if_idx[dst_idx] += 1
|
|
|
|
else:
|
|
|
|
cls._packet_count_for_dst_if_idx[dst_idx] = 1
|
|
|
|
cls._packet_infos[info.index] = info
|
2016-10-03 19:44:57 +02:00
|
|
|
return info
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
@staticmethod
|
|
|
|
def info_to_payload(info):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Convert _PacketInfo object to packet payload
|
|
|
|
|
|
|
|
:param info: _PacketInfo object
|
|
|
|
|
|
|
|
:returns: string containing serialized data from packet info
|
|
|
|
"""
|
2017-03-02 15:22:47 +01:00
|
|
|
return "%d %d %d %d %d" % (info.index, info.src, info.dst,
|
|
|
|
info.ip, info.proto)
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2019-03-06 11:58:06 -08:00
|
|
|
def payload_to_info(payload, payload_field='load'):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Convert packet payload to _PacketInfo object
|
|
|
|
|
|
|
|
:param payload: packet payload
|
2019-03-06 11:58:06 -08:00
|
|
|
:type: <class 'scapy.packet.Raw'>
|
|
|
|
:param: payload_field: packet fieldname of payload "load" for
|
|
|
|
<class 'scapy.packet.Raw'>
|
2016-10-11 11:47:09 +02:00
|
|
|
:returns: _PacketInfo object containing de-serialized data from payload
|
|
|
|
|
|
|
|
"""
|
2019-03-06 11:58:06 -08:00
|
|
|
numbers = getattr(payload, payload_field).split()
|
2016-10-03 19:44:57 +02:00
|
|
|
info = _PacketInfo()
|
|
|
|
info.index = int(numbers[0])
|
|
|
|
info.src = int(numbers[1])
|
|
|
|
info.dst = int(numbers[2])
|
2017-03-02 15:22:47 +01:00
|
|
|
info.ip = int(numbers[3])
|
|
|
|
info.proto = int(numbers[4])
|
2016-10-03 19:44:57 +02:00
|
|
|
return info
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def get_next_packet_info(self, info):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Iterate over the packet info list stored in the testcase
|
|
|
|
Start iteration with first element if info is None
|
|
|
|
Continue based on index in info if info is specified
|
|
|
|
|
|
|
|
:param info: info or None
|
|
|
|
:returns: next info in list or None if no more infos
|
|
|
|
"""
|
2016-10-03 19:44:57 +02:00
|
|
|
if info is None:
|
|
|
|
next_index = 0
|
|
|
|
else:
|
|
|
|
next_index = info.index + 1
|
2016-12-21 08:50:14 +01:00
|
|
|
if next_index == len(self._packet_infos):
|
2016-10-03 19:44:57 +02:00
|
|
|
return None
|
|
|
|
else:
|
2016-12-21 08:50:14 +01:00
|
|
|
return self._packet_infos[next_index]
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
def get_next_packet_info_for_interface(self, src_index, info):
|
|
|
|
"""
|
|
|
|
Search the packet info list for the next packet info with same source
|
|
|
|
interface index
|
|
|
|
|
|
|
|
:param src_index: source interface index to search for
|
|
|
|
:param info: packet info - where to start the search
|
|
|
|
:returns: packet info or None
|
|
|
|
|
|
|
|
"""
|
2016-10-03 19:44:57 +02:00
|
|
|
while True:
|
|
|
|
info = self.get_next_packet_info(info)
|
|
|
|
if info is None:
|
|
|
|
return None
|
2016-10-11 11:47:09 +02:00
|
|
|
if info.src == src_index:
|
2016-10-03 19:44:57 +02:00
|
|
|
return info
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
def get_next_packet_info_for_interface2(self, src_index, dst_index, info):
|
|
|
|
"""
|
|
|
|
Search the packet info list for the next packet info with same source
|
|
|
|
and destination interface indexes
|
|
|
|
|
|
|
|
:param src_index: source interface index to search for
|
|
|
|
:param dst_index: destination interface index to search for
|
|
|
|
:param info: packet info - where to start the search
|
|
|
|
:returns: packet info or None
|
|
|
|
|
|
|
|
"""
|
2016-10-03 19:44:57 +02:00
|
|
|
while True:
|
2016-10-11 11:47:09 +02:00
|
|
|
info = self.get_next_packet_info_for_interface(src_index, info)
|
2016-10-03 19:44:57 +02:00
|
|
|
if info is None:
|
|
|
|
return None
|
2016-10-11 11:47:09 +02:00
|
|
|
if info.dst == dst_index:
|
2016-10-03 19:44:57 +02:00
|
|
|
return info
|
|
|
|
|
2016-09-29 14:43:44 +02:00
|
|
|
def assert_equal(self, real_value, expected_value, name_or_class=None):
|
|
|
|
if name_or_class is None:
|
2017-02-16 10:53:53 +01:00
|
|
|
self.assertEqual(real_value, expected_value)
|
2016-09-29 14:43:44 +02:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
msg = "Invalid %s: %d('%s') does not match expected value %d('%s')"
|
|
|
|
msg = msg % (getdoc(name_or_class).strip(),
|
|
|
|
real_value, str(name_or_class(real_value)),
|
|
|
|
expected_value, str(name_or_class(expected_value)))
|
2018-03-21 12:35:51 +01:00
|
|
|
except Exception:
|
2016-09-29 14:43:44 +02:00
|
|
|
msg = "Invalid %s: %s does not match expected value %s" % (
|
|
|
|
name_or_class, real_value, expected_value)
|
|
|
|
|
|
|
|
self.assertEqual(real_value, expected_value, msg)
|
|
|
|
|
2017-01-09 07:43:48 +01:00
|
|
|
def assert_in_range(self,
|
|
|
|
real_value,
|
|
|
|
expected_min,
|
|
|
|
expected_max,
|
|
|
|
name=None):
|
2016-09-29 14:43:44 +02:00
|
|
|
if name is None:
|
|
|
|
msg = None
|
|
|
|
else:
|
|
|
|
msg = "Invalid %s: %s out of range <%s,%s>" % (
|
|
|
|
name, real_value, expected_min, expected_max)
|
|
|
|
self.assertTrue(expected_min <= real_value <= expected_max, msg)
|
|
|
|
|
2018-05-16 10:52:54 +02:00
|
|
|
def assert_packet_checksums_valid(self, packet,
|
|
|
|
ignore_zero_udp_checksums=True):
|
2019-03-10 10:04:23 -07:00
|
|
|
received = packet.__class__(scapy.compat.raw(packet))
|
2018-06-24 22:49:55 +02:00
|
|
|
self.logger.debug(
|
|
|
|
ppp("Verifying packet checksums for packet:", received))
|
2018-05-16 10:52:54 +02:00
|
|
|
udp_layers = ['UDP', 'UDPerror']
|
|
|
|
checksum_fields = ['cksum', 'chksum']
|
|
|
|
checksums = []
|
|
|
|
counter = 0
|
2019-03-10 10:04:23 -07:00
|
|
|
temp = received.__class__(scapy.compat.raw(received))
|
2018-05-16 10:52:54 +02:00
|
|
|
while True:
|
|
|
|
layer = temp.getlayer(counter)
|
|
|
|
if layer:
|
|
|
|
for cf in checksum_fields:
|
|
|
|
if hasattr(layer, cf):
|
|
|
|
if ignore_zero_udp_checksums and \
|
2018-12-21 16:04:22 +01:00
|
|
|
0 == getattr(layer, cf) and \
|
|
|
|
layer.name in udp_layers:
|
2018-05-16 10:52:54 +02:00
|
|
|
continue
|
|
|
|
delattr(layer, cf)
|
|
|
|
checksums.append((counter, cf))
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
counter = counter + 1
|
2018-06-24 22:49:55 +02:00
|
|
|
if 0 == len(checksums):
|
|
|
|
return
|
2019-03-10 10:04:23 -07:00
|
|
|
temp = temp.__class__(scapy.compat.raw(temp))
|
2018-05-16 10:52:54 +02:00
|
|
|
for layer, cf in checksums:
|
2018-06-24 22:49:55 +02:00
|
|
|
calc_sum = getattr(temp[layer], cf)
|
|
|
|
self.assert_equal(
|
|
|
|
getattr(received[layer], cf), calc_sum,
|
|
|
|
"packet checksum on layer #%d: %s" % (layer, temp[layer].name))
|
|
|
|
self.logger.debug(
|
|
|
|
"Checksum field `%s` on `%s` layer has correct value `%s`" %
|
|
|
|
(cf, temp[layer].name, calc_sum))
|
2018-05-16 10:52:54 +02:00
|
|
|
|
|
|
|
def assert_checksum_valid(self, received_packet, layer,
|
|
|
|
field_name='chksum',
|
|
|
|
ignore_zero_checksum=False):
|
|
|
|
""" Check checksum of received packet on given layer """
|
|
|
|
received_packet_checksum = getattr(received_packet[layer], field_name)
|
|
|
|
if ignore_zero_checksum and 0 == received_packet_checksum:
|
|
|
|
return
|
2019-03-10 10:04:23 -07:00
|
|
|
recalculated = received_packet.__class__(
|
|
|
|
scapy.compat.raw(received_packet))
|
2018-05-16 10:52:54 +02:00
|
|
|
delattr(recalculated[layer], field_name)
|
2019-03-10 10:04:23 -07:00
|
|
|
recalculated = recalculated.__class__(scapy.compat.raw(recalculated))
|
2018-05-16 10:52:54 +02:00
|
|
|
self.assert_equal(received_packet_checksum,
|
|
|
|
getattr(recalculated[layer], field_name),
|
|
|
|
"packet checksum on layer: %s" % layer)
|
|
|
|
|
|
|
|
def assert_ip_checksum_valid(self, received_packet,
|
|
|
|
ignore_zero_checksum=False):
|
|
|
|
self.assert_checksum_valid(received_packet, 'IP',
|
|
|
|
ignore_zero_checksum=ignore_zero_checksum)
|
|
|
|
|
|
|
|
def assert_tcp_checksum_valid(self, received_packet,
|
|
|
|
ignore_zero_checksum=False):
|
|
|
|
self.assert_checksum_valid(received_packet, 'TCP',
|
|
|
|
ignore_zero_checksum=ignore_zero_checksum)
|
|
|
|
|
|
|
|
def assert_udp_checksum_valid(self, received_packet,
|
|
|
|
ignore_zero_checksum=True):
|
|
|
|
self.assert_checksum_valid(received_packet, 'UDP',
|
|
|
|
ignore_zero_checksum=ignore_zero_checksum)
|
|
|
|
|
|
|
|
def assert_embedded_icmp_checksum_valid(self, received_packet):
|
|
|
|
if received_packet.haslayer(IPerror):
|
|
|
|
self.assert_checksum_valid(received_packet, 'IPerror')
|
|
|
|
if received_packet.haslayer(TCPerror):
|
|
|
|
self.assert_checksum_valid(received_packet, 'TCPerror')
|
|
|
|
if received_packet.haslayer(UDPerror):
|
|
|
|
self.assert_checksum_valid(received_packet, 'UDPerror',
|
|
|
|
ignore_zero_checksum=True)
|
|
|
|
if received_packet.haslayer(ICMPerror):
|
|
|
|
self.assert_checksum_valid(received_packet, 'ICMPerror')
|
|
|
|
|
|
|
|
def assert_icmp_checksum_valid(self, received_packet):
|
|
|
|
self.assert_checksum_valid(received_packet, 'ICMP')
|
|
|
|
self.assert_embedded_icmp_checksum_valid(received_packet)
|
|
|
|
|
|
|
|
def assert_icmpv6_checksum_valid(self, pkt):
|
|
|
|
if pkt.haslayer(ICMPv6DestUnreach):
|
|
|
|
self.assert_checksum_valid(pkt, 'ICMPv6DestUnreach', 'cksum')
|
|
|
|
self.assert_embedded_icmp_checksum_valid(pkt)
|
|
|
|
if pkt.haslayer(ICMPv6EchoRequest):
|
|
|
|
self.assert_checksum_valid(pkt, 'ICMPv6EchoRequest', 'cksum')
|
|
|
|
if pkt.haslayer(ICMPv6EchoReply):
|
|
|
|
self.assert_checksum_valid(pkt, 'ICMPv6EchoReply', 'cksum')
|
|
|
|
|
2018-11-08 11:24:34 +01:00
|
|
|
def assert_packet_counter_equal(self, counter, expected_value):
|
2018-12-10 13:46:09 +01:00
|
|
|
if counter.startswith("/"):
|
|
|
|
counter_value = self.statistics.get_counter(counter)
|
|
|
|
self.assert_equal(counter_value, expected_value,
|
|
|
|
"packet counter `%s'" % counter)
|
|
|
|
else:
|
|
|
|
counters = self.vapi.cli("sh errors").split('\n')
|
|
|
|
counter_value = -1
|
|
|
|
for i in range(1, len(counters) - 1):
|
|
|
|
results = counters[i].split()
|
|
|
|
if results[1] == counter:
|
|
|
|
counter_value = int(results[0])
|
|
|
|
break
|
2018-11-08 11:24:34 +01:00
|
|
|
|
2017-02-14 02:55:31 +01:00
|
|
|
@classmethod
|
|
|
|
def sleep(cls, timeout, remark=None):
|
2019-03-10 09:10:54 -07:00
|
|
|
|
|
|
|
# /* Allow sleep(0) to maintain win32 semantics, and as decreed
|
|
|
|
# * by Guido, only the main thread can be interrupted.
|
|
|
|
# */
|
|
|
|
# https://github.com/python/cpython/blob/6673decfa0fb078f60587f5cb5e98460eea137c2/Modules/timemodule.c#L1892 # noqa
|
|
|
|
if timeout == 0:
|
|
|
|
# yield quantum
|
|
|
|
if hasattr(os, 'sched_yield'):
|
|
|
|
os.sched_yield()
|
|
|
|
else:
|
|
|
|
time.sleep(0)
|
|
|
|
return
|
|
|
|
|
2017-02-14 02:55:31 +01:00
|
|
|
if hasattr(cls, 'logger'):
|
2018-12-09 15:37:04 -08:00
|
|
|
cls.logger.debug("Starting sleep for %es (%s)", timeout, remark)
|
2017-04-19 07:10:58 +00:00
|
|
|
before = time.time()
|
2017-02-02 06:58:07 +01:00
|
|
|
time.sleep(timeout)
|
2017-04-19 07:10:58 +00:00
|
|
|
after = time.time()
|
2018-12-09 15:37:04 -08:00
|
|
|
if hasattr(cls, 'logger') and after - before > 2 * timeout:
|
2019-03-10 09:10:54 -07:00
|
|
|
cls.logger.error("unexpected self.sleep() result - "
|
2018-12-09 15:37:04 -08:00
|
|
|
"slept for %es instead of ~%es!",
|
|
|
|
after - before, timeout)
|
2017-04-19 07:10:58 +00:00
|
|
|
if hasattr(cls, 'logger'):
|
|
|
|
cls.logger.debug(
|
2018-12-09 15:37:04 -08:00
|
|
|
"Finished sleep (%s) - slept %es (wanted %es)",
|
|
|
|
remark, after - before, timeout)
|
2017-02-02 06:58:07 +01:00
|
|
|
|
2019-02-20 09:01:14 -08:00
|
|
|
def pg_send(self, intf, pkts):
|
2018-01-08 04:41:42 -08:00
|
|
|
self.vapi.cli("clear trace")
|
|
|
|
intf.add_stream(pkts)
|
|
|
|
self.pg_enable_capture(self.pg_interfaces)
|
|
|
|
self.pg_start()
|
2019-02-20 09:01:14 -08:00
|
|
|
|
|
|
|
def send_and_assert_no_replies(self, intf, pkts, remark="", timeout=None):
|
|
|
|
self.pg_send(intf, pkts)
|
2018-06-07 23:48:20 -07:00
|
|
|
if not timeout:
|
|
|
|
timeout = 1
|
2018-01-08 04:41:42 -08:00
|
|
|
for i in self.pg_interfaces:
|
|
|
|
i.get_capture(0, timeout=timeout)
|
|
|
|
i.assert_nothing_captured(remark=remark)
|
|
|
|
timeout = 0.1
|
|
|
|
|
2019-02-20 09:01:14 -08:00
|
|
|
def send_and_expect(self, intf, pkts, output):
|
|
|
|
self.pg_send(intf, pkts)
|
2019-01-07 16:29:26 -08:00
|
|
|
rx = output.get_capture(len(pkts))
|
2018-10-10 07:22:51 -07:00
|
|
|
return rx
|
|
|
|
|
2019-02-20 09:01:14 -08:00
|
|
|
def send_and_expect_only(self, intf, pkts, output, timeout=None):
|
|
|
|
self.pg_send(intf, pkts)
|
2019-01-07 16:29:26 -08:00
|
|
|
rx = output.get_capture(len(pkts))
|
|
|
|
outputs = [output]
|
2018-10-10 07:22:51 -07:00
|
|
|
if not timeout:
|
|
|
|
timeout = 1
|
|
|
|
for i in self.pg_interfaces:
|
|
|
|
if i not in outputs:
|
|
|
|
i.get_capture(0, timeout=timeout)
|
|
|
|
i.assert_nothing_captured()
|
|
|
|
timeout = 0.1
|
|
|
|
|
2018-01-08 04:41:42 -08:00
|
|
|
return rx
|
|
|
|
|
2018-12-15 08:03:09 -08:00
|
|
|
def runTest(self):
|
|
|
|
""" unittest calls runTest when TestCase is instantiated without a
|
|
|
|
test case. Use case: Writing unittests against VppTestCase"""
|
|
|
|
pass
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2018-07-16 14:22:01 +02:00
|
|
|
def get_testcase_doc_name(test):
|
|
|
|
return getdoc(test.__class__).splitlines()[0]
|
|
|
|
|
|
|
|
|
2018-11-22 10:01:09 +00:00
|
|
|
def get_test_description(descriptions, test):
|
|
|
|
short_description = test.shortDescription()
|
|
|
|
if descriptions and short_description:
|
|
|
|
return short_description
|
|
|
|
else:
|
|
|
|
return str(test)
|
|
|
|
|
|
|
|
|
2018-09-21 13:55:16 +02:00
|
|
|
class TestCaseInfo(object):
|
|
|
|
def __init__(self, logger, tempdir, vpp_pid, vpp_bin_path):
|
|
|
|
self.logger = logger
|
|
|
|
self.tempdir = tempdir
|
|
|
|
self.vpp_pid = vpp_pid
|
|
|
|
self.vpp_bin_path = vpp_bin_path
|
|
|
|
self.core_crash_test = None
|
2017-03-07 11:39:27 +01:00
|
|
|
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
class VppTestResult(unittest.TestResult):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
@property result_string
|
|
|
|
String variable to store the test case result string.
|
|
|
|
@property errors
|
|
|
|
List variable containing 2-tuples of TestCase instances and strings
|
|
|
|
holding formatted tracebacks. Each tuple represents a test which
|
|
|
|
raised an unexpected exception.
|
|
|
|
@property failures
|
|
|
|
List variable containing 2-tuples of TestCase instances and strings
|
|
|
|
holding formatted tracebacks. Each tuple represents a test where
|
|
|
|
a failure was explicitly signalled using the TestCase.assert*()
|
|
|
|
methods.
|
|
|
|
"""
|
|
|
|
|
2018-09-21 13:55:16 +02:00
|
|
|
failed_test_cases_info = set()
|
|
|
|
core_crash_test_cases_info = set()
|
|
|
|
current_test_case_info = None
|
|
|
|
|
2019-01-16 11:12:50 -08:00
|
|
|
def __init__(self, stream=None, descriptions=None, verbosity=None,
|
|
|
|
runner=None):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
2017-01-04 12:58:53 +01:00
|
|
|
:param stream File descriptor to store where to report test results.
|
|
|
|
Set to the standard error stream by default.
|
|
|
|
:param descriptions Boolean variable to store information if to use
|
|
|
|
test case descriptions.
|
2016-10-11 11:47:09 +02:00
|
|
|
:param verbosity Integer variable to store required verbosity level.
|
|
|
|
"""
|
2019-01-13 16:09:10 -08:00
|
|
|
super(VppTestResult, self).__init__(stream, descriptions, verbosity)
|
2016-10-03 19:44:57 +02:00
|
|
|
self.stream = stream
|
|
|
|
self.descriptions = descriptions
|
|
|
|
self.verbosity = verbosity
|
|
|
|
self.result_string = None
|
2018-11-16 17:28:56 +01:00
|
|
|
self.runner = runner
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
def addSuccess(self, test):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Record a test succeeded result
|
|
|
|
|
|
|
|
:param test:
|
|
|
|
|
|
|
|
"""
|
2018-09-21 13:55:16 +02:00
|
|
|
if self.current_test_case_info:
|
|
|
|
self.current_test_case_info.logger.debug(
|
|
|
|
"--- addSuccess() %s.%s(%s) called" % (test.__class__.__name__,
|
|
|
|
test._testMethodName,
|
|
|
|
test._testMethodDoc))
|
2016-10-03 19:44:57 +02:00
|
|
|
unittest.TestResult.addSuccess(self, test)
|
2016-10-28 13:20:27 +02:00
|
|
|
self.result_string = colorize("OK", GREEN)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2018-09-19 15:01:47 +02:00
|
|
|
self.send_result_through_pipe(test, PASS)
|
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
def addSkip(self, test, reason):
|
|
|
|
"""
|
|
|
|
Record a test skipped.
|
|
|
|
|
|
|
|
:param test:
|
|
|
|
:param reason:
|
|
|
|
|
|
|
|
"""
|
2018-09-21 13:55:16 +02:00
|
|
|
if self.current_test_case_info:
|
|
|
|
self.current_test_case_info.logger.debug(
|
|
|
|
"--- addSkip() %s.%s(%s) called, reason is %s" %
|
|
|
|
(test.__class__.__name__, test._testMethodName,
|
|
|
|
test._testMethodDoc, reason))
|
2016-10-11 11:47:09 +02:00
|
|
|
unittest.TestResult.addSkip(self, test, reason)
|
2016-10-28 13:20:27 +02:00
|
|
|
self.result_string = colorize("SKIP", YELLOW)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2018-09-19 15:01:47 +02:00
|
|
|
self.send_result_through_pipe(test, SKIP)
|
|
|
|
|
2018-09-21 13:55:16 +02:00
|
|
|
def symlink_failed(self):
|
|
|
|
if self.current_test_case_info:
|
2017-08-15 07:09:02 +02:00
|
|
|
try:
|
2018-11-08 11:21:39 +01:00
|
|
|
failed_dir = os.getenv('FAILED_DIR')
|
2018-09-21 13:55:16 +02:00
|
|
|
link_path = os.path.join(
|
|
|
|
failed_dir,
|
|
|
|
'%s-FAILED' %
|
|
|
|
os.path.basename(self.current_test_case_info.tempdir))
|
|
|
|
if self.current_test_case_info.logger:
|
|
|
|
self.current_test_case_info.logger.debug(
|
|
|
|
"creating a link to the failed test")
|
|
|
|
self.current_test_case_info.logger.debug(
|
|
|
|
"os.symlink(%s, %s)" %
|
|
|
|
(self.current_test_case_info.tempdir, link_path))
|
2018-07-16 14:22:01 +02:00
|
|
|
if os.path.exists(link_path):
|
2018-09-21 13:55:16 +02:00
|
|
|
if self.current_test_case_info.logger:
|
|
|
|
self.current_test_case_info.logger.debug(
|
|
|
|
'symlink already exists')
|
2018-07-16 14:22:01 +02:00
|
|
|
else:
|
2018-09-21 13:55:16 +02:00
|
|
|
os.symlink(self.current_test_case_info.tempdir, link_path)
|
2018-07-16 14:22:01 +02:00
|
|
|
|
2017-08-15 07:09:02 +02:00
|
|
|
except Exception as e:
|
2018-09-21 13:55:16 +02:00
|
|
|
if self.current_test_case_info.logger:
|
|
|
|
self.current_test_case_info.logger.error(e)
|
2017-08-15 07:09:02 +02:00
|
|
|
|
2018-09-19 15:01:47 +02:00
|
|
|
def send_result_through_pipe(self, test, result):
|
|
|
|
if hasattr(self, 'test_framework_result_pipe'):
|
|
|
|
pipe = self.test_framework_result_pipe
|
2018-08-30 10:51:45 +02:00
|
|
|
if pipe:
|
2018-09-19 15:01:47 +02:00
|
|
|
pipe.send((test.id(), result))
|
2018-08-30 10:51:45 +02:00
|
|
|
|
2018-09-21 13:55:16 +02:00
|
|
|
def log_error(self, test, err, fn_name):
|
|
|
|
if self.current_test_case_info:
|
|
|
|
if isinstance(test, unittest.suite._ErrorHolder):
|
|
|
|
test_name = test.description
|
|
|
|
else:
|
|
|
|
test_name = '%s.%s(%s)' % (test.__class__.__name__,
|
|
|
|
test._testMethodName,
|
|
|
|
test._testMethodDoc)
|
|
|
|
self.current_test_case_info.logger.debug(
|
|
|
|
"--- %s() %s called, err is %s" %
|
|
|
|
(fn_name, test_name, err))
|
|
|
|
self.current_test_case_info.logger.debug(
|
|
|
|
"formatted exception is:\n%s" %
|
|
|
|
"".join(format_exception(*err)))
|
|
|
|
|
|
|
|
def add_error(self, test, err, unittest_fn, error_type):
|
|
|
|
if error_type == FAIL:
|
|
|
|
self.log_error(test, err, 'addFailure')
|
|
|
|
error_type_str = colorize("FAIL", RED)
|
|
|
|
elif error_type == ERROR:
|
|
|
|
self.log_error(test, err, 'addError')
|
|
|
|
error_type_str = colorize("ERROR", RED)
|
|
|
|
else:
|
|
|
|
raise Exception('Error type %s cannot be used to record an '
|
|
|
|
'error or a failure' % error_type)
|
|
|
|
|
|
|
|
unittest_fn(self, test, err)
|
|
|
|
if self.current_test_case_info:
|
|
|
|
self.result_string = "%s [ temp dir used by test case: %s ]" % \
|
|
|
|
(error_type_str,
|
|
|
|
self.current_test_case_info.tempdir)
|
|
|
|
self.symlink_failed()
|
|
|
|
self.failed_test_cases_info.add(self.current_test_case_info)
|
|
|
|
if is_core_present(self.current_test_case_info.tempdir):
|
|
|
|
if not self.current_test_case_info.core_crash_test:
|
|
|
|
if isinstance(test, unittest.suite._ErrorHolder):
|
|
|
|
test_name = str(test)
|
|
|
|
else:
|
2019-03-06 08:23:58 -08:00
|
|
|
test_name = "'{!s}' ({!s})".format(
|
2018-09-21 13:55:16 +02:00
|
|
|
get_testcase_doc_name(test), test.id())
|
|
|
|
self.current_test_case_info.core_crash_test = test_name
|
|
|
|
self.core_crash_test_cases_info.add(
|
|
|
|
self.current_test_case_info)
|
|
|
|
else:
|
|
|
|
self.result_string = '%s [no temp dir]' % error_type_str
|
|
|
|
|
|
|
|
self.send_result_through_pipe(test, error_type)
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def addFailure(self, test, err):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Record a test failed result
|
|
|
|
|
|
|
|
:param test:
|
|
|
|
:param err: error message
|
|
|
|
|
|
|
|
"""
|
2018-09-21 13:55:16 +02:00
|
|
|
self.add_error(test, err, unittest.TestResult.addFailure, FAIL)
|
2018-09-19 15:01:47 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def addError(self, test, err):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Record a test error result
|
|
|
|
|
|
|
|
:param test:
|
|
|
|
:param err: error message
|
|
|
|
|
|
|
|
"""
|
2018-09-21 13:55:16 +02:00
|
|
|
self.add_error(test, err, unittest.TestResult.addError, ERROR)
|
2018-09-19 15:01:47 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def getDescription(self, test):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Get test description
|
|
|
|
|
|
|
|
:param test:
|
|
|
|
:returns: test description
|
|
|
|
|
|
|
|
"""
|
2018-11-22 10:01:09 +00:00
|
|
|
return get_test_description(self.descriptions, test)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def startTest(self, test):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Start a test
|
|
|
|
|
|
|
|
:param test:
|
|
|
|
|
|
|
|
"""
|
2019-01-13 16:09:10 -08:00
|
|
|
|
|
|
|
def print_header(test):
|
|
|
|
if not hasattr(test.__class__, '_header_printed'):
|
|
|
|
print(double_line_delim)
|
|
|
|
print(colorize(getdoc(test).splitlines()[0], GREEN))
|
|
|
|
print(double_line_delim)
|
|
|
|
test.__class__._header_printed = True
|
|
|
|
|
|
|
|
print_header(test)
|
2018-09-21 13:55:16 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
unittest.TestResult.startTest(self, test)
|
|
|
|
if self.verbosity > 0:
|
2016-10-11 11:47:09 +02:00
|
|
|
self.stream.writeln(
|
|
|
|
"Starting " + self.getDescription(test) + " ...")
|
|
|
|
self.stream.writeln(single_line_delim)
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def stopTest(self, test):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
2018-08-30 10:51:45 +02:00
|
|
|
Called when the given test has been run
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
:param test:
|
|
|
|
|
|
|
|
"""
|
2016-10-03 19:44:57 +02:00
|
|
|
unittest.TestResult.stopTest(self, test)
|
|
|
|
if self.verbosity > 0:
|
2016-10-11 11:47:09 +02:00
|
|
|
self.stream.writeln(single_line_delim)
|
2017-01-13 07:25:25 +01:00
|
|
|
self.stream.writeln("%-73s%s" % (self.getDescription(test),
|
2017-01-04 12:58:53 +01:00
|
|
|
self.result_string))
|
2016-10-11 11:47:09 +02:00
|
|
|
self.stream.writeln(single_line_delim)
|
2016-10-03 19:44:57 +02:00
|
|
|
else:
|
2017-01-13 07:25:25 +01:00
|
|
|
self.stream.writeln("%-73s%s" % (self.getDescription(test),
|
2017-01-04 12:58:53 +01:00
|
|
|
self.result_string))
|
2018-09-19 15:01:47 +02:00
|
|
|
|
|
|
|
self.send_result_through_pipe(test, TEST_RUN)
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
def printErrors(self):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Print errors from running the test case
|
|
|
|
"""
|
2018-11-16 17:28:56 +01:00
|
|
|
if len(self.errors) > 0 or len(self.failures) > 0:
|
|
|
|
self.stream.writeln()
|
|
|
|
self.printErrorList('ERROR', self.errors)
|
|
|
|
self.printErrorList('FAIL', self.failures)
|
|
|
|
|
|
|
|
# ^^ that is the last output from unittest before summary
|
|
|
|
if not self.runner.print_summary:
|
|
|
|
devnull = unittest.runner._WritelnDecorator(open(os.devnull, 'w'))
|
|
|
|
self.stream = devnull
|
|
|
|
self.runner.stream = devnull
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def printErrorList(self, flavour, errors):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Print error list to the output stream together with error type
|
|
|
|
and test case description.
|
|
|
|
|
|
|
|
:param flavour: error type
|
|
|
|
:param errors: iterable errors
|
|
|
|
|
|
|
|
"""
|
2016-10-03 19:44:57 +02:00
|
|
|
for test, err in errors:
|
2016-10-11 11:47:09 +02:00
|
|
|
self.stream.writeln(double_line_delim)
|
|
|
|
self.stream.writeln("%s: %s" %
|
|
|
|
(flavour, self.getDescription(test)))
|
|
|
|
self.stream.writeln(single_line_delim)
|
2016-10-03 19:44:57 +02:00
|
|
|
self.stream.writeln("%s" % err)
|
|
|
|
|
|
|
|
|
|
|
|
class VppTestRunner(unittest.TextTestRunner):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
2017-02-03 07:29:43 +01:00
|
|
|
A basic test runner implementation which prints results to standard error.
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
2018-12-09 15:37:04 -08:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
@property
|
|
|
|
def resultclass(self):
|
|
|
|
"""Class maintaining the results of the tests"""
|
|
|
|
return VppTestResult
|
|
|
|
|
2018-07-16 14:22:01 +02:00
|
|
|
def __init__(self, keep_alive_pipe=None, descriptions=True, verbosity=1,
|
2018-09-19 15:01:47 +02:00
|
|
|
result_pipe=None, failfast=False, buffer=False,
|
2019-01-13 21:32:37 -08:00
|
|
|
resultclass=None, print_summary=True, **kwargs):
|
2017-01-17 13:42:48 +01:00
|
|
|
# ignore stream setting here, use hard-coded stdout to be in sync
|
|
|
|
# with prints from VppTestCase methods ...
|
|
|
|
super(VppTestRunner, self).__init__(sys.stdout, descriptions,
|
|
|
|
verbosity, failfast, buffer,
|
2019-01-13 21:32:37 -08:00
|
|
|
resultclass, **kwargs)
|
2018-11-21 13:20:43 +01:00
|
|
|
KeepAliveReporter.pipe = keep_alive_pipe
|
2017-02-03 07:29:43 +01:00
|
|
|
|
2018-11-16 17:28:56 +01:00
|
|
|
self.orig_stream = self.stream
|
|
|
|
self.resultclass.test_framework_result_pipe = result_pipe
|
|
|
|
|
|
|
|
self.print_summary = print_summary
|
|
|
|
|
|
|
|
def _makeResult(self):
|
|
|
|
return self.resultclass(self.stream,
|
|
|
|
self.descriptions,
|
|
|
|
self.verbosity,
|
|
|
|
self)
|
2018-08-30 10:51:45 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def run(self, test):
|
2016-10-11 11:47:09 +02:00
|
|
|
"""
|
|
|
|
Run the tests
|
|
|
|
|
|
|
|
:param test:
|
|
|
|
|
|
|
|
"""
|
2017-06-07 08:19:47 +02:00
|
|
|
faulthandler.enable() # emit stack trace to stderr if killed by signal
|
2018-07-16 14:22:01 +02:00
|
|
|
|
|
|
|
result = super(VppTestRunner, self).run(test)
|
2018-11-16 17:28:56 +01:00
|
|
|
if not self.print_summary:
|
|
|
|
self.stream = self.orig_stream
|
|
|
|
result.stream = self.orig_stream
|
2018-07-16 14:22:01 +02:00
|
|
|
return result
|
2017-10-16 04:20:13 -07:00
|
|
|
|
|
|
|
|
|
|
|
class Worker(Thread):
|
2018-02-26 14:40:13 -05:00
|
|
|
def __init__(self, args, logger, env={}):
|
2017-10-16 04:20:13 -07:00
|
|
|
self.logger = logger
|
|
|
|
self.args = args
|
|
|
|
self.result = None
|
2018-02-26 14:40:13 -05:00
|
|
|
self.env = copy.deepcopy(env)
|
2017-10-16 04:20:13 -07:00
|
|
|
super(Worker, self).__init__()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
executable = self.args[0]
|
|
|
|
self.logger.debug("Running executable w/args `%s'" % self.args)
|
|
|
|
env = os.environ.copy()
|
2018-02-16 18:31:56 -05:00
|
|
|
env.update(self.env)
|
2017-10-16 04:20:13 -07:00
|
|
|
env["CK_LOG_FILE_NAME"] = "-"
|
|
|
|
self.process = subprocess.Popen(
|
|
|
|
self.args, shell=False, env=env, preexec_fn=os.setpgrp,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
out, err = self.process.communicate()
|
|
|
|
self.logger.debug("Finished running `%s'" % executable)
|
|
|
|
self.logger.info("Return code is `%s'" % self.process.returncode)
|
|
|
|
self.logger.info(single_line_delim)
|
|
|
|
self.logger.info("Executable `%s' wrote to stdout:" % executable)
|
|
|
|
self.logger.info(single_line_delim)
|
|
|
|
self.logger.info(out)
|
|
|
|
self.logger.info(single_line_delim)
|
|
|
|
self.logger.info("Executable `%s' wrote to stderr:" % executable)
|
|
|
|
self.logger.info(single_line_delim)
|
2018-02-22 19:21:27 +01:00
|
|
|
self.logger.info(err)
|
2017-10-16 04:20:13 -07:00
|
|
|
self.logger.info(single_line_delim)
|
|
|
|
self.result = self.process.returncode
|
2019-01-13 16:09:10 -08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
pass
|