blob: 50f33ebe26e6b5898b7f6ff8c5cd7f43a6e9c4cc [file] [log] [blame]
Sean Daguea3d2ab72013-01-12 08:43:49 -05001#!/usr/bin/env python
Jay Pipes257d3f82012-07-08 23:01:31 -04002
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Jay Pipes257d3f82012-07-08 23:01:31 -04004# 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 logging
24import os
25import re
26
27from launchpadlib import launchpad
28
29BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
Giampaolo Lauria07f51e62013-05-23 16:08:07 -040030TESTDIR = os.path.join(BASEDIR, 'tempest')
Jay Pipes257d3f82012-07-08 23:01:31 -040031LPCACHEDIR = os.path.expanduser('~/.launchpadlib/cache')
32
33
34def info(msg, *args, **kwargs):
35 logging.info(msg, *args, **kwargs)
36
37
38def debug(msg, *args, **kwargs):
39 logging.debug(msg, *args, **kwargs)
40
41
42def find_skips(start=TESTDIR):
43 """
44 Returns a list of tuples (method, bug) that represent
45 test methods that have been decorated to skip because of
46 a particular bug.
47 """
Matthew Treinishbae2a992013-10-16 18:28:10 -040048 results = {}
Jay Pipes257d3f82012-07-08 23:01:31 -040049 debug("Searching in %s", start)
50 for root, _dirs, files in os.walk(start):
51 for name in files:
52 if name.startswith('test_') and name.endswith('py'):
53 path = os.path.join(root, name)
54 debug("Searching in %s", path)
Matthew Treinishbae2a992013-10-16 18:28:10 -040055 temp_result = find_skips_in_file(path)
56 for method_name, bug_no in temp_result:
57 if results.get(bug_no):
58 result_dict = results.get(bug_no)
59 if result_dict.get(name):
60 result_dict[name].append(method_name)
61 else:
62 result_dict[name] = [method_name]
63 results[bug_no] = result_dict
64 else:
65 results[bug_no] = {name: [method_name]}
Jay Pipes257d3f82012-07-08 23:01:31 -040066 return results
67
68
69def find_skips_in_file(path):
70 """
71 Return the skip tuples in a test file
72 """
Giulio Fidente83181a92013-10-01 06:02:24 +020073 BUG_RE = re.compile(r'\s*@.*skip_because\(bug=[\'"](\d+)[\'"]')
74 DEF_RE = re.compile(r'\s*def (\w+)\(')
Jay Pipes257d3f82012-07-08 23:01:31 -040075 bug_found = False
76 results = []
77 lines = open(path, 'rb').readlines()
78 for x, line in enumerate(lines):
79 if not bug_found:
80 res = BUG_RE.match(line)
81 if res:
82 bug_no = int(res.group(1))
83 debug("Found bug skip %s on line %d", bug_no, x + 1)
84 bug_found = True
85 else:
86 res = DEF_RE.match(line)
87 if res:
88 method = res.group(1)
89 debug("Found test method %s skips for bug %d", method, bug_no)
90 results.append((method, bug_no))
91 bug_found = False
92 return results
93
94
Matthew Treinishbae2a992013-10-16 18:28:10 -040095def get_results(result_dict):
96 results = []
97 for bug_no in result_dict.keys():
98 for method in result_dict[bug_no]:
99 results.append((method, bug_no))
100 return results
101
102
Jay Pipes257d3f82012-07-08 23:01:31 -0400103if __name__ == '__main__':
Jay Pipesa6aa5f22012-07-24 19:40:29 -0400104 logging.basicConfig(format='%(levelname)s: %(message)s',
105 level=logging.INFO)
Jay Pipes257d3f82012-07-08 23:01:31 -0400106 results = find_skips()
Matthew Treinishbae2a992013-10-16 18:28:10 -0400107 unique_bugs = sorted(set([bug for (method, bug) in get_results(results)]))
Jay Pipes257d3f82012-07-08 23:01:31 -0400108 unskips = []
Matthew Treinishd2a4c082013-03-11 15:13:42 -0400109 duplicates = []
Jay Pipes257d3f82012-07-08 23:01:31 -0400110 info("Total bug skips found: %d", len(results))
111 info("Total unique bugs causing skips: %d", len(unique_bugs))
Jay Pipesa6aa5f22012-07-24 19:40:29 -0400112 lp = launchpad.Launchpad.login_anonymously('grabbing bugs',
113 'production',
114 LPCACHEDIR)
Jay Pipes257d3f82012-07-08 23:01:31 -0400115 for bug_no in unique_bugs:
116 bug = lp.bugs[bug_no]
Matthew Treinishd2a4c082013-03-11 15:13:42 -0400117 duplicate = bug.duplicate_of_link
118 if duplicate is not None:
119 dup_id = duplicate.split('/')[-1]
120 duplicates.append((bug_no, dup_id))
Jay Pipes257d3f82012-07-08 23:01:31 -0400121 for task in bug.bug_tasks:
Jay Pipesa6aa5f22012-07-24 19:40:29 -0400122 info("Bug #%7s (%12s - %12s)", bug_no,
123 task.importance, task.status)
Jay Pipes257d3f82012-07-08 23:01:31 -0400124 if task.status in ('Fix Released', 'Fix Committed'):
125 unskips.append(bug_no)
126
Matthew Treinishd2a4c082013-03-11 15:13:42 -0400127 for bug_id, dup_id in duplicates:
128 if bug_id not in unskips:
129 dup_bug = lp.bugs[dup_id]
130 for task in dup_bug.bug_tasks:
131 info("Bug #%7s is a duplicate of Bug#%7s (%12s - %12s)",
132 bug_id, dup_id, task.importance, task.status)
133 if task.status in ('Fix Released', 'Fix Committed'):
134 unskips.append(bug_id)
135
136 unskips = sorted(set(unskips))
Jay Pipes257d3f82012-07-08 23:01:31 -0400137 if unskips:
Dirk Mueller1db5db22013-06-23 20:21:32 +0200138 print("The following bugs have been fixed and the corresponding skips")
139 print("should be removed from the test cases:")
140 print()
Jay Pipes257d3f82012-07-08 23:01:31 -0400141 for bug in unskips:
Matthew Treinishbae2a992013-10-16 18:28:10 -0400142 message = " %7s in " % bug
143 locations = ["%s" % x for x in results[bug].keys()]
144 message += " and ".join(locations)
145 print(message)