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_'))
103 msg = "VPP subprocess died unexpectedly with returncode %d [%s]" % (
104 self.testcase.vpp.returncode,
105 signaldict[abs(self.testcase.vpp.returncode)])
106 self.logger.critical(msg)
107 core_path = self.testcase.tempdir + '/core'
108 if os.path.isfile(core_path):
109 self.on_crash(core_path)
110 self.testcase.vpp_dead = True
111 raise VppDiedError(msg)
113 def after_api(self, api_name, api_args):
115 Check if VPP died after executing an API
117 :param api_name: name of the API
118 :param api_args: tuple containing the API arguments
119 :raises VppDiedError: exception if VPP is not running anymore
122 super(PollHook, self).after_api(api_name, api_args)
125 def after_cli(self, cli):
127 Check if VPP died after executing a CLI
129 :param cli: CLI string
130 :raises Exception: exception if VPP is not running anymore
133 super(PollHook, self).after_cli(cli)
137 class StepHook(PollHook):
138 """ Hook which requires user to press ENTER before doing any API/CLI """
140 def __init__(self, testcase):
141 self.skip_stack = None
144 super(StepHook, self).__init__(testcase)
147 if self.skip_stack is None:
149 stack = traceback.extract_stack()
153 if counter > self.skip_num:
155 if e[0] != self.skip_stack[counter][0]:
157 if e[1] != self.skip_stack[counter][1]:
164 print("%d API/CLI calls skipped in specified stack "
165 "frame" % self.skip_count)
167 self.skip_stack = None
171 def user_input(self):
172 print('number\tfunction\tfile\tcode')
174 stack = traceback.extract_stack()
176 print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3]))
178 print(single_line_delim)
179 print("You can enter a number of stack frame chosen from above")
180 print("Calls in/below that stack frame will be not be stepped anymore")
181 print(single_line_delim)
183 choice = raw_input("Enter your choice, if any, and press ENTER to "
184 "continue running the testcase...")
188 if choice is not None:
191 print("Invalid input")
193 if choice is not None and (num < 0 or num >= len(stack)):
194 print("Invalid choice")
197 if choice is not None:
198 self.skip_stack = stack
201 def before_cli(self, cli):
202 """ Wait for ENTER before executing CLI """
204 print("Skip pause before executing CLI: %s" % cli)
206 print(double_line_delim)
207 print("Test paused before executing CLI: %s" % cli)
208 print(single_line_delim)
210 super(StepHook, self).before_cli(cli)
212 def before_api(self, api_name, api_args):
213 """ Wait for ENTER before executing API """
215 print("Skip pause before executing API: %s (%s)"
216 % (api_name, api_args))
218 print(double_line_delim)
219 print("Test paused before executing API: %s (%s)"
220 % (api_name, api_args))
221 print(single_line_delim)
223 super(StepHook, self).before_api(api_name, api_args)