Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1 | import signal |
| 2 | import os |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 3 | import sys |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 4 | import traceback |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 5 | from log import RED, single_line_delim, double_line_delim |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 6 | from debug import spawn_gdb |
Klement Sekera | 9b6ece7 | 2018-03-23 10:50:11 +0100 | [diff] [blame] | 7 | from subprocess import check_output, CalledProcessError |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class Hook(object): |
| 11 | """ |
| 12 | Generic hooks before/after API/CLI calls |
| 13 | """ |
| 14 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 15 | def __init__(self, logger): |
| 16 | self.logger = logger |
| 17 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 18 | def before_api(self, api_name, api_args): |
| 19 | """ |
| 20 | Function called before API call |
| 21 | Emit a debug message describing the API name and arguments |
| 22 | |
| 23 | @param api_name: name of the API |
| 24 | @param api_args: tuple containing the API arguments |
| 25 | """ |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 26 | self.logger.debug("API: %s (%s)" % |
| 27 | (api_name, api_args), extra={'color': RED}) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 28 | |
| 29 | def after_api(self, api_name, api_args): |
| 30 | """ |
| 31 | Function called after API call |
| 32 | |
| 33 | @param api_name: name of the API |
| 34 | @param api_args: tuple containing the API arguments |
| 35 | """ |
| 36 | pass |
| 37 | |
| 38 | def before_cli(self, cli): |
| 39 | """ |
| 40 | Function called before CLI call |
| 41 | Emit a debug message describing the CLI |
| 42 | |
| 43 | @param cli: CLI string |
| 44 | """ |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 45 | self.logger.debug("CLI: %s" % (cli), extra={'color': RED}) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 46 | |
| 47 | def after_cli(self, cli): |
| 48 | """ |
| 49 | Function called after CLI call |
| 50 | """ |
| 51 | pass |
| 52 | |
| 53 | |
| 54 | class VppDiedError(Exception): |
| 55 | pass |
| 56 | |
| 57 | |
| 58 | class PollHook(Hook): |
| 59 | """ Hook which checks if the vpp subprocess is alive """ |
| 60 | |
| 61 | def __init__(self, testcase): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 62 | self.testcase = testcase |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 63 | self.logger = testcase.logger |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 64 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 65 | def on_crash(self, core_path): |
Klement Sekera | 01bbbe9 | 2016-11-02 09:25:05 +0100 | [diff] [blame] | 66 | if self.testcase.debug_core: |
Klement Sekera | 0574226 | 2018-03-14 18:14:49 +0100 | [diff] [blame] | 67 | # notify parent process that we're handling a core file |
| 68 | open('%s/_core_handled' % self.testcase.tempdir, 'a').close() |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 69 | spawn_gdb(self.testcase.vpp_bin, core_path, self.logger) |
| 70 | else: |
Klement Sekera | 9b6ece7 | 2018-03-23 10:50:11 +0100 | [diff] [blame] | 71 | self.logger.error("Core file present, debug with: gdb %s %s" % |
| 72 | (self.testcase.vpp_bin, core_path)) |
| 73 | self.logger.error("Running `file %s':" % core_path) |
| 74 | try: |
| 75 | info = check_output(["file", core_path]) |
| 76 | self.logger.error(info) |
| 77 | except CalledProcessError as e: |
| 78 | self.logger.error( |
| 79 | "Could not run `file' utility on core-file, " |
| 80 | "rc=%s" % e.returncode) |
| 81 | pass |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 82 | |
| 83 | def poll_vpp(self): |
| 84 | """ |
| 85 | Poll the vpp status and throw an exception if it's not running |
| 86 | :raises VppDiedError: exception if VPP is not running anymore |
| 87 | """ |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 88 | if self.testcase.vpp_dead: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 89 | # already dead, nothing to do |
| 90 | return |
| 91 | |
| 92 | self.testcase.vpp.poll() |
| 93 | if self.testcase.vpp.returncode is not None: |
| 94 | signaldict = dict( |
| 95 | (k, v) for v, k in reversed(sorted(signal.__dict__.items())) |
| 96 | if v.startswith('SIG') and not v.startswith('SIG_')) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 97 | |
| 98 | if self.testcase.vpp.returncode in signaldict: |
| 99 | s = signaldict[abs(self.testcase.vpp.returncode)] |
| 100 | else: |
| 101 | s = "unknown" |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 102 | msg = "VPP subprocess died unexpectedly with returncode %d [%s]" %\ |
| 103 | (self.testcase.vpp.returncode, s) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 104 | self.logger.critical(msg) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 105 | core_path = self.testcase.tempdir + '/core' |
| 106 | if os.path.isfile(core_path): |
| 107 | self.on_crash(core_path) |
| 108 | self.testcase.vpp_dead = True |
| 109 | raise VppDiedError(msg) |
| 110 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 111 | def before_api(self, api_name, api_args): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 112 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 113 | Check if VPP died before executing an API |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 114 | |
| 115 | :param api_name: name of the API |
| 116 | :param api_args: tuple containing the API arguments |
| 117 | :raises VppDiedError: exception if VPP is not running anymore |
| 118 | |
| 119 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 120 | super(PollHook, self).before_api(api_name, api_args) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 121 | self.poll_vpp() |
| 122 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 123 | def before_cli(self, cli): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 124 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 125 | Check if VPP died before executing a CLI |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 126 | |
| 127 | :param cli: CLI string |
| 128 | :raises Exception: exception if VPP is not running anymore |
| 129 | |
| 130 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 131 | super(PollHook, self).before_cli(cli) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 132 | self.poll_vpp() |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 133 | |
| 134 | |
| 135 | class StepHook(PollHook): |
| 136 | """ Hook which requires user to press ENTER before doing any API/CLI """ |
| 137 | |
| 138 | def __init__(self, testcase): |
| 139 | self.skip_stack = None |
| 140 | self.skip_num = None |
| 141 | self.skip_count = 0 |
| 142 | super(StepHook, self).__init__(testcase) |
| 143 | |
| 144 | def skip(self): |
| 145 | if self.skip_stack is None: |
| 146 | return False |
| 147 | stack = traceback.extract_stack() |
| 148 | counter = 0 |
| 149 | skip = True |
| 150 | for e in stack: |
| 151 | if counter > self.skip_num: |
| 152 | break |
| 153 | if e[0] != self.skip_stack[counter][0]: |
| 154 | skip = False |
| 155 | if e[1] != self.skip_stack[counter][1]: |
| 156 | skip = False |
| 157 | counter += 1 |
| 158 | if skip: |
| 159 | self.skip_count += 1 |
| 160 | return True |
| 161 | else: |
| 162 | print("%d API/CLI calls skipped in specified stack " |
| 163 | "frame" % self.skip_count) |
| 164 | self.skip_count = 0 |
| 165 | self.skip_stack = None |
| 166 | self.skip_num = None |
| 167 | return False |
| 168 | |
| 169 | def user_input(self): |
| 170 | print('number\tfunction\tfile\tcode') |
| 171 | counter = 0 |
| 172 | stack = traceback.extract_stack() |
| 173 | for e in stack: |
| 174 | print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3])) |
| 175 | counter += 1 |
| 176 | print(single_line_delim) |
| 177 | print("You can enter a number of stack frame chosen from above") |
| 178 | print("Calls in/below that stack frame will be not be stepped anymore") |
| 179 | print(single_line_delim) |
| 180 | while True: |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 181 | choice = sys.stdin.readline( |
| 182 | "Enter your choice, if any, and press ENTER to continue " |
| 183 | "running the testcase...") |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 184 | if choice == "": |
| 185 | choice = None |
| 186 | try: |
| 187 | if choice is not None: |
| 188 | num = int(choice) |
Klement Sekera | 13a83ef | 2018-03-21 12:35:51 +0100 | [diff] [blame] | 189 | except TypeError: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 190 | print("Invalid input") |
| 191 | continue |
| 192 | if choice is not None and (num < 0 or num >= len(stack)): |
| 193 | print("Invalid choice") |
| 194 | continue |
| 195 | break |
| 196 | if choice is not None: |
| 197 | self.skip_stack = stack |
| 198 | self.skip_num = num |
| 199 | |
| 200 | def before_cli(self, cli): |
| 201 | """ Wait for ENTER before executing CLI """ |
| 202 | if self.skip(): |
| 203 | print("Skip pause before executing CLI: %s" % cli) |
| 204 | else: |
| 205 | print(double_line_delim) |
| 206 | print("Test paused before executing CLI: %s" % cli) |
| 207 | print(single_line_delim) |
| 208 | self.user_input() |
| 209 | super(StepHook, self).before_cli(cli) |
| 210 | |
| 211 | def before_api(self, api_name, api_args): |
| 212 | """ Wait for ENTER before executing API """ |
| 213 | if self.skip(): |
| 214 | print("Skip pause before executing API: %s (%s)" |
| 215 | % (api_name, api_args)) |
| 216 | else: |
| 217 | print(double_line_delim) |
| 218 | print("Test paused before executing API: %s (%s)" |
| 219 | % (api_name, api_args)) |
| 220 | print(single_line_delim) |
| 221 | self.user_input() |
| 222 | super(StepHook, self).before_api(api_name, api_args) |