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