Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1 | import signal |
| 2 | import os |
| 3 | import pexpect |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 4 | import traceback |
| 5 | from log import * |
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 | |
| 63 | def spawn_gdb(self, gdb_path, core_path): |
| 64 | gdb_cmdline = gdb_path + ' ' + self.testcase.vpp_bin + ' ' + core_path |
| 65 | gdb = pexpect.spawn(gdb_cmdline) |
| 66 | gdb.interact() |
| 67 | try: |
| 68 | gdb.terminate(True) |
| 69 | except: |
| 70 | pass |
| 71 | if gdb.isalive(): |
| 72 | raise Exception("GDB refused to die...") |
| 73 | |
| 74 | def on_crash(self, core_path): |
Klement Sekera | 01bbbe9 | 2016-11-02 09:25:05 +0100 | [diff] [blame] | 75 | if self.testcase.debug_core: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 76 | gdb_path = '/usr/bin/gdb' |
| 77 | if os.path.isfile(gdb_path) and os.access(gdb_path, os.X_OK): |
| 78 | # automatically attach gdb |
| 79 | self.spawn_gdb(gdb_path, core_path) |
| 80 | return |
| 81 | else: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 82 | self.logger.error( |
| 83 | "Debugger '%s' does not exist or is not an executable.." % |
| 84 | gdb_path) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 85 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 86 | self.logger.critical('core file present, debug with: gdb ' + |
| 87 | self.testcase.vpp_bin + ' ' + core_path) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 88 | |
| 89 | def poll_vpp(self): |
| 90 | """ |
| 91 | Poll the vpp status and throw an exception if it's not running |
| 92 | :raises VppDiedError: exception if VPP is not running anymore |
| 93 | """ |
Klement Sekera | 085f5c0 | 2016-11-24 01:59:16 +0100 | [diff] [blame] | 94 | if self.testcase.vpp_dead: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 95 | # already dead, nothing to do |
| 96 | return |
| 97 | |
| 98 | self.testcase.vpp.poll() |
| 99 | if self.testcase.vpp.returncode is not None: |
| 100 | signaldict = dict( |
| 101 | (k, v) for v, k in reversed(sorted(signal.__dict__.items())) |
| 102 | if v.startswith('SIG') and not v.startswith('SIG_')) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 103 | |
| 104 | if self.testcase.vpp.returncode in signaldict: |
| 105 | s = signaldict[abs(self.testcase.vpp.returncode)] |
| 106 | else: |
| 107 | s = "unknown" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 108 | msg = "VPP subprocess died unexpectedly with returncode %d [%s]" % ( |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 109 | self.testcase.vpp.returncode, s) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 110 | self.logger.critical(msg) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 111 | core_path = self.testcase.tempdir + '/core' |
| 112 | if os.path.isfile(core_path): |
| 113 | self.on_crash(core_path) |
| 114 | self.testcase.vpp_dead = True |
| 115 | raise VppDiedError(msg) |
| 116 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 117 | def before_api(self, api_name, api_args): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 118 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 119 | Check if VPP died before executing an API |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 120 | |
| 121 | :param api_name: name of the API |
| 122 | :param api_args: tuple containing the API arguments |
| 123 | :raises VppDiedError: exception if VPP is not running anymore |
| 124 | |
| 125 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 126 | super(PollHook, self).before_api(api_name, api_args) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 127 | self.poll_vpp() |
| 128 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 129 | def before_cli(self, cli): |
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 | Check if VPP died before executing a CLI |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 132 | |
| 133 | :param cli: CLI string |
| 134 | :raises Exception: exception if VPP is not running anymore |
| 135 | |
| 136 | """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 137 | super(PollHook, self).before_cli(cli) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 138 | self.poll_vpp() |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 139 | |
| 140 | |
| 141 | class StepHook(PollHook): |
| 142 | """ Hook which requires user to press ENTER before doing any API/CLI """ |
| 143 | |
| 144 | def __init__(self, testcase): |
| 145 | self.skip_stack = None |
| 146 | self.skip_num = None |
| 147 | self.skip_count = 0 |
| 148 | super(StepHook, self).__init__(testcase) |
| 149 | |
| 150 | def skip(self): |
| 151 | if self.skip_stack is None: |
| 152 | return False |
| 153 | stack = traceback.extract_stack() |
| 154 | counter = 0 |
| 155 | skip = True |
| 156 | for e in stack: |
| 157 | if counter > self.skip_num: |
| 158 | break |
| 159 | if e[0] != self.skip_stack[counter][0]: |
| 160 | skip = False |
| 161 | if e[1] != self.skip_stack[counter][1]: |
| 162 | skip = False |
| 163 | counter += 1 |
| 164 | if skip: |
| 165 | self.skip_count += 1 |
| 166 | return True |
| 167 | else: |
| 168 | print("%d API/CLI calls skipped in specified stack " |
| 169 | "frame" % self.skip_count) |
| 170 | self.skip_count = 0 |
| 171 | self.skip_stack = None |
| 172 | self.skip_num = None |
| 173 | return False |
| 174 | |
| 175 | def user_input(self): |
| 176 | print('number\tfunction\tfile\tcode') |
| 177 | counter = 0 |
| 178 | stack = traceback.extract_stack() |
| 179 | for e in stack: |
| 180 | print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3])) |
| 181 | counter += 1 |
| 182 | print(single_line_delim) |
| 183 | print("You can enter a number of stack frame chosen from above") |
| 184 | print("Calls in/below that stack frame will be not be stepped anymore") |
| 185 | print(single_line_delim) |
| 186 | while True: |
| 187 | choice = raw_input("Enter your choice, if any, and press ENTER to " |
| 188 | "continue running the testcase...") |
| 189 | if choice == "": |
| 190 | choice = None |
| 191 | try: |
| 192 | if choice is not None: |
| 193 | num = int(choice) |
| 194 | except: |
| 195 | print("Invalid input") |
| 196 | continue |
| 197 | if choice is not None and (num < 0 or num >= len(stack)): |
| 198 | print("Invalid choice") |
| 199 | continue |
| 200 | break |
| 201 | if choice is not None: |
| 202 | self.skip_stack = stack |
| 203 | self.skip_num = num |
| 204 | |
| 205 | def before_cli(self, cli): |
| 206 | """ Wait for ENTER before executing CLI """ |
| 207 | if self.skip(): |
| 208 | print("Skip pause before executing CLI: %s" % cli) |
| 209 | else: |
| 210 | print(double_line_delim) |
| 211 | print("Test paused before executing CLI: %s" % cli) |
| 212 | print(single_line_delim) |
| 213 | self.user_input() |
| 214 | super(StepHook, self).before_cli(cli) |
| 215 | |
| 216 | def before_api(self, api_name, api_args): |
| 217 | """ Wait for ENTER before executing API """ |
| 218 | if self.skip(): |
| 219 | print("Skip pause before executing API: %s (%s)" |
| 220 | % (api_name, api_args)) |
| 221 | else: |
| 222 | print(double_line_delim) |
| 223 | print("Test paused before executing API: %s (%s)" |
| 224 | % (api_name, api_args)) |
| 225 | print(single_line_delim) |
| 226 | self.user_input() |
| 227 | super(StepHook, self).before_api(api_name, api_args) |