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 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 3 | import subprocess |
| 4 | import unittest |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 5 | import tempfile |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 6 | import time |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 7 | import resource |
| 8 | from time import sleep |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 9 | from collections import deque |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 10 | from threading import Thread |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 11 | from inspect import getdoc |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 12 | from hook import StepHook, PollHook |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 13 | from vpp_pg_interface import VppPGInterface |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 14 | from vpp_lo_interface import VppLoInterface |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 15 | from vpp_papi_provider import VppPapiProvider |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 16 | from scapy.packet import Raw |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 17 | from log import * |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 18 | |
| 19 | """ |
| 20 | Test framework module. |
| 21 | |
| 22 | The module provides a set of tools for constructing and running tests and |
| 23 | representing the results. |
| 24 | """ |
| 25 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 26 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 27 | class _PacketInfo(object): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 28 | """Private class to create packet info object. |
| 29 | |
| 30 | Help process information about the next packet. |
| 31 | Set variables to default values. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 32 | """ |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 33 | #: Store the index of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 34 | index = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 35 | #: Store the index of the source packet generator interface of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 36 | src = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 37 | #: Store the index of the destination packet generator interface |
| 38 | #: of the packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 39 | dst = -1 |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 40 | #: Store the copy of the former packet. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 41 | data = None |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 42 | |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 43 | def __eq__(self, other): |
| 44 | index = self.index == other.index |
| 45 | src = self.src == other.src |
| 46 | dst = self.dst == other.dst |
| 47 | data = self.data == other.data |
| 48 | return index and src and dst and data |
| 49 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 50 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 51 | def pump_output(out, deque): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 52 | for line in iter(out.readline, b''): |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 53 | deque.append(line) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 54 | |
| 55 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 56 | class VppTestCase(unittest.TestCase): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 57 | """This subclass is a base class for VPP test cases that are implemented as |
| 58 | classes. It provides methods to create and run test case. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 59 | """ |
| 60 | |
| 61 | @property |
| 62 | def packet_infos(self): |
| 63 | """List of packet infos""" |
| 64 | return self._packet_infos |
| 65 | |
| 66 | @packet_infos.setter |
| 67 | def packet_infos(self, value): |
| 68 | self._packet_infos = value |
| 69 | |
| 70 | @classmethod |
| 71 | def instance(cls): |
| 72 | """Return the instance of this testcase""" |
| 73 | return cls.test_instance |
| 74 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 75 | @classmethod |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 76 | def set_debug_flags(cls, d): |
| 77 | cls.debug_core = False |
| 78 | cls.debug_gdb = False |
| 79 | cls.debug_gdbserver = False |
| 80 | if d is None: |
| 81 | return |
| 82 | dl = d.lower() |
| 83 | if dl == "core": |
| 84 | if resource.getrlimit(resource.RLIMIT_CORE)[0] <= 0: |
| 85 | # give a heads up if this is actually useless |
| 86 | cls.logger.critical("WARNING: core size limit is set 0, core " |
| 87 | "files will NOT be created") |
| 88 | cls.debug_core = True |
| 89 | elif dl == "gdb": |
| 90 | cls.debug_gdb = True |
| 91 | elif dl == "gdbserver": |
| 92 | cls.debug_gdbserver = True |
| 93 | else: |
| 94 | raise Exception("Unrecognized DEBUG option: '%s'" % d) |
| 95 | |
| 96 | @classmethod |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 97 | def setUpConstants(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 98 | """ Set-up the test case class based on environment variables """ |
| 99 | try: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 100 | s = os.getenv("STEP") |
| 101 | cls.step = True if s.lower() in ("y", "yes", "1") else False |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 102 | except: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 103 | cls.step = False |
| 104 | try: |
| 105 | d = os.getenv("DEBUG") |
| 106 | except: |
| 107 | d = None |
| 108 | cls.set_debug_flags(d) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 109 | cls.vpp_bin = os.getenv('VPP_TEST_BIN', "vpp") |
Pierre Pfister | cd8e318 | 2016-10-07 16:30:03 +0100 | [diff] [blame] | 110 | cls.plugin_path = os.getenv('VPP_TEST_PLUGIN_PATH') |
Klement Sekera | 01bbbe9 | 2016-11-02 09:25:05 +0100 | [diff] [blame] | 111 | debug_cli = "" |
| 112 | if cls.step or cls.debug_gdb or cls.debug_gdbserver: |
| 113 | debug_cli = "cli-listen localhost:5002" |
| 114 | cls.vpp_cmdline = [cls.vpp_bin, "unix", "{", "nodaemon", debug_cli, "}", |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 115 | "api-segment", "{", "prefix", cls.shm_prefix, "}"] |
Pierre Pfister | cd8e318 | 2016-10-07 16:30:03 +0100 | [diff] [blame] | 116 | if cls.plugin_path is not None: |
| 117 | cls.vpp_cmdline.extend(["plugin_path", cls.plugin_path]) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 118 | cls.logger.info("vpp_cmdline: %s" % cls.vpp_cmdline) |
| 119 | |
| 120 | @classmethod |
| 121 | def wait_for_enter(cls): |
| 122 | if cls.debug_gdbserver: |
| 123 | print(double_line_delim) |
| 124 | print("Spawned GDB server with PID: %d" % cls.vpp.pid) |
| 125 | elif cls.debug_gdb: |
| 126 | print(double_line_delim) |
| 127 | print("Spawned VPP with PID: %d" % cls.vpp.pid) |
| 128 | else: |
| 129 | cls.logger.debug("Spawned VPP with PID: %d" % cls.vpp.pid) |
| 130 | return |
| 131 | print(single_line_delim) |
| 132 | print("You can debug the VPP using e.g.:") |
| 133 | if cls.debug_gdbserver: |
| 134 | print("gdb " + cls.vpp_bin + " -ex 'target remote localhost:7777'") |
| 135 | print("Now is the time to attach a gdb by running the above " |
| 136 | "command, set up breakpoints etc. and then resume VPP from " |
| 137 | "within gdb by issuing the 'continue' command") |
| 138 | elif cls.debug_gdb: |
| 139 | print("gdb " + cls.vpp_bin + " -ex 'attach %s'" % cls.vpp.pid) |
| 140 | print("Now is the time to attach a gdb by running the above " |
| 141 | "command and set up breakpoints etc.") |
| 142 | print(single_line_delim) |
| 143 | raw_input("Press ENTER to continue running the testcase...") |
| 144 | |
| 145 | @classmethod |
| 146 | def run_vpp(cls): |
| 147 | cmdline = cls.vpp_cmdline |
| 148 | |
| 149 | if cls.debug_gdbserver: |
Klement Sekera | 931be3a | 2016-11-03 05:36:01 +0100 | [diff] [blame] | 150 | gdbserver = '/usr/bin/gdbserver' |
| 151 | if not os.path.isfile(gdbserver) or \ |
| 152 | not os.access(gdbserver, os.X_OK): |
| 153 | raise Exception("gdbserver binary '%s' does not exist or is " |
| 154 | "not executable" % gdbserver) |
| 155 | |
| 156 | cmdline = [gdbserver, 'localhost:7777'] + cls.vpp_cmdline |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 157 | cls.logger.info("Gdbserver cmdline is %s", " ".join(cmdline)) |
| 158 | |
Klement Sekera | 931be3a | 2016-11-03 05:36:01 +0100 | [diff] [blame] | 159 | try: |
| 160 | cls.vpp = subprocess.Popen(cmdline, |
| 161 | stdout=subprocess.PIPE, |
| 162 | stderr=subprocess.PIPE, |
| 163 | bufsize=1) |
| 164 | except Exception as e: |
| 165 | cls.logger.critical("Couldn't start vpp: %s" % e) |
| 166 | raise |
| 167 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 168 | cls.wait_for_enter() |
Pierre Pfister | cd8e318 | 2016-10-07 16:30:03 +0100 | [diff] [blame] | 169 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 170 | @classmethod |
| 171 | def setUpClass(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 172 | """ |
| 173 | Perform class setup before running the testcase |
| 174 | Remove shared memory files, start vpp and connect the vpp-api |
| 175 | """ |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 176 | cls.logger = getLogger(cls.__name__) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 177 | cls.tempdir = tempfile.mkdtemp( |
| 178 | prefix='vpp-unittest-' + cls.__name__ + '-') |
| 179 | cls.shm_prefix = cls.tempdir.split("/")[-1] |
| 180 | os.chdir(cls.tempdir) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 181 | cls.logger.info("Temporary dir is %s, shm prefix is %s", |
| 182 | cls.tempdir, cls.shm_prefix) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 183 | cls.setUpConstants() |
| 184 | cls.pg_streams = [] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 185 | cls.packet_infos = {} |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 186 | cls.verbose = 0 |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 187 | cls.vpp_dead = False |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 188 | print(double_line_delim) |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 189 | print(colorize(getdoc(cls).splitlines()[0], YELLOW)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 190 | print(double_line_delim) |
| 191 | # need to catch exceptions here because if we raise, then the cleanup |
| 192 | # doesn't get called and we might end with a zombie vpp |
| 193 | try: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 194 | cls.run_vpp() |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 195 | cls.vpp_stdout_deque = deque() |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 196 | cls.vpp_stdout_reader_thread = Thread(target=pump_output, args=( |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 197 | cls.vpp.stdout, cls.vpp_stdout_deque)) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 198 | cls.vpp_stdout_reader_thread.start() |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 199 | cls.vpp_stderr_deque = deque() |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 200 | cls.vpp_stderr_reader_thread = Thread(target=pump_output, args=( |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 201 | cls.vpp.stderr, cls.vpp_stderr_deque)) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 202 | cls.vpp_stderr_reader_thread.start() |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 203 | cls.vapi = VppPapiProvider(cls.shm_prefix, cls.shm_prefix, cls) |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 204 | if cls.step: |
| 205 | hook = StepHook(cls) |
| 206 | else: |
| 207 | hook = PollHook(cls) |
| 208 | cls.vapi.register_hook(hook) |
| 209 | time.sleep(0.1) |
| 210 | hook.poll_vpp() |
| 211 | try: |
| 212 | cls.vapi.connect() |
| 213 | except: |
| 214 | if cls.debug_gdbserver: |
| 215 | print(colorize("You're running VPP inside gdbserver but " |
| 216 | "VPP-API connection failed, did you forget " |
| 217 | "to 'continue' VPP from within gdb?", RED)) |
| 218 | raise |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 219 | except: |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 220 | t, v, tb = sys.exc_info() |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 221 | try: |
| 222 | cls.quit() |
| 223 | except: |
| 224 | pass |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 225 | raise t, v, tb |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 226 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 227 | @classmethod |
| 228 | def quit(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 229 | """ |
| 230 | Disconnect vpp-api, kill vpp and cleanup shared memory files |
| 231 | """ |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 232 | if (cls.debug_gdbserver or cls.debug_gdb) and hasattr(cls, 'vpp'): |
| 233 | cls.vpp.poll() |
| 234 | if cls.vpp.returncode is None: |
| 235 | print(double_line_delim) |
| 236 | print("VPP or GDB server is still running") |
| 237 | print(single_line_delim) |
| 238 | raw_input("When done debugging, press ENTER to kill the process" |
| 239 | " and finish running the testcase...") |
| 240 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 241 | if hasattr(cls, 'vpp'): |
Klement Sekera | 0529a74 | 2016-12-02 07:05:24 +0100 | [diff] [blame] | 242 | if hasattr(cls, 'vapi'): |
| 243 | cls.vapi.disconnect() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 244 | cls.vpp.poll() |
| 245 | if cls.vpp.returncode is None: |
| 246 | cls.vpp.terminate() |
| 247 | del cls.vpp |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 248 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 249 | if hasattr(cls, 'vpp_stdout_deque'): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 250 | cls.logger.info(single_line_delim) |
| 251 | cls.logger.info('VPP output to stdout while running %s:', |
| 252 | cls.__name__) |
| 253 | cls.logger.info(single_line_delim) |
| 254 | f = open(cls.tempdir + '/vpp_stdout.txt', 'w') |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 255 | vpp_output = "".join(cls.vpp_stdout_deque) |
| 256 | f.write(vpp_output) |
| 257 | cls.logger.info('\n%s', vpp_output) |
| 258 | cls.logger.info(single_line_delim) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 259 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 260 | if hasattr(cls, 'vpp_stderr_deque'): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 261 | cls.logger.info(single_line_delim) |
| 262 | cls.logger.info('VPP output to stderr while running %s:', |
| 263 | cls.__name__) |
| 264 | cls.logger.info(single_line_delim) |
| 265 | f = open(cls.tempdir + '/vpp_stderr.txt', 'w') |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 266 | vpp_output = "".join(cls.vpp_stderr_deque) |
| 267 | f.write(vpp_output) |
| 268 | cls.logger.info('\n%s', vpp_output) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 269 | cls.logger.info(single_line_delim) |
| 270 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 271 | @classmethod |
| 272 | def tearDownClass(cls): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 273 | """ Perform final cleanup after running all tests in this test-case """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 274 | cls.quit() |
| 275 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 276 | def tearDown(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 277 | """ Show various debug prints after each test """ |
| 278 | if not self.vpp_dead: |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 279 | self.logger.debug(self.vapi.cli("show trace")) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 280 | self.logger.info(self.vapi.ppcli("show int")) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 281 | self.logger.info(self.vapi.ppcli("show hardware")) |
| 282 | self.logger.info(self.vapi.ppcli("show error")) |
| 283 | self.logger.info(self.vapi.ppcli("show run")) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 284 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 285 | def setUp(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 286 | """ Clear trace before running each test""" |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 287 | if self.vpp_dead: |
| 288 | raise Exception("VPP is dead when setting up the test") |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 289 | time.sleep(.1) |
| 290 | self.vpp_stdout_deque.append( |
| 291 | "--- test setUp() for %s.%s(%s) starts here ---\n" % |
| 292 | (self.__class__.__name__, self._testMethodName, |
| 293 | self._testMethodDoc)) |
| 294 | self.vpp_stderr_deque.append( |
| 295 | "--- test setUp() for %s.%s(%s) starts here ---\n" % |
| 296 | (self.__class__.__name__, self._testMethodName, |
| 297 | self._testMethodDoc)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 298 | self.vapi.cli("clear trace") |
| 299 | # store the test instance inside the test class - so that objects |
| 300 | # holding the class can access instance methods (like assertEqual) |
| 301 | type(self).test_instance = self |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 302 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 303 | @classmethod |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 304 | def pg_enable_capture(cls, interfaces): |
| 305 | """ |
| 306 | Enable capture on packet-generator interfaces |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 307 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 308 | :param interfaces: iterable interface indexes |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 309 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 310 | """ |
| 311 | for i in interfaces: |
| 312 | i.enable_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 313 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 314 | @classmethod |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 315 | def pg_start(cls, sleep_time=1): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 316 | """ |
| 317 | Enable the packet-generator and send all prepared packet streams |
| 318 | Remove the packet streams afterwards |
| 319 | """ |
| 320 | cls.vapi.cli("trace add pg-input 50") # 50 is maximum |
| 321 | cls.vapi.cli('packet-generator enable') |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 322 | sleep(sleep_time) # give VPP some time to process the packets |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 323 | for stream in cls.pg_streams: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 324 | cls.vapi.cli('packet-generator delete %s' % stream) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 325 | cls.pg_streams = [] |
| 326 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 327 | @classmethod |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 328 | def create_pg_interfaces(cls, interfaces): |
| 329 | """ |
| 330 | Create packet-generator interfaces |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 331 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 332 | :param interfaces: iterable indexes of the interfaces |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 333 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 334 | """ |
| 335 | result = [] |
| 336 | for i in interfaces: |
| 337 | intf = VppPGInterface(cls, i) |
| 338 | setattr(cls, intf.name, intf) |
| 339 | result.append(intf) |
| 340 | cls.pg_interfaces = result |
| 341 | return result |
| 342 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 343 | @classmethod |
| 344 | def create_loopback_interfaces(cls, interfaces): |
| 345 | """ |
| 346 | Create loopback interfaces |
| 347 | |
| 348 | :param interfaces: iterable indexes of the interfaces |
| 349 | |
| 350 | """ |
| 351 | result = [] |
| 352 | for i in interfaces: |
| 353 | intf = VppLoInterface(cls, i) |
| 354 | setattr(cls, intf.name, intf) |
| 355 | result.append(intf) |
| 356 | cls.lo_interfaces = result |
| 357 | return result |
| 358 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 359 | @staticmethod |
| 360 | def extend_packet(packet, size): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 361 | """ |
| 362 | Extend packet to given size by padding with spaces |
| 363 | NOTE: Currently works only when Raw layer is present. |
| 364 | |
| 365 | :param packet: packet |
| 366 | :param size: target size |
| 367 | |
| 368 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 369 | packet_len = len(packet) + 4 |
| 370 | extend = size - packet_len |
| 371 | if extend > 0: |
| 372 | packet[Raw].load += ' ' * extend |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 373 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 374 | def add_packet_info_to_list(self, info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 375 | """ |
| 376 | Add packet info to the testcase's packet info list |
| 377 | |
| 378 | :param info: packet info |
| 379 | |
| 380 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 381 | info.index = len(self.packet_infos) |
| 382 | self.packet_infos[info.index] = info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 383 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 384 | def create_packet_info(self, src_pg_index, dst_pg_index): |
| 385 | """ |
| 386 | Create packet info object containing the source and destination indexes |
| 387 | and add it to the testcase's packet info list |
| 388 | |
| 389 | :param src_pg_index: source packet-generator index |
| 390 | :param dst_pg_index: destination packet-generator index |
| 391 | |
| 392 | :returns: _PacketInfo object |
| 393 | |
| 394 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 395 | info = _PacketInfo() |
| 396 | self.add_packet_info_to_list(info) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 397 | info.src = src_pg_index |
| 398 | info.dst = dst_pg_index |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 399 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 400 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 401 | @staticmethod |
| 402 | def info_to_payload(info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 403 | """ |
| 404 | Convert _PacketInfo object to packet payload |
| 405 | |
| 406 | :param info: _PacketInfo object |
| 407 | |
| 408 | :returns: string containing serialized data from packet info |
| 409 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 410 | return "%d %d %d" % (info.index, info.src, info.dst) |
| 411 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 412 | @staticmethod |
| 413 | def payload_to_info(payload): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 414 | """ |
| 415 | Convert packet payload to _PacketInfo object |
| 416 | |
| 417 | :param payload: packet payload |
| 418 | |
| 419 | :returns: _PacketInfo object containing de-serialized data from payload |
| 420 | |
| 421 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 422 | numbers = payload.split() |
| 423 | info = _PacketInfo() |
| 424 | info.index = int(numbers[0]) |
| 425 | info.src = int(numbers[1]) |
| 426 | info.dst = int(numbers[2]) |
| 427 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 428 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 429 | def get_next_packet_info(self, info): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 430 | """ |
| 431 | Iterate over the packet info list stored in the testcase |
| 432 | Start iteration with first element if info is None |
| 433 | Continue based on index in info if info is specified |
| 434 | |
| 435 | :param info: info or None |
| 436 | :returns: next info in list or None if no more infos |
| 437 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 438 | if info is None: |
| 439 | next_index = 0 |
| 440 | else: |
| 441 | next_index = info.index + 1 |
| 442 | if next_index == len(self.packet_infos): |
| 443 | return None |
| 444 | else: |
| 445 | return self.packet_infos[next_index] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 446 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 447 | def get_next_packet_info_for_interface(self, src_index, info): |
| 448 | """ |
| 449 | Search the packet info list for the next packet info with same source |
| 450 | interface index |
| 451 | |
| 452 | :param src_index: source interface index to search for |
| 453 | :param info: packet info - where to start the search |
| 454 | :returns: packet info or None |
| 455 | |
| 456 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 457 | while True: |
| 458 | info = self.get_next_packet_info(info) |
| 459 | if info is None: |
| 460 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 461 | if info.src == src_index: |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 462 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 463 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 464 | def get_next_packet_info_for_interface2(self, src_index, dst_index, info): |
| 465 | """ |
| 466 | Search the packet info list for the next packet info with same source |
| 467 | and destination interface indexes |
| 468 | |
| 469 | :param src_index: source interface index to search for |
| 470 | :param dst_index: destination interface index to search for |
| 471 | :param info: packet info - where to start the search |
| 472 | :returns: packet info or None |
| 473 | |
| 474 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 475 | while True: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 476 | info = self.get_next_packet_info_for_interface(src_index, info) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 477 | if info is None: |
| 478 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 479 | if info.dst == dst_index: |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 480 | return info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 481 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 482 | def assert_equal(self, real_value, expected_value, name_or_class=None): |
| 483 | if name_or_class is None: |
| 484 | self.assertEqual(real_value, expected_value, msg) |
| 485 | return |
| 486 | try: |
| 487 | msg = "Invalid %s: %d('%s') does not match expected value %d('%s')" |
| 488 | msg = msg % (getdoc(name_or_class).strip(), |
| 489 | real_value, str(name_or_class(real_value)), |
| 490 | expected_value, str(name_or_class(expected_value))) |
| 491 | except: |
| 492 | msg = "Invalid %s: %s does not match expected value %s" % ( |
| 493 | name_or_class, real_value, expected_value) |
| 494 | |
| 495 | self.assertEqual(real_value, expected_value, msg) |
| 496 | |
| 497 | def assert_in_range( |
| 498 | self, |
| 499 | real_value, |
| 500 | expected_min, |
| 501 | expected_max, |
| 502 | name=None): |
| 503 | if name is None: |
| 504 | msg = None |
| 505 | else: |
| 506 | msg = "Invalid %s: %s out of range <%s,%s>" % ( |
| 507 | name, real_value, expected_min, expected_max) |
| 508 | self.assertTrue(expected_min <= real_value <= expected_max, msg) |
| 509 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 510 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 511 | class VppTestResult(unittest.TestResult): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 512 | """ |
| 513 | @property result_string |
| 514 | String variable to store the test case result string. |
| 515 | @property errors |
| 516 | List variable containing 2-tuples of TestCase instances and strings |
| 517 | holding formatted tracebacks. Each tuple represents a test which |
| 518 | raised an unexpected exception. |
| 519 | @property failures |
| 520 | List variable containing 2-tuples of TestCase instances and strings |
| 521 | holding formatted tracebacks. Each tuple represents a test where |
| 522 | a failure was explicitly signalled using the TestCase.assert*() |
| 523 | methods. |
| 524 | """ |
| 525 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 526 | def __init__(self, stream, descriptions, verbosity): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 527 | """ |
| 528 | :param stream File descriptor to store where to report test results. Set |
| 529 | to the standard error stream by default. |
| 530 | :param descriptions Boolean variable to store information if to use test |
| 531 | case descriptions. |
| 532 | :param verbosity Integer variable to store required verbosity level. |
| 533 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 534 | unittest.TestResult.__init__(self, stream, descriptions, verbosity) |
| 535 | self.stream = stream |
| 536 | self.descriptions = descriptions |
| 537 | self.verbosity = verbosity |
| 538 | self.result_string = None |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 539 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 540 | def addSuccess(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 541 | """ |
| 542 | Record a test succeeded result |
| 543 | |
| 544 | :param test: |
| 545 | |
| 546 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 547 | unittest.TestResult.addSuccess(self, test) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 548 | self.result_string = colorize("OK", GREEN) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 549 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 550 | def addSkip(self, test, reason): |
| 551 | """ |
| 552 | Record a test skipped. |
| 553 | |
| 554 | :param test: |
| 555 | :param reason: |
| 556 | |
| 557 | """ |
| 558 | unittest.TestResult.addSkip(self, test, reason) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 559 | self.result_string = colorize("SKIP", YELLOW) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 560 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 561 | def addFailure(self, test, err): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 562 | """ |
| 563 | Record a test failed result |
| 564 | |
| 565 | :param test: |
| 566 | :param err: error message |
| 567 | |
| 568 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 569 | unittest.TestResult.addFailure(self, test, err) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 570 | if hasattr(test, 'tempdir'): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 571 | self.result_string = colorize("FAIL", RED) + \ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 572 | ' [ temp dir used by test case: ' + test.tempdir + ' ]' |
| 573 | else: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 574 | self.result_string = colorize("FAIL", RED) + ' [no temp dir]' |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 575 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 576 | def addError(self, test, err): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 577 | """ |
| 578 | Record a test error result |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 579 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 580 | :param test: |
| 581 | :param err: error message |
| 582 | |
| 583 | """ |
| 584 | unittest.TestResult.addError(self, test, err) |
| 585 | if hasattr(test, 'tempdir'): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 586 | self.result_string = colorize("ERROR", RED) + \ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 587 | ' [ temp dir used by test case: ' + test.tempdir + ' ]' |
| 588 | else: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 589 | self.result_string = colorize("ERROR", RED) + ' [no temp dir]' |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 590 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 591 | def getDescription(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 592 | """ |
| 593 | Get test description |
| 594 | |
| 595 | :param test: |
| 596 | :returns: test description |
| 597 | |
| 598 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 599 | # TODO: if none print warning not raise exception |
| 600 | short_description = test.shortDescription() |
| 601 | if self.descriptions and short_description: |
| 602 | return short_description |
| 603 | else: |
| 604 | return str(test) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 605 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 606 | def startTest(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 607 | """ |
| 608 | Start a test |
| 609 | |
| 610 | :param test: |
| 611 | |
| 612 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 613 | unittest.TestResult.startTest(self, test) |
| 614 | if self.verbosity > 0: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 615 | self.stream.writeln( |
| 616 | "Starting " + self.getDescription(test) + " ...") |
| 617 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 618 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 619 | def stopTest(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 620 | """ |
| 621 | Stop a test |
| 622 | |
| 623 | :param test: |
| 624 | |
| 625 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 626 | unittest.TestResult.stopTest(self, test) |
| 627 | if self.verbosity > 0: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 628 | self.stream.writeln(single_line_delim) |
| 629 | self.stream.writeln("%-60s%s" % |
| 630 | (self.getDescription(test), self.result_string)) |
| 631 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 632 | else: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 633 | self.stream.writeln("%-60s%s" % |
| 634 | (self.getDescription(test), self.result_string)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 635 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 636 | def printErrors(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 637 | """ |
| 638 | Print errors from running the test case |
| 639 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 640 | self.stream.writeln() |
| 641 | self.printErrorList('ERROR', self.errors) |
| 642 | self.printErrorList('FAIL', self.failures) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 643 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 644 | def printErrorList(self, flavour, errors): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 645 | """ |
| 646 | Print error list to the output stream together with error type |
| 647 | and test case description. |
| 648 | |
| 649 | :param flavour: error type |
| 650 | :param errors: iterable errors |
| 651 | |
| 652 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 653 | for test, err in errors: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 654 | self.stream.writeln(double_line_delim) |
| 655 | self.stream.writeln("%s: %s" % |
| 656 | (flavour, self.getDescription(test))) |
| 657 | self.stream.writeln(single_line_delim) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 658 | self.stream.writeln("%s" % err) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 659 | |
| 660 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 661 | class VppTestRunner(unittest.TextTestRunner): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 662 | """ |
| 663 | A basic test runner implementation which prints results on standard error. |
| 664 | """ |
| 665 | @property |
| 666 | def resultclass(self): |
| 667 | """Class maintaining the results of the tests""" |
| 668 | return VppTestResult |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 669 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 670 | def run(self, test): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 671 | """ |
| 672 | Run the tests |
| 673 | |
| 674 | :param test: |
| 675 | |
| 676 | """ |
| 677 | print("Running tests using custom test runner") # debug message |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 678 | return super(VppTestRunner, self).run(test) |