Sean Dague | a3d2ab7 | 2013-01-12 08:43:49 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 2 | # 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 | """ |
| 20 | Track test skips via launchpadlib API and raise alerts if a bug |
| 21 | is fixed but a skip is still in the Tempest test code |
| 22 | """ |
| 23 | |
| 24 | import logging |
| 25 | import os |
| 26 | import re |
| 27 | |
| 28 | from launchpadlib import launchpad |
| 29 | |
| 30 | BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 31 | TESTDIR = os.path.join(BASEDIR, 'tempest', 'tests') |
| 32 | LPCACHEDIR = os.path.expanduser('~/.launchpadlib/cache') |
| 33 | |
| 34 | |
| 35 | def info(msg, *args, **kwargs): |
| 36 | logging.info(msg, *args, **kwargs) |
| 37 | |
| 38 | |
| 39 | def debug(msg, *args, **kwargs): |
| 40 | logging.debug(msg, *args, **kwargs) |
| 41 | |
| 42 | |
| 43 | def 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 | |
| 60 | def find_skips_in_file(path): |
| 61 | """ |
| 62 | Return the skip tuples in a test file |
| 63 | """ |
| 64 | BUG_RE = re.compile(r'.*skip\(.*[bB]ug\s*(\d+)') |
| 65 | 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 | |
| 86 | if __name__ == '__main__': |
Jay Pipes | a6aa5f2 | 2012-07-24 19:40:29 -0400 | [diff] [blame] | 87 | logging.basicConfig(format='%(levelname)s: %(message)s', |
| 88 | level=logging.INFO) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 89 | results = find_skips() |
| 90 | unique_bugs = sorted(set([bug for (method, bug) in results])) |
| 91 | unskips = [] |
| 92 | info("Total bug skips found: %d", len(results)) |
| 93 | info("Total unique bugs causing skips: %d", len(unique_bugs)) |
Jay Pipes | a6aa5f2 | 2012-07-24 19:40:29 -0400 | [diff] [blame] | 94 | lp = launchpad.Launchpad.login_anonymously('grabbing bugs', |
| 95 | 'production', |
| 96 | LPCACHEDIR) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 97 | for bug_no in unique_bugs: |
| 98 | bug = lp.bugs[bug_no] |
| 99 | for task in bug.bug_tasks: |
Jay Pipes | a6aa5f2 | 2012-07-24 19:40:29 -0400 | [diff] [blame] | 100 | info("Bug #%7s (%12s - %12s)", bug_no, |
| 101 | task.importance, task.status) |
Jay Pipes | 257d3f8 | 2012-07-08 23:01:31 -0400 | [diff] [blame] | 102 | if task.status in ('Fix Released', 'Fix Committed'): |
| 103 | unskips.append(bug_no) |
| 104 | |
| 105 | if unskips: |
| 106 | print "The following bugs have been fixed and the corresponding skips" |
| 107 | print "should be removed from the test cases:" |
| 108 | print |
| 109 | for bug in unskips: |
| 110 | print " %7s" % bug |