blob: 87806b7fc15f7662f63e6a4244fd70a113f90aeb [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
Matthew Treinish9e26ca82016-02-23 11:43:20 -050024import os
25import re
26
zhuzeyuf5501332017-01-23 18:27:18 +080027from oslo_log import log as logging
28
Matthew Treinish9e26ca82016-02-23 11:43:20 -050029try:
30 from launchpadlib import launchpad
31except ImportError:
32 launchpad = None
33
34LPCACHEDIR = os.path.expanduser('~/.launchpadlib/cache')
zhuzeyuf5501332017-01-23 18:27:18 +080035LOG = logging.getLogger(__name__)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050036
Masayuki Igawa3bc73952017-04-20 17:11:58 +090037BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..'))
38TESTDIR = os.path.join(BASEDIR, 'tempest')
39
Matthew Treinish9e26ca82016-02-23 11:43:20 -050040
41def parse_args():
42 parser = argparse.ArgumentParser()
Masayuki Igawa3bc73952017-04-20 17:11:58 +090043 parser.add_argument('test_path', nargs='?', default=TESTDIR,
44 help='Path of test dir')
Matthew Treinish9e26ca82016-02-23 11:43:20 -050045 return parser.parse_args()
46
47
48def info(msg, *args, **kwargs):
zhuzeyuf5501332017-01-23 18:27:18 +080049 LOG.info(msg, *args, **kwargs)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050050
51
52def debug(msg, *args, **kwargs):
zhuzeyuf5501332017-01-23 18:27:18 +080053 LOG.debug(msg, *args, **kwargs)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050054
55
56def find_skips(start):
zhufl0892cb22016-05-06 14:46:00 +080057 """Find the entire list of skipped tests.
Matthew Treinish9e26ca82016-02-23 11:43:20 -050058
59 Returns a list of tuples (method, bug) that represent
60 test methods that have been decorated to skip because of
61 a particular bug.
62 """
63 results = {}
64 debug("Searching in %s", start)
65 for root, _dirs, files in os.walk(start):
66 for name in files:
67 if name.startswith('test_') and name.endswith('py'):
68 path = os.path.join(root, name)
69 debug("Searching in %s", path)
70 temp_result = find_skips_in_file(path)
71 for method_name, bug_no in temp_result:
72 if results.get(bug_no):
73 result_dict = results.get(bug_no)
74 if result_dict.get(name):
75 result_dict[name].append(method_name)
76 else:
77 result_dict[name] = [method_name]
78 results[bug_no] = result_dict
79 else:
80 results[bug_no] = {name: [method_name]}
81 return results
82
83
84def find_skips_in_file(path):
85 """Return the skip tuples in a test file."""
86 BUG_RE = re.compile(r'\s*@.*skip_because\(bug=[\'"](\d+)[\'"]')
87 DEF_RE = re.compile(r'\s*def (\w+)\(')
88 bug_found = False
89 results = []
zhang.leia4b1cef2016-03-01 10:50:01 +080090 with open(path, 'rb') as content:
91 lines = content.readlines()
92 for x, line in enumerate(lines):
93 if not bug_found:
94 res = BUG_RE.match(line)
95 if res:
96 bug_no = int(res.group(1))
97 debug("Found bug skip %s on line %d", bug_no, x + 1)
98 bug_found = True
99 else:
100 res = DEF_RE.match(line)
101 if res:
102 method = res.group(1)
103 debug("Found test method %s skips for bug %d",
104 method, bug_no)
105 results.append((method, bug_no))
106 bug_found = False
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500107 return results
108
109
110def get_results(result_dict):
111 results = []
Joe H. Rahmea72f2c62016-07-11 16:28:19 +0200112 for bug_no in result_dict:
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500113 for method in result_dict[bug_no]:
114 results.append((method, bug_no))
115 return results
116
117
118def main():
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500119 parser = parse_args()
120 results = find_skips(parser.test_path)
121 unique_bugs = sorted(set([bug for (method, bug) in get_results(results)]))
122 unskips = []
123 duplicates = []
124 info("Total bug skips found: %d", len(results))
125 info("Total unique bugs causing skips: %d", len(unique_bugs))
126 if launchpad is not None:
127 lp = launchpad.Launchpad.login_anonymously('grabbing bugs',
128 'production',
129 LPCACHEDIR)
130 else:
131 print("To check the bug status launchpadlib should be installed")
132 exit(1)
133
134 for bug_no in unique_bugs:
135 bug = lp.bugs[bug_no]
136 duplicate = bug.duplicate_of_link
137 if duplicate is not None:
138 dup_id = duplicate.split('/')[-1]
139 duplicates.append((bug_no, dup_id))
140 for task in bug.bug_tasks:
141 info("Bug #%7s (%12s - %12s)", bug_no,
142 task.importance, task.status)
143 if task.status in ('Fix Released', 'Fix Committed'):
144 unskips.append(bug_no)
145
146 for bug_id, dup_id in duplicates:
147 if bug_id not in unskips:
148 dup_bug = lp.bugs[dup_id]
149 for task in dup_bug.bug_tasks:
150 info("Bug #%7s is a duplicate of Bug#%7s (%12s - %12s)",
151 bug_id, dup_id, task.importance, task.status)
152 if task.status in ('Fix Released', 'Fix Committed'):
153 unskips.append(bug_id)
154
155 unskips = sorted(set(unskips))
156 if unskips:
157 print("The following bugs have been fixed and the corresponding skips")
158 print("should be removed from the test cases:")
159 print()
160 for bug in unskips:
161 message = " %7s in " % bug
162 locations = ["%s" % x for x in results[bug].keys()]
163 message += " and ".join(locations)
164 print(message)
165
166
167if __name__ == '__main__':
168 main()