blob: 6c36d82e3d81c32c635bab5484d6d146a2c79719 [file] [log] [blame]
Stephen Lowriec8548fc2016-05-24 15:57:35 -05001# Copyright 2016 Rackspace
2#
3# All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17"""
18subunit-describe-calls is a parser for subunit streams to determine what REST
19API calls are made inside of a test and in what order they are called.
20
21Runtime Arguments
22-----------------
23
Masayuki Igawabbbaad62017-11-21 16:04:03 +090024* ``--subunit, -s``: (Optional) The path to the subunit file being parsed,
25 defaults to stdin
26* ``--non-subunit-name, -n``: (Optional) The file_name that the logs are being
27 stored in
28* ``--output-file, -o``: (Optional) The path where the JSON output will be
29 written to. This contains more information than is present in stdout.
30* ``--ports, -p``: (Optional) The path to a JSON file describing the ports
31 being used by different services
Doug Schveninger8b3dc862018-02-16 21:42:27 -060032* ``--verbose, -v``: (Optional) Print Request and Response Headers and Body
Doug Schveninger6aa733e2020-07-18 11:03:21 -050033 data to stdout in the non cliff deprecated CLI
34* ``--all-stdout, -a``: (Optional) Print Request and Response Headers and Body
Doug Schveninger8b3dc862018-02-16 21:42:27 -060035 data to stdout
36
Stephen Lowriec8548fc2016-05-24 15:57:35 -050037
38Usage
39-----
40
Stephen Lowrieb85502d2016-06-27 15:05:47 -050041subunit-describe-calls will take in either stdin subunit v1 or v2 stream or a
42file path which contains either a subunit v1 or v2 stream passed via the
Masayuki Igawaf7d53292017-12-13 14:13:28 +090043``--subunit`` parameter. This is then parsed checking for details contained in
44the file_bytes of the ``--non-subunit-name`` parameter (the default is
45pythonlogging which is what Tempest uses to store logs). By default `the
46OpenStack default ports
47<https://docs.openstack.org/install-guide/firewalls-default-ports.html>`_
48are used unless a file is provided via the ``--ports`` option. The resulting
49output is dumped in JSON output to the path provided in the ``--output-file``
50option.
Stephen Lowriec8548fc2016-05-24 15:57:35 -050051
52Ports file JSON structure
53^^^^^^^^^^^^^^^^^^^^^^^^^
Masayuki Igawa62f421d2016-06-29 14:54:04 +090054::
Stephen Lowriec8548fc2016-05-24 15:57:35 -050055
56 {
57 "<port number>": "<name of service>",
58 ...
59 }
60
61
62Output file JSON structure
63^^^^^^^^^^^^^^^^^^^^^^^^^^
Masayuki Igawa62f421d2016-06-29 14:54:04 +090064::
65
Stephen Lowriec8548fc2016-05-24 15:57:35 -050066 {
67 "full_test_name[with_id_and_tags]": [
68 {
69 "name": "The ClassName.MethodName that made the call",
70 "verb": "HTTP Verb",
71 "service": "Name of the service",
72 "url": "A shortened version of the URL called",
Stephen Lowrieb85502d2016-06-27 15:05:47 -050073 "status_code": "The status code of the response",
74 "request_headers": "The headers of the request",
75 "request_body": "The body of the request",
76 "response_headers": "The headers of the response",
77 "response_body": "The body of the response"
Stephen Lowriec8548fc2016-05-24 15:57:35 -050078 }
79 ]
80 }
81"""
82import argparse
83import collections
84import io
Stephen Lowriec8548fc2016-05-24 15:57:35 -050085import os
86import re
Stephen Lowrieb85502d2016-06-27 15:05:47 -050087import sys
Soniya Vyas55ad7cd2019-11-11 11:48:35 +053088import traceback
Stephen Lowriec8548fc2016-05-24 15:57:35 -050089
Soniya Vyas55ad7cd2019-11-11 11:48:35 +053090from cliff.command import Command
zhulingjie92b87a52019-02-21 01:05:54 +080091from oslo_serialization import jsonutils as json
Stephen Lowriec8548fc2016-05-24 15:57:35 -050092import subunit
93import testtools
94
95
Soniya Vyas55ad7cd2019-11-11 11:48:35 +053096DESCRIPTION = "Outputs all HTTP calls a given test made that were logged."
97
98
Stephen Lowriec8548fc2016-05-24 15:57:35 -050099class UrlParser(testtools.TestResult):
Soniya Vyas55ad7cd2019-11-11 11:48:35 +0530100
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500101 uuid_re = re.compile(r'(^|[^0-9a-f])[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-'
102 '[0-9a-f]{4}-[0-9a-f]{12}([^0-9a-f]|$)')
103 id_re = re.compile(r'(^|[^0-9a-z])[0-9a-z]{8}[0-9a-z]{4}[0-9a-z]{4}'
104 '[0-9a-z]{4}[0-9a-z]{12}([^0-9a-z]|$)')
105 ip_re = re.compile(r'(^|[^0-9])[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]'
106 '{1,3}([^0-9]|$)')
107 url_re = re.compile(r'.*INFO.*Request \((?P<name>.*)\): (?P<code>[\d]{3}) '
Stephen Finucane7f4a6212018-07-06 13:58:21 +0100108 r'(?P<verb>\w*) (?P<url>.*) .*')
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500109 port_re = re.compile(r'.*:(?P<port>\d+).*')
110 path_re = re.compile(r'http[s]?://[^/]*/(?P<path>.*)')
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500111 request_re = re.compile(r'.* Request - Headers: (?P<headers>.*)')
112 response_re = re.compile(r'.* Response - Headers: (?P<headers>.*)')
113 body_re = re.compile(r'.*Body: (?P<body>.*)')
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500114
Masayuki Igawaf7d53292017-12-13 14:13:28 +0900115 # Based on OpenStack default ports:
116 # https://docs.openstack.org/install-guide/firewalls-default-ports.html
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500117 services = {
118 "8776": "Block Storage",
119 "8774": "Nova",
120 "8773": "Nova-API", "8775": "Nova-API",
121 "8386": "Sahara",
122 "35357": "Keystone", "5000": "Keystone",
123 "9292": "Glance", "9191": "Glance",
124 "9696": "Neutron",
125 "6000": "Swift", "6001": "Swift", "6002": "Swift",
126 "8004": "Heat", "8000": "Heat", "8003": "Heat",
127 "8777": "Ceilometer",
128 "80": "Horizon",
129 "8080": "Swift",
130 "443": "SSL",
131 "873": "rsync",
132 "3260": "iSCSI",
133 "3306": "MySQL",
Chandan Kumar7d216dc2017-07-28 20:50:39 +0530134 "5672": "AMQP",
Masayuki Igawaf7d53292017-12-13 14:13:28 +0900135 "8082": "murano",
136 "8778": "Clustering",
137 "8999": "Vitrage",
138 "8989": "Mistral"}
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500139
140 def __init__(self, services=None):
141 super(UrlParser, self).__init__()
142 self.test_logs = {}
143 self.services = services or self.services
144
145 def addSuccess(self, test, details=None):
146 output = test.shortDescription() or test.id()
147 calls = self.parse_details(details)
148 self.test_logs.update({output: calls})
149
150 def addSkip(self, test, err, details=None):
151 output = test.shortDescription() or test.id()
152 calls = self.parse_details(details)
153 self.test_logs.update({output: calls})
154
155 def addError(self, test, err, details=None):
156 output = test.shortDescription() or test.id()
157 calls = self.parse_details(details)
158 self.test_logs.update({output: calls})
159
160 def addFailure(self, test, err, details=None):
161 output = test.shortDescription() or test.id()
162 calls = self.parse_details(details)
163 self.test_logs.update({output: calls})
164
165 def stopTestRun(self):
166 super(UrlParser, self).stopTestRun()
167
168 def startTestRun(self):
169 super(UrlParser, self).startTestRun()
170
171 def parse_details(self, details):
172 if details is None:
173 return
174
175 calls = []
176 for _, detail in details.items():
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500177 in_request = False
178 in_response = False
179 current_call = {}
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500180 for line in detail.as_text().split("\n"):
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500181 url_match = self.url_re.match(line)
182 request_match = self.request_re.match(line)
183 response_match = self.response_re.match(line)
184 body_match = self.body_re.match(line)
185
186 if url_match is not None:
187 if current_call != {}:
188 calls.append(current_call.copy())
189 current_call = {}
190 in_request, in_response = False, False
191 current_call.update({
192 "name": url_match.group("name"),
193 "verb": url_match.group("verb"),
194 "status_code": url_match.group("code"),
195 "service": self.get_service(url_match.group("url")),
196 "url": self.url_path(url_match.group("url"))})
197 elif request_match is not None:
198 in_request, in_response = True, False
199 current_call.update(
200 {"request_headers": request_match.group("headers")})
201 elif in_request and body_match is not None:
202 in_request = False
203 current_call.update(
204 {"request_body": body_match.group(
205 "body")})
206 elif response_match is not None:
207 in_request, in_response = False, True
208 current_call.update(
209 {"response_headers": response_match.group(
210 "headers")})
211 elif in_response and body_match is not None:
212 in_response = False
213 current_call.update(
214 {"response_body": body_match.group("body")})
215 if current_call != {}:
216 calls.append(current_call.copy())
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500217
218 return calls
219
220 def get_service(self, url):
221 match = self.port_re.match(url)
222 if match is not None:
223 return self.services.get(match.group("port"), "Unknown")
224 return "Unknown"
225
226 def url_path(self, url):
227 match = self.path_re.match(url)
228 if match is not None:
229 path = match.group("path")
230 path = self.uuid_re.sub(r'\1<uuid>\2', path)
231 path = self.ip_re.sub(r'\1<ip>\2', path)
232 path = self.id_re.sub(r'\1<id>\2', path)
233 return path
234 return url
235
236
237class FileAccumulator(testtools.StreamResult):
238
239 def __init__(self, non_subunit_name='pythonlogging'):
240 super(FileAccumulator, self).__init__()
241 self.route_codes = collections.defaultdict(io.BytesIO)
242 self.non_subunit_name = non_subunit_name
243
244 def status(self, **kwargs):
245 if kwargs.get('file_name') != self.non_subunit_name:
246 return
247 file_bytes = kwargs.get('file_bytes')
248 if not file_bytes:
249 return
250 route_code = kwargs.get('route_code')
251 stream = self.route_codes[route_code]
252 stream.write(file_bytes)
253
254
255class ArgumentParser(argparse.ArgumentParser):
Soniya Vyas55ad7cd2019-11-11 11:48:35 +0530256
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500257 def __init__(self):
Soniya Vyas55ad7cd2019-11-11 11:48:35 +0530258 desc = DESCRIPTION
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500259 super(ArgumentParser, self).__init__(description=desc)
Masayuki Igawa2f03bc92016-07-20 18:21:14 +0900260 self.prog = "subunit-describe-calls"
Soniya Vyas55ad7cd2019-11-11 11:48:35 +0530261 _parser_add_args(self)
Doug Schveninger8b3dc862018-02-16 21:42:27 -0600262
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500263
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500264def parse(stream, non_subunit_name, ports):
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500265 if ports is not None and os.path.exists(ports):
266 ports = json.loads(open(ports).read())
267
268 url_parser = UrlParser(ports)
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500269 suite = subunit.ByteStreamToStreamResult(
270 stream, non_subunit_name=non_subunit_name)
271 result = testtools.StreamToExtendedDecorator(url_parser)
272 accumulator = FileAccumulator(non_subunit_name)
273 result = testtools.StreamResultRouter(result)
274 result.add_rule(accumulator, 'test_id', test_id=None)
275 result.startTestRun()
276 suite.run(result)
277
278 for bytes_io in accumulator.route_codes.values(): # v1 processing
279 bytes_io.seek(0)
280 suite = subunit.ProtocolTestCase(bytes_io)
281 suite.run(url_parser)
282 result.stopTestRun()
283
284 return url_parser
285
286
Doug Schveninger6aa733e2020-07-18 11:03:21 -0500287def output(url_parser, output_file, all_stdout):
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500288 if output_file is not None:
289 with open(output_file, "w") as outfile:
290 outfile.write(json.dumps(url_parser.test_logs))
291 return
292
Andrea Frittolie6a375e2017-02-27 16:06:23 +0000293 for test_name in url_parser.test_logs:
294 items = url_parser.test_logs[test_name]
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500295 sys.stdout.write('{0}\n'.format(test_name))
296 if not items:
297 sys.stdout.write('\n')
298 continue
299 for item in items:
300 sys.stdout.write('\t- {0} {1} request for {2} to {3}\n'.format(
301 item.get('status_code'), item.get('verb'),
302 item.get('service'), item.get('url')))
Doug Schveninger6aa733e2020-07-18 11:03:21 -0500303 if all_stdout:
Doug Schveninger8b3dc862018-02-16 21:42:27 -0600304 sys.stdout.write('\t\t- request headers: {0}\n'.format(
305 item.get('request_headers')))
306 sys.stdout.write('\t\t- request body: {0}\n'.format(
307 item.get('request_body')))
308 sys.stdout.write('\t\t- response headers: {0}\n'.format(
309 item.get('response_headers')))
310 sys.stdout.write('\t\t- response body: {0}\n'.format(
311 item.get('response_body')))
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500312 sys.stdout.write('\n')
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500313
314
Soniya Vyas55ad7cd2019-11-11 11:48:35 +0530315def entry_point(cl_args=None):
316 print('Running subunit_describe_calls ...')
317 if not cl_args:
318 print("Use of: 'subunit-describe-calls' is deprecated, "
319 "please use: 'tempest subunit-describe-calls'")
320 cl_args = ArgumentParser().parse_args()
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500321 parser = parse(cl_args.subunit, cl_args.non_subunit_name, cl_args.ports)
Doug Schveninger6aa733e2020-07-18 11:03:21 -0500322 output(parser, cl_args.output_file, cl_args.all_stdout)
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500323
324
Soniya Vyas55ad7cd2019-11-11 11:48:35 +0530325def _parser_add_args(parser):
326 parser.add_argument(
327 "-s", "--subunit", metavar="<subunit file>",
328 nargs="?", type=argparse.FileType('rb'), default=sys.stdin,
329 help="The path to the subunit output file(default:stdin v1/v2 stream)"
330 )
331
332 parser.add_argument(
333 "-n", "--non-subunit-name", metavar="<non subunit name>",
334 default="pythonlogging",
335 help="The name used in subunit to describe the file contents."
336 )
337
338 parser.add_argument(
339 "-o", "--output-file", metavar="<output file>", default=None,
340 help="The output file name for the json."
341 )
342
343 parser.add_argument(
344 "-p", "--ports", metavar="<ports file>", default=None,
345 help="A JSON file describing the ports for each service."
346 )
347
Doug Schveninger6aa733e2020-07-18 11:03:21 -0500348 group = parser.add_mutually_exclusive_group()
349 # the -v and --verbose command are for the old subunit-describe-calls
350 # main() CLI interface. It does not work with the new
351 # tempest subunit-describe-callss CLI. So when the main CLI approach is
352 # deleted this argument is not needed.
353 group.add_argument(
354 "-v", "--verbose", action='store_true', dest='all_stdout',
355 help='Add Request and Response header and body data to stdout print.'
356 ' NOTE: This argument deprecated and does not work with'
357 ' tempest subunit-describe-calls CLI.'
358 ' Use new option: "-a", "--all-stdout"'
359 )
360 group.add_argument(
361 "-a", "--all-stdout", action='store_true',
362 help="Add Request and Response header and body data to stdout print."
363 " Note: this argument work with the subunit-describe-calls and"
364 " tempest subunit-describe-calls CLI commands."
Soniya Vyas55ad7cd2019-11-11 11:48:35 +0530365 )
366
367
368class TempestSubunitDescribeCalls(Command):
369
370 def get_parser(self, prog_name):
371 parser = super(TempestSubunitDescribeCalls, self).get_parser(prog_name)
372 _parser_add_args(parser)
373 return parser
374
375 def take_action(self, parsed_args):
376 try:
377 entry_point(parsed_args)
378
379 except Exception:
380 traceback.print_exc()
381 raise
382
383 def get_description(self):
384 return DESCRIPTION
385
386
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500387if __name__ == "__main__":
388 entry_point()