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 | |
| 24 | **--subunit, -s**: (Required) The path to the subunit file being parsed |
| 25 | |
| 26 | **--non-subunit-name, -n**: (Optional) The file_name that the logs are being |
| 27 | stored in |
| 28 | |
| 29 | **--output-file, -o**: (Required) The path where the JSON output will be |
| 30 | written to |
| 31 | |
| 32 | **--ports, -p**: (Optional) The path to a JSON file describing the ports being |
| 33 | used by different services |
| 34 | |
| 35 | Usage |
| 36 | ----- |
| 37 | |
| 38 | subunit-describe-calls will take in a file path via the --subunit parameter |
| 39 | which contains either a subunit v1 or v2 stream. This is then parsed checking |
| 40 | for details contained in the file_bytes of the --non-subunit-name parameter |
| 41 | (the default is pythonlogging which is what Tempest uses to store logs). By |
| 42 | default the OpenStack Kilo release port defaults (http://bit.ly/22jpF5P) |
| 43 | are used unless a file is provided via the --ports option. The resulting output |
| 44 | is dumped in JSON output to the path provided in the --output-file option. |
| 45 | |
| 46 | Ports file JSON structure |
| 47 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
Masayuki Igawa | 62f421d | 2016-06-29 14:54:04 +0900 | [diff] [blame] | 48 | :: |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 49 | |
| 50 | { |
| 51 | "<port number>": "<name of service>", |
| 52 | ... |
| 53 | } |
| 54 | |
| 55 | |
| 56 | Output file JSON structure |
| 57 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Masayuki Igawa | 62f421d | 2016-06-29 14:54:04 +0900 | [diff] [blame] | 58 | :: |
| 59 | |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 60 | { |
| 61 | "full_test_name[with_id_and_tags]": [ |
| 62 | { |
| 63 | "name": "The ClassName.MethodName that made the call", |
| 64 | "verb": "HTTP Verb", |
| 65 | "service": "Name of the service", |
| 66 | "url": "A shortened version of the URL called", |
| 67 | "status_code": "The status code of the response" |
| 68 | } |
| 69 | ] |
| 70 | } |
| 71 | """ |
| 72 | import argparse |
| 73 | import collections |
| 74 | import io |
| 75 | import json |
| 76 | import os |
| 77 | import re |
| 78 | |
| 79 | import subunit |
| 80 | import testtools |
| 81 | |
| 82 | |
| 83 | class UrlParser(testtools.TestResult): |
| 84 | uuid_re = re.compile(r'(^|[^0-9a-f])[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-' |
| 85 | '[0-9a-f]{4}-[0-9a-f]{12}([^0-9a-f]|$)') |
| 86 | id_re = re.compile(r'(^|[^0-9a-z])[0-9a-z]{8}[0-9a-z]{4}[0-9a-z]{4}' |
| 87 | '[0-9a-z]{4}[0-9a-z]{12}([^0-9a-z]|$)') |
| 88 | ip_re = re.compile(r'(^|[^0-9])[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]' |
| 89 | '{1,3}([^0-9]|$)') |
| 90 | url_re = re.compile(r'.*INFO.*Request \((?P<name>.*)\): (?P<code>[\d]{3}) ' |
| 91 | '(?P<verb>\w*) (?P<url>.*) .*') |
| 92 | port_re = re.compile(r'.*:(?P<port>\d+).*') |
| 93 | path_re = re.compile(r'http[s]?://[^/]*/(?P<path>.*)') |
| 94 | |
| 95 | # Based on mitaka defaults: |
| 96 | # http://docs.openstack.org/mitaka/config-reference/ |
| 97 | # firewalls-default-ports.html |
| 98 | services = { |
| 99 | "8776": "Block Storage", |
| 100 | "8774": "Nova", |
| 101 | "8773": "Nova-API", "8775": "Nova-API", |
| 102 | "8386": "Sahara", |
| 103 | "35357": "Keystone", "5000": "Keystone", |
| 104 | "9292": "Glance", "9191": "Glance", |
| 105 | "9696": "Neutron", |
| 106 | "6000": "Swift", "6001": "Swift", "6002": "Swift", |
| 107 | "8004": "Heat", "8000": "Heat", "8003": "Heat", |
| 108 | "8777": "Ceilometer", |
| 109 | "80": "Horizon", |
| 110 | "8080": "Swift", |
| 111 | "443": "SSL", |
| 112 | "873": "rsync", |
| 113 | "3260": "iSCSI", |
| 114 | "3306": "MySQL", |
| 115 | "5672": "AMQP"} |
| 116 | |
| 117 | def __init__(self, services=None): |
| 118 | super(UrlParser, self).__init__() |
| 119 | self.test_logs = {} |
| 120 | self.services = services or self.services |
| 121 | |
| 122 | def addSuccess(self, test, details=None): |
| 123 | output = test.shortDescription() or test.id() |
| 124 | calls = self.parse_details(details) |
| 125 | self.test_logs.update({output: calls}) |
| 126 | |
| 127 | def addSkip(self, test, err, details=None): |
| 128 | output = test.shortDescription() or test.id() |
| 129 | calls = self.parse_details(details) |
| 130 | self.test_logs.update({output: calls}) |
| 131 | |
| 132 | def addError(self, test, err, details=None): |
| 133 | output = test.shortDescription() or test.id() |
| 134 | calls = self.parse_details(details) |
| 135 | self.test_logs.update({output: calls}) |
| 136 | |
| 137 | def addFailure(self, test, err, details=None): |
| 138 | output = test.shortDescription() or test.id() |
| 139 | calls = self.parse_details(details) |
| 140 | self.test_logs.update({output: calls}) |
| 141 | |
| 142 | def stopTestRun(self): |
| 143 | super(UrlParser, self).stopTestRun() |
| 144 | |
| 145 | def startTestRun(self): |
| 146 | super(UrlParser, self).startTestRun() |
| 147 | |
| 148 | def parse_details(self, details): |
| 149 | if details is None: |
| 150 | return |
| 151 | |
| 152 | calls = [] |
| 153 | for _, detail in details.items(): |
| 154 | for line in detail.as_text().split("\n"): |
| 155 | match = self.url_re.match(line) |
| 156 | if match is not None: |
| 157 | calls.append({ |
| 158 | "name": match.group("name"), |
| 159 | "verb": match.group("verb"), |
| 160 | "status_code": match.group("code"), |
| 161 | "service": self.get_service(match.group("url")), |
| 162 | "url": self.url_path(match.group("url"))}) |
| 163 | |
| 164 | return calls |
| 165 | |
| 166 | def get_service(self, url): |
| 167 | match = self.port_re.match(url) |
| 168 | if match is not None: |
| 169 | return self.services.get(match.group("port"), "Unknown") |
| 170 | return "Unknown" |
| 171 | |
| 172 | def url_path(self, url): |
| 173 | match = self.path_re.match(url) |
| 174 | if match is not None: |
| 175 | path = match.group("path") |
| 176 | path = self.uuid_re.sub(r'\1<uuid>\2', path) |
| 177 | path = self.ip_re.sub(r'\1<ip>\2', path) |
| 178 | path = self.id_re.sub(r'\1<id>\2', path) |
| 179 | return path |
| 180 | return url |
| 181 | |
| 182 | |
| 183 | class FileAccumulator(testtools.StreamResult): |
| 184 | |
| 185 | def __init__(self, non_subunit_name='pythonlogging'): |
| 186 | super(FileAccumulator, self).__init__() |
| 187 | self.route_codes = collections.defaultdict(io.BytesIO) |
| 188 | self.non_subunit_name = non_subunit_name |
| 189 | |
| 190 | def status(self, **kwargs): |
| 191 | if kwargs.get('file_name') != self.non_subunit_name: |
| 192 | return |
| 193 | file_bytes = kwargs.get('file_bytes') |
| 194 | if not file_bytes: |
| 195 | return |
| 196 | route_code = kwargs.get('route_code') |
| 197 | stream = self.route_codes[route_code] |
| 198 | stream.write(file_bytes) |
| 199 | |
| 200 | |
| 201 | class ArgumentParser(argparse.ArgumentParser): |
| 202 | def __init__(self): |
| 203 | desc = "Outputs all HTTP calls a given test made that were logged." |
| 204 | super(ArgumentParser, self).__init__(description=desc) |
| 205 | |
Masayuki Igawa | 2f03bc9 | 2016-07-20 18:21:14 +0900 | [diff] [blame^] | 206 | self.prog = "subunit-describe-calls" |
Stephen Lowrie | c8548fc | 2016-05-24 15:57:35 -0500 | [diff] [blame] | 207 | |
| 208 | self.add_argument( |
| 209 | "-s", "--subunit", metavar="<subunit file>", required=True, |
| 210 | default=None, help="The path to the subunit output file.") |
| 211 | |
| 212 | self.add_argument( |
| 213 | "-n", "--non-subunit-name", metavar="<non subunit name>", |
| 214 | default="pythonlogging", |
| 215 | help="The name used in subunit to describe the file contents.") |
| 216 | |
| 217 | self.add_argument( |
| 218 | "-o", "--output-file", metavar="<output file>", default=None, |
| 219 | help="The output file name for the json.", required=True) |
| 220 | |
| 221 | self.add_argument( |
| 222 | "-p", "--ports", metavar="<ports file>", default=None, |
| 223 | help="A JSON file describing the ports for each service.") |
| 224 | |
| 225 | |
| 226 | def parse(subunit_file, non_subunit_name, ports): |
| 227 | if ports is not None and os.path.exists(ports): |
| 228 | ports = json.loads(open(ports).read()) |
| 229 | |
| 230 | url_parser = UrlParser(ports) |
| 231 | stream = open(subunit_file, 'rb') |
| 232 | suite = subunit.ByteStreamToStreamResult( |
| 233 | stream, non_subunit_name=non_subunit_name) |
| 234 | result = testtools.StreamToExtendedDecorator(url_parser) |
| 235 | accumulator = FileAccumulator(non_subunit_name) |
| 236 | result = testtools.StreamResultRouter(result) |
| 237 | result.add_rule(accumulator, 'test_id', test_id=None) |
| 238 | result.startTestRun() |
| 239 | suite.run(result) |
| 240 | |
| 241 | for bytes_io in accumulator.route_codes.values(): # v1 processing |
| 242 | bytes_io.seek(0) |
| 243 | suite = subunit.ProtocolTestCase(bytes_io) |
| 244 | suite.run(url_parser) |
| 245 | result.stopTestRun() |
| 246 | |
| 247 | return url_parser |
| 248 | |
| 249 | |
| 250 | def output(url_parser, output_file): |
| 251 | with open(output_file, "w") as outfile: |
| 252 | outfile.write(json.dumps(url_parser.test_logs)) |
| 253 | |
| 254 | |
| 255 | def entry_point(): |
| 256 | cl_args = ArgumentParser().parse_args() |
| 257 | parser = parse(cl_args.subunit, cl_args.non_subunit_name, cl_args.ports) |
| 258 | output(parser, cl_args.output_file) |
| 259 | |
| 260 | |
| 261 | if __name__ == "__main__": |
| 262 | entry_point() |