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):
60 self.testcase = testcase
61 self.logger = testcase.logger
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)
72 raise Exception("GDB refused to die...")
74 def on_crash(self, core_path):
75 if self.testcase.debug_core:
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)
83 "Debugger '%s' does not exist or is not an executable.." %
86 self.logger.critical('core file present, debug with: gdb ' +
87 self.testcase.vpp_bin + ' ' + core_path)
91 Poll the vpp status and throw an exception if it's not running
92 :raises VppDiedError: exception if VPP is not running anymore
94 if self.testcase.vpp_dead:
95 # already dead, nothing to do
98 self.testcase.vpp.poll()
99 if self.testcase.vpp.returncode is not None:
101 (k, v) for v, k in reversed(sorted(signal.__dict__.items()))
102 if v.startswith('SIG') and not v.startswith('SIG_'))
104 if self.testcase.vpp.returncode in signaldict:
105 s = signaldict[abs(self.testcase.vpp.returncode)]
108 msg = "VPP subprocess died unexpectedly with returncode %d [%s]" %\
109 (self.testcase.vpp.returncode, s)
110 self.logger.critical(msg)
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)
117 def before_api(self, api_name, api_args):
119 Check if VPP died before executing an API
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
126 super(PollHook, self).before_api(api_name, api_args)
129 def before_cli(self, cli):
131 Check if VPP died before executing a CLI
133 :param cli: CLI string
134 :raises Exception: exception if VPP is not running anymore
137 super(PollHook, self).before_cli(cli)
141 class StepHook(PollHook):
142 """ Hook which requires user to press ENTER before doing any API/CLI """
144 def __init__(self, testcase):
145 self.skip_stack = None
148 super(StepHook, self).__init__(testcase)
151 if self.skip_stack is None:
153 stack = traceback.extract_stack()
157 if counter > self.skip_num:
159 if e[0] != self.skip_stack[counter][0]:
161 if e[1] != self.skip_stack[counter][1]:
168 print("%d API/CLI calls skipped in specified stack "
169 "frame" % self.skip_count)
171 self.skip_stack = None
175 def user_input(self):
176 print('number\tfunction\tfile\tcode')
178 stack = traceback.extract_stack()
180 print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3]))
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)
187 choice = raw_input("Enter your choice, if any, and press ENTER to "
188 "continue running the testcase...")
192 if choice is not None:
195 print("Invalid input")
197 if choice is not None and (num < 0 or num >= len(stack)):
198 print("Invalid choice")
201 if choice is not None:
202 self.skip_stack = stack
205 def before_cli(self, cli):
206 """ Wait for ENTER before executing CLI """
208 print("Skip pause before executing CLI: %s" % cli)
210 print(double_line_delim)
211 print("Test paused before executing CLI: %s" % cli)
212 print(single_line_delim)
214 super(StepHook, self).before_cli(cli)
216 def before_api(self, api_name, api_args):
217 """ Wait for ENTER before executing API """
219 print("Skip pause before executing API: %s (%s)"
220 % (api_name, api_args))
222 print(double_line_delim)
223 print("Test paused before executing API: %s (%s)"
224 % (api_name, api_args))
225 print(single_line_delim)
227 super(StepHook, self).before_api(api_name, api_args)