blob: 1ed696150f97de4861c678745921aececea9b298 [file] [log] [blame]
Sean Daguea3d2ab72013-01-12 08:43:49 -05001#!/usr/bin/env python
Jay Pipes257d3f82012-07-08 23:01:31 -04002# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
4# Copyright 2012 OpenStack, LLC
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"""
20Track test skips via launchpadlib API and raise alerts if a bug
21is fixed but a skip is still in the Tempest test code
22"""
23
24import logging
25import os
26import re
27
28from launchpadlib import launchpad
29
30BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
Giampaolo Lauria07f51e62013-05-23 16:08:07 -040031TESTDIR = os.path.join(BASEDIR, 'tempest')
Jay Pipes257d3f82012-07-08 23:01:31 -040032LPCACHEDIR = os.path.expanduser('~/.launchpadlib/cache')
33
34
35def info(msg, *args, **kwargs):
36 logging.info(msg, *args, **kwargs)
37
38
39def debug(msg, *args, **kwargs):
40 logging.debug(msg, *args, **kwargs)
41
42
43def 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 """
49 results = []
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)
56 results += find_skips_in_file(path)
57 return results
58
59
60def find_skips_in_file(path):
61 """
62 Return the skip tuples in a test file
63 """
Matthew Treinish1f82a172013-03-13 17:25:05 -040064 BUG_RE = re.compile(r'.*skip\(.*bug:*\s*\#*(\d+)', re.IGNORECASE)
Jay Pipes257d3f82012-07-08 23:01:31 -040065 DEF_RE = re.compile(r'.*def (\w+)\(')
66 bug_found = False
67 results = []
68 lines = open(path, 'rb').readlines()
69 for x, line in enumerate(lines):
70 if not bug_found:
71 res = BUG_RE.match(line)
72 if res:
73 bug_no = int(res.group(1))
74 debug("Found bug skip %s on line %d", bug_no, x + 1)
75 bug_found = True
76 else:
77 res = DEF_RE.match(line)
78 if res:
79 method = res.group(1)
80 debug("Found test method %s skips for bug %d", method, bug_no)
81 results.append((method, bug_no))
82 bug_found = False
83 return results
84
85
86if __name__ == '__main__':
Jay Pipesa6aa5f22012-07-24 19:40:29 -040087 logging.basicConfig(format='%(levelname)s: %(message)s',
88 level=logging.INFO)
Jay Pipes257d3f82012-07-08 23:01:31 -040089 results = find_skips()
90 unique_bugs = sorted(set([bug for (method, bug) in results]))
91 unskips = []
Matthew Treinishd2a4c082013-03-11 15:13:42 -040092 duplicates = []
Jay Pipes257d3f82012-07-08 23:01:31 -040093 info("Total bug skips found: %d", len(results))
94 info("Total unique bugs causing skips: %d", len(unique_bugs))
Jay Pipesa6aa5f22012-07-24 19:40:29 -040095 lp = launchpad.Launchpad.login_anonymously('grabbing bugs',
96 'production',
97 LPCACHEDIR)
Jay Pipes257d3f82012-07-08 23:01:31 -040098 for bug_no in unique_bugs:
99 bug = lp.bugs[bug_no]
Matthew Treinishd2a4c082013-03-11 15:13:42 -0400100 duplicate = bug.duplicate_of_link
101 if duplicate is not None:
102 dup_id = duplicate.split('/')[-1]
103 duplicates.append((bug_no, dup_id))
Jay Pipes257d3f82012-07-08 23:01:31 -0400104 for task in bug.bug_tasks:
Jay Pipesa6aa5f22012-07-24 19:40:29 -0400105 info("Bug #%7s (%12s - %12s)", bug_no,
106 task.importance, task.status)
Jay Pipes257d3f82012-07-08 23:01:31 -0400107 if task.status in ('Fix Released', 'Fix Committed'):
108 unskips.append(bug_no)
109
Matthew Treinishd2a4c082013-03-11 15:13:42 -0400110 for bug_id, dup_id in duplicates:
111 if bug_id not in unskips:
112 dup_bug = lp.bugs[dup_id]
113 for task in dup_bug.bug_tasks:
114 info("Bug #%7s is a duplicate of Bug#%7s (%12s - %12s)",
115 bug_id, dup_id, task.importance, task.status)
116 if task.status in ('Fix Released', 'Fix Committed'):
117 unskips.append(bug_id)
118
119 unskips = sorted(set(unskips))
Jay Pipes257d3f82012-07-08 23:01:31 -0400120 if unskips:
Dirk Mueller1db5db22013-06-23 20:21:32 +0200121 print("The following bugs have been fixed and the corresponding skips")
122 print("should be removed from the test cases:")
123 print()
Jay Pipes257d3f82012-07-08 23:01:31 -0400124 for bug in unskips:
Dirk Mueller1db5db22013-06-23 20:21:32 +0200125 print(" %7s" % bug)