Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 2 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 3 | from __future__ import print_function |
| 4 | import gc |
Paul Vinciguerra | 72f0004 | 2018-11-25 11:05:13 -0800 | [diff] [blame] | 5 | import sys |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 6 | import os |
| 7 | import select |
| 8 | import unittest |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 9 | import tempfile |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 10 | import time |
Paul Vinciguerra | 72f0004 | 2018-11-25 11:05:13 -0800 | [diff] [blame] | 11 | import faulthandler |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 12 | import random |
| 13 | import copy |
Paul Vinciguerra | 72f0004 | 2018-11-25 11:05:13 -0800 | [diff] [blame] | 14 | import psutil |
juraj.linkes | 68ebc83 | 2018-11-29 09:37:08 +0100 | [diff] [blame] | 15 | import platform |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 16 | from collections import deque |
| 17 | from threading import Thread, Event |
| 18 | from inspect import getdoc, isclass |
| 19 | from traceback import format_exception |
| 20 | from logging import FileHandler, DEBUG, Formatter |
| 21 | from scapy.packet import Raw |
| 22 | from hook import StepHook, PollHook, VppDiedError |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 23 | from vpp_config import VppTestCaseVppConfig |
| 24 | from vpp_interface import VppInterface |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 25 | from vpp_sub_interface import VppSubInterface |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 26 | from vpp_pg_interface import VppPGInterface |
Ole Trøan | 162989e | 2018-11-26 10:27:50 +0000 | [diff] [blame] | 27 | from vpp_lo_interface import VppLoInterface |
| 28 | from vpp_papi_provider import VppPapiProvider |
| 29 | from vpp_papi.vpp_stats import VPPStats |
| 30 | from log import RED, GREEN, YELLOW, double_line_delim, single_line_delim, \ |
| 31 | get_logger, colorize |
| 32 | from vpp_object import VppObjectRegistry |
| 33 | from util import ppp, is_core_present |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 34 | from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror |
| 35 | from scapy.layers.inet6 import ICMPv6DestUnreach, ICMPv6EchoRequest |
| 36 | from scapy.layers.inet6 import ICMPv6EchoReply |
Paul Vinciguerra | 6919b0d | 2018-12-09 15:37:04 -0800 | [diff] [blame] | 37 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 38 | if os.name == 'posix' and sys.version_info[0] < 3: |
| 39 | # using subprocess32 is recommended by python official documentation |
| 40 | # @ https://docs.python.org/2/library/subprocess.html |
| 41 | import subprocess32 as subprocess |
| 42 | else: |
| 43 | import subprocess |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 44 | |
Paul Vinciguerra | 852f5ef | 2018-12-15 10:16:35 -0800 | [diff] [blame] | 45 | # Python2/3 compatible |
| 46 | try: |
| 47 | input = raw_input |
| 48 | except NameError: |
| 49 | pass |
| 50 | |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 51 | PASS = 0 |
| 52 | FAIL = 1 |
| 53 | ERROR = 2 |
| 54 | SKIP = 3 |
| 55 | TEST_RUN = 4 |
| 56 | |
Klement Sekera | ebbaf55 | 2018-02-17 13:41:33 +0100 | [diff] [blame] | 57 | debug_framework = False |
| 58 | if os.getenv('TEST_DEBUG', "0") == "1": |
| 59 | debug_framework = True |
| 60 | import debug_internal |
| 61 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 62 | """ |
| 63 | Test framework module. |
| 64 | |
| 65 | The module provides a set of tools for constructing and running tests and |
| 66 | representing the results. |
| 67 | """ |
| 68 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 69 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 70 | class _PacketInfo(object): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 71 | """Private class to create packet info object. |
| 72 | |
| 73 | Help process information about the next packet. |
| 74 | Set variables to default values. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 75 | """ |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 76 | #: Store the index of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 77 | index = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 78 | #: Store the index of the source packet generator interface of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 79 | src = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 80 | #: Store the index of the destination packet generator interface |
| 81 | #: of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 82 | dst = -1 |
Pavel Kotucek | 59dda06 | 2017-03-02 15:22:47 +0100 | [diff] [blame] | 83 | #: Store expected ip version |
| 84 | ip = -1 |
| 85 | #: Store expected upper protocol |
| 86 | proto = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 87 | #: Store the copy of the former packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 88 | data = None |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 89 | |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 90 | def __eq__(self, other): |
| 91 | index = self.index == other.index |
| 92 | src = self.src == other.src |
| 93 | dst = self.dst == other.dst |
| 94 | data = self.data == other.data |
| 95 | return index and src and dst and data |
| 96 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 97 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 98 | def pump_output(testclass): |
| 99 | """ pump output from vpp stdout/stderr to proper queues """ |
Klement Sekera | 6a6f4f7 | 2017-11-09 09:16:39 +0100 | [diff] [blame] | 100 | stdout_fragment = "" |
| 101 | stderr_fragment = "" |
Neale Ranns | 1678236 | 2018-07-23 05:35:56 -0400 | [diff] [blame] | 102 | while not testclass.pump_thread_stop_flag.is_set(): |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 103 | readable = select.select([testclass.vpp.stdout.fileno(), |
| 104 | testclass.vpp.stderr.fileno(), |
| 105 | testclass.pump_thread_wakeup_pipe[0]], |
| 106 | [], [])[0] |
| 107 | if testclass.vpp.stdout.fileno() in readable: |
Klement Sekera | 6a6f4f7 | 2017-11-09 09:16:39 +0100 | [diff] [blame] | 108 | read = os.read(testclass.vpp.stdout.fileno(), 102400) |
| 109 | if len(read) > 0: |
| 110 | split = read.splitlines(True) |
| 111 | if len(stdout_fragment) > 0: |
| 112 | split[0] = "%s%s" % (stdout_fragment, split[0]) |
| 113 | if len(split) > 0 and split[-1].endswith("\n"): |
| 114 | limit = None |
| 115 | else: |
| 116 | limit = -1 |
| 117 | stdout_fragment = split[-1] |
| 118 | testclass.vpp_stdout_deque.extend(split[:limit]) |
| 119 | if not testclass.cache_vpp_output: |
| 120 | for line in split[:limit]: |
| 121 | testclass.logger.debug( |
| 122 | "VPP STDOUT: %s" % line.rstrip("\n")) |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 123 | if testclass.vpp.stderr.fileno() in readable: |
Klement Sekera | 6a6f4f7 | 2017-11-09 09:16:39 +0100 | [diff] [blame] | 124 | read = os.read(testclass.vpp.stderr.fileno(), 102400) |
| 125 | if len(read) > 0: |
| 126 | split = read.splitlines(True) |
| 127 | if len(stderr_fragment) > 0: |
| 128 | split[0] = "%s%s" % (stderr_fragment, split[0]) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 129 | if len(split) > 0 and split[-1].endswith(b"\n"): |
Klement Sekera | 6a6f4f7 | 2017-11-09 09:16:39 +0100 | [diff] [blame] | 130 | limit = None |
| 131 | else: |
| 132 | limit = -1 |
| 133 | stderr_fragment = split[-1] |
| 134 | testclass.vpp_stderr_deque.extend(split[:limit]) |
| 135 | if not testclass.cache_vpp_output: |
| 136 | for line in split[:limit]: |
| 137 | testclass.logger.debug( |
| 138 | "VPP STDERR: %s" % line.rstrip("\n")) |
Paul Vinciguerra | 6919b0d | 2018-12-09 15:37:04 -0800 | [diff] [blame] | 139 | # ignoring the dummy pipe here intentionally - the |
| 140 | # flag will take care of properly terminating the loop |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 141 | |
| 142 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 143 | def _is_skip_aarch64_set(): |
juraj.linkes | 68ebc83 | 2018-11-29 09:37:08 +0100 | [diff] [blame] | 144 | return os.getenv('SKIP_AARCH64', 'n').lower() in ('yes', 'y', '1') |
| 145 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 146 | is_skip_aarch64_set = _is_skip_aarch64_set() |
juraj.linkes | 68ebc83 | 2018-11-29 09:37:08 +0100 | [diff] [blame] | 147 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 148 | |
| 149 | def _is_platform_aarch64(): |
juraj.linkes | 68ebc83 | 2018-11-29 09:37:08 +0100 | [diff] [blame] | 150 | return platform.machine() == 'aarch64' |
| 151 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 152 | is_platform_aarch64 = _is_platform_aarch64() |
juraj.linkes | 68ebc83 | 2018-11-29 09:37:08 +0100 | [diff] [blame] | 153 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 154 | |
| 155 | def _running_extended_tests(): |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 156 | s = os.getenv("EXTENDED_TESTS", "n") |
| 157 | return True if s.lower() in ("y", "yes", "1") else False |
Klement Sekera | 8713493 | 2017-03-07 11:39:27 +0100 | [diff] [blame] | 158 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 159 | running_extended_tests = _running_extended_tests() |
Klement Sekera | 8713493 | 2017-03-07 11:39:27 +0100 | [diff] [blame] | 160 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 161 | |
| 162 | def _running_on_centos(): |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 163 | os_id = os.getenv("OS_ID", "") |
| 164 | return True if "centos" in os_id.lower() else False |
Klement Sekera | d3e671e | 2017-09-29 12:36:37 +0200 | [diff] [blame] | 165 | |
Paul Vinciguerra | defde0f | 2018-12-06 07:46:13 -0800 | [diff] [blame] | 166 | running_on_centos = _running_on_centos |
| 167 | |
Klement Sekera | d3e671e | 2017-09-29 12:36:37 +0200 | [diff] [blame] | 168 | |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 169 | class KeepAliveReporter(object): |
| 170 | """ |
| 171 | Singleton object which reports test start to parent process |
| 172 | """ |
| 173 | _shared_state = {} |
| 174 | |
| 175 | def __init__(self): |
| 176 | self.__dict__ = self._shared_state |
Paul Vinciguerra | c7b03fe | 2018-11-18 08:17:34 -0800 | [diff] [blame] | 177 | self._pipe = None |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 178 | |
| 179 | @property |
| 180 | def pipe(self): |
| 181 | return self._pipe |
| 182 | |
| 183 | @pipe.setter |
| 184 | def pipe(self, pipe): |
Paul Vinciguerra | c7b03fe | 2018-11-18 08:17:34 -0800 | [diff] [blame] | 185 | if self._pipe is not None: |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 186 | raise Exception("Internal error - pipe should only be set once.") |
| 187 | self._pipe = pipe |
| 188 | |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 189 | def send_keep_alive(self, test, desc=None): |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 190 | """ |
| 191 | Write current test tmpdir & desc to keep-alive pipe to signal liveness |
| 192 | """ |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 193 | if self.pipe is None: |
| 194 | # if not running forked.. |
| 195 | return |
| 196 | |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 197 | if isclass(test): |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 198 | desc = '%s (%s)' % (desc, unittest.util.strclass(test)) |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 199 | else: |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 200 | desc = test.id() |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 201 | |
Dave Wallace | e2efd12 | 2017-09-30 22:04:21 -0400 | [diff] [blame] | 202 | self.pipe.send((desc, test.vpp_bin, test.tempdir, test.vpp.pid)) |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 203 | |
| 204 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 205 | class VppTestCase(unittest.TestCase): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 206 | """This subclass is a base class for VPP test cases that are implemented as |
| 207 | classes. It provides methods to create and run test case. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 208 | """ |
| 209 | |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 210 | CLI_LISTEN_DEFAULT = 'localhost:5002' |
| 211 | config = VppTestCaseVppConfig() |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 212 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 213 | @property |
| 214 | def packet_infos(self): |
| 215 | """List of packet infos""" |
| 216 | return self._packet_infos |
| 217 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 218 | @classmethod |
| 219 | def get_packet_count_for_if_idx(cls, dst_if_index): |
| 220 | """Get the number of packet info for specified destination if index""" |
| 221 | if dst_if_index in cls._packet_count_for_dst_if_idx: |
| 222 | return cls._packet_count_for_dst_if_idx[dst_if_index] |
| 223 | else: |
| 224 | return 0 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 225 | |
| 226 | @classmethod |
| 227 | def instance(cls): |
| 228 | """Return the instance of this testcase""" |
| 229 | return cls.test_instance |
| 230 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 231 | @classmethod |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 232 | def set_debug_flags(cls, d): |
| 233 | cls.debug_core = False |
| 234 | cls.debug_gdb = False |
| 235 | cls.debug_gdbserver = False |
| 236 | if d is None: |
| 237 | return |
| 238 | dl = d.lower() |
| 239 | if dl == "core": |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 240 | cls.debug_core = True |
| 241 | elif dl == "gdb": |
| 242 | cls.debug_gdb = True |
| 243 | elif dl == "gdbserver": |
| 244 | cls.debug_gdbserver = True |
| 245 | else: |
| 246 | raise Exception("Unrecognized DEBUG option: '%s'" % d) |
| 247 | |
Paul Vinciguerra | 86ebba6 | 2018-11-21 09:28:32 -0800 | [diff] [blame] | 248 | @staticmethod |
| 249 | def get_least_used_cpu(): |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 250 | cpu_usage_list = [set(range(psutil.cpu_count()))] |
| 251 | vpp_processes = [p for p in psutil.process_iter(attrs=['pid', 'name']) |
| 252 | if 'vpp_main' == p.info['name']] |
| 253 | for vpp_process in vpp_processes: |
| 254 | for cpu_usage_set in cpu_usage_list: |
| 255 | try: |
| 256 | cpu_num = vpp_process.cpu_num() |
| 257 | if cpu_num in cpu_usage_set: |
| 258 | cpu_usage_set_index = cpu_usage_list.index( |
| 259 | cpu_usage_set) |
| 260 | if cpu_usage_set_index == len(cpu_usage_list) - 1: |
| 261 | cpu_usage_list.append({cpu_num}) |
| 262 | else: |
| 263 | cpu_usage_list[cpu_usage_set_index + 1].add( |
| 264 | cpu_num) |
| 265 | cpu_usage_set.remove(cpu_num) |
| 266 | break |
| 267 | except psutil.NoSuchProcess: |
| 268 | pass |
| 269 | |
| 270 | for cpu_usage_set in cpu_usage_list: |
| 271 | if len(cpu_usage_set) > 0: |
| 272 | min_usage_set = cpu_usage_set |
| 273 | break |
| 274 | |
| 275 | return random.choice(tuple(min_usage_set)) |
| 276 | |
Paul Vinciguerra | 86ebba6 | 2018-11-21 09:28:32 -0800 | [diff] [blame] | 277 | @classmethod |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 278 | def print_header(cls): |
| 279 | if not hasattr(cls, '_header_printed'): |
| 280 | print(double_line_delim) |
| 281 | print(colorize(getdoc(cls).splitlines()[0], GREEN)) |
| 282 | print(double_line_delim) |
| 283 | cls._header_printed = True |
| 284 | |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 285 | @classmethod |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 286 | def setUpConstants(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 287 | """ Set-up the test case class based on environment variables """ |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 288 | s = os.getenv("STEP", "n") |
| 289 | cls.step = True if s.lower() in ("y", "yes", "1") else False |
| 290 | d = os.getenv("DEBUG", None) |
| 291 | c = os.getenv("CACHE_OUTPUT", "1") |
| 292 | cls.cache_vpp_output = False if c.lower() in ("n", "no", "0") else True |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 293 | cls.set_debug_flags(d) |
Klement Sekera | b8c72a4 | 2018-11-08 11:21:39 +0100 | [diff] [blame] | 294 | cls.vpp_bin = os.getenv('VPP_BIN', "vpp") |
| 295 | cls.plugin_path = os.getenv('VPP_PLUGIN_PATH') |
Klement Sekera | 47e275b | 2017-03-21 08:21:25 +0100 | [diff] [blame] | 296 | cls.extern_plugin_path = os.getenv('EXTERN_PLUGINS') |
| 297 | plugin_path = None |
| 298 | if cls.plugin_path is not None: |
| 299 | if cls.extern_plugin_path is not None: |
| 300 | plugin_path = "%s:%s" % ( |
| 301 | cls.plugin_path, cls.extern_plugin_path) |
Klement Sekera | 6abbc28 | 2017-03-24 05:47:15 +0100 | [diff] [blame] | 302 | else: |
| 303 | plugin_path = cls.plugin_path |
Klement Sekera | 47e275b | 2017-03-21 08:21:25 +0100 | [diff] [blame] | 304 | elif cls.extern_plugin_path is not None: |
| 305 | plugin_path = cls.extern_plugin_path |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 306 | |
Klement Sekera | 01bbbe9 | 2016-11-02 09:25:05 +0100 | [diff] [blame] | 307 | if cls.step or cls.debug_gdb or cls.debug_gdbserver: |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 308 | cls.config.add('unix', 'cli-listen', cls.CLI_LISTEN_DEFAULT) |
| 309 | |
Klement Sekera | 80a7f0a | 2017-03-02 11:27:11 +0100 | [diff] [blame] | 310 | coredump_size = None |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 311 | size = os.getenv("COREDUMP_SIZE") |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 312 | cls.config.add('unix', 'coredump-size', |
| 313 | size if size is not None else 'unlimited') |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 314 | |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 315 | cls.config.add('unix', 'runtime-dir', cls.tempdir) |
| 316 | cls.config.add('api-segment', 'prefix', cls.shm_prefix) |
| 317 | cls.config.add('cpu', 'main-core', str(cls.get_least_used_cpu())) |
| 318 | cls.config.add('statseg', 'socket-name', cls.stats_sock) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 319 | |
Klement Sekera | 47e275b | 2017-03-21 08:21:25 +0100 | [diff] [blame] | 320 | if plugin_path is not None: |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 321 | cls.config.add('plugins', 'path', plugin_path) |
| 322 | cls.config.add_plugin('dpdk_plugin.so', 'disable') |
| 323 | cls.config.add_plugin('unittest_plugin.so', 'enable') |
| 324 | |
| 325 | cls.vpp_cmdline = [cls.vpp_bin] + cls.config.shlex() |
Klement Sekera | f37c3ba | 2018-11-08 11:24:34 +0100 | [diff] [blame] | 326 | cls.logger.info("vpp_cmdline args: %s" % cls.vpp_cmdline) |
| 327 | cls.logger.info("vpp_cmdline: %s" % " ".join(cls.vpp_cmdline)) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 328 | |
| 329 | @classmethod |
| 330 | def wait_for_enter(cls): |
| 331 | if cls.debug_gdbserver: |
| 332 | print(double_line_delim) |
| 333 | print("Spawned GDB server with PID: %d" % cls.vpp.pid) |
| 334 | elif cls.debug_gdb: |
| 335 | print(double_line_delim) |
| 336 | print("Spawned VPP with PID: %d" % cls.vpp.pid) |
| 337 | else: |
| 338 | cls.logger.debug("Spawned VPP with PID: %d" % cls.vpp.pid) |
| 339 | return |
| 340 | print(single_line_delim) |
| 341 | print("You can debug the VPP using e.g.:") |
| 342 | if cls.debug_gdbserver: |
| 343 | print("gdb " + cls.vpp_bin + " -ex 'target remote localhost:7777'") |
| 344 | print("Now is the time to attach a gdb by running the above " |
| 345 | "command, set up breakpoints etc. and then resume VPP from " |
| 346 | "within gdb by issuing the 'continue' command") |
| 347 | elif cls.debug_gdb: |
| 348 | print("gdb " + cls.vpp_bin + " -ex 'attach %s'" % cls.vpp.pid) |
| 349 | print("Now is the time to attach a gdb by running the above " |
| 350 | "command and set up breakpoints etc.") |
| 351 | print(single_line_delim) |
Paul Vinciguerra | 852f5ef | 2018-12-15 10:16:35 -0800 | [diff] [blame] | 352 | input("Press ENTER to continue running the testcase...") |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 353 | |
| 354 | @classmethod |
| 355 | def run_vpp(cls): |
| 356 | cmdline = cls.vpp_cmdline |
| 357 | |
| 358 | if cls.debug_gdbserver: |
Klement Sekera | 931be3a | 2016-11-03 05:36:01 +0100 | [diff] [blame] | 359 | gdbserver = '/usr/bin/gdbserver' |
| 360 | if not os.path.isfile(gdbserver) or \ |
| 361 | not os.access(gdbserver, os.X_OK): |
| 362 | raise Exception("gdbserver binary '%s' does not exist or is " |
| 363 | "not executable" % gdbserver) |
| 364 | |
| 365 | cmdline = [gdbserver, 'localhost:7777'] + cls.vpp_cmdline |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 366 | cls.logger.info("Gdbserver cmdline is %s", " ".join(cmdline)) |
| 367 | |
Klement Sekera | 931be3a | 2016-11-03 05:36:01 +0100 | [diff] [blame] | 368 | try: |
| 369 | cls.vpp = subprocess.Popen(cmdline, |
| 370 | stdout=subprocess.PIPE, |
| 371 | stderr=subprocess.PIPE, |
| 372 | bufsize=1) |
Paul Vinciguerra | 61e63bf | 2018-11-24 21:19:38 -0800 | [diff] [blame] | 373 | except subprocess.CalledProcessError as e: |
Klement Sekera | 931be3a | 2016-11-03 05:36:01 +0100 | [diff] [blame] | 374 | cls.logger.critical("Couldn't start vpp: %s" % e) |
| 375 | raise |
| 376 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 377 | cls.wait_for_enter() |
Pierre Pfister | cd8e318 | 2016-10-07 16:30:03 +0100 | [diff] [blame] | 378 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 379 | @classmethod |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 380 | def wait_for_stats_socket(cls): |
| 381 | deadline = time.time() + 3 |
Paul Vinciguerra | dd4b2bb | 2018-11-15 08:13:03 -0800 | [diff] [blame] | 382 | ok = False |
| 383 | while time.time() < deadline or \ |
| 384 | cls.debug_gdb or cls.debug_gdbserver: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 385 | if os.path.exists(cls.stats_sock): |
Paul Vinciguerra | dd4b2bb | 2018-11-15 08:13:03 -0800 | [diff] [blame] | 386 | ok = True |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 387 | break |
Paul Vinciguerra | dd4b2bb | 2018-11-15 08:13:03 -0800 | [diff] [blame] | 388 | time.sleep(0.8) |
| 389 | if not ok: |
| 390 | cls.logger.critical("Couldn't stat : {}".format(cls.stats_sock)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 391 | |
| 392 | @classmethod |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 393 | def setUpClass(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 394 | """ |
| 395 | Perform class setup before running the testcase |
| 396 | Remove shared memory files, start vpp and connect the vpp-api |
| 397 | """ |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 398 | gc.collect() # run garbage collection first |
Klement Sekera | 96867ba | 2018-02-02 11:27:53 +0100 | [diff] [blame] | 399 | random.seed() |
Paul Vinciguerra | 86ebba6 | 2018-11-21 09:28:32 -0800 | [diff] [blame] | 400 | cls.print_header() |
juraj.linkes | dfb5f2a | 2018-11-09 11:58:54 +0100 | [diff] [blame] | 401 | cls.logger = get_logger(cls.__name__) |
| 402 | if hasattr(cls, 'parallel_handler'): |
| 403 | cls.logger.addHandler(cls.parallel_handler) |
juraj.linkes | 3d9b92a | 2018-11-21 13:13:39 +0100 | [diff] [blame] | 404 | cls.logger.propagate = False |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 405 | cls.tempdir = tempfile.mkdtemp( |
Klement Sekera | f413bef | 2017-08-15 07:09:02 +0200 | [diff] [blame] | 406 | prefix='vpp-unittest-%s-' % cls.__name__) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 407 | cls.stats_sock = "%s/stats.sock" % cls.tempdir |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 408 | cls.file_handler = FileHandler("%s/log.txt" % cls.tempdir) |
| 409 | cls.file_handler.setFormatter( |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 410 | Formatter(fmt='%(asctime)s,%(msecs)03d %(message)s', |
| 411 | datefmt="%H:%M:%S")) |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 412 | cls.file_handler.setLevel(DEBUG) |
| 413 | cls.logger.addHandler(cls.file_handler) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 414 | cls.shm_prefix = os.path.basename(cls.tempdir) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 415 | os.chdir(cls.tempdir) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 416 | cls.logger.info("Temporary dir is %s, shm prefix is %s", |
| 417 | cls.tempdir, cls.shm_prefix) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 418 | cls.setUpConstants() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 419 | cls.reset_packet_infos() |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 420 | cls._captures = [] |
| 421 | cls._zombie_captures = [] |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 422 | cls.verbose = 0 |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 423 | cls.vpp_dead = False |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 424 | cls.registry = VppObjectRegistry() |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 425 | cls.vpp_startup_failed = False |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 426 | cls.reporter = KeepAliveReporter() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 427 | # need to catch exceptions here because if we raise, then the cleanup |
| 428 | # doesn't get called and we might end with a zombie vpp |
| 429 | try: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 430 | cls.run_vpp() |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 431 | cls.reporter.send_keep_alive(cls, 'setUpClass') |
| 432 | VppTestResult.current_test_case_info = TestCaseInfo( |
| 433 | cls.logger, cls.tempdir, cls.vpp.pid, cls.vpp_bin) |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 434 | cls.vpp_stdout_deque = deque() |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 435 | cls.vpp_stderr_deque = deque() |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 436 | cls.pump_thread_stop_flag = Event() |
| 437 | cls.pump_thread_wakeup_pipe = os.pipe() |
| 438 | cls.pump_thread = Thread(target=pump_output, args=(cls,)) |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 439 | cls.pump_thread.daemon = True |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 440 | cls.pump_thread.start() |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 441 | if cls.debug_gdb or cls.debug_gdbserver: |
| 442 | read_timeout = 0 |
| 443 | else: |
| 444 | read_timeout = 5 |
| 445 | cls.vapi = VppPapiProvider(cls.shm_prefix, cls.shm_prefix, cls, |
| 446 | read_timeout) |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 447 | if cls.step: |
| 448 | hook = StepHook(cls) |
| 449 | else: |
| 450 | hook = PollHook(cls) |
| 451 | cls.vapi.register_hook(hook) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 452 | cls.wait_for_stats_socket() |
| 453 | cls.statistics = VPPStats(socketname=cls.stats_sock) |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 454 | try: |
| 455 | hook.poll_vpp() |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 456 | except VppDiedError: |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 457 | cls.vpp_startup_failed = True |
| 458 | cls.logger.critical( |
| 459 | "VPP died shortly after startup, check the" |
| 460 | " output to standard error for possible cause") |
| 461 | raise |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 462 | try: |
| 463 | cls.vapi.connect() |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 464 | except Exception: |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 465 | try: |
| 466 | cls.vapi.disconnect() |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 467 | except Exception: |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 468 | pass |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 469 | if cls.debug_gdbserver: |
| 470 | print(colorize("You're running VPP inside gdbserver but " |
| 471 | "VPP-API connection failed, did you forget " |
| 472 | "to 'continue' VPP from within gdb?", RED)) |
| 473 | raise |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 474 | except Exception: |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 475 | try: |
| 476 | cls.quit() |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 477 | except Exception: |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 478 | pass |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 479 | raise |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 480 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 481 | @classmethod |
| 482 | def quit(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 483 | """ |
| 484 | Disconnect vpp-api, kill vpp and cleanup shared memory files |
| 485 | """ |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 486 | if (cls.debug_gdbserver or cls.debug_gdb) and hasattr(cls, 'vpp'): |
| 487 | cls.vpp.poll() |
| 488 | if cls.vpp.returncode is None: |
| 489 | print(double_line_delim) |
| 490 | print("VPP or GDB server is still running") |
| 491 | print(single_line_delim) |
Paul Vinciguerra | 852f5ef | 2018-12-15 10:16:35 -0800 | [diff] [blame] | 492 | input("When done debugging, press ENTER to kill the " |
| 493 | "process and finish running the testcase...") |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 494 | |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 495 | # first signal that we want to stop the pump thread, then wake it up |
| 496 | if hasattr(cls, 'pump_thread_stop_flag'): |
| 497 | cls.pump_thread_stop_flag.set() |
| 498 | if hasattr(cls, 'pump_thread_wakeup_pipe'): |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 499 | os.write(cls.pump_thread_wakeup_pipe[1], b'ding dong wake up') |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 500 | if hasattr(cls, 'pump_thread'): |
| 501 | cls.logger.debug("Waiting for pump thread to stop") |
| 502 | cls.pump_thread.join() |
| 503 | if hasattr(cls, 'vpp_stderr_reader_thread'): |
| 504 | cls.logger.debug("Waiting for stdderr pump to stop") |
| 505 | cls.vpp_stderr_reader_thread.join() |
| 506 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 507 | if hasattr(cls, 'vpp'): |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 508 | if hasattr(cls, 'vapi'): |
| 509 | cls.vapi.disconnect() |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 510 | del cls.vapi |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 511 | cls.vpp.poll() |
| 512 | if cls.vpp.returncode is None: |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 513 | cls.logger.debug("Sending TERM to vpp") |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 514 | cls.vpp.kill() |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 515 | cls.logger.debug("Waiting for vpp to die") |
| 516 | cls.vpp.communicate() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 517 | del cls.vpp |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 518 | |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 519 | if cls.vpp_startup_failed: |
| 520 | stdout_log = cls.logger.info |
| 521 | stderr_log = cls.logger.critical |
| 522 | else: |
| 523 | stdout_log = cls.logger.info |
| 524 | stderr_log = cls.logger.info |
| 525 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 526 | if hasattr(cls, 'vpp_stdout_deque'): |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 527 | stdout_log(single_line_delim) |
| 528 | stdout_log('VPP output to stdout while running %s:', cls.__name__) |
| 529 | stdout_log(single_line_delim) |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 530 | vpp_output = "".join(cls.vpp_stdout_deque) |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 531 | with open(cls.tempdir + '/vpp_stdout.txt', 'w') as f: |
| 532 | f.write(vpp_output) |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 533 | stdout_log('\n%s', vpp_output) |
| 534 | stdout_log(single_line_delim) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 535 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 536 | if hasattr(cls, 'vpp_stderr_deque'): |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 537 | stderr_log(single_line_delim) |
| 538 | stderr_log('VPP output to stderr while running %s:', cls.__name__) |
| 539 | stderr_log(single_line_delim) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 540 | vpp_output = "".join(str(cls.vpp_stderr_deque)) |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 541 | with open(cls.tempdir + '/vpp_stderr.txt', 'w') as f: |
| 542 | f.write(vpp_output) |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 543 | stderr_log('\n%s', vpp_output) |
| 544 | stderr_log(single_line_delim) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 545 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 546 | @classmethod |
| 547 | def tearDownClass(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 548 | """ Perform final cleanup after running all tests in this test-case """ |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 549 | cls.reporter.send_keep_alive(cls, 'tearDownClass') |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 550 | cls.quit() |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 551 | cls.file_handler.close() |
Klement Sekera | ebbaf55 | 2018-02-17 13:41:33 +0100 | [diff] [blame] | 552 | cls.reset_packet_infos() |
| 553 | if debug_framework: |
| 554 | debug_internal.on_tear_down_class(cls) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 555 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 556 | def tearDown(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 557 | """ Show various debug prints after each test """ |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 558 | self.logger.debug("--- tearDown() for %s.%s(%s) called ---" % |
| 559 | (self.__class__.__name__, self._testMethodName, |
| 560 | self._testMethodDoc)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 561 | if not self.vpp_dead: |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 562 | self.logger.debug(self.vapi.cli("show trace")) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 563 | self.logger.info(self.vapi.ppcli("show interface")) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 564 | self.logger.info(self.vapi.ppcli("show hardware")) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 565 | self.logger.info(self.statistics.set_errors_str()) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 566 | self.logger.info(self.vapi.ppcli("show run")) |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 567 | self.logger.info(self.vapi.ppcli("show log")) |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 568 | self.registry.remove_vpp_config(self.logger) |
Dave Wallace | 90c5572 | 2017-02-16 11:25:26 -0500 | [diff] [blame] | 569 | # Save/Dump VPP api trace log |
| 570 | api_trace = "vpp_api_trace.%s.log" % self._testMethodName |
| 571 | tmp_api_trace = "/tmp/%s" % api_trace |
| 572 | vpp_api_trace_log = "%s/%s" % (self.tempdir, api_trace) |
| 573 | self.logger.info(self.vapi.ppcli("api trace save %s" % api_trace)) |
| 574 | self.logger.info("Moving %s to %s\n" % (tmp_api_trace, |
| 575 | vpp_api_trace_log)) |
| 576 | os.rename(tmp_api_trace, vpp_api_trace_log) |
Dave Wallace | 5ba5837 | 2018-02-13 16:14:06 -0500 | [diff] [blame] | 577 | self.logger.info(self.vapi.ppcli("api trace custom-dump %s" % |
Dave Wallace | 90c5572 | 2017-02-16 11:25:26 -0500 | [diff] [blame] | 578 | vpp_api_trace_log)) |
Klement Sekera | 1b68640 | 2017-03-02 11:29:19 +0100 | [diff] [blame] | 579 | else: |
| 580 | self.registry.unregister_all(self.logger) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 581 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 582 | def setUp(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 583 | """ Clear trace before running each test""" |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 584 | self.reporter.send_keep_alive(self) |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 585 | self.logger.debug("--- setUp() for %s.%s(%s) called ---" % |
| 586 | (self.__class__.__name__, self._testMethodName, |
| 587 | self._testMethodDoc)) |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 588 | if self.vpp_dead: |
| 589 | raise Exception("VPP is dead when setting up the test") |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 590 | self.sleep(.1, "during setUp") |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 591 | self.vpp_stdout_deque.append( |
| 592 | "--- test setUp() for %s.%s(%s) starts here ---\n" % |
| 593 | (self.__class__.__name__, self._testMethodName, |
| 594 | self._testMethodDoc)) |
| 595 | self.vpp_stderr_deque.append( |
| 596 | "--- test setUp() for %s.%s(%s) starts here ---\n" % |
| 597 | (self.__class__.__name__, self._testMethodName, |
| 598 | self._testMethodDoc)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 599 | self.vapi.cli("clear trace") |
| 600 | # store the test instance inside the test class - so that objects |
| 601 | # holding the class can access instance methods (like assertEqual) |
| 602 | type(self).test_instance = self |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 603 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 604 | @classmethod |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 605 | def pg_enable_capture(cls, interfaces=None): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 606 | """ |
| 607 | Enable capture on packet-generator interfaces |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 608 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 609 | :param interfaces: iterable interface indexes (if None, |
| 610 | use self.pg_interfaces) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 611 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 612 | """ |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 613 | if interfaces is None: |
| 614 | interfaces = cls.pg_interfaces |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 615 | for i in interfaces: |
| 616 | i.enable_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 617 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 618 | @classmethod |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 619 | def register_capture(cls, cap_name): |
| 620 | """ Register a capture in the testclass """ |
| 621 | # add to the list of captures with current timestamp |
| 622 | cls._captures.append((time.time(), cap_name)) |
| 623 | # filter out from zombies |
| 624 | cls._zombie_captures = [(stamp, name) |
| 625 | for (stamp, name) in cls._zombie_captures |
| 626 | if name != cap_name] |
| 627 | |
| 628 | @classmethod |
| 629 | def pg_start(cls): |
| 630 | """ Remove any zombie captures and enable the packet generator """ |
| 631 | # how long before capture is allowed to be deleted - otherwise vpp |
| 632 | # crashes - 100ms seems enough (this shouldn't be needed at all) |
| 633 | capture_ttl = 0.1 |
| 634 | now = time.time() |
| 635 | for stamp, cap_name in cls._zombie_captures: |
| 636 | wait = stamp + capture_ttl - now |
| 637 | if wait > 0: |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 638 | cls.sleep(wait, "before deleting capture %s" % cap_name) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 639 | now = time.time() |
| 640 | cls.logger.debug("Removing zombie capture %s" % cap_name) |
| 641 | cls.vapi.cli('packet-generator delete %s' % cap_name) |
| 642 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 643 | cls.vapi.cli("trace add pg-input 50") # 50 is maximum |
| 644 | cls.vapi.cli('packet-generator enable') |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 645 | cls._zombie_captures = cls._captures |
| 646 | cls._captures = [] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 647 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 648 | @classmethod |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 649 | def create_pg_interfaces(cls, interfaces): |
| 650 | """ |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 651 | Create packet-generator interfaces. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 652 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 653 | :param interfaces: iterable indexes of the interfaces. |
| 654 | :returns: List of created interfaces. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 655 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 656 | """ |
| 657 | result = [] |
| 658 | for i in interfaces: |
| 659 | intf = VppPGInterface(cls, i) |
| 660 | setattr(cls, intf.name, intf) |
| 661 | result.append(intf) |
| 662 | cls.pg_interfaces = result |
| 663 | return result |
| 664 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 665 | @classmethod |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 666 | def create_loopback_interfaces(cls, count): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 667 | """ |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 668 | Create loopback interfaces. |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 669 | |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 670 | :param count: number of interfaces created. |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 671 | :returns: List of created interfaces. |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 672 | """ |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 673 | result = [VppLoInterface(cls) for i in range(count)] |
| 674 | for intf in result: |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 675 | setattr(cls, intf.name, intf) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 676 | cls.lo_interfaces = result |
| 677 | return result |
| 678 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 679 | @staticmethod |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 680 | def extend_packet(packet, size, padding=' '): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 681 | """ |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 682 | Extend packet to given size by padding with spaces or custom padding |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 683 | NOTE: Currently works only when Raw layer is present. |
| 684 | |
| 685 | :param packet: packet |
| 686 | :param size: target size |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 687 | :param padding: padding used to extend the payload |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 688 | |
| 689 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 690 | packet_len = len(packet) + 4 |
| 691 | extend = size - packet_len |
| 692 | if extend > 0: |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 693 | num = (extend / len(padding)) + 1 |
| 694 | packet[Raw].load += (padding * num)[:extend] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 695 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 696 | @classmethod |
| 697 | def reset_packet_infos(cls): |
| 698 | """ Reset the list of packet info objects and packet counts to zero """ |
| 699 | cls._packet_infos = {} |
| 700 | cls._packet_count_for_dst_if_idx = {} |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 701 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 702 | @classmethod |
| 703 | def create_packet_info(cls, src_if, dst_if): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 704 | """ |
| 705 | Create packet info object containing the source and destination indexes |
| 706 | and add it to the testcase's packet info list |
| 707 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 708 | :param VppInterface src_if: source interface |
| 709 | :param VppInterface dst_if: destination interface |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 710 | |
| 711 | :returns: _PacketInfo object |
| 712 | |
| 713 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 714 | info = _PacketInfo() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 715 | info.index = len(cls._packet_infos) |
| 716 | info.src = src_if.sw_if_index |
| 717 | info.dst = dst_if.sw_if_index |
| 718 | if isinstance(dst_if, VppSubInterface): |
| 719 | dst_idx = dst_if.parent.sw_if_index |
| 720 | else: |
| 721 | dst_idx = dst_if.sw_if_index |
| 722 | if dst_idx in cls._packet_count_for_dst_if_idx: |
| 723 | cls._packet_count_for_dst_if_idx[dst_idx] += 1 |
| 724 | else: |
| 725 | cls._packet_count_for_dst_if_idx[dst_idx] = 1 |
| 726 | cls._packet_infos[info.index] = info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 727 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 728 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 729 | @staticmethod |
| 730 | def info_to_payload(info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 731 | """ |
| 732 | Convert _PacketInfo object to packet payload |
| 733 | |
| 734 | :param info: _PacketInfo object |
| 735 | |
| 736 | :returns: string containing serialized data from packet info |
| 737 | """ |
Pavel Kotucek | 59dda06 | 2017-03-02 15:22:47 +0100 | [diff] [blame] | 738 | return "%d %d %d %d %d" % (info.index, info.src, info.dst, |
| 739 | info.ip, info.proto) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 740 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 741 | @staticmethod |
| 742 | def payload_to_info(payload): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 743 | """ |
| 744 | Convert packet payload to _PacketInfo object |
| 745 | |
| 746 | :param payload: packet payload |
| 747 | |
| 748 | :returns: _PacketInfo object containing de-serialized data from payload |
| 749 | |
| 750 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 751 | numbers = payload.split() |
| 752 | info = _PacketInfo() |
| 753 | info.index = int(numbers[0]) |
| 754 | info.src = int(numbers[1]) |
| 755 | info.dst = int(numbers[2]) |
Pavel Kotucek | 59dda06 | 2017-03-02 15:22:47 +0100 | [diff] [blame] | 756 | info.ip = int(numbers[3]) |
| 757 | info.proto = int(numbers[4]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 758 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 759 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 760 | def get_next_packet_info(self, info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 761 | """ |
| 762 | Iterate over the packet info list stored in the testcase |
| 763 | Start iteration with first element if info is None |
| 764 | Continue based on index in info if info is specified |
| 765 | |
| 766 | :param info: info or None |
| 767 | :returns: next info in list or None if no more infos |
| 768 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 769 | if info is None: |
| 770 | next_index = 0 |
| 771 | else: |
| 772 | next_index = info.index + 1 |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 773 | if next_index == len(self._packet_infos): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 774 | return None |
| 775 | else: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 776 | return self._packet_infos[next_index] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 777 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 778 | def get_next_packet_info_for_interface(self, src_index, info): |
| 779 | """ |
| 780 | Search the packet info list for the next packet info with same source |
| 781 | interface index |
| 782 | |
| 783 | :param src_index: source interface index to search for |
| 784 | :param info: packet info - where to start the search |
| 785 | :returns: packet info or None |
| 786 | |
| 787 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 788 | while True: |
| 789 | info = self.get_next_packet_info(info) |
| 790 | if info is None: |
| 791 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 792 | if info.src == src_index: |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 793 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 794 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 795 | def get_next_packet_info_for_interface2(self, src_index, dst_index, info): |
| 796 | """ |
| 797 | Search the packet info list for the next packet info with same source |
| 798 | and destination interface indexes |
| 799 | |
| 800 | :param src_index: source interface index to search for |
| 801 | :param dst_index: destination interface index to search for |
| 802 | :param info: packet info - where to start the search |
| 803 | :returns: packet info or None |
| 804 | |
| 805 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 806 | while True: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 807 | info = self.get_next_packet_info_for_interface(src_index, info) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 808 | if info is None: |
| 809 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 810 | if info.dst == dst_index: |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 811 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 812 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 813 | def assert_equal(self, real_value, expected_value, name_or_class=None): |
| 814 | if name_or_class is None: |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 815 | self.assertEqual(real_value, expected_value) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 816 | return |
| 817 | try: |
| 818 | msg = "Invalid %s: %d('%s') does not match expected value %d('%s')" |
| 819 | msg = msg % (getdoc(name_or_class).strip(), |
| 820 | real_value, str(name_or_class(real_value)), |
| 821 | expected_value, str(name_or_class(expected_value))) |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 822 | except Exception: |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 823 | msg = "Invalid %s: %s does not match expected value %s" % ( |
| 824 | name_or_class, real_value, expected_value) |
| 825 | |
| 826 | self.assertEqual(real_value, expected_value, msg) |
| 827 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 828 | def assert_in_range(self, |
| 829 | real_value, |
| 830 | expected_min, |
| 831 | expected_max, |
| 832 | name=None): |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 833 | if name is None: |
| 834 | msg = None |
| 835 | else: |
| 836 | msg = "Invalid %s: %s out of range <%s,%s>" % ( |
| 837 | name, real_value, expected_min, expected_max) |
| 838 | self.assertTrue(expected_min <= real_value <= expected_max, msg) |
| 839 | |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 840 | def assert_packet_checksums_valid(self, packet, |
| 841 | ignore_zero_udp_checksums=True): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 842 | received = packet.__class__(str(packet)) |
| 843 | self.logger.debug( |
| 844 | ppp("Verifying packet checksums for packet:", received)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 845 | udp_layers = ['UDP', 'UDPerror'] |
| 846 | checksum_fields = ['cksum', 'chksum'] |
| 847 | checksums = [] |
| 848 | counter = 0 |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 849 | temp = received.__class__(str(received)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 850 | while True: |
| 851 | layer = temp.getlayer(counter) |
| 852 | if layer: |
| 853 | for cf in checksum_fields: |
| 854 | if hasattr(layer, cf): |
| 855 | if ignore_zero_udp_checksums and \ |
Paul Vinciguerra | 919efad | 2018-12-17 21:43:43 -0800 | [diff] [blame^] | 856 | 0 == getattr(layer, cf) and \ |
| 857 | layer.name in udp_layers: |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 858 | continue |
| 859 | delattr(layer, cf) |
| 860 | checksums.append((counter, cf)) |
| 861 | else: |
| 862 | break |
| 863 | counter = counter + 1 |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 864 | if 0 == len(checksums): |
| 865 | return |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 866 | temp = temp.__class__(str(temp)) |
| 867 | for layer, cf in checksums: |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 868 | calc_sum = getattr(temp[layer], cf) |
| 869 | self.assert_equal( |
| 870 | getattr(received[layer], cf), calc_sum, |
| 871 | "packet checksum on layer #%d: %s" % (layer, temp[layer].name)) |
| 872 | self.logger.debug( |
| 873 | "Checksum field `%s` on `%s` layer has correct value `%s`" % |
| 874 | (cf, temp[layer].name, calc_sum)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 875 | |
| 876 | def assert_checksum_valid(self, received_packet, layer, |
| 877 | field_name='chksum', |
| 878 | ignore_zero_checksum=False): |
| 879 | """ Check checksum of received packet on given layer """ |
| 880 | received_packet_checksum = getattr(received_packet[layer], field_name) |
| 881 | if ignore_zero_checksum and 0 == received_packet_checksum: |
| 882 | return |
| 883 | recalculated = received_packet.__class__(str(received_packet)) |
| 884 | delattr(recalculated[layer], field_name) |
| 885 | recalculated = recalculated.__class__(str(recalculated)) |
| 886 | self.assert_equal(received_packet_checksum, |
| 887 | getattr(recalculated[layer], field_name), |
| 888 | "packet checksum on layer: %s" % layer) |
| 889 | |
| 890 | def assert_ip_checksum_valid(self, received_packet, |
| 891 | ignore_zero_checksum=False): |
| 892 | self.assert_checksum_valid(received_packet, 'IP', |
| 893 | ignore_zero_checksum=ignore_zero_checksum) |
| 894 | |
| 895 | def assert_tcp_checksum_valid(self, received_packet, |
| 896 | ignore_zero_checksum=False): |
| 897 | self.assert_checksum_valid(received_packet, 'TCP', |
| 898 | ignore_zero_checksum=ignore_zero_checksum) |
| 899 | |
| 900 | def assert_udp_checksum_valid(self, received_packet, |
| 901 | ignore_zero_checksum=True): |
| 902 | self.assert_checksum_valid(received_packet, 'UDP', |
| 903 | ignore_zero_checksum=ignore_zero_checksum) |
| 904 | |
| 905 | def assert_embedded_icmp_checksum_valid(self, received_packet): |
| 906 | if received_packet.haslayer(IPerror): |
| 907 | self.assert_checksum_valid(received_packet, 'IPerror') |
| 908 | if received_packet.haslayer(TCPerror): |
| 909 | self.assert_checksum_valid(received_packet, 'TCPerror') |
| 910 | if received_packet.haslayer(UDPerror): |
| 911 | self.assert_checksum_valid(received_packet, 'UDPerror', |
| 912 | ignore_zero_checksum=True) |
| 913 | if received_packet.haslayer(ICMPerror): |
| 914 | self.assert_checksum_valid(received_packet, 'ICMPerror') |
| 915 | |
| 916 | def assert_icmp_checksum_valid(self, received_packet): |
| 917 | self.assert_checksum_valid(received_packet, 'ICMP') |
| 918 | self.assert_embedded_icmp_checksum_valid(received_packet) |
| 919 | |
| 920 | def assert_icmpv6_checksum_valid(self, pkt): |
| 921 | if pkt.haslayer(ICMPv6DestUnreach): |
| 922 | self.assert_checksum_valid(pkt, 'ICMPv6DestUnreach', 'cksum') |
| 923 | self.assert_embedded_icmp_checksum_valid(pkt) |
| 924 | if pkt.haslayer(ICMPv6EchoRequest): |
| 925 | self.assert_checksum_valid(pkt, 'ICMPv6EchoRequest', 'cksum') |
| 926 | if pkt.haslayer(ICMPv6EchoReply): |
| 927 | self.assert_checksum_valid(pkt, 'ICMPv6EchoReply', 'cksum') |
| 928 | |
Klement Sekera | f37c3ba | 2018-11-08 11:24:34 +0100 | [diff] [blame] | 929 | def assert_packet_counter_equal(self, counter, expected_value): |
Klement Sekera | 14d7e90 | 2018-12-10 13:46:09 +0100 | [diff] [blame] | 930 | if counter.startswith("/"): |
| 931 | counter_value = self.statistics.get_counter(counter) |
| 932 | self.assert_equal(counter_value, expected_value, |
| 933 | "packet counter `%s'" % counter) |
| 934 | else: |
| 935 | counters = self.vapi.cli("sh errors").split('\n') |
| 936 | counter_value = -1 |
| 937 | for i in range(1, len(counters) - 1): |
| 938 | results = counters[i].split() |
| 939 | if results[1] == counter: |
| 940 | counter_value = int(results[0]) |
| 941 | break |
Klement Sekera | f37c3ba | 2018-11-08 11:24:34 +0100 | [diff] [blame] | 942 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 943 | @classmethod |
| 944 | def sleep(cls, timeout, remark=None): |
| 945 | if hasattr(cls, 'logger'): |
Paul Vinciguerra | 6919b0d | 2018-12-09 15:37:04 -0800 | [diff] [blame] | 946 | cls.logger.debug("Starting sleep for %es (%s)", timeout, remark) |
Klement Sekera | 3cfa558 | 2017-04-19 07:10:58 +0000 | [diff] [blame] | 947 | before = time.time() |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 948 | time.sleep(timeout) |
Klement Sekera | 3cfa558 | 2017-04-19 07:10:58 +0000 | [diff] [blame] | 949 | after = time.time() |
Paul Vinciguerra | 6919b0d | 2018-12-09 15:37:04 -0800 | [diff] [blame] | 950 | if hasattr(cls, 'logger') and after - before > 2 * timeout: |
Klement Sekera | 60c1223 | 2017-07-18 10:33:06 +0200 | [diff] [blame] | 951 | cls.logger.error("unexpected time.sleep() result - " |
Paul Vinciguerra | 6919b0d | 2018-12-09 15:37:04 -0800 | [diff] [blame] | 952 | "slept for %es instead of ~%es!", |
| 953 | after - before, timeout) |
Klement Sekera | 3cfa558 | 2017-04-19 07:10:58 +0000 | [diff] [blame] | 954 | if hasattr(cls, 'logger'): |
| 955 | cls.logger.debug( |
Paul Vinciguerra | 6919b0d | 2018-12-09 15:37:04 -0800 | [diff] [blame] | 956 | "Finished sleep (%s) - slept %es (wanted %es)", |
| 957 | remark, after - before, timeout) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 958 | |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 959 | def send_and_assert_no_replies(self, intf, pkts, remark="", timeout=None): |
Neale Ranns | 52fae86 | 2018-01-08 04:41:42 -0800 | [diff] [blame] | 960 | self.vapi.cli("clear trace") |
| 961 | intf.add_stream(pkts) |
| 962 | self.pg_enable_capture(self.pg_interfaces) |
| 963 | self.pg_start() |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 964 | if not timeout: |
| 965 | timeout = 1 |
Neale Ranns | 52fae86 | 2018-01-08 04:41:42 -0800 | [diff] [blame] | 966 | for i in self.pg_interfaces: |
| 967 | i.get_capture(0, timeout=timeout) |
| 968 | i.assert_nothing_captured(remark=remark) |
| 969 | timeout = 0.1 |
| 970 | |
| 971 | def send_and_expect(self, input, pkts, output): |
| 972 | self.vapi.cli("clear trace") |
| 973 | input.add_stream(pkts) |
| 974 | self.pg_enable_capture(self.pg_interfaces) |
| 975 | self.pg_start() |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 976 | if isinstance(object, (list,)): |
| 977 | rx = [] |
| 978 | for o in output: |
| 979 | rx.append(output.get_capture(len(pkts))) |
| 980 | else: |
| 981 | rx = output.get_capture(len(pkts)) |
| 982 | return rx |
| 983 | |
| 984 | def send_and_expect_only(self, input, pkts, output, timeout=None): |
| 985 | self.vapi.cli("clear trace") |
| 986 | input.add_stream(pkts) |
| 987 | self.pg_enable_capture(self.pg_interfaces) |
| 988 | self.pg_start() |
| 989 | if isinstance(object, (list,)): |
| 990 | outputs = output |
| 991 | rx = [] |
| 992 | for o in outputs: |
| 993 | rx.append(output.get_capture(len(pkts))) |
| 994 | else: |
| 995 | rx = output.get_capture(len(pkts)) |
| 996 | outputs = [output] |
| 997 | if not timeout: |
| 998 | timeout = 1 |
| 999 | for i in self.pg_interfaces: |
| 1000 | if i not in outputs: |
| 1001 | i.get_capture(0, timeout=timeout) |
| 1002 | i.assert_nothing_captured() |
| 1003 | timeout = 0.1 |
| 1004 | |
Neale Ranns | 52fae86 | 2018-01-08 04:41:42 -0800 | [diff] [blame] | 1005 | return rx |
| 1006 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1007 | |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 1008 | def get_testcase_doc_name(test): |
| 1009 | return getdoc(test.__class__).splitlines()[0] |
| 1010 | |
| 1011 | |
Ole Trøan | 5ba9159 | 2018-11-22 10:01:09 +0000 | [diff] [blame] | 1012 | def get_test_description(descriptions, test): |
| 1013 | short_description = test.shortDescription() |
| 1014 | if descriptions and short_description: |
| 1015 | return short_description |
| 1016 | else: |
| 1017 | return str(test) |
| 1018 | |
| 1019 | |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1020 | class TestCaseInfo(object): |
| 1021 | def __init__(self, logger, tempdir, vpp_pid, vpp_bin_path): |
| 1022 | self.logger = logger |
| 1023 | self.tempdir = tempdir |
| 1024 | self.vpp_pid = vpp_pid |
| 1025 | self.vpp_bin_path = vpp_bin_path |
| 1026 | self.core_crash_test = None |
Klement Sekera | 8713493 | 2017-03-07 11:39:27 +0100 | [diff] [blame] | 1027 | |
| 1028 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1029 | class VppTestResult(unittest.TestResult): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1030 | """ |
| 1031 | @property result_string |
| 1032 | String variable to store the test case result string. |
| 1033 | @property errors |
| 1034 | List variable containing 2-tuples of TestCase instances and strings |
| 1035 | holding formatted tracebacks. Each tuple represents a test which |
| 1036 | raised an unexpected exception. |
| 1037 | @property failures |
| 1038 | List variable containing 2-tuples of TestCase instances and strings |
| 1039 | holding formatted tracebacks. Each tuple represents a test where |
| 1040 | a failure was explicitly signalled using the TestCase.assert*() |
| 1041 | methods. |
| 1042 | """ |
| 1043 | |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1044 | failed_test_cases_info = set() |
| 1045 | core_crash_test_cases_info = set() |
| 1046 | current_test_case_info = None |
| 1047 | |
juraj.linkes | abec012 | 2018-11-16 17:28:56 +0100 | [diff] [blame] | 1048 | def __init__(self, stream, descriptions, verbosity, runner): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1049 | """ |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 1050 | :param stream File descriptor to store where to report test results. |
| 1051 | Set to the standard error stream by default. |
| 1052 | :param descriptions Boolean variable to store information if to use |
| 1053 | test case descriptions. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1054 | :param verbosity Integer variable to store required verbosity level. |
| 1055 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1056 | unittest.TestResult.__init__(self, stream, descriptions, verbosity) |
| 1057 | self.stream = stream |
| 1058 | self.descriptions = descriptions |
| 1059 | self.verbosity = verbosity |
| 1060 | self.result_string = None |
juraj.linkes | abec012 | 2018-11-16 17:28:56 +0100 | [diff] [blame] | 1061 | self.runner = runner |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1062 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1063 | def addSuccess(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1064 | """ |
| 1065 | Record a test succeeded result |
| 1066 | |
| 1067 | :param test: |
| 1068 | |
| 1069 | """ |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1070 | if self.current_test_case_info: |
| 1071 | self.current_test_case_info.logger.debug( |
| 1072 | "--- addSuccess() %s.%s(%s) called" % (test.__class__.__name__, |
| 1073 | test._testMethodName, |
| 1074 | test._testMethodDoc)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1075 | unittest.TestResult.addSuccess(self, test) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 1076 | self.result_string = colorize("OK", GREEN) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1077 | |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 1078 | self.send_result_through_pipe(test, PASS) |
| 1079 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1080 | def addSkip(self, test, reason): |
| 1081 | """ |
| 1082 | Record a test skipped. |
| 1083 | |
| 1084 | :param test: |
| 1085 | :param reason: |
| 1086 | |
| 1087 | """ |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1088 | if self.current_test_case_info: |
| 1089 | self.current_test_case_info.logger.debug( |
| 1090 | "--- addSkip() %s.%s(%s) called, reason is %s" % |
| 1091 | (test.__class__.__name__, test._testMethodName, |
| 1092 | test._testMethodDoc, reason)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1093 | unittest.TestResult.addSkip(self, test, reason) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 1094 | self.result_string = colorize("SKIP", YELLOW) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1095 | |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 1096 | self.send_result_through_pipe(test, SKIP) |
| 1097 | |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1098 | def symlink_failed(self): |
| 1099 | if self.current_test_case_info: |
Klement Sekera | f413bef | 2017-08-15 07:09:02 +0200 | [diff] [blame] | 1100 | try: |
Klement Sekera | b8c72a4 | 2018-11-08 11:21:39 +0100 | [diff] [blame] | 1101 | failed_dir = os.getenv('FAILED_DIR') |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1102 | link_path = os.path.join( |
| 1103 | failed_dir, |
| 1104 | '%s-FAILED' % |
| 1105 | os.path.basename(self.current_test_case_info.tempdir)) |
| 1106 | if self.current_test_case_info.logger: |
| 1107 | self.current_test_case_info.logger.debug( |
| 1108 | "creating a link to the failed test") |
| 1109 | self.current_test_case_info.logger.debug( |
| 1110 | "os.symlink(%s, %s)" % |
| 1111 | (self.current_test_case_info.tempdir, link_path)) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 1112 | if os.path.exists(link_path): |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1113 | if self.current_test_case_info.logger: |
| 1114 | self.current_test_case_info.logger.debug( |
| 1115 | 'symlink already exists') |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 1116 | else: |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1117 | os.symlink(self.current_test_case_info.tempdir, link_path) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 1118 | |
Klement Sekera | f413bef | 2017-08-15 07:09:02 +0200 | [diff] [blame] | 1119 | except Exception as e: |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1120 | if self.current_test_case_info.logger: |
| 1121 | self.current_test_case_info.logger.error(e) |
Klement Sekera | f413bef | 2017-08-15 07:09:02 +0200 | [diff] [blame] | 1122 | |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 1123 | def send_result_through_pipe(self, test, result): |
| 1124 | if hasattr(self, 'test_framework_result_pipe'): |
| 1125 | pipe = self.test_framework_result_pipe |
juraj.linkes | 5e2c54d | 2018-08-30 10:51:45 +0200 | [diff] [blame] | 1126 | if pipe: |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 1127 | pipe.send((test.id(), result)) |
juraj.linkes | 5e2c54d | 2018-08-30 10:51:45 +0200 | [diff] [blame] | 1128 | |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1129 | def log_error(self, test, err, fn_name): |
| 1130 | if self.current_test_case_info: |
| 1131 | if isinstance(test, unittest.suite._ErrorHolder): |
| 1132 | test_name = test.description |
| 1133 | else: |
| 1134 | test_name = '%s.%s(%s)' % (test.__class__.__name__, |
| 1135 | test._testMethodName, |
| 1136 | test._testMethodDoc) |
| 1137 | self.current_test_case_info.logger.debug( |
| 1138 | "--- %s() %s called, err is %s" % |
| 1139 | (fn_name, test_name, err)) |
| 1140 | self.current_test_case_info.logger.debug( |
| 1141 | "formatted exception is:\n%s" % |
| 1142 | "".join(format_exception(*err))) |
| 1143 | |
| 1144 | def add_error(self, test, err, unittest_fn, error_type): |
| 1145 | if error_type == FAIL: |
| 1146 | self.log_error(test, err, 'addFailure') |
| 1147 | error_type_str = colorize("FAIL", RED) |
| 1148 | elif error_type == ERROR: |
| 1149 | self.log_error(test, err, 'addError') |
| 1150 | error_type_str = colorize("ERROR", RED) |
| 1151 | else: |
| 1152 | raise Exception('Error type %s cannot be used to record an ' |
| 1153 | 'error or a failure' % error_type) |
| 1154 | |
| 1155 | unittest_fn(self, test, err) |
| 1156 | if self.current_test_case_info: |
| 1157 | self.result_string = "%s [ temp dir used by test case: %s ]" % \ |
| 1158 | (error_type_str, |
| 1159 | self.current_test_case_info.tempdir) |
| 1160 | self.symlink_failed() |
| 1161 | self.failed_test_cases_info.add(self.current_test_case_info) |
| 1162 | if is_core_present(self.current_test_case_info.tempdir): |
| 1163 | if not self.current_test_case_info.core_crash_test: |
| 1164 | if isinstance(test, unittest.suite._ErrorHolder): |
| 1165 | test_name = str(test) |
| 1166 | else: |
| 1167 | test_name = "'{}' ({})".format( |
| 1168 | get_testcase_doc_name(test), test.id()) |
| 1169 | self.current_test_case_info.core_crash_test = test_name |
| 1170 | self.core_crash_test_cases_info.add( |
| 1171 | self.current_test_case_info) |
| 1172 | else: |
| 1173 | self.result_string = '%s [no temp dir]' % error_type_str |
| 1174 | |
| 1175 | self.send_result_through_pipe(test, error_type) |
| 1176 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1177 | def addFailure(self, test, err): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1178 | """ |
| 1179 | Record a test failed result |
| 1180 | |
| 1181 | :param test: |
| 1182 | :param err: error message |
| 1183 | |
| 1184 | """ |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1185 | self.add_error(test, err, unittest.TestResult.addFailure, FAIL) |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 1186 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1187 | def addError(self, test, err): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1188 | """ |
| 1189 | Record a test error result |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1190 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1191 | :param test: |
| 1192 | :param err: error message |
| 1193 | |
| 1194 | """ |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1195 | self.add_error(test, err, unittest.TestResult.addError, ERROR) |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 1196 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1197 | def getDescription(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1198 | """ |
| 1199 | Get test description |
| 1200 | |
| 1201 | :param test: |
| 1202 | :returns: test description |
| 1203 | |
| 1204 | """ |
Ole Trøan | 5ba9159 | 2018-11-22 10:01:09 +0000 | [diff] [blame] | 1205 | return get_test_description(self.descriptions, test) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1206 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1207 | def startTest(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1208 | """ |
| 1209 | Start a test |
| 1210 | |
| 1211 | :param test: |
| 1212 | |
| 1213 | """ |
Paul Vinciguerra | 86ebba6 | 2018-11-21 09:28:32 -0800 | [diff] [blame] | 1214 | test.print_header() |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 1215 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1216 | unittest.TestResult.startTest(self, test) |
| 1217 | if self.verbosity > 0: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1218 | self.stream.writeln( |
| 1219 | "Starting " + self.getDescription(test) + " ...") |
| 1220 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1221 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1222 | def stopTest(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1223 | """ |
juraj.linkes | 5e2c54d | 2018-08-30 10:51:45 +0200 | [diff] [blame] | 1224 | Called when the given test has been run |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1225 | |
| 1226 | :param test: |
| 1227 | |
| 1228 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1229 | unittest.TestResult.stopTest(self, test) |
| 1230 | if self.verbosity > 0: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1231 | self.stream.writeln(single_line_delim) |
Klement Sekera | 52e84f3 | 2017-01-13 07:25:25 +0100 | [diff] [blame] | 1232 | self.stream.writeln("%-73s%s" % (self.getDescription(test), |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 1233 | self.result_string)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1234 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1235 | else: |
Klement Sekera | 52e84f3 | 2017-01-13 07:25:25 +0100 | [diff] [blame] | 1236 | self.stream.writeln("%-73s%s" % (self.getDescription(test), |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 1237 | self.result_string)) |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 1238 | |
| 1239 | self.send_result_through_pipe(test, TEST_RUN) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1240 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1241 | def printErrors(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1242 | """ |
| 1243 | Print errors from running the test case |
| 1244 | """ |
juraj.linkes | abec012 | 2018-11-16 17:28:56 +0100 | [diff] [blame] | 1245 | if len(self.errors) > 0 or len(self.failures) > 0: |
| 1246 | self.stream.writeln() |
| 1247 | self.printErrorList('ERROR', self.errors) |
| 1248 | self.printErrorList('FAIL', self.failures) |
| 1249 | |
| 1250 | # ^^ that is the last output from unittest before summary |
| 1251 | if not self.runner.print_summary: |
| 1252 | devnull = unittest.runner._WritelnDecorator(open(os.devnull, 'w')) |
| 1253 | self.stream = devnull |
| 1254 | self.runner.stream = devnull |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1255 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1256 | def printErrorList(self, flavour, errors): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1257 | """ |
| 1258 | Print error list to the output stream together with error type |
| 1259 | and test case description. |
| 1260 | |
| 1261 | :param flavour: error type |
| 1262 | :param errors: iterable errors |
| 1263 | |
| 1264 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1265 | for test, err in errors: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1266 | self.stream.writeln(double_line_delim) |
| 1267 | self.stream.writeln("%s: %s" % |
| 1268 | (flavour, self.getDescription(test))) |
| 1269 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1270 | self.stream.writeln("%s" % err) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1271 | |
| 1272 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1273 | class VppTestRunner(unittest.TextTestRunner): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1274 | """ |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 1275 | A basic test runner implementation which prints results to standard error. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1276 | """ |
Paul Vinciguerra | 6919b0d | 2018-12-09 15:37:04 -0800 | [diff] [blame] | 1277 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1278 | @property |
| 1279 | def resultclass(self): |
| 1280 | """Class maintaining the results of the tests""" |
| 1281 | return VppTestResult |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1282 | |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 1283 | def __init__(self, keep_alive_pipe=None, descriptions=True, verbosity=1, |
juraj.linkes | cae64f8 | 2018-09-19 15:01:47 +0200 | [diff] [blame] | 1284 | result_pipe=None, failfast=False, buffer=False, |
juraj.linkes | abec012 | 2018-11-16 17:28:56 +0100 | [diff] [blame] | 1285 | resultclass=None, print_summary=True): |
Klement Sekera | 7a161da | 2017-01-17 13:42:48 +0100 | [diff] [blame] | 1286 | # ignore stream setting here, use hard-coded stdout to be in sync |
| 1287 | # with prints from VppTestCase methods ... |
| 1288 | super(VppTestRunner, self).__init__(sys.stdout, descriptions, |
| 1289 | verbosity, failfast, buffer, |
| 1290 | resultclass) |
juraj.linkes | ccfead6 | 2018-11-21 13:20:43 +0100 | [diff] [blame] | 1291 | KeepAliveReporter.pipe = keep_alive_pipe |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 1292 | |
juraj.linkes | abec012 | 2018-11-16 17:28:56 +0100 | [diff] [blame] | 1293 | self.orig_stream = self.stream |
| 1294 | self.resultclass.test_framework_result_pipe = result_pipe |
| 1295 | |
| 1296 | self.print_summary = print_summary |
| 1297 | |
| 1298 | def _makeResult(self): |
| 1299 | return self.resultclass(self.stream, |
| 1300 | self.descriptions, |
| 1301 | self.verbosity, |
| 1302 | self) |
juraj.linkes | 5e2c54d | 2018-08-30 10:51:45 +0200 | [diff] [blame] | 1303 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1304 | def run(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1305 | """ |
| 1306 | Run the tests |
| 1307 | |
| 1308 | :param test: |
| 1309 | |
| 1310 | """ |
Klement Sekera | 3658adc | 2017-06-07 08:19:47 +0200 | [diff] [blame] | 1311 | faulthandler.enable() # emit stack trace to stderr if killed by signal |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 1312 | |
| 1313 | result = super(VppTestRunner, self).run(test) |
juraj.linkes | abec012 | 2018-11-16 17:28:56 +0100 | [diff] [blame] | 1314 | if not self.print_summary: |
| 1315 | self.stream = self.orig_stream |
| 1316 | result.stream = self.orig_stream |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 1317 | return result |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1318 | |
| 1319 | |
| 1320 | class Worker(Thread): |
Dave Wallace | 42996c0 | 2018-02-26 14:40:13 -0500 | [diff] [blame] | 1321 | def __init__(self, args, logger, env={}): |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1322 | self.logger = logger |
| 1323 | self.args = args |
| 1324 | self.result = None |
Dave Wallace | 42996c0 | 2018-02-26 14:40:13 -0500 | [diff] [blame] | 1325 | self.env = copy.deepcopy(env) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1326 | super(Worker, self).__init__() |
| 1327 | |
| 1328 | def run(self): |
| 1329 | executable = self.args[0] |
| 1330 | self.logger.debug("Running executable w/args `%s'" % self.args) |
| 1331 | env = os.environ.copy() |
Dave Wallace | cfcf2f4 | 2018-02-16 18:31:56 -0500 | [diff] [blame] | 1332 | env.update(self.env) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1333 | env["CK_LOG_FILE_NAME"] = "-" |
| 1334 | self.process = subprocess.Popen( |
| 1335 | self.args, shell=False, env=env, preexec_fn=os.setpgrp, |
| 1336 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 1337 | out, err = self.process.communicate() |
| 1338 | self.logger.debug("Finished running `%s'" % executable) |
| 1339 | self.logger.info("Return code is `%s'" % self.process.returncode) |
| 1340 | self.logger.info(single_line_delim) |
| 1341 | self.logger.info("Executable `%s' wrote to stdout:" % executable) |
| 1342 | self.logger.info(single_line_delim) |
| 1343 | self.logger.info(out) |
| 1344 | self.logger.info(single_line_delim) |
| 1345 | self.logger.info("Executable `%s' wrote to stderr:" % executable) |
| 1346 | self.logger.info(single_line_delim) |
Klement Sekera | de0203e | 2018-02-22 19:21:27 +0100 | [diff] [blame] | 1347 | self.logger.info(err) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1348 | self.logger.info(single_line_delim) |
| 1349 | self.result = self.process.returncode |