blob: b34066f8fbd3f2756bc7b2826e0ce17e8b67938f [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001#!/usr/bin/env python
2
3# Copyright 2014 Mirantis, Inc.
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
17import argparse
18import ast
lkuchlanc8b966f2020-01-07 12:53:55 +020019import contextlib
Matthew Treinish9e26ca82016-02-23 11:43:20 -050020import importlib
21import inspect
22import os
23import sys
24import unittest
25import uuid
26
janonymous69413b92016-12-06 13:34:19 +053027from oslo_utils import uuidutils
Matthew Treinish9e26ca82016-02-23 11:43:20 -050028import six.moves.urllib.parse as urlparse
29
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -080030DECORATOR_MODULE = 'decorators'
Matthew Treinish9e26ca82016-02-23 11:43:20 -050031DECORATOR_NAME = 'idempotent_id'
lkuchlanc8b966f2020-01-07 12:53:55 +020032DECORATOR_IMPORT = 'tempest.lib.%s' % DECORATOR_MODULE
Jeremy Liu12afdb82017-04-01 19:32:26 +080033IMPORT_LINE = 'from tempest.lib import %s' % DECORATOR_MODULE
Matthew Treinish9e26ca82016-02-23 11:43:20 -050034DECORATOR_TEMPLATE = "@%s.%s('%%s')" % (DECORATOR_MODULE,
35 DECORATOR_NAME)
36UNIT_TESTS_EXCLUDE = 'tempest.tests'
37
38
39class SourcePatcher(object):
40
41 """"Lazy patcher for python source files"""
42
43 def __init__(self):
44 self.source_files = None
45 self.patches = None
46 self.clear()
47
48 def clear(self):
49 """Clear inner state"""
50 self.source_files = {}
51 self.patches = {}
52
53 @staticmethod
54 def _quote(s):
55 return urlparse.quote(s)
56
57 @staticmethod
58 def _unquote(s):
59 return urlparse.unquote(s)
60
61 def add_patch(self, filename, patch, line_no):
62 """Add lazy patch"""
63 if filename not in self.source_files:
64 with open(filename) as f:
65 self.source_files[filename] = self._quote(f.read())
janonymous69413b92016-12-06 13:34:19 +053066 patch_id = uuidutils.generate_uuid()
Matthew Treinish9e26ca82016-02-23 11:43:20 -050067 if not patch.endswith('\n'):
68 patch += '\n'
69 self.patches[patch_id] = self._quote(patch)
70 lines = self.source_files[filename].split(self._quote('\n'))
71 lines[line_no - 1] = ''.join(('{%s:s}' % patch_id, lines[line_no - 1]))
72 self.source_files[filename] = self._quote('\n').join(lines)
73
guo yunxian149c9832016-09-26 16:13:13 +080074 @staticmethod
75 def _save_changes(filename, source):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050076 print('%s fixed' % filename)
77 with open(filename, 'w') as f:
78 f.write(source)
79
80 def apply_patches(self):
81 """Apply all patches"""
82 for filename in self.source_files:
83 patched_source = self._unquote(
84 self.source_files[filename].format(**self.patches)
85 )
86 self._save_changes(filename, patched_source)
87 self.clear()
88
89
90class TestChecker(object):
91
92 def __init__(self, package):
93 self.package = package
94 self.base_path = os.path.abspath(os.path.dirname(package.__file__))
95
96 def _path_to_package(self, path):
97 relative_path = path[len(self.base_path) + 1:]
98 if relative_path:
99 return '.'.join((self.package.__name__,) +
100 tuple(relative_path.split('/')))
101 else:
102 return self.package.__name__
103
104 def _modules_search(self):
105 """Recursive search for python modules in base package"""
106 modules = []
Federico Ressi2d6bcaa2018-04-11 12:37:36 +0200107 for root, _, files in os.walk(self.base_path):
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500108 if not os.path.exists(os.path.join(root, '__init__.py')):
109 continue
110 root_package = self._path_to_package(root)
111 for item in files:
112 if item.endswith('.py'):
113 module_name = '.'.join((root_package,
afazekas40fcb9b2019-03-08 11:25:11 +0100114 os.path.splitext(item)[0]))
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500115 if not module_name.startswith(UNIT_TESTS_EXCLUDE):
116 modules.append(module_name)
117 return modules
118
119 @staticmethod
120 def _get_idempotent_id(test_node):
Ken'ichi Ohmichi44f01272017-01-27 18:44:14 -0800121 "Return key-value dict with metadata from @decorators.idempotent_id"
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500122 idempotent_id = None
123 for decorator in test_node.decorator_list:
124 if (hasattr(decorator, 'func') and
Federico Ressi2d6bcaa2018-04-11 12:37:36 +0200125 hasattr(decorator.func, 'attr') and
126 decorator.func.attr == DECORATOR_NAME and
127 hasattr(decorator.func, 'value') and
128 decorator.func.value.id == DECORATOR_MODULE):
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500129 for arg in decorator.args:
130 idempotent_id = ast.literal_eval(arg)
131 return idempotent_id
132
133 @staticmethod
134 def _is_decorator(line):
135 return line.strip().startswith('@')
136
137 @staticmethod
138 def _is_def(line):
139 return line.strip().startswith('def ')
140
141 def _add_uuid_to_test(self, patcher, test_node, source_path):
142 with open(source_path) as src:
143 src_lines = src.read().split('\n')
144 lineno = test_node.lineno
145 insert_position = lineno
146 while True:
147 if (self._is_def(src_lines[lineno - 1]) or
148 (self._is_decorator(src_lines[lineno - 1]) and
149 (DECORATOR_TEMPLATE.split('(')[0] <=
150 src_lines[lineno - 1].strip().split('(')[0]))):
151 insert_position = lineno
152 break
153 lineno += 1
154 patcher.add_patch(
155 source_path,
156 ' ' * test_node.col_offset + DECORATOR_TEMPLATE % uuid.uuid4(),
157 insert_position
158 )
159
160 @staticmethod
161 def _is_test_case(module, node):
162 if (node.__class__ is ast.ClassDef and
163 hasattr(module, node.name) and
164 inspect.isclass(getattr(module, node.name))):
165 return issubclass(getattr(module, node.name), unittest.TestCase)
166
167 @staticmethod
168 def _is_test_method(node):
Federico Ressi2d6bcaa2018-04-11 12:37:36 +0200169 return (node.__class__ is ast.FunctionDef and
170 node.name.startswith('test_'))
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500171
172 @staticmethod
173 def _next_node(body, node):
174 if body.index(node) < len(body):
175 return body[body.index(node) + 1]
176
177 @staticmethod
178 def _import_name(node):
Brandon Palmecd2ec02016-02-25 09:38:36 -0600179 if isinstance(node, ast.Import):
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500180 return node.names[0].name
Brandon Palmecd2ec02016-02-25 09:38:36 -0600181 elif isinstance(node, ast.ImportFrom):
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500182 return '%s.%s' % (node.module, node.names[0].name)
183
lkuchlanc8b966f2020-01-07 12:53:55 +0200184 @contextlib.contextmanager
185 def ignore_site_packages_paths(self):
186 """Removes site-packages directories from the sys.path
187
188 Source:
189 - StackOverflow: https://stackoverflow.com/questions/22195382/
190 - Author: https://stackoverflow.com/users/485844/
191 """
192
193 paths = sys.path
194 # remove all third-party paths
195 # so that only stdlib imports will succeed
196 sys.path = list(filter(
197 None,
198 filter(lambda i: 'site-packages' not in i, sys.path)
199 ))
200 yield
201 sys.path = paths
202
203 def is_std_lib(self, module):
204 """Checks whether the module is part of the stdlib or not
205
206 Source:
207 - StackOverflow: https://stackoverflow.com/questions/22195382/
208 - Author: https://stackoverflow.com/users/485844/
209 """
210
211 if module in sys.builtin_module_names:
212 return True
213
214 with self.ignore_site_packages_paths():
215 imported_module = sys.modules.pop(module, None)
216 try:
217 importlib.import_module(module)
218 except ImportError:
219 return False
220 else:
221 return True
222 finally:
223 if imported_module:
224 sys.modules[module] = imported_module
225
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500226 def _add_import_for_test_uuid(self, patcher, src_parsed, source_path):
lkuchlanc8b966f2020-01-07 12:53:55 +0200227 import_list = [node for node in src_parsed.body
228 if isinstance(node, ast.Import) or
229 isinstance(node, ast.ImportFrom)]
230
231 if not import_list:
232 print("(WARNING) %s: The file is not valid as it does not contain "
233 "any import line! Therefore the import needed by "
234 "@decorators.idempotent_id is not added!" % source_path)
235 return
236
237 tempest_imports = [node for node in import_list
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500238 if self._import_name(node) and
239 'tempest.' in self._import_name(node)]
lkuchlanc8b966f2020-01-07 12:53:55 +0200240
241 for node in tempest_imports:
242 if self._import_name(node) < DECORATOR_IMPORT:
243 continue
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500244 else:
lkuchlanc8b966f2020-01-07 12:53:55 +0200245 line_no = node.lineno
246 break
247 else:
248 if tempest_imports:
249 line_no = tempest_imports[-1].lineno + 1
250
251 # Insert import line between existing tempest imports
252 if tempest_imports:
253 patcher.add_patch(source_path, IMPORT_LINE, line_no)
254 return
255
256 # Group space separated imports together
257 grouped_imports = {}
258 first_import_line = import_list[0].lineno
259 for idx, import_line in enumerate(import_list, first_import_line):
260 group_no = import_line.lineno - idx
261 group = grouped_imports.get(group_no, [])
262 group.append(import_line)
263 grouped_imports[group_no] = group
264
265 if len(grouped_imports) > 3:
266 print("(WARNING) %s: The file contains more than three import "
267 "groups! This is not valid according to the PEP8 "
268 "style guide. " % source_path)
269
270 # Divide grouped_imports into groupes based on PEP8 style guide
271 pep8_groups = {}
272 package_name = self.package.__name__.split(".")[0]
273 for key in grouped_imports:
274 module = self._import_name(grouped_imports[key][0]).split(".")[0]
275 if module.startswith(package_name):
276 group = pep8_groups.get('3rd_group', [])
277 pep8_groups['3rd_group'] = group + grouped_imports[key]
278 elif self.is_std_lib(module):
279 group = pep8_groups.get('1st_group', [])
280 pep8_groups['1st_group'] = group + grouped_imports[key]
281 else:
282 group = pep8_groups.get('2nd_group', [])
283 pep8_groups['2nd_group'] = group + grouped_imports[key]
284
285 for node in pep8_groups.get('2nd_group', []):
286 if self._import_name(node) < DECORATOR_IMPORT:
287 continue
288 else:
289 line_no = node.lineno
290 import_snippet = IMPORT_LINE
291 break
292 else:
293 if pep8_groups.get('2nd_group', []):
294 line_no = pep8_groups['2nd_group'][-1].lineno + 1
295 import_snippet = IMPORT_LINE
296 elif pep8_groups.get('1st_group', []):
297 line_no = pep8_groups['1st_group'][-1].lineno + 1
298 import_snippet = '\n' + IMPORT_LINE
299 else:
300 line_no = pep8_groups['3rd_group'][0].lineno
301 import_snippet = IMPORT_LINE + '\n\n'
302
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500303 patcher.add_patch(source_path, import_snippet, line_no)
304
305 def get_tests(self):
306 """Get test methods with sources from base package with metadata"""
307 tests = {}
308 for module_name in self._modules_search():
309 tests[module_name] = {}
310 module = importlib.import_module(module_name)
311 source_path = '.'.join(
312 (os.path.splitext(module.__file__)[0], 'py')
313 )
314 with open(source_path, 'r') as f:
315 source = f.read()
316 tests[module_name]['source_path'] = source_path
317 tests[module_name]['tests'] = {}
318 source_parsed = ast.parse(source)
319 tests[module_name]['ast'] = source_parsed
320 tests[module_name]['import_valid'] = (
321 hasattr(module, DECORATOR_MODULE) and
322 inspect.ismodule(getattr(module, DECORATOR_MODULE))
323 )
324 test_cases = (node for node in source_parsed.body
325 if self._is_test_case(module, node))
326 for node in test_cases:
327 for subnode in filter(self._is_test_method, node.body):
Matt Riedemann91d92422019-01-29 16:19:49 -0500328 test_name = '%s.%s' % (node.name, subnode.name)
329 tests[module_name]['tests'][test_name] = subnode
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500330 return tests
331
332 @staticmethod
333 def _filter_tests(function, tests):
334 """Filter tests with condition 'function(test_node) == True'"""
335 result = {}
336 for module_name in tests:
337 for test_name in tests[module_name]['tests']:
338 if function(module_name, test_name, tests):
339 if module_name not in result:
340 result[module_name] = {
341 'ast': tests[module_name]['ast'],
342 'source_path': tests[module_name]['source_path'],
343 'import_valid': tests[module_name]['import_valid'],
344 'tests': {}
345 }
346 result[module_name]['tests'][test_name] = \
347 tests[module_name]['tests'][test_name]
348 return result
349
350 def find_untagged(self, tests):
351 """Filter all tests without uuid in metadata"""
352 def check_uuid_in_meta(module_name, test_name, tests):
353 idempotent_id = self._get_idempotent_id(
354 tests[module_name]['tests'][test_name])
355 return not idempotent_id
356 return self._filter_tests(check_uuid_in_meta, tests)
357
358 def report_collisions(self, tests):
359 """Reports collisions if there are any
360
361 Returns true if collisions exist.
362 """
363 uuids = {}
364
365 def report(module_name, test_name, tests):
366 test_uuid = self._get_idempotent_id(
367 tests[module_name]['tests'][test_name])
368 if not test_uuid:
369 return
370 if test_uuid in uuids:
371 error_str = "%s:%s\n uuid %s collision: %s<->%s\n%s:%s" % (
372 tests[module_name]['source_path'],
373 tests[module_name]['tests'][test_name].lineno,
374 test_uuid,
375 test_name,
376 uuids[test_uuid]['test_name'],
377 uuids[test_uuid]['source_path'],
378 uuids[test_uuid]['test_node'].lineno,
379 )
380 print(error_str)
381 print("cannot automatically resolve the collision, please "
382 "manually remove the duplicate value on the new test.")
383 return True
384 else:
385 uuids[test_uuid] = {
386 'module': module_name,
387 'test_name': test_name,
388 'test_node': tests[module_name]['tests'][test_name],
389 'source_path': tests[module_name]['source_path']
390 }
391 return bool(self._filter_tests(report, tests))
392
393 def report_untagged(self, tests):
394 """Reports untagged tests if there are any
395
396 Returns true if untagged tests exist.
397 """
398 def report(module_name, test_name, tests):
Ken'ichi Ohmichi44f01272017-01-27 18:44:14 -0800399 error_str = ("%s:%s\nmissing @decorators.idempotent_id"
400 "('...')\n%s\n") % (
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500401 tests[module_name]['source_path'],
402 tests[module_name]['tests'][test_name].lineno,
403 test_name
404 )
405 print(error_str)
406 return True
407 return bool(self._filter_tests(report, tests))
408
409 def fix_tests(self, tests):
410 """Add uuids to all specified in tests and fix it in source files"""
411 patcher = SourcePatcher()
412 for module_name in tests:
413 add_import_once = True
414 for test_name in tests[module_name]['tests']:
415 if not tests[module_name]['import_valid'] and add_import_once:
416 self._add_import_for_test_uuid(
417 patcher,
418 tests[module_name]['ast'],
419 tests[module_name]['source_path']
420 )
421 add_import_once = False
422 self._add_uuid_to_test(
423 patcher, tests[module_name]['tests'][test_name],
424 tests[module_name]['source_path'])
425 patcher.apply_patches()
426
427
428def run():
429 parser = argparse.ArgumentParser()
430 parser.add_argument('--package', action='store', dest='package',
431 default='tempest', type=str,
432 help='Package with tests')
433 parser.add_argument('--fix', action='store_true', dest='fix_tests',
434 help='Attempt to fix tests without UUIDs')
435 args = parser.parse_args()
436 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
437 pkg = importlib.import_module(args.package)
438 checker = TestChecker(pkg)
439 errors = False
440 tests = checker.get_tests()
441 untagged = checker.find_untagged(tests)
442 errors = checker.report_collisions(tests) or errors
443 if args.fix_tests and untagged:
444 checker.fix_tests(untagged)
445 else:
446 errors = checker.report_untagged(untagged) or errors
447 if errors:
Ken'ichi Ohmichi44f01272017-01-27 18:44:14 -0800448 sys.exit("@decorators.idempotent_id existence and uniqueness checks "
449 "failed\n"
Hai Shi6f52fc52017-04-03 21:17:37 +0800450 "Run 'tox -v -e uuidgen' to automatically fix tests with\n"
Ken'ichi Ohmichi8a082112017-03-06 16:03:17 -0800451 "missing @decorators.idempotent_id decorators.")
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500452
Stephen Finucane7f4a6212018-07-06 13:58:21 +0100453
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500454if __name__ == '__main__':
455 run()