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