5 from log import RED, single_line_delim, double_line_delim
6 from debug import spawn_gdb
7 from subprocess import check_output, CalledProcessError
8 from util import check_core_path
13 Generic hooks before/after API/CLI calls
16 def __init__(self, logger):
19 def before_api(self, api_name, api_args):
21 Function called before API call
22 Emit a debug message describing the API name and arguments
24 @param api_name: name of the API
25 @param api_args: tuple containing the API arguments
27 self.logger.debug("API: %s (%s)" %
28 (api_name, api_args), extra={'color': RED})
30 def after_api(self, api_name, api_args):
32 Function called after API call
34 @param api_name: name of the API
35 @param api_args: tuple containing the API arguments
39 def before_cli(self, cli):
41 Function called before CLI call
42 Emit a debug message describing the CLI
44 @param cli: CLI string
46 self.logger.debug("CLI: %s" % (cli), extra={'color': RED})
48 def after_cli(self, cli):
50 Function called after CLI call
55 class VppDiedError(Exception):
60 """ Hook which checks if the vpp subprocess is alive """
62 def __init__(self, testcase):
63 self.testcase = testcase
64 self.logger = testcase.logger
66 def on_crash(self, core_path):
67 if self.testcase.debug_core:
68 # notify parent process that we're handling a core file
69 open('%s/_core_handled' % self.testcase.tempdir, 'a').close()
70 spawn_gdb(self.testcase.vpp_bin, core_path, self.logger)
72 self.logger.error("Core file present, debug with: gdb %s %s" %
73 (self.testcase.vpp_bin, core_path))
74 check_core_path(self.logger, core_path)
75 self.logger.error("Running `file %s':" % core_path)
77 info = check_output(["file", core_path])
78 self.logger.error(info)
79 except CalledProcessError as e:
81 "Could not run `file' utility on core-file, "
82 "rc=%s" % e.returncode)
87 Poll the vpp status and throw an exception if it's not running
88 :raises VppDiedError: exception if VPP is not running anymore
90 if self.testcase.vpp_dead:
91 # already dead, nothing to do
94 self.testcase.vpp.poll()
95 if self.testcase.vpp.returncode is not None:
97 (k, v) for v, k in reversed(sorted(signal.__dict__.items()))
98 if v.startswith('SIG') and not v.startswith('SIG_'))
100 if self.testcase.vpp.returncode in signaldict:
101 s = signaldict[abs(self.testcase.vpp.returncode)]
104 msg = "VPP subprocess died unexpectedly with returncode %d [%s]" %\
105 (self.testcase.vpp.returncode, s)
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 before_api(self, api_name, api_args):
115 Check if VPP died before 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).before_api(api_name, api_args)
125 def before_cli(self, cli):
127 Check if VPP died before executing a CLI
129 :param cli: CLI string
130 :raises Exception: exception if VPP is not running anymore
133 super(PollHook, self).before_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 may 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 print("Enter your choice, if any, and press ENTER to continue "
184 "running the testcase...")
185 choice = sys.stdin.readline().rstrip('\r\n')
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)