Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 1 | # 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 | """ |
| 18 | subunit-describe-calls is a parser for subunit streams to determine what REST |
| 19 | API calls are made inside of a test and in what order they are called. |
| 20 | |
| 21 | Runtime Arguments |
| 22 | ----------------- |
| 23 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 24 | * ``--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 Schveninger | 8b3dc86 | 2018-02-16 21:42:27 -0600 | [diff] [blame] | 32 | * ``--verbose, -v``: (Optional) Print Request and Response Headers and Body |
Doug Schveninger | 6aa733e | 2020-07-18 11:03:21 -0500 | [diff] [blame] | 33 | data to stdout in the non cliff deprecated CLI |
| 34 | * ``--all-stdout, -a``: (Optional) Print Request and Response Headers and Body |
Doug Schveninger | 8b3dc86 | 2018-02-16 21:42:27 -0600 | [diff] [blame] | 35 | data to stdout |
| 36 | |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 37 | |
| 38 | Usage |
| 39 | ----- |
| 40 | |
Stephen Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 41 | subunit-describe-calls will take in either stdin subunit v1 or v2 stream or a |
| 42 | file path which contains either a subunit v1 or v2 stream passed via the |
Masayuki Igawa | f7d5329 | 2017-12-13 14:13:28 +0900 | [diff] [blame] | 43 | ``--subunit`` parameter. This is then parsed checking for details contained in |
| 44 | the file_bytes of the ``--non-subunit-name`` parameter (the default is |
| 45 | pythonlogging which is what Tempest uses to store logs). By default `the |
| 46 | OpenStack default ports |
| 47 | <https://docs.openstack.org/install-guide/firewalls-default-ports.html>`_ |
| 48 | are used unless a file is provided via the ``--ports`` option. The resulting |
| 49 | output is dumped in JSON output to the path provided in the ``--output-file`` |
| 50 | option. |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 51 | |
| 52 | Ports file JSON structure |
| 53 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
Masayuki Igawa | 62f421d | 2016-06-29 14:54:04 +0900 | [diff] [blame] | 54 | :: |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 55 | |
| 56 | { |
| 57 | "<port number>": "<name of service>", |
| 58 | ... |
| 59 | } |
| 60 | |
| 61 | |
| 62 | Output file JSON structure |
| 63 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Masayuki Igawa | 62f421d | 2016-06-29 14:54:04 +0900 | [diff] [blame] | 64 | :: |
| 65 | |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 66 | { |
| 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 Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 73 | "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 Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 78 | } |
| 79 | ] |
| 80 | } |
| 81 | """ |
| 82 | import argparse |
| 83 | import collections |
| 84 | import io |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 85 | import os |
| 86 | import re |
Stephen Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 87 | import sys |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 88 | import traceback |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 89 | |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 90 | from cliff.command import Command |
zhulingjie | 92b87a5 | 2019-02-21 01:05:54 +0800 | [diff] [blame] | 91 | from oslo_serialization import jsonutils as json |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 92 | import subunit |
| 93 | import testtools |
| 94 | |
| 95 | |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 96 | DESCRIPTION = "Outputs all HTTP calls a given test made that were logged." |
| 97 | |
| 98 | |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 99 | class UrlParser(testtools.TestResult): |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 100 | |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 101 | 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 Finucane | 7f4a621 | 2018-07-06 13:58:21 +0100 | [diff] [blame] | 108 | r'(?P<verb>\w*) (?P<url>.*) .*') |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 109 | port_re = re.compile(r'.*:(?P<port>\d+).*') |
| 110 | path_re = re.compile(r'http[s]?://[^/]*/(?P<path>.*)') |
Stephen Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 111 | 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 Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 114 | |
Masayuki Igawa | f7d5329 | 2017-12-13 14:13:28 +0900 | [diff] [blame] | 115 | # Based on OpenStack default ports: |
| 116 | # https://docs.openstack.org/install-guide/firewalls-default-ports.html |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 117 | 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 Kumar | 7d216dc | 2017-07-28 20:50:39 +0530 | [diff] [blame] | 134 | "5672": "AMQP", |
Masayuki Igawa | f7d5329 | 2017-12-13 14:13:28 +0900 | [diff] [blame] | 135 | "8082": "murano", |
| 136 | "8778": "Clustering", |
| 137 | "8999": "Vitrage", |
| 138 | "8989": "Mistral"} |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 139 | |
| 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 Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 177 | in_request = False |
| 178 | in_response = False |
| 179 | current_call = {} |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 180 | for line in detail.as_text().split("\n"): |
Stephen Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 181 | 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 Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 217 | |
| 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 | |
| 237 | class 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 | |
| 255 | class ArgumentParser(argparse.ArgumentParser): |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 256 | |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 257 | def __init__(self): |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 258 | desc = DESCRIPTION |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 259 | super(ArgumentParser, self).__init__(description=desc) |
Masayuki Igawa | 2f03bc9 | 2016-07-20 18:21:14 +0900 | [diff] [blame] | 260 | self.prog = "subunit-describe-calls" |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 261 | _parser_add_args(self) |
Doug Schveninger | 8b3dc86 | 2018-02-16 21:42:27 -0600 | [diff] [blame] | 262 | |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 263 | |
Stephen Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 264 | def parse(stream, non_subunit_name, ports): |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 265 | if ports is not None and os.path.exists(ports): |
| 266 | ports = json.loads(open(ports).read()) |
| 267 | |
| 268 | url_parser = UrlParser(ports) |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 269 | 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 Schveninger | 6aa733e | 2020-07-18 11:03:21 -0500 | [diff] [blame] | 287 | def output(url_parser, output_file, all_stdout): |
Stephen Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 288 | 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 Frittoli | e6a375e | 2017-02-27 16:06:23 +0000 | [diff] [blame] | 293 | for test_name in url_parser.test_logs: |
| 294 | items = url_parser.test_logs[test_name] |
Stephen Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 295 | 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 Schveninger | 6aa733e | 2020-07-18 11:03:21 -0500 | [diff] [blame] | 303 | if all_stdout: |
Doug Schveninger | 8b3dc86 | 2018-02-16 21:42:27 -0600 | [diff] [blame] | 304 | 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 Lowrie | b85502d | 2016-06-27 15:05:47 -0500 | [diff] [blame] | 312 | sys.stdout.write('\n') |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 313 | |
| 314 | |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 315 | def 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 Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 321 | parser = parse(cl_args.subunit, cl_args.non_subunit_name, cl_args.ports) |
Doug Schveninger | 6aa733e | 2020-07-18 11:03:21 -0500 | [diff] [blame] | 322 | output(parser, cl_args.output_file, cl_args.all_stdout) |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 323 | |
| 324 | |
Soniya Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 325 | def _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 Schveninger | 6aa733e | 2020-07-18 11:03:21 -0500 | [diff] [blame] | 348 | 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 Vyas | 55ad7cd | 2019-11-11 11:48:35 +0530 | [diff] [blame] | 365 | ) |
| 366 | |
| 367 | |
| 368 | class 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 Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 387 | if __name__ == "__main__": |
| 388 | entry_point() |