4 from log import RED, single_line_delim, double_line_delim
5 from debug import spawn_gdb
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 on_crash(self, core_path):
64 if self.testcase.debug_core:
65 spawn_gdb(self.testcase.vpp_bin, core_path, self.logger)
67 self.logger.critical("Core file present, debug with: gdb %s %s" %
68 (self.testcase.vpp_bin, core_path))
72 Poll the vpp status and throw an exception if it's not running
73 :raises VppDiedError: exception if VPP is not running anymore
75 if self.testcase.vpp_dead:
76 # already dead, nothing to do
79 self.testcase.vpp.poll()
80 if self.testcase.vpp.returncode is not None:
82 (k, v) for v, k in reversed(sorted(signal.__dict__.items()))
83 if v.startswith('SIG') and not v.startswith('SIG_'))
85 if self.testcase.vpp.returncode in signaldict:
86 s = signaldict[abs(self.testcase.vpp.returncode)]
89 msg = "VPP subprocess died unexpectedly with returncode %d [%s]" %\
90 (self.testcase.vpp.returncode, s)
91 self.logger.critical(msg)
92 core_path = self.testcase.tempdir + '/core'
93 if os.path.isfile(core_path):
94 self.on_crash(core_path)
95 self.testcase.vpp_dead = True
96 raise VppDiedError(msg)
98 def before_api(self, api_name, api_args):
100 Check if VPP died before executing an API
102 :param api_name: name of the API
103 :param api_args: tuple containing the API arguments
104 :raises VppDiedError: exception if VPP is not running anymore
107 super(PollHook, self).before_api(api_name, api_args)
110 def before_cli(self, cli):
112 Check if VPP died before executing a CLI
114 :param cli: CLI string
115 :raises Exception: exception if VPP is not running anymore
118 super(PollHook, self).before_cli(cli)
122 class StepHook(PollHook):
123 """ Hook which requires user to press ENTER before doing any API/CLI """
125 def __init__(self, testcase):
126 self.skip_stack = None
129 super(StepHook, self).__init__(testcase)
132 if self.skip_stack is None:
134 stack = traceback.extract_stack()
138 if counter > self.skip_num:
140 if e[0] != self.skip_stack[counter][0]:
142 if e[1] != self.skip_stack[counter][1]:
149 print("%d API/CLI calls skipped in specified stack "
150 "frame" % self.skip_count)
152 self.skip_stack = None
156 def user_input(self):
157 print('number\tfunction\tfile\tcode')
159 stack = traceback.extract_stack()
161 print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3]))
163 print(single_line_delim)
164 print("You can enter a number of stack frame chosen from above")
165 print("Calls in/below that stack frame will be not be stepped anymore")
166 print(single_line_delim)
168 choice = raw_input("Enter your choice, if any, and press ENTER to "
169 "continue running the testcase...")
173 if choice is not None:
176 print("Invalid input")
178 if choice is not None and (num < 0 or num >= len(stack)):
179 print("Invalid choice")
182 if choice is not None:
183 self.skip_stack = stack
186 def before_cli(self, cli):
187 """ Wait for ENTER before executing CLI """
189 print("Skip pause before executing CLI: %s" % cli)
191 print(double_line_delim)
192 print("Test paused before executing CLI: %s" % cli)
193 print(single_line_delim)
195 super(StepHook, self).before_cli(cli)
197 def before_api(self, api_name, api_args):
198 """ Wait for ENTER before executing API """
200 print("Skip pause before executing API: %s (%s)"
201 % (api_name, api_args))
203 print(double_line_delim)
204 print("Test paused before executing API: %s (%s)"
205 % (api_name, api_args))
206 print(single_line_delim)
208 super(StepHook, self).before_api(api_name, api_args)