blob: 0f868a93366779d258def3c43e0c3a6497c9751b [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
Stephen Lowrieb85502d2016-06-27 15:05:47 -050024**--subunit, -s**: (Optional) The path to the subunit file being parsed,
25defaults to stdin
Stephen Lowriec8548fc2016-05-24 15:57:35 -050026
27**--non-subunit-name, -n**: (Optional) The file_name that the logs are being
28stored in
29
Stephen Lowrieb85502d2016-06-27 15:05:47 -050030**--output-file, -o**: (Optional) The path where the JSON output will be
31written to. This contains more information than is present in stdout.
Stephen Lowriec8548fc2016-05-24 15:57:35 -050032
33**--ports, -p**: (Optional) The path to a JSON file describing the ports being
34used by different services
35
36Usage
37-----
38
Stephen Lowrieb85502d2016-06-27 15:05:47 -050039subunit-describe-calls will take in either stdin subunit v1 or v2 stream or a
40file path which contains either a subunit v1 or v2 stream passed via the
41--subunit parameter. This is then parsed checking for details contained in the
42file_bytes of the --non-subunit-name parameter (the default is pythonlogging
43which is what Tempest uses to store logs). By default the OpenStack Kilo
44release port defaults (http://bit.ly/22jpF5P) are used unless a file is
45provided via the --ports option. The resulting output is dumped in JSON output
46to the path provided in the --output-file option.
Stephen Lowriec8548fc2016-05-24 15:57:35 -050047
48Ports file JSON structure
49^^^^^^^^^^^^^^^^^^^^^^^^^
Masayuki Igawa62f421d2016-06-29 14:54:04 +090050::
Stephen Lowriec8548fc2016-05-24 15:57:35 -050051
52 {
53 "<port number>": "<name of service>",
54 ...
55 }
56
57
58Output file JSON structure
59^^^^^^^^^^^^^^^^^^^^^^^^^^
Masayuki Igawa62f421d2016-06-29 14:54:04 +090060::
61
Stephen Lowriec8548fc2016-05-24 15:57:35 -050062 {
63 "full_test_name[with_id_and_tags]": [
64 {
65 "name": "The ClassName.MethodName that made the call",
66 "verb": "HTTP Verb",
67 "service": "Name of the service",
68 "url": "A shortened version of the URL called",
Stephen Lowrieb85502d2016-06-27 15:05:47 -050069 "status_code": "The status code of the response",
70 "request_headers": "The headers of the request",
71 "request_body": "The body of the request",
72 "response_headers": "The headers of the response",
73 "response_body": "The body of the response"
Stephen Lowriec8548fc2016-05-24 15:57:35 -050074 }
75 ]
76 }
77"""
78import argparse
79import collections
80import io
81import json
82import os
83import re
Stephen Lowrieb85502d2016-06-27 15:05:47 -050084import sys
Stephen Lowriec8548fc2016-05-24 15:57:35 -050085
86import subunit
87import testtools
88
89
90class UrlParser(testtools.TestResult):
91 uuid_re = re.compile(r'(^|[^0-9a-f])[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-'
92 '[0-9a-f]{4}-[0-9a-f]{12}([^0-9a-f]|$)')
93 id_re = re.compile(r'(^|[^0-9a-z])[0-9a-z]{8}[0-9a-z]{4}[0-9a-z]{4}'
94 '[0-9a-z]{4}[0-9a-z]{12}([^0-9a-z]|$)')
95 ip_re = re.compile(r'(^|[^0-9])[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]'
96 '{1,3}([^0-9]|$)')
97 url_re = re.compile(r'.*INFO.*Request \((?P<name>.*)\): (?P<code>[\d]{3}) '
98 '(?P<verb>\w*) (?P<url>.*) .*')
99 port_re = re.compile(r'.*:(?P<port>\d+).*')
100 path_re = re.compile(r'http[s]?://[^/]*/(?P<path>.*)')
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500101 request_re = re.compile(r'.* Request - Headers: (?P<headers>.*)')
102 response_re = re.compile(r'.* Response - Headers: (?P<headers>.*)')
103 body_re = re.compile(r'.*Body: (?P<body>.*)')
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500104
105 # Based on mitaka defaults:
106 # http://docs.openstack.org/mitaka/config-reference/
107 # firewalls-default-ports.html
108 services = {
109 "8776": "Block Storage",
110 "8774": "Nova",
111 "8773": "Nova-API", "8775": "Nova-API",
112 "8386": "Sahara",
113 "35357": "Keystone", "5000": "Keystone",
114 "9292": "Glance", "9191": "Glance",
115 "9696": "Neutron",
116 "6000": "Swift", "6001": "Swift", "6002": "Swift",
117 "8004": "Heat", "8000": "Heat", "8003": "Heat",
118 "8777": "Ceilometer",
119 "80": "Horizon",
120 "8080": "Swift",
121 "443": "SSL",
122 "873": "rsync",
123 "3260": "iSCSI",
124 "3306": "MySQL",
125 "5672": "AMQP"}
126
127 def __init__(self, services=None):
128 super(UrlParser, self).__init__()
129 self.test_logs = {}
130 self.services = services or self.services
131
132 def addSuccess(self, test, 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 addSkip(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 addError(self, test, err, details=None):
143 output = test.shortDescription() or test.id()
144 calls = self.parse_details(details)
145 self.test_logs.update({output: calls})
146
147 def addFailure(self, test, err, details=None):
148 output = test.shortDescription() or test.id()
149 calls = self.parse_details(details)
150 self.test_logs.update({output: calls})
151
152 def stopTestRun(self):
153 super(UrlParser, self).stopTestRun()
154
155 def startTestRun(self):
156 super(UrlParser, self).startTestRun()
157
158 def parse_details(self, details):
159 if details is None:
160 return
161
162 calls = []
163 for _, detail in details.items():
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500164 in_request = False
165 in_response = False
166 current_call = {}
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500167 for line in detail.as_text().split("\n"):
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500168 url_match = self.url_re.match(line)
169 request_match = self.request_re.match(line)
170 response_match = self.response_re.match(line)
171 body_match = self.body_re.match(line)
172
173 if url_match is not None:
174 if current_call != {}:
175 calls.append(current_call.copy())
176 current_call = {}
177 in_request, in_response = False, False
178 current_call.update({
179 "name": url_match.group("name"),
180 "verb": url_match.group("verb"),
181 "status_code": url_match.group("code"),
182 "service": self.get_service(url_match.group("url")),
183 "url": self.url_path(url_match.group("url"))})
184 elif request_match is not None:
185 in_request, in_response = True, False
186 current_call.update(
187 {"request_headers": request_match.group("headers")})
188 elif in_request and body_match is not None:
189 in_request = False
190 current_call.update(
191 {"request_body": body_match.group(
192 "body")})
193 elif response_match is not None:
194 in_request, in_response = False, True
195 current_call.update(
196 {"response_headers": response_match.group(
197 "headers")})
198 elif in_response and body_match is not None:
199 in_response = False
200 current_call.update(
201 {"response_body": body_match.group("body")})
202 if current_call != {}:
203 calls.append(current_call.copy())
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500204
205 return calls
206
207 def get_service(self, url):
208 match = self.port_re.match(url)
209 if match is not None:
210 return self.services.get(match.group("port"), "Unknown")
211 return "Unknown"
212
213 def url_path(self, url):
214 match = self.path_re.match(url)
215 if match is not None:
216 path = match.group("path")
217 path = self.uuid_re.sub(r'\1<uuid>\2', path)
218 path = self.ip_re.sub(r'\1<ip>\2', path)
219 path = self.id_re.sub(r'\1<id>\2', path)
220 return path
221 return url
222
223
224class FileAccumulator(testtools.StreamResult):
225
226 def __init__(self, non_subunit_name='pythonlogging'):
227 super(FileAccumulator, self).__init__()
228 self.route_codes = collections.defaultdict(io.BytesIO)
229 self.non_subunit_name = non_subunit_name
230
231 def status(self, **kwargs):
232 if kwargs.get('file_name') != self.non_subunit_name:
233 return
234 file_bytes = kwargs.get('file_bytes')
235 if not file_bytes:
236 return
237 route_code = kwargs.get('route_code')
238 stream = self.route_codes[route_code]
239 stream.write(file_bytes)
240
241
242class ArgumentParser(argparse.ArgumentParser):
243 def __init__(self):
244 desc = "Outputs all HTTP calls a given test made that were logged."
245 super(ArgumentParser, self).__init__(description=desc)
246
Masayuki Igawa2f03bc92016-07-20 18:21:14 +0900247 self.prog = "subunit-describe-calls"
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500248
249 self.add_argument(
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500250 "-s", "--subunit", metavar="<subunit file>",
251 nargs="?", type=argparse.FileType('rb'), default=sys.stdin,
252 help="The path to the subunit output file.")
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500253
254 self.add_argument(
255 "-n", "--non-subunit-name", metavar="<non subunit name>",
256 default="pythonlogging",
257 help="The name used in subunit to describe the file contents.")
258
259 self.add_argument(
260 "-o", "--output-file", metavar="<output file>", default=None,
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500261 help="The output file name for the json.")
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500262
263 self.add_argument(
264 "-p", "--ports", metavar="<ports file>", default=None,
265 help="A JSON file describing the ports for each service.")
266
267
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500268def parse(stream, non_subunit_name, ports):
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500269 if ports is not None and os.path.exists(ports):
270 ports = json.loads(open(ports).read())
271
272 url_parser = UrlParser(ports)
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500273 suite = subunit.ByteStreamToStreamResult(
274 stream, non_subunit_name=non_subunit_name)
275 result = testtools.StreamToExtendedDecorator(url_parser)
276 accumulator = FileAccumulator(non_subunit_name)
277 result = testtools.StreamResultRouter(result)
278 result.add_rule(accumulator, 'test_id', test_id=None)
279 result.startTestRun()
280 suite.run(result)
281
282 for bytes_io in accumulator.route_codes.values(): # v1 processing
283 bytes_io.seek(0)
284 suite = subunit.ProtocolTestCase(bytes_io)
285 suite.run(url_parser)
286 result.stopTestRun()
287
288 return url_parser
289
290
291def output(url_parser, output_file):
Stephen Lowrieb85502d2016-06-27 15:05:47 -0500292 if output_file is not None:
293 with open(output_file, "w") as outfile:
294 outfile.write(json.dumps(url_parser.test_logs))
295 return
296
297 for test_name, items in url_parser.test_logs.iteritems():
298 sys.stdout.write('{0}\n'.format(test_name))
299 if not items:
300 sys.stdout.write('\n')
301 continue
302 for item in items:
303 sys.stdout.write('\t- {0} {1} request for {2} to {3}\n'.format(
304 item.get('status_code'), item.get('verb'),
305 item.get('service'), item.get('url')))
306 sys.stdout.write('\n')
Stephen Lowriec8548fc2016-05-24 15:57:35 -0500307
308
309def entry_point():
310 cl_args = ArgumentParser().parse_args()
311 parser = parse(cl_args.subunit, cl_args.non_subunit_name, cl_args.ports)
312 output(parser, cl_args.output_file)
313
314
315if __name__ == "__main__":
316 entry_point()