10 Generic hooks before/after API/CLI calls
13 def __init__(self, logger):
16 def before_api(self, api_name, api_args):
18 Function called before API call
19 Emit a debug message describing the API name and arguments
21 @param api_name: name of the API
22 @param api_args: tuple containing the API arguments
24 self.logger.debug("API: %s (%s)" %
25 (api_name, api_args), extra={'color': RED})
27 def after_api(self, api_name, api_args):
29 Function called after API call
31 @param api_name: name of the API
32 @param api_args: tuple containing the API arguments
36 def before_cli(self, cli):
38 Function called before CLI call
39 Emit a debug message describing the CLI
41 @param cli: CLI string
43 self.logger.debug("CLI: %s" % (cli), extra={'color': RED})
45 def after_cli(self, cli):
47 Function called after CLI call
52 class VppDiedError(Exception):
57 """ Hook which checks if the vpp subprocess is alive """
59 def __init__(self, testcase):
61 self.testcase = testcase
62 self.logger = testcase.logger
64 def spawn_gdb(self, gdb_path, core_path):
65 gdb_cmdline = gdb_path + ' ' + self.testcase.vpp_bin + ' ' + core_path
66 gdb = pexpect.spawn(gdb_cmdline)
73 raise Exception("GDB refused to die...")
75 def on_crash(self, core_path):
76 if self.testcase.debug_core:
77 gdb_path = '/usr/bin/gdb'
78 if os.path.isfile(gdb_path) and os.access(gdb_path, os.X_OK):
79 # automatically attach gdb
80 self.spawn_gdb(gdb_path, core_path)
84 "Debugger '%s' does not exist or is not an executable.." %
87 self.logger.critical('core file present, debug with: gdb ' +
88 self.testcase.vpp_bin + ' ' + core_path)
92 Poll the vpp status and throw an exception if it's not running
93 :raises VppDiedError: exception if VPP is not running anymore
96 # already dead, nothing to do
99 self.testcase.vpp.poll()
100 if self.testcase.vpp.returncode is not None:
102 (k, v) for v, k in reversed(sorted(signal.__dict__.items()))
103 if v.startswith('SIG') and not v.startswith('SIG_'))
104 msg = "VPP subprocess died unexpectedly with returncode %d [%s]" % (
105 self.testcase.vpp.returncode,
106 signaldict[abs(self.testcase.vpp.returncode)])
107 self.logger.critical(msg)
108 core_path = self.testcase.tempdir + '/core'
109 if os.path.isfile(core_path):
110 self.on_crash(core_path)
111 self.testcase.vpp_dead = True
112 raise VppDiedError(msg)
114 def after_api(self, api_name, api_args):
116 Check if VPP died after executing an API
118 :param api_name: name of the API
119 :param api_args: tuple containing the API arguments
120 :raises VppDiedError: exception if VPP is not running anymore
123 super(PollHook, self).after_api(api_name, api_args)
126 def after_cli(self, cli):
128 Check if VPP died after executing a CLI
130 :param cli: CLI string
131 :raises Exception: exception if VPP is not running anymore
134 super(PollHook, self).after_cli(cli)
138 class StepHook(PollHook):
139 """ Hook which requires user to press ENTER before doing any API/CLI """
141 def __init__(self, testcase):
142 self.skip_stack = None
145 super(StepHook, self).__init__(testcase)
148 if self.skip_stack is None:
150 stack = traceback.extract_stack()
154 if counter > self.skip_num:
156 if e[0] != self.skip_stack[counter][0]:
158 if e[1] != self.skip_stack[counter][1]:
165 print("%d API/CLI calls skipped in specified stack "
166 "frame" % self.skip_count)
168 self.skip_stack = None
172 def user_input(self):
173 print('number\tfunction\tfile\tcode')
175 stack = traceback.extract_stack()
177 print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3]))
179 print(single_line_delim)
180 print("You can enter a number of stack frame chosen from above")
181 print("Calls in/below that stack frame will be not be stepped anymore")
182 print(single_line_delim)
184 choice = raw_input("Enter your choice, if any, and press ENTER to "
185 "continue running the testcase...")
189 if choice is not None:
192 print("Invalid input")
194 if choice is not None and (num < 0 or num >= len(stack)):
195 print("Invalid choice")
198 if choice is not None:
199 self.skip_stack = stack
202 def before_cli(self, cli):
203 """ Wait for ENTER before executing CLI """
205 print("Skip pause before executing CLI: %s" % cli)
207 print(double_line_delim)
208 print("Test paused before executing CLI: %s" % cli)
209 print(single_line_delim)
211 super(StepHook, self).before_cli(cli)
213 def before_api(self, api_name, api_args):
214 """ Wait for ENTER before executing API """
216 print("Skip pause before executing API: %s (%s)"
217 % (api_name, api_args))
219 print(double_line_delim)
220 print("Test paused before executing API: %s (%s)"
221 % (api_name, api_args))
222 print(single_line_delim)
224 super(StepHook, self).before_api(api_name, api_args)