5 from log import RED, single_line_delim, double_line_delim
6 from debug import spawn_gdb
11 Generic hooks before/after API/CLI calls
14 def __init__(self, logger):
17 def before_api(self, api_name, api_args):
19 Function called before API call
20 Emit a debug message describing the API name and arguments
22 @param api_name: name of the API
23 @param api_args: tuple containing the API arguments
25 self.logger.debug("API: %s (%s)" %
26 (api_name, api_args), extra={'color': RED})
28 def after_api(self, api_name, api_args):
30 Function called after API call
32 @param api_name: name of the API
33 @param api_args: tuple containing the API arguments
37 def before_cli(self, cli):
39 Function called before CLI call
40 Emit a debug message describing the CLI
42 @param cli: CLI string
44 self.logger.debug("CLI: %s" % (cli), extra={'color': RED})
46 def after_cli(self, cli):
48 Function called after CLI call
53 class VppDiedError(Exception):
58 """ Hook which checks if the vpp subprocess is alive """
60 def __init__(self, testcase):
61 self.testcase = testcase
62 self.logger = testcase.logger
64 def on_crash(self, core_path):
65 if self.testcase.debug_core:
66 # notify parent process that we're handling a core file
67 open('%s/_core_handled' % self.testcase.tempdir, 'a').close()
68 spawn_gdb(self.testcase.vpp_bin, core_path, self.logger)
70 self.logger.critical("Core file present, debug with: gdb %s %s" %
71 (self.testcase.vpp_bin, core_path))
75 Poll the vpp status and throw an exception if it's not running
76 :raises VppDiedError: exception if VPP is not running anymore
78 if self.testcase.vpp_dead:
79 # already dead, nothing to do
82 self.testcase.vpp.poll()
83 if self.testcase.vpp.returncode is not None:
85 (k, v) for v, k in reversed(sorted(signal.__dict__.items()))
86 if v.startswith('SIG') and not v.startswith('SIG_'))
88 if self.testcase.vpp.returncode in signaldict:
89 s = signaldict[abs(self.testcase.vpp.returncode)]
92 msg = "VPP subprocess died unexpectedly with returncode %d [%s]" %\
93 (self.testcase.vpp.returncode, s)
94 self.logger.critical(msg)
95 core_path = self.testcase.tempdir + '/core'
96 if os.path.isfile(core_path):
97 self.on_crash(core_path)
98 self.testcase.vpp_dead = True
99 raise VppDiedError(msg)
101 def before_api(self, api_name, api_args):
103 Check if VPP died before executing an API
105 :param api_name: name of the API
106 :param api_args: tuple containing the API arguments
107 :raises VppDiedError: exception if VPP is not running anymore
110 super(PollHook, self).before_api(api_name, api_args)
113 def before_cli(self, cli):
115 Check if VPP died before executing a CLI
117 :param cli: CLI string
118 :raises Exception: exception if VPP is not running anymore
121 super(PollHook, self).before_cli(cli)
125 class StepHook(PollHook):
126 """ Hook which requires user to press ENTER before doing any API/CLI """
128 def __init__(self, testcase):
129 self.skip_stack = None
132 super(StepHook, self).__init__(testcase)
135 if self.skip_stack is None:
137 stack = traceback.extract_stack()
141 if counter > self.skip_num:
143 if e[0] != self.skip_stack[counter][0]:
145 if e[1] != self.skip_stack[counter][1]:
152 print("%d API/CLI calls skipped in specified stack "
153 "frame" % self.skip_count)
155 self.skip_stack = None
159 def user_input(self):
160 print('number\tfunction\tfile\tcode')
162 stack = traceback.extract_stack()
164 print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3]))
166 print(single_line_delim)
167 print("You can enter a number of stack frame chosen from above")
168 print("Calls in/below that stack frame will be not be stepped anymore")
169 print(single_line_delim)
171 choice = sys.stdin.readline(
172 "Enter your choice, if any, and press ENTER to continue "
173 "running the testcase...")
177 if choice is not None:
180 print("Invalid input")
182 if choice is not None and (num < 0 or num >= len(stack)):
183 print("Invalid choice")
186 if choice is not None:
187 self.skip_stack = stack
190 def before_cli(self, cli):
191 """ Wait for ENTER before executing CLI """
193 print("Skip pause before executing CLI: %s" % cli)
195 print(double_line_delim)
196 print("Test paused before executing CLI: %s" % cli)
197 print(single_line_delim)
199 super(StepHook, self).before_cli(cli)
201 def before_api(self, api_name, api_args):
202 """ Wait for ENTER before executing API """
204 print("Skip pause before executing API: %s (%s)"
205 % (api_name, api_args))
207 print(double_line_delim)
208 print("Test paused before executing API: %s (%s)"
209 % (api_name, api_args))
210 print(single_line_delim)
212 super(StepHook, self).before_api(api_name, api_args)