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