5 from subprocess import check_output, CalledProcessError
9 from config import config
10 from log import RED, single_line_delim, double_line_delim
11 from util import check_core_path, get_core_path
16 Generic hooks before/after API/CLI calls
19 def __init__(self, test):
21 self.logger = test.logger
23 def before_api(self, api_name, api_args):
25 Function called before API call
26 Emit a debug message describing the API name and arguments
28 @param api_name: name of the API
29 @param api_args: tuple containing the API arguments
32 def _friendly_format(val):
33 if not isinstance(val, str):
36 return "{!s} ({!s})".format(
37 val, ":".join(["{:02x}".format(scapy.compat.orb(x)) for x in val])
40 # we don't call test_type(val) because it is a packed value.
41 return "{!s} ({!s})".format(val, str(ipaddress.ip_address(val)))
46 "{!s}={!r}".format(key, _friendly_format(val))
47 for (key, val) in api_args.items()
49 self.logger.debug("API: %s (%s)" % (api_name, _args), extra={"color": RED})
51 def after_api(self, api_name, api_args):
53 Function called after API call
55 @param api_name: name of the API
56 @param api_args: tuple containing the API arguments
60 def before_cli(self, cli):
62 Function called before CLI call
63 Emit a debug message describing the CLI
65 @param cli: CLI string
67 self.logger.debug("CLI: %s" % (cli), extra={"color": RED})
69 def after_cli(self, cli):
71 Function called after CLI call
77 """Hook which checks if the vpp subprocess is alive"""
79 def __init__(self, test):
80 super(PollHook, self).__init__(test)
82 def on_crash(self, core_path):
84 "Core file present, debug with: gdb %s %s", config.vpp, core_path
86 check_core_path(self.logger, core_path)
87 self.logger.error("Running `file %s':", core_path)
89 info = check_output(["file", core_path])
90 self.logger.error(info)
91 except CalledProcessError as e:
93 "Subprocess returned with error running `file' utility on "
100 "Subprocess returned OS error running `file' utility on "
106 except Exception as e:
108 "Subprocess returned unanticipated error running `file' "
109 "utility on core-file, "
116 Poll the vpp status and throw an exception if it's not running
117 :raises VppDiedError: exception if VPP is not running anymore
119 if not hasattr(self.test, "vpp") or self.test.vpp_dead:
120 # already dead, nothing to do
124 if self.test.vpp.returncode is not None:
125 self.test.vpp_dead = True
126 raise framework.VppDiedError(rv=self.test.vpp.returncode)
127 core_path = get_core_path(self.test.tempdir)
128 if os.path.isfile(core_path):
129 self.on_crash(core_path)
131 def before_api(self, api_name, api_args):
133 Check if VPP died before executing an API
135 :param api_name: name of the API
136 :param api_args: tuple containing the API arguments
137 :raises VppDiedError: exception if VPP is not running anymore
140 super(PollHook, self).before_api(api_name, api_args)
143 def before_cli(self, cli):
145 Check if VPP died before executing a CLI
147 :param cli: CLI string
148 :raises Exception: exception if VPP is not running anymore
151 super(PollHook, self).before_cli(cli)
155 class StepHook(PollHook):
156 """Hook which requires user to press ENTER before doing any API/CLI"""
158 def __init__(self, test):
159 self.skip_stack = None
162 self.break_func = None
163 super(StepHook, self).__init__(test)
166 if self.break_func is not None:
167 return self.should_skip_func_based()
168 if self.skip_stack is not None:
169 return self.should_skip_stack_based()
171 def should_skip_func_based(self):
172 stack = traceback.extract_stack()
174 if e[2] == self.break_func:
175 self.break_func = None
179 def should_skip_stack_based(self):
180 stack = traceback.extract_stack()
184 if counter > self.skip_num:
186 if e[0] != self.skip_stack[counter][0]:
188 if e[1] != self.skip_stack[counter][1]:
195 print("%d API/CLI calls skipped in specified stack frame" % self.skip_count)
197 self.skip_stack = None
201 def user_input(self):
202 print("number\tfunction\tfile\tcode")
204 stack = traceback.extract_stack()
206 print("%02d.\t%s\t%s:%d\t[%s]" % (counter, e[2], e[0], e[1], e[3]))
208 print(single_line_delim)
209 print("You may enter a number of stack frame chosen from above")
210 print("Calls in/below that stack frame will be not be stepped anymore")
211 print("Alternatively, enter a test function name to stop at")
212 print(single_line_delim)
215 "Enter your choice, if any, and press ENTER to continue "
216 "running the testcase..."
218 choice = sys.stdin.readline().rstrip("\r\n")
222 if choice is not None:
225 if choice.startswith("test_"):
227 print("Invalid input")
229 if choice is not None and (num < 0 or num >= len(stack)):
230 print("Invalid choice")
233 if choice is not None:
234 if choice.startswith("test_"):
235 self.break_func = choice
237 self.break_func = None
238 self.skip_stack = stack
241 def before_cli(self, cli):
242 """Wait for ENTER before executing CLI"""
244 print("Skip pause before executing CLI: %s" % cli)
246 print(double_line_delim)
247 print("Test paused before executing CLI: %s" % cli)
248 print(single_line_delim)
250 super(StepHook, self).before_cli(cli)
252 def before_api(self, api_name, api_args):
253 """Wait for ENTER before executing API"""
255 print("Skip pause before executing API: %s (%s)" % (api_name, api_args))
257 print(double_line_delim)
258 print("Test paused before executing API: %s (%s)" % (api_name, api_args))
259 print(single_line_delim)
261 super(StepHook, self).before_api(api_name, api_args)