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 |
| 5 | import sys |
| 6 | import os |
| 7 | import select |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 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 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 11 | import resource |
Klement Sekera | 3658adc | 2017-06-07 08:19:47 +0200 | [diff] [blame] | 12 | import faulthandler |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 13 | from collections import deque |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 14 | from threading import Thread, Event |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 15 | from inspect import getdoc, isclass |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 16 | from traceback import format_exception |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 17 | from logging import FileHandler, DEBUG, Formatter |
| 18 | from scapy.packet import Raw |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 19 | from hook import StepHook, PollHook |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 20 | from vpp_pg_interface import VppPGInterface |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 21 | from vpp_sub_interface import VppSubInterface |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 22 | from vpp_lo_interface import VppLoInterface |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 23 | from vpp_papi_provider import VppPapiProvider |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 24 | from log import * |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 25 | from vpp_object import VppObjectRegistry |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 26 | if os.name == 'posix' and sys.version_info[0] < 3: |
| 27 | # using subprocess32 is recommended by python official documentation |
| 28 | # @ https://docs.python.org/2/library/subprocess.html |
| 29 | import subprocess32 as subprocess |
| 30 | else: |
| 31 | import subprocess |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 32 | |
| 33 | """ |
| 34 | Test framework module. |
| 35 | |
| 36 | The module provides a set of tools for constructing and running tests and |
| 37 | representing the results. |
| 38 | """ |
| 39 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 40 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 41 | class _PacketInfo(object): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 42 | """Private class to create packet info object. |
| 43 | |
| 44 | Help process information about the next packet. |
| 45 | Set variables to default values. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 46 | """ |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 47 | #: Store the index of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 48 | index = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 49 | #: Store the index of the source packet generator interface of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 50 | src = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 51 | #: Store the index of the destination packet generator interface |
| 52 | #: of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 53 | dst = -1 |
Pavel Kotucek | 59dda06 | 2017-03-02 15:22:47 +0100 | [diff] [blame] | 54 | #: Store expected ip version |
| 55 | ip = -1 |
| 56 | #: Store expected upper protocol |
| 57 | proto = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 58 | #: Store the copy of the former packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 59 | data = None |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 60 | |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 61 | def __eq__(self, other): |
| 62 | index = self.index == other.index |
| 63 | src = self.src == other.src |
| 64 | dst = self.dst == other.dst |
| 65 | data = self.data == other.data |
| 66 | return index and src and dst and data |
| 67 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 68 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 69 | def pump_output(testclass): |
| 70 | """ pump output from vpp stdout/stderr to proper queues """ |
| 71 | while not testclass.pump_thread_stop_flag.wait(0): |
| 72 | readable = select.select([testclass.vpp.stdout.fileno(), |
| 73 | testclass.vpp.stderr.fileno(), |
| 74 | testclass.pump_thread_wakeup_pipe[0]], |
| 75 | [], [])[0] |
| 76 | if testclass.vpp.stdout.fileno() in readable: |
| 77 | read = os.read(testclass.vpp.stdout.fileno(), 1024) |
| 78 | testclass.vpp_stdout_deque.append(read) |
| 79 | if testclass.vpp.stderr.fileno() in readable: |
| 80 | read = os.read(testclass.vpp.stderr.fileno(), 1024) |
| 81 | testclass.vpp_stderr_deque.append(read) |
| 82 | # ignoring the dummy pipe here intentionally - the flag will take care |
| 83 | # of properly terminating the loop |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 84 | |
| 85 | |
Klement Sekera | 8713493 | 2017-03-07 11:39:27 +0100 | [diff] [blame] | 86 | def running_extended_tests(): |
| 87 | try: |
| 88 | s = os.getenv("EXTENDED_TESTS") |
| 89 | return True if s.lower() in ("y", "yes", "1") else False |
| 90 | except: |
| 91 | return False |
| 92 | return False |
| 93 | |
| 94 | |
Klement Sekera | d3e671e | 2017-09-29 12:36:37 +0200 | [diff] [blame] | 95 | def running_on_centos(): |
| 96 | try: |
| 97 | os_id = os.getenv("OS_ID") |
| 98 | return True if "centos" in os_id.lower() else False |
| 99 | except: |
| 100 | return False |
| 101 | return False |
| 102 | |
| 103 | |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 104 | class KeepAliveReporter(object): |
| 105 | """ |
| 106 | Singleton object which reports test start to parent process |
| 107 | """ |
| 108 | _shared_state = {} |
| 109 | |
| 110 | def __init__(self): |
| 111 | self.__dict__ = self._shared_state |
| 112 | |
| 113 | @property |
| 114 | def pipe(self): |
| 115 | return self._pipe |
| 116 | |
| 117 | @pipe.setter |
| 118 | def pipe(self, pipe): |
| 119 | if hasattr(self, '_pipe'): |
| 120 | raise Exception("Internal error - pipe should only be set once.") |
| 121 | self._pipe = pipe |
| 122 | |
| 123 | def send_keep_alive(self, test): |
| 124 | """ |
| 125 | Write current test tmpdir & desc to keep-alive pipe to signal liveness |
| 126 | """ |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 127 | if self.pipe is None: |
| 128 | # if not running forked.. |
| 129 | return |
| 130 | |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 131 | if isclass(test): |
| 132 | desc = test.__name__ |
| 133 | else: |
| 134 | desc = test.shortDescription() |
| 135 | if not desc: |
| 136 | desc = str(test) |
| 137 | |
Dave Wallace | e2efd12 | 2017-09-30 22:04:21 -0400 | [diff] [blame] | 138 | self.pipe.send((desc, test.vpp_bin, test.tempdir, test.vpp.pid)) |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 139 | |
| 140 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 141 | class VppTestCase(unittest.TestCase): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 142 | """This subclass is a base class for VPP test cases that are implemented as |
| 143 | classes. It provides methods to create and run test case. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 144 | """ |
| 145 | |
| 146 | @property |
| 147 | def packet_infos(self): |
| 148 | """List of packet infos""" |
| 149 | return self._packet_infos |
| 150 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 151 | @classmethod |
| 152 | def get_packet_count_for_if_idx(cls, dst_if_index): |
| 153 | """Get the number of packet info for specified destination if index""" |
| 154 | if dst_if_index in cls._packet_count_for_dst_if_idx: |
| 155 | return cls._packet_count_for_dst_if_idx[dst_if_index] |
| 156 | else: |
| 157 | return 0 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 158 | |
| 159 | @classmethod |
| 160 | def instance(cls): |
| 161 | """Return the instance of this testcase""" |
| 162 | return cls.test_instance |
| 163 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 164 | @classmethod |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 165 | def set_debug_flags(cls, d): |
| 166 | cls.debug_core = False |
| 167 | cls.debug_gdb = False |
| 168 | cls.debug_gdbserver = False |
| 169 | if d is None: |
| 170 | return |
| 171 | dl = d.lower() |
| 172 | if dl == "core": |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 173 | cls.debug_core = True |
| 174 | elif dl == "gdb": |
| 175 | cls.debug_gdb = True |
| 176 | elif dl == "gdbserver": |
| 177 | cls.debug_gdbserver = True |
| 178 | else: |
| 179 | raise Exception("Unrecognized DEBUG option: '%s'" % d) |
| 180 | |
| 181 | @classmethod |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 182 | def setUpConstants(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 183 | """ Set-up the test case class based on environment variables """ |
| 184 | try: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 185 | s = os.getenv("STEP") |
| 186 | cls.step = True if s.lower() in ("y", "yes", "1") else False |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 187 | except: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 188 | cls.step = False |
| 189 | try: |
| 190 | d = os.getenv("DEBUG") |
| 191 | except: |
| 192 | d = None |
| 193 | cls.set_debug_flags(d) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 194 | cls.vpp_bin = os.getenv('VPP_TEST_BIN', "vpp") |
Pierre Pfister | cd8e318 | 2016-10-07 16:30:03 +0100 | [diff] [blame] | 195 | cls.plugin_path = os.getenv('VPP_TEST_PLUGIN_PATH') |
Klement Sekera | 47e275b | 2017-03-21 08:21:25 +0100 | [diff] [blame] | 196 | cls.extern_plugin_path = os.getenv('EXTERN_PLUGINS') |
| 197 | plugin_path = None |
| 198 | if cls.plugin_path is not None: |
| 199 | if cls.extern_plugin_path is not None: |
| 200 | plugin_path = "%s:%s" % ( |
| 201 | cls.plugin_path, cls.extern_plugin_path) |
Klement Sekera | 6abbc28 | 2017-03-24 05:47:15 +0100 | [diff] [blame] | 202 | else: |
| 203 | plugin_path = cls.plugin_path |
Klement Sekera | 47e275b | 2017-03-21 08:21:25 +0100 | [diff] [blame] | 204 | elif cls.extern_plugin_path is not None: |
| 205 | plugin_path = cls.extern_plugin_path |
Klement Sekera | 01bbbe9 | 2016-11-02 09:25:05 +0100 | [diff] [blame] | 206 | debug_cli = "" |
| 207 | if cls.step or cls.debug_gdb or cls.debug_gdbserver: |
| 208 | debug_cli = "cli-listen localhost:5002" |
Klement Sekera | 80a7f0a | 2017-03-02 11:27:11 +0100 | [diff] [blame] | 209 | coredump_size = None |
| 210 | try: |
| 211 | size = os.getenv("COREDUMP_SIZE") |
| 212 | if size is not None: |
| 213 | coredump_size = "coredump-size %s" % size |
| 214 | except: |
| 215 | pass |
| 216 | if coredump_size is None: |
Dave Wallace | e2efd12 | 2017-09-30 22:04:21 -0400 | [diff] [blame] | 217 | coredump_size = "coredump-size unlimited" |
Klement Sekera | 80a7f0a | 2017-03-02 11:27:11 +0100 | [diff] [blame] | 218 | cls.vpp_cmdline = [cls.vpp_bin, "unix", |
Dave Wallace | e2efd12 | 2017-09-30 22:04:21 -0400 | [diff] [blame] | 219 | "{", "nodaemon", debug_cli, "full-coredump", |
| 220 | coredump_size, "}", "api-trace", "{", "on", "}", |
Damjan Marion | 374e2c5 | 2017-03-09 20:38:15 +0100 | [diff] [blame] | 221 | "api-segment", "{", "prefix", cls.shm_prefix, "}", |
| 222 | "plugins", "{", "plugin", "dpdk_plugin.so", "{", |
| 223 | "disable", "}", "}"] |
Klement Sekera | 47e275b | 2017-03-21 08:21:25 +0100 | [diff] [blame] | 224 | if plugin_path is not None: |
| 225 | cls.vpp_cmdline.extend(["plugin_path", plugin_path]) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 226 | cls.logger.info("vpp_cmdline: %s" % cls.vpp_cmdline) |
| 227 | |
| 228 | @classmethod |
| 229 | def wait_for_enter(cls): |
| 230 | if cls.debug_gdbserver: |
| 231 | print(double_line_delim) |
| 232 | print("Spawned GDB server with PID: %d" % cls.vpp.pid) |
| 233 | elif cls.debug_gdb: |
| 234 | print(double_line_delim) |
| 235 | print("Spawned VPP with PID: %d" % cls.vpp.pid) |
| 236 | else: |
| 237 | cls.logger.debug("Spawned VPP with PID: %d" % cls.vpp.pid) |
| 238 | return |
| 239 | print(single_line_delim) |
| 240 | print("You can debug the VPP using e.g.:") |
| 241 | if cls.debug_gdbserver: |
| 242 | print("gdb " + cls.vpp_bin + " -ex 'target remote localhost:7777'") |
| 243 | print("Now is the time to attach a gdb by running the above " |
| 244 | "command, set up breakpoints etc. and then resume VPP from " |
| 245 | "within gdb by issuing the 'continue' command") |
| 246 | elif cls.debug_gdb: |
| 247 | print("gdb " + cls.vpp_bin + " -ex 'attach %s'" % cls.vpp.pid) |
| 248 | print("Now is the time to attach a gdb by running the above " |
| 249 | "command and set up breakpoints etc.") |
| 250 | print(single_line_delim) |
| 251 | raw_input("Press ENTER to continue running the testcase...") |
| 252 | |
| 253 | @classmethod |
| 254 | def run_vpp(cls): |
| 255 | cmdline = cls.vpp_cmdline |
| 256 | |
| 257 | if cls.debug_gdbserver: |
Klement Sekera | 931be3a | 2016-11-03 05:36:01 +0100 | [diff] [blame] | 258 | gdbserver = '/usr/bin/gdbserver' |
| 259 | if not os.path.isfile(gdbserver) or \ |
| 260 | not os.access(gdbserver, os.X_OK): |
| 261 | raise Exception("gdbserver binary '%s' does not exist or is " |
| 262 | "not executable" % gdbserver) |
| 263 | |
| 264 | cmdline = [gdbserver, 'localhost:7777'] + cls.vpp_cmdline |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 265 | cls.logger.info("Gdbserver cmdline is %s", " ".join(cmdline)) |
| 266 | |
Klement Sekera | 931be3a | 2016-11-03 05:36:01 +0100 | [diff] [blame] | 267 | try: |
| 268 | cls.vpp = subprocess.Popen(cmdline, |
| 269 | stdout=subprocess.PIPE, |
| 270 | stderr=subprocess.PIPE, |
| 271 | bufsize=1) |
| 272 | except Exception as e: |
| 273 | cls.logger.critical("Couldn't start vpp: %s" % e) |
| 274 | raise |
| 275 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 276 | cls.wait_for_enter() |
Pierre Pfister | cd8e318 | 2016-10-07 16:30:03 +0100 | [diff] [blame] | 277 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 278 | @classmethod |
| 279 | def setUpClass(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 280 | """ |
| 281 | Perform class setup before running the testcase |
| 282 | Remove shared memory files, start vpp and connect the vpp-api |
| 283 | """ |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 284 | gc.collect() # run garbage collection first |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 285 | cls.logger = getLogger(cls.__name__) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 286 | cls.tempdir = tempfile.mkdtemp( |
Klement Sekera | f413bef | 2017-08-15 07:09:02 +0200 | [diff] [blame] | 287 | prefix='vpp-unittest-%s-' % cls.__name__) |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 288 | cls.file_handler = FileHandler("%s/log.txt" % cls.tempdir) |
| 289 | cls.file_handler.setFormatter( |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 290 | Formatter(fmt='%(asctime)s,%(msecs)03d %(message)s', |
| 291 | datefmt="%H:%M:%S")) |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 292 | cls.file_handler.setLevel(DEBUG) |
| 293 | cls.logger.addHandler(cls.file_handler) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 294 | cls.shm_prefix = cls.tempdir.split("/")[-1] |
| 295 | os.chdir(cls.tempdir) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 296 | cls.logger.info("Temporary dir is %s, shm prefix is %s", |
| 297 | cls.tempdir, cls.shm_prefix) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 298 | cls.setUpConstants() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 299 | cls.reset_packet_infos() |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 300 | cls._captures = [] |
| 301 | cls._zombie_captures = [] |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 302 | cls.verbose = 0 |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 303 | cls.vpp_dead = False |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 304 | cls.registry = VppObjectRegistry() |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 305 | cls.vpp_startup_failed = False |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 306 | cls.reporter = KeepAliveReporter() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 307 | # need to catch exceptions here because if we raise, then the cleanup |
| 308 | # doesn't get called and we might end with a zombie vpp |
| 309 | try: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 310 | cls.run_vpp() |
Dave Wallace | e2efd12 | 2017-09-30 22:04:21 -0400 | [diff] [blame] | 311 | cls.reporter.send_keep_alive(cls) |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 312 | cls.vpp_stdout_deque = deque() |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 313 | cls.vpp_stderr_deque = deque() |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 314 | cls.pump_thread_stop_flag = Event() |
| 315 | cls.pump_thread_wakeup_pipe = os.pipe() |
| 316 | cls.pump_thread = Thread(target=pump_output, args=(cls,)) |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 317 | cls.pump_thread.daemon = True |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 318 | cls.pump_thread.start() |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 319 | cls.vapi = VppPapiProvider(cls.shm_prefix, cls.shm_prefix, cls) |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 320 | if cls.step: |
| 321 | hook = StepHook(cls) |
| 322 | else: |
| 323 | hook = PollHook(cls) |
| 324 | cls.vapi.register_hook(hook) |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 325 | cls.sleep(0.1, "after vpp startup, before initial poll") |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 326 | try: |
| 327 | hook.poll_vpp() |
| 328 | except: |
| 329 | cls.vpp_startup_failed = True |
| 330 | cls.logger.critical( |
| 331 | "VPP died shortly after startup, check the" |
| 332 | " output to standard error for possible cause") |
| 333 | raise |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 334 | try: |
| 335 | cls.vapi.connect() |
| 336 | except: |
| 337 | if cls.debug_gdbserver: |
| 338 | print(colorize("You're running VPP inside gdbserver but " |
| 339 | "VPP-API connection failed, did you forget " |
| 340 | "to 'continue' VPP from within gdb?", RED)) |
| 341 | raise |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 342 | except: |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 343 | t, v, tb = sys.exc_info() |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 344 | try: |
| 345 | cls.quit() |
| 346 | except: |
| 347 | pass |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 348 | raise t, v, tb |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 349 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 350 | @classmethod |
| 351 | def quit(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 352 | """ |
| 353 | Disconnect vpp-api, kill vpp and cleanup shared memory files |
| 354 | """ |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 355 | if (cls.debug_gdbserver or cls.debug_gdb) and hasattr(cls, 'vpp'): |
| 356 | cls.vpp.poll() |
| 357 | if cls.vpp.returncode is None: |
| 358 | print(double_line_delim) |
| 359 | print("VPP or GDB server is still running") |
| 360 | print(single_line_delim) |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 361 | raw_input("When done debugging, press ENTER to kill the " |
| 362 | "process and finish running the testcase...") |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 363 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 364 | os.write(cls.pump_thread_wakeup_pipe[1], 'ding dong wake up') |
| 365 | cls.pump_thread_stop_flag.set() |
| 366 | if hasattr(cls, 'pump_thread'): |
| 367 | cls.logger.debug("Waiting for pump thread to stop") |
| 368 | cls.pump_thread.join() |
| 369 | if hasattr(cls, 'vpp_stderr_reader_thread'): |
| 370 | cls.logger.debug("Waiting for stdderr pump to stop") |
| 371 | cls.vpp_stderr_reader_thread.join() |
| 372 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 373 | if hasattr(cls, 'vpp'): |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 374 | if hasattr(cls, 'vapi'): |
| 375 | cls.vapi.disconnect() |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 376 | del cls.vapi |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 377 | cls.vpp.poll() |
| 378 | if cls.vpp.returncode is None: |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 379 | cls.logger.debug("Sending TERM to vpp") |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 380 | cls.vpp.terminate() |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 381 | cls.logger.debug("Waiting for vpp to die") |
| 382 | cls.vpp.communicate() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 383 | del cls.vpp |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 384 | |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 385 | if cls.vpp_startup_failed: |
| 386 | stdout_log = cls.logger.info |
| 387 | stderr_log = cls.logger.critical |
| 388 | else: |
| 389 | stdout_log = cls.logger.info |
| 390 | stderr_log = cls.logger.info |
| 391 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 392 | if hasattr(cls, 'vpp_stdout_deque'): |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 393 | stdout_log(single_line_delim) |
| 394 | stdout_log('VPP output to stdout while running %s:', cls.__name__) |
| 395 | stdout_log(single_line_delim) |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 396 | vpp_output = "".join(cls.vpp_stdout_deque) |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 397 | with open(cls.tempdir + '/vpp_stdout.txt', 'w') as f: |
| 398 | f.write(vpp_output) |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 399 | stdout_log('\n%s', vpp_output) |
| 400 | stdout_log(single_line_delim) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 401 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 402 | if hasattr(cls, 'vpp_stderr_deque'): |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 403 | stderr_log(single_line_delim) |
| 404 | stderr_log('VPP output to stderr while running %s:', cls.__name__) |
| 405 | stderr_log(single_line_delim) |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 406 | vpp_output = "".join(cls.vpp_stderr_deque) |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 407 | with open(cls.tempdir + '/vpp_stderr.txt', 'w') as f: |
| 408 | f.write(vpp_output) |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 409 | stderr_log('\n%s', vpp_output) |
| 410 | stderr_log(single_line_delim) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 411 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 412 | @classmethod |
| 413 | def tearDownClass(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 414 | """ Perform final cleanup after running all tests in this test-case """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 415 | cls.quit() |
Klement Sekera | 027dbd5 | 2017-04-11 06:01:53 +0200 | [diff] [blame] | 416 | cls.file_handler.close() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 417 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 418 | def tearDown(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 419 | """ Show various debug prints after each test """ |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 420 | self.logger.debug("--- tearDown() for %s.%s(%s) called ---" % |
| 421 | (self.__class__.__name__, self._testMethodName, |
| 422 | self._testMethodDoc)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 423 | if not self.vpp_dead: |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 424 | self.logger.debug(self.vapi.cli("show trace")) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 425 | self.logger.info(self.vapi.ppcli("show interface")) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 426 | self.logger.info(self.vapi.ppcli("show hardware")) |
| 427 | self.logger.info(self.vapi.ppcli("show error")) |
| 428 | self.logger.info(self.vapi.ppcli("show run")) |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 429 | self.registry.remove_vpp_config(self.logger) |
Dave Wallace | 90c5572 | 2017-02-16 11:25:26 -0500 | [diff] [blame] | 430 | # Save/Dump VPP api trace log |
| 431 | api_trace = "vpp_api_trace.%s.log" % self._testMethodName |
| 432 | tmp_api_trace = "/tmp/%s" % api_trace |
| 433 | vpp_api_trace_log = "%s/%s" % (self.tempdir, api_trace) |
| 434 | self.logger.info(self.vapi.ppcli("api trace save %s" % api_trace)) |
| 435 | self.logger.info("Moving %s to %s\n" % (tmp_api_trace, |
| 436 | vpp_api_trace_log)) |
| 437 | os.rename(tmp_api_trace, vpp_api_trace_log) |
| 438 | self.logger.info(self.vapi.ppcli("api trace dump %s" % |
| 439 | vpp_api_trace_log)) |
Klement Sekera | 1b68640 | 2017-03-02 11:29:19 +0100 | [diff] [blame] | 440 | else: |
| 441 | self.registry.unregister_all(self.logger) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 442 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 443 | def setUp(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 444 | """ Clear trace before running each test""" |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 445 | self.reporter.send_keep_alive(self) |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 446 | self.logger.debug("--- setUp() for %s.%s(%s) called ---" % |
| 447 | (self.__class__.__name__, self._testMethodName, |
| 448 | self._testMethodDoc)) |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 449 | if self.vpp_dead: |
| 450 | raise Exception("VPP is dead when setting up the test") |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 451 | self.sleep(.1, "during setUp") |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 452 | self.vpp_stdout_deque.append( |
| 453 | "--- test setUp() for %s.%s(%s) starts here ---\n" % |
| 454 | (self.__class__.__name__, self._testMethodName, |
| 455 | self._testMethodDoc)) |
| 456 | self.vpp_stderr_deque.append( |
| 457 | "--- test setUp() for %s.%s(%s) starts here ---\n" % |
| 458 | (self.__class__.__name__, self._testMethodName, |
| 459 | self._testMethodDoc)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 460 | self.vapi.cli("clear trace") |
| 461 | # store the test instance inside the test class - so that objects |
| 462 | # holding the class can access instance methods (like assertEqual) |
| 463 | type(self).test_instance = self |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 464 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 465 | @classmethod |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 466 | def pg_enable_capture(cls, interfaces): |
| 467 | """ |
| 468 | Enable capture on packet-generator interfaces |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 469 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 470 | :param interfaces: iterable interface indexes |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 471 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 472 | """ |
| 473 | for i in interfaces: |
| 474 | i.enable_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 475 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 476 | @classmethod |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 477 | def register_capture(cls, cap_name): |
| 478 | """ Register a capture in the testclass """ |
| 479 | # add to the list of captures with current timestamp |
| 480 | cls._captures.append((time.time(), cap_name)) |
| 481 | # filter out from zombies |
| 482 | cls._zombie_captures = [(stamp, name) |
| 483 | for (stamp, name) in cls._zombie_captures |
| 484 | if name != cap_name] |
| 485 | |
| 486 | @classmethod |
| 487 | def pg_start(cls): |
| 488 | """ Remove any zombie captures and enable the packet generator """ |
| 489 | # how long before capture is allowed to be deleted - otherwise vpp |
| 490 | # crashes - 100ms seems enough (this shouldn't be needed at all) |
| 491 | capture_ttl = 0.1 |
| 492 | now = time.time() |
| 493 | for stamp, cap_name in cls._zombie_captures: |
| 494 | wait = stamp + capture_ttl - now |
| 495 | if wait > 0: |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 496 | cls.sleep(wait, "before deleting capture %s" % cap_name) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 497 | now = time.time() |
| 498 | cls.logger.debug("Removing zombie capture %s" % cap_name) |
| 499 | cls.vapi.cli('packet-generator delete %s' % cap_name) |
| 500 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 501 | cls.vapi.cli("trace add pg-input 50") # 50 is maximum |
| 502 | cls.vapi.cli('packet-generator enable') |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 503 | cls._zombie_captures = cls._captures |
| 504 | cls._captures = [] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 505 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 506 | @classmethod |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 507 | def create_pg_interfaces(cls, interfaces): |
| 508 | """ |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 509 | Create packet-generator interfaces. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 510 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 511 | :param interfaces: iterable indexes of the interfaces. |
| 512 | :returns: List of created interfaces. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 513 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 514 | """ |
| 515 | result = [] |
| 516 | for i in interfaces: |
| 517 | intf = VppPGInterface(cls, i) |
| 518 | setattr(cls, intf.name, intf) |
| 519 | result.append(intf) |
| 520 | cls.pg_interfaces = result |
| 521 | return result |
| 522 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 523 | @classmethod |
| 524 | def create_loopback_interfaces(cls, interfaces): |
| 525 | """ |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 526 | Create loopback interfaces. |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 527 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 528 | :param interfaces: iterable indexes of the interfaces. |
| 529 | :returns: List of created interfaces. |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 530 | """ |
| 531 | result = [] |
| 532 | for i in interfaces: |
| 533 | intf = VppLoInterface(cls, i) |
| 534 | setattr(cls, intf.name, intf) |
| 535 | result.append(intf) |
| 536 | cls.lo_interfaces = result |
| 537 | return result |
| 538 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 539 | @staticmethod |
| 540 | def extend_packet(packet, size): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 541 | """ |
| 542 | Extend packet to given size by padding with spaces |
| 543 | NOTE: Currently works only when Raw layer is present. |
| 544 | |
| 545 | :param packet: packet |
| 546 | :param size: target size |
| 547 | |
| 548 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 549 | packet_len = len(packet) + 4 |
| 550 | extend = size - packet_len |
| 551 | if extend > 0: |
| 552 | packet[Raw].load += ' ' * extend |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 553 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 554 | @classmethod |
| 555 | def reset_packet_infos(cls): |
| 556 | """ Reset the list of packet info objects and packet counts to zero """ |
| 557 | cls._packet_infos = {} |
| 558 | cls._packet_count_for_dst_if_idx = {} |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 559 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 560 | @classmethod |
| 561 | def create_packet_info(cls, src_if, dst_if): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 562 | """ |
| 563 | Create packet info object containing the source and destination indexes |
| 564 | and add it to the testcase's packet info list |
| 565 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 566 | :param VppInterface src_if: source interface |
| 567 | :param VppInterface dst_if: destination interface |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 568 | |
| 569 | :returns: _PacketInfo object |
| 570 | |
| 571 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 572 | info = _PacketInfo() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 573 | info.index = len(cls._packet_infos) |
| 574 | info.src = src_if.sw_if_index |
| 575 | info.dst = dst_if.sw_if_index |
| 576 | if isinstance(dst_if, VppSubInterface): |
| 577 | dst_idx = dst_if.parent.sw_if_index |
| 578 | else: |
| 579 | dst_idx = dst_if.sw_if_index |
| 580 | if dst_idx in cls._packet_count_for_dst_if_idx: |
| 581 | cls._packet_count_for_dst_if_idx[dst_idx] += 1 |
| 582 | else: |
| 583 | cls._packet_count_for_dst_if_idx[dst_idx] = 1 |
| 584 | cls._packet_infos[info.index] = info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 585 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 586 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 587 | @staticmethod |
| 588 | def info_to_payload(info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 589 | """ |
| 590 | Convert _PacketInfo object to packet payload |
| 591 | |
| 592 | :param info: _PacketInfo object |
| 593 | |
| 594 | :returns: string containing serialized data from packet info |
| 595 | """ |
Pavel Kotucek | 59dda06 | 2017-03-02 15:22:47 +0100 | [diff] [blame] | 596 | return "%d %d %d %d %d" % (info.index, info.src, info.dst, |
| 597 | info.ip, info.proto) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 598 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 599 | @staticmethod |
| 600 | def payload_to_info(payload): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 601 | """ |
| 602 | Convert packet payload to _PacketInfo object |
| 603 | |
| 604 | :param payload: packet payload |
| 605 | |
| 606 | :returns: _PacketInfo object containing de-serialized data from payload |
| 607 | |
| 608 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 609 | numbers = payload.split() |
| 610 | info = _PacketInfo() |
| 611 | info.index = int(numbers[0]) |
| 612 | info.src = int(numbers[1]) |
| 613 | info.dst = int(numbers[2]) |
Pavel Kotucek | 59dda06 | 2017-03-02 15:22:47 +0100 | [diff] [blame] | 614 | info.ip = int(numbers[3]) |
| 615 | info.proto = int(numbers[4]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 616 | return info |
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 | def get_next_packet_info(self, info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 619 | """ |
| 620 | Iterate over the packet info list stored in the testcase |
| 621 | Start iteration with first element if info is None |
| 622 | Continue based on index in info if info is specified |
| 623 | |
| 624 | :param info: info or None |
| 625 | :returns: next info in list or None if no more infos |
| 626 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 627 | if info is None: |
| 628 | next_index = 0 |
| 629 | else: |
| 630 | next_index = info.index + 1 |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 631 | if next_index == len(self._packet_infos): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 632 | return None |
| 633 | else: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 634 | return self._packet_infos[next_index] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 635 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 636 | def get_next_packet_info_for_interface(self, src_index, info): |
| 637 | """ |
| 638 | Search the packet info list for the next packet info with same source |
| 639 | interface index |
| 640 | |
| 641 | :param src_index: source interface index to search for |
| 642 | :param info: packet info - where to start the search |
| 643 | :returns: packet info or None |
| 644 | |
| 645 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 646 | while True: |
| 647 | info = self.get_next_packet_info(info) |
| 648 | if info is None: |
| 649 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 650 | if info.src == src_index: |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 651 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 652 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 653 | def get_next_packet_info_for_interface2(self, src_index, dst_index, info): |
| 654 | """ |
| 655 | Search the packet info list for the next packet info with same source |
| 656 | and destination interface indexes |
| 657 | |
| 658 | :param src_index: source interface index to search for |
| 659 | :param dst_index: destination interface index to search for |
| 660 | :param info: packet info - where to start the search |
| 661 | :returns: packet info or None |
| 662 | |
| 663 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 664 | while True: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 665 | info = self.get_next_packet_info_for_interface(src_index, info) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 666 | if info is None: |
| 667 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 668 | if info.dst == dst_index: |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 669 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 670 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 671 | def assert_equal(self, real_value, expected_value, name_or_class=None): |
| 672 | if name_or_class is None: |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 673 | self.assertEqual(real_value, expected_value) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 674 | return |
| 675 | try: |
| 676 | msg = "Invalid %s: %d('%s') does not match expected value %d('%s')" |
| 677 | msg = msg % (getdoc(name_or_class).strip(), |
| 678 | real_value, str(name_or_class(real_value)), |
| 679 | expected_value, str(name_or_class(expected_value))) |
| 680 | except: |
| 681 | msg = "Invalid %s: %s does not match expected value %s" % ( |
| 682 | name_or_class, real_value, expected_value) |
| 683 | |
| 684 | self.assertEqual(real_value, expected_value, msg) |
| 685 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 686 | def assert_in_range(self, |
| 687 | real_value, |
| 688 | expected_min, |
| 689 | expected_max, |
| 690 | name=None): |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 691 | if name is None: |
| 692 | msg = None |
| 693 | else: |
| 694 | msg = "Invalid %s: %s out of range <%s,%s>" % ( |
| 695 | name, real_value, expected_min, expected_max) |
| 696 | self.assertTrue(expected_min <= real_value <= expected_max, msg) |
| 697 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 698 | @classmethod |
| 699 | def sleep(cls, timeout, remark=None): |
| 700 | if hasattr(cls, 'logger'): |
Klement Sekera | 3cfa558 | 2017-04-19 07:10:58 +0000 | [diff] [blame] | 701 | cls.logger.debug("Starting sleep for %ss (%s)" % (timeout, remark)) |
| 702 | before = time.time() |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 703 | time.sleep(timeout) |
Klement Sekera | 3cfa558 | 2017-04-19 07:10:58 +0000 | [diff] [blame] | 704 | after = time.time() |
| 705 | if after - before > 2 * timeout: |
Klement Sekera | 60c1223 | 2017-07-18 10:33:06 +0200 | [diff] [blame] | 706 | cls.logger.error("unexpected time.sleep() result - " |
| 707 | "slept for %ss instead of ~%ss!" % ( |
| 708 | after - before, timeout)) |
Klement Sekera | 3cfa558 | 2017-04-19 07:10:58 +0000 | [diff] [blame] | 709 | if hasattr(cls, 'logger'): |
| 710 | cls.logger.debug( |
| 711 | "Finished sleep (%s) - slept %ss (wanted %ss)" % ( |
| 712 | remark, after - before, timeout)) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 713 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 714 | |
Klement Sekera | 8713493 | 2017-03-07 11:39:27 +0100 | [diff] [blame] | 715 | class TestCasePrinter(object): |
| 716 | _shared_state = {} |
| 717 | |
| 718 | def __init__(self): |
| 719 | self.__dict__ = self._shared_state |
| 720 | if not hasattr(self, "_test_case_set"): |
| 721 | self._test_case_set = set() |
| 722 | |
| 723 | def print_test_case_heading_if_first_time(self, case): |
| 724 | if case.__class__ not in self._test_case_set: |
| 725 | print(double_line_delim) |
| 726 | print(colorize(getdoc(case.__class__).splitlines()[0], YELLOW)) |
| 727 | print(double_line_delim) |
| 728 | self._test_case_set.add(case.__class__) |
| 729 | |
| 730 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 731 | class VppTestResult(unittest.TestResult): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 732 | """ |
| 733 | @property result_string |
| 734 | String variable to store the test case result string. |
| 735 | @property errors |
| 736 | List variable containing 2-tuples of TestCase instances and strings |
| 737 | holding formatted tracebacks. Each tuple represents a test which |
| 738 | raised an unexpected exception. |
| 739 | @property failures |
| 740 | List variable containing 2-tuples of TestCase instances and strings |
| 741 | holding formatted tracebacks. Each tuple represents a test where |
| 742 | a failure was explicitly signalled using the TestCase.assert*() |
| 743 | methods. |
| 744 | """ |
| 745 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 746 | def __init__(self, stream, descriptions, verbosity): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 747 | """ |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 748 | :param stream File descriptor to store where to report test results. |
| 749 | Set to the standard error stream by default. |
| 750 | :param descriptions Boolean variable to store information if to use |
| 751 | test case descriptions. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 752 | :param verbosity Integer variable to store required verbosity level. |
| 753 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 754 | unittest.TestResult.__init__(self, stream, descriptions, verbosity) |
| 755 | self.stream = stream |
| 756 | self.descriptions = descriptions |
| 757 | self.verbosity = verbosity |
| 758 | self.result_string = None |
Klement Sekera | 8713493 | 2017-03-07 11:39:27 +0100 | [diff] [blame] | 759 | self.printer = TestCasePrinter() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 760 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 761 | def addSuccess(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 762 | """ |
| 763 | Record a test succeeded result |
| 764 | |
| 765 | :param test: |
| 766 | |
| 767 | """ |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 768 | if hasattr(test, 'logger'): |
| 769 | test.logger.debug("--- addSuccess() %s.%s(%s) called" |
| 770 | % (test.__class__.__name__, |
| 771 | test._testMethodName, |
| 772 | test._testMethodDoc)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 773 | unittest.TestResult.addSuccess(self, test) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 774 | self.result_string = colorize("OK", GREEN) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 775 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 776 | def addSkip(self, test, reason): |
| 777 | """ |
| 778 | Record a test skipped. |
| 779 | |
| 780 | :param test: |
| 781 | :param reason: |
| 782 | |
| 783 | """ |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 784 | if hasattr(test, 'logger'): |
| 785 | test.logger.debug("--- addSkip() %s.%s(%s) called, reason is %s" |
| 786 | % (test.__class__.__name__, |
| 787 | test._testMethodName, |
| 788 | test._testMethodDoc, |
| 789 | reason)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 790 | unittest.TestResult.addSkip(self, test, reason) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 791 | self.result_string = colorize("SKIP", YELLOW) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 792 | |
Klement Sekera | f413bef | 2017-08-15 07:09:02 +0200 | [diff] [blame] | 793 | def symlink_failed(self, test): |
| 794 | logger = None |
| 795 | if hasattr(test, 'logger'): |
| 796 | logger = test.logger |
| 797 | if hasattr(test, 'tempdir'): |
| 798 | try: |
| 799 | failed_dir = os.getenv('VPP_TEST_FAILED_DIR') |
| 800 | link_path = '%s/%s-FAILED' % (failed_dir, |
| 801 | test.tempdir.split("/")[-1]) |
| 802 | if logger: |
| 803 | logger.debug("creating a link to the failed test") |
| 804 | logger.debug("os.symlink(%s, %s)" % |
| 805 | (test.tempdir, link_path)) |
| 806 | os.symlink(test.tempdir, link_path) |
| 807 | except Exception as e: |
| 808 | if logger: |
| 809 | logger.error(e) |
| 810 | |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 811 | def send_failure_through_pipe(self, test): |
| 812 | if hasattr(self, 'test_framework_failed_pipe'): |
| 813 | pipe = self.test_framework_failed_pipe |
| 814 | if pipe: |
| 815 | pipe.send(test.__class__) |
| 816 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 817 | def addFailure(self, test, err): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 818 | """ |
| 819 | Record a test failed result |
| 820 | |
| 821 | :param test: |
| 822 | :param err: error message |
| 823 | |
| 824 | """ |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 825 | if hasattr(test, 'logger'): |
| 826 | test.logger.debug("--- addFailure() %s.%s(%s) called, err is %s" |
| 827 | % (test.__class__.__name__, |
| 828 | test._testMethodName, |
| 829 | test._testMethodDoc, err)) |
| 830 | test.logger.debug("formatted exception is:\n%s" % |
| 831 | "".join(format_exception(*err))) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 832 | unittest.TestResult.addFailure(self, test, err) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 833 | if hasattr(test, 'tempdir'): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 834 | self.result_string = colorize("FAIL", RED) + \ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 835 | ' [ temp dir used by test case: ' + test.tempdir + ' ]' |
Klement Sekera | f413bef | 2017-08-15 07:09:02 +0200 | [diff] [blame] | 836 | self.symlink_failed(test) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 837 | else: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 838 | self.result_string = colorize("FAIL", RED) + ' [no temp dir]' |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 839 | |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 840 | self.send_failure_through_pipe(test) |
| 841 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 842 | def addError(self, test, err): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 843 | """ |
| 844 | Record a test error result |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 845 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 846 | :param test: |
| 847 | :param err: error message |
| 848 | |
| 849 | """ |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 850 | if hasattr(test, 'logger'): |
| 851 | test.logger.debug("--- addError() %s.%s(%s) called, err is %s" |
| 852 | % (test.__class__.__name__, |
| 853 | test._testMethodName, |
| 854 | test._testMethodDoc, err)) |
| 855 | test.logger.debug("formatted exception is:\n%s" % |
| 856 | "".join(format_exception(*err))) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 857 | unittest.TestResult.addError(self, test, err) |
| 858 | if hasattr(test, 'tempdir'): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 859 | self.result_string = colorize("ERROR", RED) + \ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 860 | ' [ temp dir used by test case: ' + test.tempdir + ' ]' |
Klement Sekera | f413bef | 2017-08-15 07:09:02 +0200 | [diff] [blame] | 861 | self.symlink_failed(test) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 862 | else: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 863 | self.result_string = colorize("ERROR", RED) + ' [no temp dir]' |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 864 | |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 865 | self.send_failure_through_pipe(test) |
| 866 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 867 | def getDescription(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 868 | """ |
| 869 | Get test description |
| 870 | |
| 871 | :param test: |
| 872 | :returns: test description |
| 873 | |
| 874 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 875 | # TODO: if none print warning not raise exception |
| 876 | short_description = test.shortDescription() |
| 877 | if self.descriptions and short_description: |
| 878 | return short_description |
| 879 | else: |
| 880 | return str(test) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 881 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 882 | def startTest(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 883 | """ |
| 884 | Start a test |
| 885 | |
| 886 | :param test: |
| 887 | |
| 888 | """ |
Klement Sekera | 8713493 | 2017-03-07 11:39:27 +0100 | [diff] [blame] | 889 | self.printer.print_test_case_heading_if_first_time(test) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 890 | unittest.TestResult.startTest(self, test) |
| 891 | if self.verbosity > 0: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 892 | self.stream.writeln( |
| 893 | "Starting " + self.getDescription(test) + " ...") |
| 894 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 895 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 896 | def stopTest(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 897 | """ |
| 898 | Stop a test |
| 899 | |
| 900 | :param test: |
| 901 | |
| 902 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 903 | unittest.TestResult.stopTest(self, test) |
| 904 | if self.verbosity > 0: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 905 | self.stream.writeln(single_line_delim) |
Klement Sekera | 52e84f3 | 2017-01-13 07:25:25 +0100 | [diff] [blame] | 906 | self.stream.writeln("%-73s%s" % (self.getDescription(test), |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 907 | self.result_string)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 908 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 909 | else: |
Klement Sekera | 52e84f3 | 2017-01-13 07:25:25 +0100 | [diff] [blame] | 910 | self.stream.writeln("%-73s%s" % (self.getDescription(test), |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 911 | self.result_string)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 912 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 913 | def printErrors(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 914 | """ |
| 915 | Print errors from running the test case |
| 916 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 917 | self.stream.writeln() |
| 918 | self.printErrorList('ERROR', self.errors) |
| 919 | self.printErrorList('FAIL', self.failures) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 920 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 921 | def printErrorList(self, flavour, errors): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 922 | """ |
| 923 | Print error list to the output stream together with error type |
| 924 | and test case description. |
| 925 | |
| 926 | :param flavour: error type |
| 927 | :param errors: iterable errors |
| 928 | |
| 929 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 930 | for test, err in errors: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 931 | self.stream.writeln(double_line_delim) |
| 932 | self.stream.writeln("%s: %s" % |
| 933 | (flavour, self.getDescription(test))) |
| 934 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 935 | self.stream.writeln("%s" % err) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 936 | |
| 937 | |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 938 | class Filter_by_test_option: |
| 939 | def __init__(self, filter_file_name, filter_class_name, filter_func_name): |
| 940 | self.filter_file_name = filter_file_name |
| 941 | self.filter_class_name = filter_class_name |
| 942 | self.filter_func_name = filter_func_name |
| 943 | |
| 944 | def __call__(self, file_name, class_name, func_name): |
| 945 | if self.filter_file_name and file_name != self.filter_file_name: |
| 946 | return False |
| 947 | if self.filter_class_name and class_name != self.filter_class_name: |
| 948 | return False |
| 949 | if self.filter_func_name and func_name != self.filter_func_name: |
| 950 | return False |
| 951 | return True |
| 952 | |
| 953 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 954 | class VppTestRunner(unittest.TextTestRunner): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 955 | """ |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 956 | A basic test runner implementation which prints results to standard error. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 957 | """ |
| 958 | @property |
| 959 | def resultclass(self): |
| 960 | """Class maintaining the results of the tests""" |
| 961 | return VppTestResult |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 962 | |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 963 | def __init__(self, keep_alive_pipe=None, failed_pipe=None, |
| 964 | stream=sys.stderr, descriptions=True, |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 965 | verbosity=1, failfast=False, buffer=False, resultclass=None): |
Klement Sekera | 7a161da | 2017-01-17 13:42:48 +0100 | [diff] [blame] | 966 | # ignore stream setting here, use hard-coded stdout to be in sync |
| 967 | # with prints from VppTestCase methods ... |
| 968 | super(VppTestRunner, self).__init__(sys.stdout, descriptions, |
| 969 | verbosity, failfast, buffer, |
| 970 | resultclass) |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 971 | reporter = KeepAliveReporter() |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 972 | reporter.pipe = keep_alive_pipe |
| 973 | # this is super-ugly, but very simple to implement and works as long |
| 974 | # as we run only one test at the same time |
| 975 | VppTestResult.test_framework_failed_pipe = failed_pipe |
Klement Sekera | 7a161da | 2017-01-17 13:42:48 +0100 | [diff] [blame] | 976 | |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 977 | test_option = "TEST" |
| 978 | |
| 979 | def parse_test_option(self): |
| 980 | try: |
| 981 | f = os.getenv(self.test_option) |
| 982 | except: |
| 983 | f = None |
| 984 | filter_file_name = None |
| 985 | filter_class_name = None |
| 986 | filter_func_name = None |
| 987 | if f: |
| 988 | if '.' in f: |
| 989 | parts = f.split('.') |
| 990 | if len(parts) > 3: |
| 991 | raise Exception("Unrecognized %s option: %s" % |
| 992 | (self.test_option, f)) |
| 993 | if len(parts) > 2: |
| 994 | if parts[2] not in ('*', ''): |
| 995 | filter_func_name = parts[2] |
| 996 | if parts[1] not in ('*', ''): |
| 997 | filter_class_name = parts[1] |
| 998 | if parts[0] not in ('*', ''): |
| 999 | if parts[0].startswith('test_'): |
| 1000 | filter_file_name = parts[0] |
| 1001 | else: |
| 1002 | filter_file_name = 'test_%s' % parts[0] |
| 1003 | else: |
| 1004 | if f.startswith('test_'): |
| 1005 | filter_file_name = f |
| 1006 | else: |
| 1007 | filter_file_name = 'test_%s' % f |
| 1008 | return filter_file_name, filter_class_name, filter_func_name |
| 1009 | |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 1010 | @staticmethod |
| 1011 | def filter_tests(tests, filter_cb): |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 1012 | result = unittest.suite.TestSuite() |
| 1013 | for t in tests: |
| 1014 | if isinstance(t, unittest.suite.TestSuite): |
| 1015 | # this is a bunch of tests, recursively filter... |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 1016 | x = filter_tests(t, filter_cb) |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 1017 | if x.countTestCases() > 0: |
| 1018 | result.addTest(x) |
| 1019 | elif isinstance(t, unittest.TestCase): |
| 1020 | # this is a single test |
| 1021 | parts = t.id().split('.') |
| 1022 | # t.id() for common cases like this: |
| 1023 | # test_classifier.TestClassifier.test_acl_ip |
| 1024 | # apply filtering only if it is so |
| 1025 | if len(parts) == 3: |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 1026 | if not filter_cb(parts[0], parts[1], parts[2]): |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 1027 | continue |
| 1028 | result.addTest(t) |
| 1029 | else: |
| 1030 | # unexpected object, don't touch it |
| 1031 | result.addTest(t) |
| 1032 | return result |
| 1033 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1034 | def run(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1035 | """ |
| 1036 | Run the tests |
| 1037 | |
| 1038 | :param test: |
| 1039 | |
| 1040 | """ |
Klement Sekera | 3658adc | 2017-06-07 08:19:47 +0200 | [diff] [blame] | 1041 | faulthandler.enable() # emit stack trace to stderr if killed by signal |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1042 | print("Running tests using custom test runner") # debug message |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 1043 | filter_file, filter_class, filter_func = self.parse_test_option() |
| 1044 | print("Active filters: file=%s, class=%s, function=%s" % ( |
| 1045 | filter_file, filter_class, filter_func)) |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 1046 | filter_cb = Filter_by_test_option( |
| 1047 | filter_file, filter_class, filter_func) |
| 1048 | filtered = self.filter_tests(test, filter_cb) |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 1049 | print("%s out of %s tests match specified filters" % ( |
| 1050 | filtered.countTestCases(), test.countTestCases())) |
Klement Sekera | 3747c75 | 2017-04-10 06:30:17 +0200 | [diff] [blame] | 1051 | if not running_extended_tests(): |
| 1052 | print("Not running extended tests (some tests will be skipped)") |
Klement Sekera | 104543f | 2017-02-03 07:29:43 +0100 | [diff] [blame] | 1053 | return super(VppTestRunner, self).run(filtered) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame^] | 1054 | |
| 1055 | |
| 1056 | class Worker(Thread): |
| 1057 | def __init__(self, args, logger): |
| 1058 | self.logger = logger |
| 1059 | self.args = args |
| 1060 | self.result = None |
| 1061 | super(Worker, self).__init__() |
| 1062 | |
| 1063 | def run(self): |
| 1064 | executable = self.args[0] |
| 1065 | self.logger.debug("Running executable w/args `%s'" % self.args) |
| 1066 | env = os.environ.copy() |
| 1067 | env["CK_LOG_FILE_NAME"] = "-" |
| 1068 | self.process = subprocess.Popen( |
| 1069 | self.args, shell=False, env=env, preexec_fn=os.setpgrp, |
| 1070 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 1071 | out, err = self.process.communicate() |
| 1072 | self.logger.debug("Finished running `%s'" % executable) |
| 1073 | self.logger.info("Return code is `%s'" % self.process.returncode) |
| 1074 | self.logger.info(single_line_delim) |
| 1075 | self.logger.info("Executable `%s' wrote to stdout:" % executable) |
| 1076 | self.logger.info(single_line_delim) |
| 1077 | self.logger.info(out) |
| 1078 | self.logger.info(single_line_delim) |
| 1079 | self.logger.info("Executable `%s' wrote to stderr:" % executable) |
| 1080 | self.logger.info(single_line_delim) |
| 1081 | self.logger.error(err) |
| 1082 | self.logger.info(single_line_delim) |
| 1083 | self.result = self.process.returncode |