Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1 | import os |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 2 | import sys |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 3 | import traceback |
Paul Vinciguerra | 1314ec6 | 2018-12-12 01:04:20 -0800 | [diff] [blame] | 4 | import ipaddress |
Klement Sekera | 9b6ece7 | 2018-03-23 10:50:11 +0100 | [diff] [blame] | 5 | from subprocess import check_output, CalledProcessError |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 6 | |
| 7 | import scapy.compat |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 8 | import asfframework |
Dmitry Valter | 3ace4d6 | 2022-03-26 15:43:14 +0000 | [diff] [blame] | 9 | from config import config |
Paul Vinciguerra | 496b0de | 2019-06-20 12:24:12 -0400 | [diff] [blame] | 10 | from log import RED, single_line_delim, double_line_delim |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 11 | from util import check_core_path, get_core_path |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 12 | |
| 13 | |
Paul Vinciguerra | e061dad | 2020-12-04 14:57:51 -0500 | [diff] [blame] | 14 | class Hook: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 15 | """ |
| 16 | Generic hooks before/after API/CLI calls |
| 17 | """ |
| 18 | |
Paul Vinciguerra | 895e2f8 | 2019-01-08 20:37:40 -0800 | [diff] [blame] | 19 | def __init__(self, test): |
| 20 | self.test = test |
| 21 | self.logger = test.logger |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 22 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 23 | def before_api(self, api_name, api_args): |
| 24 | """ |
| 25 | Function called before API call |
| 26 | Emit a debug message describing the API name and arguments |
| 27 | |
| 28 | @param api_name: name of the API |
| 29 | @param api_args: tuple containing the API arguments |
| 30 | """ |
Paul Vinciguerra | 1314ec6 | 2018-12-12 01:04:20 -0800 | [diff] [blame] | 31 | |
| 32 | def _friendly_format(val): |
| 33 | if not isinstance(val, str): |
| 34 | return val |
| 35 | if len(val) == 6: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 36 | return "{!s} ({!s})".format( |
| 37 | val, ":".join(["{:02x}".format(scapy.compat.orb(x)) for x in val]) |
| 38 | ) |
Paul Vinciguerra | 1314ec6 | 2018-12-12 01:04:20 -0800 | [diff] [blame] | 39 | try: |
Paul Vinciguerra | 9e31595 | 2019-01-29 11:51:44 -0800 | [diff] [blame] | 40 | # we don't call test_type(val) because it is a packed value. |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 41 | return "{!s} ({!s})".format(val, str(ipaddress.ip_address(val))) |
Naveen Joy | 64f7530 | 2019-03-27 14:28:50 -0700 | [diff] [blame] | 42 | except ValueError: |
Paul Vinciguerra | 1314ec6 | 2018-12-12 01:04:20 -0800 | [diff] [blame] | 43 | return val |
| 44 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 45 | _args = ", ".join( |
| 46 | "{!s}={!r}".format(key, _friendly_format(val)) |
| 47 | for (key, val) in api_args.items() |
| 48 | ) |
| 49 | self.logger.debug("API: %s (%s)" % (api_name, _args), extra={"color": RED}) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 50 | |
| 51 | def after_api(self, api_name, api_args): |
| 52 | """ |
| 53 | Function called after API call |
| 54 | |
| 55 | @param api_name: name of the API |
| 56 | @param api_args: tuple containing the API arguments |
| 57 | """ |
| 58 | pass |
| 59 | |
| 60 | def before_cli(self, cli): |
| 61 | """ |
| 62 | Function called before CLI call |
| 63 | Emit a debug message describing the CLI |
| 64 | |
| 65 | @param cli: CLI string |
| 66 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 67 | self.logger.debug("CLI: %s" % (cli), extra={"color": RED}) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 68 | |
| 69 | def after_cli(self, cli): |
| 70 | """ |
| 71 | Function called after CLI call |
| 72 | """ |
| 73 | pass |
| 74 | |
| 75 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 76 | class PollHook(Hook): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 77 | """Hook which checks if the vpp subprocess is alive""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 78 | |
Paul Vinciguerra | 895e2f8 | 2019-01-08 20:37:40 -0800 | [diff] [blame] | 79 | def __init__(self, test): |
| 80 | super(PollHook, self).__init__(test) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 81 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 82 | def on_crash(self, core_path): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 83 | self.logger.error( |
| 84 | "Core file present, debug with: gdb %s %s", config.vpp, core_path |
| 85 | ) |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 86 | check_core_path(self.logger, core_path) |
Paul Vinciguerra | 38a4ec7 | 2018-11-28 11:34:21 -0800 | [diff] [blame] | 87 | self.logger.error("Running `file %s':", core_path) |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 88 | try: |
| 89 | info = check_output(["file", core_path]) |
| 90 | self.logger.error(info) |
| 91 | except CalledProcessError as e: |
| 92 | self.logger.error( |
Paul Vinciguerra | 38a4ec7 | 2018-11-28 11:34:21 -0800 | [diff] [blame] | 93 | "Subprocess returned with error running `file' utility on " |
| 94 | "core-file, " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 95 | "rc=%s", |
| 96 | e.returncode, |
| 97 | ) |
Paul Vinciguerra | 38a4ec7 | 2018-11-28 11:34:21 -0800 | [diff] [blame] | 98 | except OSError as e: |
| 99 | self.logger.error( |
| 100 | "Subprocess returned OS error running `file' utility on " |
| 101 | "core-file, " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 102 | "oserror=(%s) %s", |
| 103 | e.errno, |
| 104 | e.strerror, |
| 105 | ) |
Paul Vinciguerra | 38a4ec7 | 2018-11-28 11:34:21 -0800 | [diff] [blame] | 106 | except Exception as e: |
| 107 | self.logger.error( |
| 108 | "Subprocess returned unanticipated error running `file' " |
| 109 | "utility on core-file, " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 110 | "%s", |
| 111 | e, |
| 112 | ) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 113 | |
| 114 | def poll_vpp(self): |
| 115 | """ |
| 116 | Poll the vpp status and throw an exception if it's not running |
| 117 | :raises VppDiedError: exception if VPP is not running anymore |
| 118 | """ |
Dave Wallace | 670724c | 2022-09-20 21:52:18 -0400 | [diff] [blame] | 119 | if not hasattr(self.test, "vpp") or self.test.vpp_dead: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 120 | # already dead, nothing to do |
| 121 | return |
| 122 | |
Paul Vinciguerra | 895e2f8 | 2019-01-08 20:37:40 -0800 | [diff] [blame] | 123 | self.test.vpp.poll() |
| 124 | if self.test.vpp.returncode is not None: |
Paul Vinciguerra | 496b0de | 2019-06-20 12:24:12 -0400 | [diff] [blame] | 125 | self.test.vpp_dead = True |
Paul Vinciguerra | 895e2f8 | 2019-01-08 20:37:40 -0800 | [diff] [blame] | 126 | core_path = get_core_path(self.test.tempdir) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 127 | if os.path.isfile(core_path): |
| 128 | self.on_crash(core_path) |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 129 | raise asfframework.VppDiedError(rv=self.test.vpp.returncode) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 130 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 131 | def before_api(self, api_name, api_args): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 132 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 133 | Check if VPP died before executing an API |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 134 | |
| 135 | :param api_name: name of the API |
| 136 | :param api_args: tuple containing the API arguments |
| 137 | :raises VppDiedError: exception if VPP is not running anymore |
| 138 | |
| 139 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 140 | super(PollHook, self).before_api(api_name, api_args) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 141 | self.poll_vpp() |
| 142 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 143 | def before_cli(self, cli): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 144 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 145 | Check if VPP died before executing a CLI |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 146 | |
| 147 | :param cli: CLI string |
| 148 | :raises Exception: exception if VPP is not running anymore |
| 149 | |
| 150 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 151 | super(PollHook, self).before_cli(cli) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 152 | self.poll_vpp() |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 153 | |
| 154 | |
| 155 | class StepHook(PollHook): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 156 | """Hook which requires user to press ENTER before doing any API/CLI""" |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 157 | |
Paul Vinciguerra | 895e2f8 | 2019-01-08 20:37:40 -0800 | [diff] [blame] | 158 | def __init__(self, test): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 159 | self.skip_stack = None |
| 160 | self.skip_num = None |
| 161 | self.skip_count = 0 |
Klement Sekera | c0a2f0e | 2022-01-28 11:31:01 +0000 | [diff] [blame] | 162 | self.break_func = None |
Paul Vinciguerra | 895e2f8 | 2019-01-08 20:37:40 -0800 | [diff] [blame] | 163 | super(StepHook, self).__init__(test) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 164 | |
| 165 | def skip(self): |
Klement Sekera | c0a2f0e | 2022-01-28 11:31:01 +0000 | [diff] [blame] | 166 | if self.break_func is not None: |
| 167 | return self.should_skip_func_based() |
| 168 | if self.skip_stack is not None: |
| 169 | return self.should_skip_stack_based() |
| 170 | |
| 171 | def should_skip_func_based(self): |
| 172 | stack = traceback.extract_stack() |
| 173 | for e in stack: |
| 174 | if e[2] == self.break_func: |
| 175 | self.break_func = None |
| 176 | return False |
| 177 | return True |
| 178 | |
| 179 | def should_skip_stack_based(self): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 180 | stack = traceback.extract_stack() |
| 181 | counter = 0 |
| 182 | skip = True |
| 183 | for e in stack: |
| 184 | if counter > self.skip_num: |
| 185 | break |
| 186 | if e[0] != self.skip_stack[counter][0]: |
| 187 | skip = False |
| 188 | if e[1] != self.skip_stack[counter][1]: |
| 189 | skip = False |
| 190 | counter += 1 |
| 191 | if skip: |
| 192 | self.skip_count += 1 |
| 193 | return True |
| 194 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 195 | print("%d API/CLI calls skipped in specified stack frame" % self.skip_count) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 196 | self.skip_count = 0 |
| 197 | self.skip_stack = None |
| 198 | self.skip_num = None |
| 199 | return False |
| 200 | |
| 201 | def user_input(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 202 | print("number\tfunction\tfile\tcode") |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 203 | counter = 0 |
| 204 | stack = traceback.extract_stack() |
| 205 | for e in stack: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 206 | print("%02d.\t%s\t%s:%d\t[%s]" % (counter, e[2], e[0], e[1], e[3])) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 207 | counter += 1 |
| 208 | print(single_line_delim) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 209 | print("You may enter a number of stack frame chosen from above") |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 210 | print("Calls in/below that stack frame will be not be stepped anymore") |
Klement Sekera | c0a2f0e | 2022-01-28 11:31:01 +0000 | [diff] [blame] | 211 | print("Alternatively, enter a test function name to stop at") |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 212 | print(single_line_delim) |
| 213 | while True: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 214 | print( |
| 215 | "Enter your choice, if any, and press ENTER to continue " |
| 216 | "running the testcase..." |
| 217 | ) |
| 218 | choice = sys.stdin.readline().rstrip("\r\n") |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 219 | if choice == "": |
| 220 | choice = None |
| 221 | try: |
| 222 | if choice is not None: |
| 223 | num = int(choice) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 224 | except ValueError: |
Klement Sekera | c0a2f0e | 2022-01-28 11:31:01 +0000 | [diff] [blame] | 225 | if choice.startswith("test_"): |
| 226 | break |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 227 | print("Invalid input") |
| 228 | continue |
| 229 | if choice is not None and (num < 0 or num >= len(stack)): |
| 230 | print("Invalid choice") |
| 231 | continue |
| 232 | break |
| 233 | if choice is not None: |
Klement Sekera | c0a2f0e | 2022-01-28 11:31:01 +0000 | [diff] [blame] | 234 | if choice.startswith("test_"): |
| 235 | self.break_func = choice |
| 236 | else: |
| 237 | self.break_func = None |
| 238 | self.skip_stack = stack |
| 239 | self.skip_num = num |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 240 | |
| 241 | def before_cli(self, cli): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 242 | """Wait for ENTER before executing CLI""" |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 243 | if self.skip(): |
| 244 | print("Skip pause before executing CLI: %s" % cli) |
| 245 | else: |
| 246 | print(double_line_delim) |
| 247 | print("Test paused before executing CLI: %s" % cli) |
| 248 | print(single_line_delim) |
| 249 | self.user_input() |
| 250 | super(StepHook, self).before_cli(cli) |
| 251 | |
| 252 | def before_api(self, api_name, api_args): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 253 | """Wait for ENTER before executing API""" |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 254 | if self.skip(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 255 | print("Skip pause before executing API: %s (%s)" % (api_name, api_args)) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 256 | else: |
| 257 | print(double_line_delim) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 258 | print("Test paused before executing API: %s (%s)" % (api_name, api_args)) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 259 | print(single_line_delim) |
| 260 | self.user_input() |
| 261 | super(StepHook, self).before_api(api_name, api_args) |