Sean Dague | a3d2ab7 | 2013-01-12 08:43:49 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 2 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 3 | |
ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 4 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 5 | # All Rights Reserved. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. You may obtain |
| 9 | # a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | # License for the specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | |
| 19 | """ |
| 20 | Track test skips via launchpadlib API and raise alerts if a bug |
| 21 | is fixed but a skip is still in the Tempest test code |
| 22 | """ |
| 23 | |
| 24 | import logging |
| 25 | import os |
| 26 | import re |
| 27 | |
| 28 | from launchpadlib import launchpad |
| 29 | |
| 30 | BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
Giampaolo Lauria | 07f51e6 | 2013-05-23 16:08:07 -0400 | [diff] [blame] | 31 | TESTDIR = os.path.join(BASEDIR, 'tempest') |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 32 | LPCACHEDIR = os.path.expanduser('~/.launchpadlib/cache') |
| 33 | |
| 34 | |
| 35 | def info(msg, *args, **kwargs): |
| 36 | logging.info(msg, *args, **kwargs) |
| 37 | |
| 38 | |
| 39 | def debug(msg, *args, **kwargs): |
| 40 | logging.debug(msg, *args, **kwargs) |
| 41 | |
| 42 | |
| 43 | def find_skips(start=TESTDIR): |
| 44 | """ |
| 45 | Returns a list of tuples (method, bug) that represent |
| 46 | test methods that have been decorated to skip because of |
| 47 | a particular bug. |
| 48 | """ |
Matthew Treinish | bae2a99 | 2013-10-16 18:28:10 -0400 | [diff] [blame] | 49 | results = {} |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 50 | debug("Searching in %s", start) |
| 51 | for root, _dirs, files in os.walk(start): |
| 52 | for name in files: |
| 53 | if name.startswith('test_') and name.endswith('py'): |
| 54 | path = os.path.join(root, name) |
| 55 | debug("Searching in %s", path) |
Matthew Treinish | bae2a99 | 2013-10-16 18:28:10 -0400 | [diff] [blame] | 56 | temp_result = find_skips_in_file(path) |
| 57 | for method_name, bug_no in temp_result: |
| 58 | if results.get(bug_no): |
| 59 | result_dict = results.get(bug_no) |
| 60 | if result_dict.get(name): |
| 61 | result_dict[name].append(method_name) |
| 62 | else: |
| 63 | result_dict[name] = [method_name] |
| 64 | results[bug_no] = result_dict |
| 65 | else: |
| 66 | results[bug_no] = {name: [method_name]} |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 67 | return results |
| 68 | |
| 69 | |
| 70 | def find_skips_in_file(path): |
| 71 | """ |
| 72 | Return the skip tuples in a test file |
| 73 | """ |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 74 | BUG_RE = re.compile(r'\s*@.*skip_because\(bug=[\'"](\d+)[\'"]') |
| 75 | DEF_RE = re.compile(r'\s*def (\w+)\(') |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 76 | bug_found = False |
| 77 | results = [] |
| 78 | lines = open(path, 'rb').readlines() |
| 79 | for x, line in enumerate(lines): |
| 80 | if not bug_found: |
| 81 | res = BUG_RE.match(line) |
| 82 | if res: |
| 83 | bug_no = int(res.group(1)) |
| 84 | debug("Found bug skip %s on line %d", bug_no, x + 1) |
| 85 | bug_found = True |
| 86 | else: |
| 87 | res = DEF_RE.match(line) |
| 88 | if res: |
| 89 | method = res.group(1) |
| 90 | debug("Found test method %s skips for bug %d", method, bug_no) |
| 91 | results.append((method, bug_no)) |
| 92 | bug_found = False |
| 93 | return results |
| 94 | |
| 95 | |
Matthew Treinish | bae2a99 | 2013-10-16 18:28:10 -0400 | [diff] [blame] | 96 | def get_results(result_dict): |
| 97 | results = [] |
| 98 | for bug_no in result_dict.keys(): |
| 99 | for method in result_dict[bug_no]: |
| 100 | results.append((method, bug_no)) |
| 101 | return results |
| 102 | |
| 103 | |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 104 | if __name__ == '__main__': |
Jay Pipes | a6aa5f2 | 2012-07-24 19:40:29 -0400 | [diff] [blame] | 105 | logging.basicConfig(format='%(levelname)s: %(message)s', |
| 106 | level=logging.INFO) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 107 | results = find_skips() |
Matthew Treinish | bae2a99 | 2013-10-16 18:28:10 -0400 | [diff] [blame] | 108 | unique_bugs = sorted(set([bug for (method, bug) in get_results(results)])) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 109 | unskips = [] |
Matthew Treinish | d2a4c08 | 2013-03-11 15:13:42 -0400 | [diff] [blame] | 110 | duplicates = [] |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 111 | info("Total bug skips found: %d", len(results)) |
| 112 | info("Total unique bugs causing skips: %d", len(unique_bugs)) |
Jay Pipes | a6aa5f2 | 2012-07-24 19:40:29 -0400 | [diff] [blame] | 113 | lp = launchpad.Launchpad.login_anonymously('grabbing bugs', |
| 114 | 'production', |
| 115 | LPCACHEDIR) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 116 | for bug_no in unique_bugs: |
| 117 | bug = lp.bugs[bug_no] |
Matthew Treinish | d2a4c08 | 2013-03-11 15:13:42 -0400 | [diff] [blame] | 118 | duplicate = bug.duplicate_of_link |
| 119 | if duplicate is not None: |
| 120 | dup_id = duplicate.split('/')[-1] |
| 121 | duplicates.append((bug_no, dup_id)) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 122 | for task in bug.bug_tasks: |
Jay Pipes | a6aa5f2 | 2012-07-24 19:40:29 -0400 | [diff] [blame] | 123 | info("Bug #%7s (%12s - %12s)", bug_no, |
| 124 | task.importance, task.status) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 125 | if task.status in ('Fix Released', 'Fix Committed'): |
| 126 | unskips.append(bug_no) |
| 127 | |
Matthew Treinish | d2a4c08 | 2013-03-11 15:13:42 -0400 | [diff] [blame] | 128 | for bug_id, dup_id in duplicates: |
| 129 | if bug_id not in unskips: |
| 130 | dup_bug = lp.bugs[dup_id] |
| 131 | for task in dup_bug.bug_tasks: |
| 132 | info("Bug #%7s is a duplicate of Bug#%7s (%12s - %12s)", |
| 133 | bug_id, dup_id, task.importance, task.status) |
| 134 | if task.status in ('Fix Released', 'Fix Committed'): |
| 135 | unskips.append(bug_id) |
| 136 | |
| 137 | unskips = sorted(set(unskips)) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 138 | if unskips: |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 139 | print("The following bugs have been fixed and the corresponding skips") |
| 140 | print("should be removed from the test cases:") |
| 141 | print() |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 142 | for bug in unskips: |
Matthew Treinish | bae2a99 | 2013-10-16 18:28:10 -0400 | [diff] [blame] | 143 | message = " %7s in " % bug |
| 144 | locations = ["%s" % x for x in results[bug].keys()] |
| 145 | message += " and ".join(locations) |
| 146 | print(message) |