blob: d95aa4664d11c03b8d798dc218a8b7d1e1510301 [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):
zhufl0892cb22016-05-06 14:46:00 +080051 """Find the entire list of skipped tests.
Matthew Treinish9e26ca82016-02-23 11:43:20 -050052
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 = []
zhang.leia4b1cef2016-03-01 10:50:01 +080084 with open(path, 'rb') as content:
85 lines = content.readlines()
86 for x, line in enumerate(lines):
87 if not bug_found:
88 res = BUG_RE.match(line)
89 if res:
90 bug_no = int(res.group(1))
91 debug("Found bug skip %s on line %d", bug_no, x + 1)
92 bug_found = True
93 else:
94 res = DEF_RE.match(line)
95 if res:
96 method = res.group(1)
97 debug("Found test method %s skips for bug %d",
98 method, bug_no)
99 results.append((method, bug_no))
100 bug_found = False
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500101 return results
102
103
104def get_results(result_dict):
105 results = []
Joe H. Rahmea72f2c62016-07-11 16:28:19 +0200106 for bug_no in result_dict:
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500107 for method in result_dict[bug_no]:
108 results.append((method, bug_no))
109 return results
110
111
112def main():
113 logging.basicConfig(format='%(levelname)s: %(message)s',
114 level=logging.INFO)
115 parser = parse_args()
116 results = find_skips(parser.test_path)
117 unique_bugs = sorted(set([bug for (method, bug) in get_results(results)]))
118 unskips = []
119 duplicates = []
120 info("Total bug skips found: %d", len(results))
121 info("Total unique bugs causing skips: %d", len(unique_bugs))
122 if launchpad is not None:
123 lp = launchpad.Launchpad.login_anonymously('grabbing bugs',
124 'production',
125 LPCACHEDIR)
126 else:
127 print("To check the bug status launchpadlib should be installed")
128 exit(1)
129
130 for bug_no in unique_bugs:
131 bug = lp.bugs[bug_no]
132 duplicate = bug.duplicate_of_link
133 if duplicate is not None:
134 dup_id = duplicate.split('/')[-1]
135 duplicates.append((bug_no, dup_id))
136 for task in bug.bug_tasks:
137 info("Bug #%7s (%12s - %12s)", bug_no,
138 task.importance, task.status)
139 if task.status in ('Fix Released', 'Fix Committed'):
140 unskips.append(bug_no)
141
142 for bug_id, dup_id in duplicates:
143 if bug_id not in unskips:
144 dup_bug = lp.bugs[dup_id]
145 for task in dup_bug.bug_tasks:
146 info("Bug #%7s is a duplicate of Bug#%7s (%12s - %12s)",
147 bug_id, dup_id, task.importance, task.status)
148 if task.status in ('Fix Released', 'Fix Committed'):
149 unskips.append(bug_id)
150
151 unskips = sorted(set(unskips))
152 if unskips:
153 print("The following bugs have been fixed and the corresponding skips")
154 print("should be removed from the test cases:")
155 print()
156 for bug in unskips:
157 message = " %7s in " % bug
158 locations = ["%s" % x for x in results[bug].keys()]
159 message += " and ".join(locations)
160 print(message)
161
162
163if __name__ == '__main__':
164 main()