Siddharth Shukla | 8e64d90 | 2017-03-12 19:50:18 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 2 | # Copyright 2015, Google Inc. |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 31 | """Filter out tests based on file differences compared to merge target branch""" |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 32 | |
Siddharth Shukla | d194f59 | 2017-03-11 19:12:43 +0100 | [diff] [blame] | 33 | from __future__ import print_function |
| 34 | |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 35 | import re |
Siddharth Shukla | d194f59 | 2017-03-11 19:12:43 +0100 | [diff] [blame] | 36 | import six |
Matt Kwong | 7e9bd6c | 2016-10-24 17:30:25 -0700 | [diff] [blame] | 37 | from subprocess import check_output |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 38 | |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 39 | |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 40 | class TestSuite: |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 41 | """ |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 42 | Contains label to identify job as belonging to this test suite and |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 43 | triggers to identify if changed files are relevant |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 44 | """ |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 45 | def __init__(self, labels): |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 46 | """ |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 47 | Build TestSuite to group tests based on labeling |
| 48 | :param label: strings that should match a jobs's platform, config, language, or test group |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 49 | """ |
| 50 | self.triggers = [] |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 51 | self.labels = labels |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 52 | |
| 53 | def add_trigger(self, trigger): |
| 54 | """ |
| 55 | Add a regex to list of triggers that determine if a changed file should run tests |
| 56 | :param trigger: regex matching file relevant to tests |
| 57 | """ |
| 58 | self.triggers.append(trigger) |
| 59 | |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 60 | |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 61 | # Create test suites |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 62 | _CORE_TEST_SUITE = TestSuite(['c']) |
| 63 | _CPP_TEST_SUITE = TestSuite(['c++']) |
| 64 | _CSHARP_TEST_SUITE = TestSuite(['csharp']) |
| 65 | _NODE_TEST_SUITE = TestSuite(['node']) |
| 66 | _OBJC_TEST_SUITE = TestSuite(['objc']) |
| 67 | _PHP_TEST_SUITE = TestSuite(['php', 'php7']) |
| 68 | _PYTHON_TEST_SUITE = TestSuite(['python']) |
| 69 | _RUBY_TEST_SUITE = TestSuite(['ruby']) |
| 70 | _LINUX_TEST_SUITE = TestSuite(['linux']) |
| 71 | _WINDOWS_TEST_SUITE = TestSuite(['windows']) |
| 72 | _MACOS_TEST_SUITE = TestSuite(['macos']) |
Matt Kwong | fe1bcd9 | 2016-11-07 14:07:06 -0800 | [diff] [blame] | 73 | _ALL_TEST_SUITES = [_CORE_TEST_SUITE, _CPP_TEST_SUITE, _CSHARP_TEST_SUITE, |
| 74 | _NODE_TEST_SUITE, _OBJC_TEST_SUITE, _PHP_TEST_SUITE, |
| 75 | _PYTHON_TEST_SUITE, _RUBY_TEST_SUITE, _LINUX_TEST_SUITE, |
| 76 | _WINDOWS_TEST_SUITE, _MACOS_TEST_SUITE] |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 77 | |
| 78 | # Dictionary of whitelistable files where the key is a regex matching changed files |
| 79 | # and the value is a list of tests that should be run. An empty list means that |
| 80 | # the changed files should not trigger any tests. Any changed file that does not |
| 81 | # match any of these regexes will trigger all tests |
Matt Kwong | 5c691c6 | 2016-10-20 17:11:18 -0700 | [diff] [blame] | 82 | # DO NOT CHANGE THIS UNLESS YOU KNOW WHAT YOU ARE DOING (be careful even if you do) |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 83 | _WHITELIST_DICT = { |
Matt Kwong | 1fb0e47 | 2016-10-17 12:16:14 -0700 | [diff] [blame] | 84 | '^doc/': [], |
| 85 | '^examples/': [], |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 86 | '^include/grpc\+\+/': [_CPP_TEST_SUITE], |
Matt Kwong | 1fb0e47 | 2016-10-17 12:16:14 -0700 | [diff] [blame] | 87 | '^summerofcode/': [], |
Matt Kwong | 1fb0e47 | 2016-10-17 12:16:14 -0700 | [diff] [blame] | 88 | '^src/cpp/': [_CPP_TEST_SUITE], |
| 89 | '^src/csharp/': [_CSHARP_TEST_SUITE], |
| 90 | '^src/node/': [_NODE_TEST_SUITE], |
| 91 | '^src/objective\-c/': [_OBJC_TEST_SUITE], |
| 92 | '^src/php/': [_PHP_TEST_SUITE], |
| 93 | '^src/python/': [_PYTHON_TEST_SUITE], |
| 94 | '^src/ruby/': [_RUBY_TEST_SUITE], |
Matt Kwong | fe1bcd9 | 2016-11-07 14:07:06 -0800 | [diff] [blame] | 95 | '^templates/': [], |
Matt Kwong | 1fb0e47 | 2016-10-17 12:16:14 -0700 | [diff] [blame] | 96 | '^test/core/': [_CORE_TEST_SUITE], |
| 97 | '^test/cpp/': [_CPP_TEST_SUITE], |
| 98 | '^test/distrib/cpp/': [_CPP_TEST_SUITE], |
| 99 | '^test/distrib/csharp/': [_CSHARP_TEST_SUITE], |
| 100 | '^test/distrib/node/': [_NODE_TEST_SUITE], |
| 101 | '^test/distrib/php/': [_PHP_TEST_SUITE], |
| 102 | '^test/distrib/python/': [_PYTHON_TEST_SUITE], |
| 103 | '^test/distrib/ruby/': [_RUBY_TEST_SUITE], |
Matt Kwong | 28b0f2a | 2017-02-27 00:52:47 -0800 | [diff] [blame] | 104 | '^tools/internal_ci/': [], |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 105 | '^vsprojects/': [_WINDOWS_TEST_SUITE], |
| 106 | 'binding\.gyp$': [_NODE_TEST_SUITE], |
| 107 | 'composer\.json$': [_PHP_TEST_SUITE], |
| 108 | 'config\.m4$': [_PHP_TEST_SUITE], |
| 109 | 'CONTRIBUTING\.md$': [], |
| 110 | 'Gemfile$': [_RUBY_TEST_SUITE], |
Matt Kwong | 7e9bd6c | 2016-10-24 17:30:25 -0700 | [diff] [blame] | 111 | 'grpc\.def$': [_WINDOWS_TEST_SUITE], |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 112 | 'grpc\.gemspec$': [_RUBY_TEST_SUITE], |
| 113 | 'gRPC\.podspec$': [_OBJC_TEST_SUITE], |
| 114 | 'gRPC\-Core\.podspec$': [_OBJC_TEST_SUITE], |
| 115 | 'gRPC\-ProtoRPC\.podspec$': [_OBJC_TEST_SUITE], |
| 116 | 'gRPC\-RxLibrary\.podspec$': [_OBJC_TEST_SUITE], |
| 117 | 'INSTALL\.md$': [], |
| 118 | 'LICENSE$': [], |
| 119 | 'MANIFEST\.md$': [], |
| 120 | 'package\.json$': [_PHP_TEST_SUITE], |
| 121 | 'package\.xml$': [_PHP_TEST_SUITE], |
| 122 | 'PATENTS$': [], |
| 123 | 'PYTHON\-MANIFEST\.in$': [_PYTHON_TEST_SUITE], |
| 124 | 'README\.md$': [], |
| 125 | 'requirements\.txt$': [_PYTHON_TEST_SUITE], |
| 126 | 'setup\.cfg$': [_PYTHON_TEST_SUITE], |
| 127 | 'setup\.py$': [_PYTHON_TEST_SUITE] |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 128 | } |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 129 | |
Matt Kwong | 5eecba4 | 2017-04-05 15:09:21 -0700 | [diff] [blame] | 130 | # Regex that combines all keys in _WHITELIST_DICT |
| 131 | _ALL_TRIGGERS = "(" + ")|(".join(_WHITELIST_DICT.keys()) + ")" |
| 132 | |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 133 | # Add all triggers to their respective test suites |
Siddharth Shukla | d194f59 | 2017-03-11 19:12:43 +0100 | [diff] [blame] | 134 | for trigger, test_suites in six.iteritems(_WHITELIST_DICT): |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 135 | for test_suite in test_suites: |
| 136 | test_suite.add_trigger(trigger) |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 137 | |
| 138 | |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame] | 139 | def _get_changed_files(base_branch): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 140 | """ |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 141 | Get list of changed files between current branch and base of target merge branch |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 142 | """ |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 143 | # Get file changes between branch and merge-base of specified branch |
| 144 | # Not combined to be Windows friendly |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame] | 145 | base_commit = check_output(["git", "merge-base", base_branch, "HEAD"]).rstrip() |
| 146 | return check_output(["git", "diff", base_commit, "--name-only"]).splitlines() |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 147 | |
| 148 | |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 149 | def _can_skip_tests(file_names, triggers): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 150 | """ |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 151 | Determines if tests are skippable based on if all files do not match list of regexes |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 152 | :param file_names: list of changed files generated by _get_changed_files() |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 153 | :param triggers: list of regexes matching file name that indicates tests should be run |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 154 | :return: safe to skip tests |
| 155 | """ |
| 156 | for file_name in file_names: |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 157 | if any(re.match(trigger, file_name) for trigger in triggers): |
| 158 | return False |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 159 | return True |
| 160 | |
| 161 | |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 162 | def _remove_irrelevant_tests(tests, skippable_labels): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 163 | """ |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame] | 164 | Filters out tests by config or language - will not remove sanitizer tests |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 165 | :param tests: list of all tests generated by run_tests_matrix.py |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 166 | :param skippable_labels: list of languages and platforms with skippable tests |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 167 | :return: list of relevant tests |
| 168 | """ |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 169 | # test.labels[0] is platform and test.labels[2] is language |
| 170 | # We skip a test if both are considered safe to skip |
| 171 | return [test for test in tests if test.labels[0] not in skippable_labels or \ |
| 172 | test.labels[2] not in skippable_labels] |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 173 | |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame] | 174 | |
Matt Kwong | 5eecba4 | 2017-04-05 15:09:21 -0700 | [diff] [blame] | 175 | def affects_c_cpp(base_branch): |
| 176 | """ |
| 177 | Determines if a pull request's changes affect C/C++. This function exists because |
| 178 | there are pull request tests that only test C/C++ code |
| 179 | :param base_branch: branch that a pull request is requesting to merge into |
| 180 | :return: boolean indicating whether C/C++ changes are made in pull request |
| 181 | """ |
| 182 | changed_files = _get_changed_files(base_branch) |
| 183 | # Run all tests if any changed file is not in the whitelist dictionary |
| 184 | for changed_file in changed_files: |
| 185 | if not re.match(_ALL_TRIGGERS, changed_file): |
| 186 | return True |
| 187 | return not _can_skip_tests(changed_files, _CPP_TEST_SUITE.triggers + _CORE_TEST_SUITE.triggers) |
| 188 | |
| 189 | |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame] | 190 | def filter_tests(tests, base_branch): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 191 | """ |
| 192 | Filters out tests that are safe to ignore |
| 193 | :param tests: list of all tests generated by run_tests_matrix.py |
| 194 | :return: list of relevant tests |
| 195 | """ |
Matt Kwong | 7e9bd6c | 2016-10-24 17:30:25 -0700 | [diff] [blame] | 196 | print('Finding file differences between gRPC %s branch and pull request...\n' % base_branch) |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame] | 197 | changed_files = _get_changed_files(base_branch) |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 198 | for changed_file in changed_files: |
Matt Kwong | 7e9bd6c | 2016-10-24 17:30:25 -0700 | [diff] [blame] | 199 | print(' %s' % changed_file) |
| 200 | print('') |
Matt Kwong | 5c691c6 | 2016-10-20 17:11:18 -0700 | [diff] [blame] | 201 | |
Matt Kwong | 5eecba4 | 2017-04-05 15:09:21 -0700 | [diff] [blame] | 202 | # Run all tests if any changed file is not in the whitelist dictionary |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 203 | for changed_file in changed_files: |
Matt Kwong | 5eecba4 | 2017-04-05 15:09:21 -0700 | [diff] [blame] | 204 | if not re.match(_ALL_TRIGGERS, changed_file): |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 205 | return(tests) |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 206 | # Figure out which language and platform tests to run |
| 207 | skippable_labels = [] |
Matt Kwong | 1fb0e47 | 2016-10-17 12:16:14 -0700 | [diff] [blame] | 208 | for test_suite in _ALL_TEST_SUITES: |
Matt Kwong | f01122c | 2016-10-12 18:24:53 -0700 | [diff] [blame] | 209 | if _can_skip_tests(changed_files, test_suite.triggers): |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 210 | for label in test_suite.labels: |
Matt Kwong | 7e9bd6c | 2016-10-24 17:30:25 -0700 | [diff] [blame] | 211 | print(' %s tests safe to skip' % label) |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 212 | skippable_labels.append(label) |
Matt Kwong | 2c4453d | 2016-10-19 16:35:50 -0700 | [diff] [blame] | 213 | tests = _remove_irrelevant_tests(tests, skippable_labels) |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 214 | return tests |