5 from log import RED, single_line_delim, double_line_delim
6 from subprocess import check_output, CalledProcessError
7 from util import check_core_path, get_core_path
12 Generic hooks before/after API/CLI calls
15 def __init__(self, logger):
18 def before_api(self, api_name, api_args):
20 Function called before API call
21 Emit a debug message describing the API name and arguments
23 @param api_name: name of the API
24 @param api_args: tuple containing the API arguments
26 self.logger.debug("API: %s (%s)" %
27 (api_name, api_args), extra={'color': RED})
29 def after_api(self, api_name, api_args):
31 Function called after API call
33 @param api_name: name of the API
34 @param api_args: tuple containing the API arguments
38 def before_cli(self, cli):
40 Function called before CLI call
41 Emit a debug message describing the CLI
43 @param cli: CLI string
45 self.logger.debug("CLI: %s" % (cli), extra={'color': RED})
47 def after_cli(self, cli):
49 Function called after CLI call
54 class VppDiedError(Exception):
59 """ Hook which checks if the vpp subprocess is alive """
61 def __init__(self, testcase):
62 super(PollHook, self).__init__(testcase.logger)
63 self.testcase = testcase
65 def on_crash(self, core_path):
66 self.logger.error("Core file present, debug with: gdb %s %s" %
67 (self.testcase.vpp_bin, core_path))
68 check_core_path(self.logger, core_path)
69 self.logger.error("Running `file %s':" % core_path)
71 info = check_output(["file", core_path])
72 self.logger.error(info)
73 except CalledProcessError as e:
75 "Could not run `file' utility on core-file, "
76 "rc=%s" % e.returncode)
80 Poll the vpp status and throw an exception if it's not running
81 :raises VppDiedError: exception if VPP is not running anymore
83 if self.testcase.vpp_dead:
84 # already dead, nothing to do
87 self.testcase.vpp.poll()
88 if self.testcase.vpp.returncode is not None:
90 (k, v) for v, k in reversed(sorted(signal.__dict__.items()))
91 if v.startswith('SIG') and not v.startswith('SIG_'))
93 if self.testcase.vpp.returncode in signaldict:
94 s = signaldict[abs(self.testcase.vpp.returncode)]
97 msg = "VPP subprocess died unexpectedly with returncode %d [%s]" %\
98 (self.testcase.vpp.returncode, s)
99 self.logger.critical(msg)
100 core_path = get_core_path(self.testcase.tempdir)
101 if os.path.isfile(core_path):
102 self.on_crash(core_path)
103 self.testcase.vpp_dead = True
104 raise VppDiedError(msg)
106 def before_api(self, api_name, api_args):
108 Check if VPP died before executing an API
110 :param api_name: name of the API
111 :param api_args: tuple containing the API arguments
112 :raises VppDiedError: exception if VPP is not running anymore
115 super(PollHook, self).before_api(api_name, api_args)
118 def before_cli(self, cli):
120 Check if VPP died before executing a CLI
122 :param cli: CLI string
123 :raises Exception: exception if VPP is not running anymore
126 super(PollHook, self).before_cli(cli)
130 class StepHook(PollHook):
131 """ Hook which requires user to press ENTER before doing any API/CLI """
133 def __init__(self, testcase):
134 self.skip_stack = None
137 super(StepHook, self).__init__(testcase)
140 if self.skip_stack is None:
142 stack = traceback.extract_stack()
146 if counter > self.skip_num:
148 if e[0] != self.skip_stack[counter][0]:
150 if e[1] != self.skip_stack[counter][1]:
157 print("%d API/CLI calls skipped in specified stack "
158 "frame" % self.skip_count)
160 self.skip_stack = None
164 def user_input(self):
165 print('number\tfunction\tfile\tcode')
167 stack = traceback.extract_stack()
169 print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3]))
171 print(single_line_delim)
172 print("You may enter a number of stack frame chosen from above")
173 print("Calls in/below that stack frame will be not be stepped anymore")
174 print(single_line_delim)
176 print("Enter your choice, if any, and press ENTER to continue "
177 "running the testcase...")
178 choice = sys.stdin.readline().rstrip('\r\n')
182 if choice is not None:
185 print("Invalid input")
187 if choice is not None and (num < 0 or num >= len(stack)):
188 print("Invalid choice")
191 if choice is not None:
192 self.skip_stack = stack
195 def before_cli(self, cli):
196 """ Wait for ENTER before executing CLI """
198 print("Skip pause before executing CLI: %s" % cli)
200 print(double_line_delim)
201 print("Test paused before executing CLI: %s" % cli)
202 print(single_line_delim)
204 super(StepHook, self).before_cli(cli)
206 def before_api(self, api_name, api_args):
207 """ Wait for ENTER before executing API """
209 print("Skip pause before executing API: %s (%s)"
210 % (api_name, api_args))
212 print(double_line_delim)
213 print("Test paused before executing API: %s (%s)"
214 % (api_name, api_args))
215 print(single_line_delim)
217 super(StepHook, self).before_api(api_name, api_args)