blob: 97b05d05153941d6169e3dea3282aa31fcc6d09f [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001import os
Klement Sekera13a83ef2018-03-21 12:35:51 +01002import sys
Klement Sekera277b89c2016-10-28 13:20:27 +02003import traceback
Paul Vinciguerra1314ec62018-12-12 01:04:20 -08004import ipaddress
Klement Sekera9b6ece72018-03-23 10:50:11 +01005from subprocess import check_output, CalledProcessError
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07006
7import scapy.compat
Paul Vinciguerra496b0de2019-06-20 12:24:12 -04008import framework
9from log import RED, single_line_delim, double_line_delim
juraj.linkes40dd73b2018-09-21 13:55:16 +020010from util import check_core_path, get_core_path
Klement Sekeraf62ae122016-10-11 11:47:09 +020011
12
13class Hook(object):
14 """
15 Generic hooks before/after API/CLI calls
16 """
17
Paul Vinciguerra895e2f82019-01-08 20:37:40 -080018 def __init__(self, test):
19 self.test = test
20 self.logger = test.logger
Klement Sekera277b89c2016-10-28 13:20:27 +020021
Klement Sekeraf62ae122016-10-11 11:47:09 +020022 def before_api(self, api_name, api_args):
23 """
24 Function called before API call
25 Emit a debug message describing the API name and arguments
26
27 @param api_name: name of the API
28 @param api_args: tuple containing the API arguments
29 """
Paul Vinciguerra1314ec62018-12-12 01:04:20 -080030
31 def _friendly_format(val):
32 if not isinstance(val, str):
33 return val
34 if len(val) == 6:
35 return '{!s} ({!s})'.format(val, ':'.join(['{:02x}'.format(
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -070036 scapy.compat.orb(x)) for x in val]))
Paul Vinciguerra1314ec62018-12-12 01:04:20 -080037 try:
Paul Vinciguerra9e315952019-01-29 11:51:44 -080038 # we don't call test_type(val) because it is a packed value.
Paul Vinciguerra1314ec62018-12-12 01:04:20 -080039 return '{!s} ({!s})'.format(val, str(
40 ipaddress.ip_address(val)))
Naveen Joy64f75302019-03-27 14:28:50 -070041 except ValueError:
Paul Vinciguerra1314ec62018-12-12 01:04:20 -080042 return val
43
44 _args = ', '.join("{!s}={!r}".format(key, _friendly_format(val)) for
45 (key, val) in api_args.items())
Klement Sekera277b89c2016-10-28 13:20:27 +020046 self.logger.debug("API: %s (%s)" %
Paul Vinciguerra1314ec62018-12-12 01:04:20 -080047 (api_name, _args), extra={'color': RED})
Klement Sekeraf62ae122016-10-11 11:47:09 +020048
49 def after_api(self, api_name, api_args):
50 """
51 Function called after API call
52
53 @param api_name: name of the API
54 @param api_args: tuple containing the API arguments
55 """
56 pass
57
58 def before_cli(self, cli):
59 """
60 Function called before CLI call
61 Emit a debug message describing the CLI
62
63 @param cli: CLI string
64 """
Klement Sekera277b89c2016-10-28 13:20:27 +020065 self.logger.debug("CLI: %s" % (cli), extra={'color': RED})
Klement Sekeraf62ae122016-10-11 11:47:09 +020066
67 def after_cli(self, cli):
68 """
69 Function called after CLI call
70 """
71 pass
72
73
Klement Sekeraf62ae122016-10-11 11:47:09 +020074class PollHook(Hook):
75 """ Hook which checks if the vpp subprocess is alive """
76
Paul Vinciguerra895e2f82019-01-08 20:37:40 -080077 def __init__(self, test):
78 super(PollHook, self).__init__(test)
Klement Sekeraf62ae122016-10-11 11:47:09 +020079
Klement Sekeraf62ae122016-10-11 11:47:09 +020080 def on_crash(self, core_path):
Paul Vinciguerra38a4ec72018-11-28 11:34:21 -080081 self.logger.error("Core file present, debug with: gdb %s %s",
Paul Vinciguerraa1bfb3a2019-03-07 17:30:28 -080082 self.test.vpp_bin, core_path)
juraj.linkes40dd73b2018-09-21 13:55:16 +020083 check_core_path(self.logger, core_path)
Paul Vinciguerra38a4ec72018-11-28 11:34:21 -080084 self.logger.error("Running `file %s':", core_path)
juraj.linkes40dd73b2018-09-21 13:55:16 +020085 try:
86 info = check_output(["file", core_path])
87 self.logger.error(info)
88 except CalledProcessError as e:
89 self.logger.error(
Paul Vinciguerra38a4ec72018-11-28 11:34:21 -080090 "Subprocess returned with error running `file' utility on "
91 "core-file, "
92 "rc=%s", e.returncode)
93 except OSError as e:
94 self.logger.error(
95 "Subprocess returned OS error running `file' utility on "
96 "core-file, "
97 "oserror=(%s) %s", e.errno, e.strerror)
98 except Exception as e:
99 self.logger.error(
100 "Subprocess returned unanticipated error running `file' "
101 "utility on core-file, "
102 "%s", e)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200103
104 def poll_vpp(self):
105 """
106 Poll the vpp status and throw an exception if it's not running
107 :raises VppDiedError: exception if VPP is not running anymore
108 """
Paul Vinciguerra895e2f82019-01-08 20:37:40 -0800109 if self.test.vpp_dead:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200110 # already dead, nothing to do
111 return
112
Paul Vinciguerra895e2f82019-01-08 20:37:40 -0800113 self.test.vpp.poll()
114 if self.test.vpp.returncode is not None:
Paul Vinciguerra496b0de2019-06-20 12:24:12 -0400115 self.test.vpp_dead = True
116 raise framework.VppDiedError(rv=self.test.vpp.returncode)
Paul Vinciguerra895e2f82019-01-08 20:37:40 -0800117 core_path = get_core_path(self.test.tempdir)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200118 if os.path.isfile(core_path):
119 self.on_crash(core_path)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200120
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200121 def before_api(self, api_name, api_args):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200122 """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200123 Check if VPP died before executing an API
Klement Sekeraf62ae122016-10-11 11:47:09 +0200124
125 :param api_name: name of the API
126 :param api_args: tuple containing the API arguments
127 :raises VppDiedError: exception if VPP is not running anymore
128
129 """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200130 super(PollHook, self).before_api(api_name, api_args)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200131 self.poll_vpp()
132
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200133 def before_cli(self, cli):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200134 """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200135 Check if VPP died before executing a CLI
Klement Sekeraf62ae122016-10-11 11:47:09 +0200136
137 :param cli: CLI string
138 :raises Exception: exception if VPP is not running anymore
139
140 """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200141 super(PollHook, self).before_cli(cli)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200142 self.poll_vpp()
Klement Sekera277b89c2016-10-28 13:20:27 +0200143
144
145class StepHook(PollHook):
146 """ Hook which requires user to press ENTER before doing any API/CLI """
147
Paul Vinciguerra895e2f82019-01-08 20:37:40 -0800148 def __init__(self, test):
Klement Sekera277b89c2016-10-28 13:20:27 +0200149 self.skip_stack = None
150 self.skip_num = None
151 self.skip_count = 0
Paul Vinciguerra895e2f82019-01-08 20:37:40 -0800152 super(StepHook, self).__init__(test)
Klement Sekera277b89c2016-10-28 13:20:27 +0200153
154 def skip(self):
155 if self.skip_stack is None:
156 return False
157 stack = traceback.extract_stack()
158 counter = 0
159 skip = True
160 for e in stack:
161 if counter > self.skip_num:
162 break
163 if e[0] != self.skip_stack[counter][0]:
164 skip = False
165 if e[1] != self.skip_stack[counter][1]:
166 skip = False
167 counter += 1
168 if skip:
169 self.skip_count += 1
170 return True
171 else:
172 print("%d API/CLI calls skipped in specified stack "
173 "frame" % self.skip_count)
174 self.skip_count = 0
175 self.skip_stack = None
176 self.skip_num = None
177 return False
178
179 def user_input(self):
180 print('number\tfunction\tfile\tcode')
181 counter = 0
182 stack = traceback.extract_stack()
183 for e in stack:
184 print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3]))
185 counter += 1
186 print(single_line_delim)
juraj.linkes184870a2018-07-16 14:22:01 +0200187 print("You may enter a number of stack frame chosen from above")
Klement Sekera277b89c2016-10-28 13:20:27 +0200188 print("Calls in/below that stack frame will be not be stepped anymore")
189 print(single_line_delim)
190 while True:
juraj.linkes184870a2018-07-16 14:22:01 +0200191 print("Enter your choice, if any, and press ENTER to continue "
192 "running the testcase...")
juraj.linkesbe460e72018-08-28 18:45:18 +0200193 choice = sys.stdin.readline().rstrip('\r\n')
Klement Sekera277b89c2016-10-28 13:20:27 +0200194 if choice == "":
195 choice = None
196 try:
197 if choice is not None:
198 num = int(choice)
juraj.linkes184870a2018-07-16 14:22:01 +0200199 except ValueError:
Klement Sekera277b89c2016-10-28 13:20:27 +0200200 print("Invalid input")
201 continue
202 if choice is not None and (num < 0 or num >= len(stack)):
203 print("Invalid choice")
204 continue
205 break
206 if choice is not None:
207 self.skip_stack = stack
208 self.skip_num = num
209
210 def before_cli(self, cli):
211 """ Wait for ENTER before executing CLI """
212 if self.skip():
213 print("Skip pause before executing CLI: %s" % cli)
214 else:
215 print(double_line_delim)
216 print("Test paused before executing CLI: %s" % cli)
217 print(single_line_delim)
218 self.user_input()
219 super(StepHook, self).before_cli(cli)
220
221 def before_api(self, api_name, api_args):
222 """ Wait for ENTER before executing API """
223 if self.skip():
224 print("Skip pause before executing API: %s (%s)"
225 % (api_name, api_args))
226 else:
227 print(double_line_delim)
228 print("Test paused before executing API: %s (%s)"
229 % (api_name, api_args))
230 print(single_line_delim)
231 self.user_input()
232 super(StepHook, self).before_api(api_name, api_args)