Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 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 | |
| 33 | from subprocess import call, check_output |
| 34 | |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame^] | 35 | # Whitelist for all tests |
| 36 | # Update all instances in corresponding trigger lists when modifying this |
| 37 | starts_with_whitelist = ['templates/', |
| 38 | 'doc/', |
| 39 | 'examples/', |
| 40 | 'summerofcode/', |
| 41 | 'src/cpp', |
| 42 | 'src/csharp', |
| 43 | 'src/node', |
| 44 | 'src/objective-c', |
| 45 | 'src/php', |
| 46 | 'src/python', |
| 47 | 'src/ruby', |
| 48 | 'test/core', |
| 49 | 'test/cpp', |
| 50 | 'test/distrib/cpp', |
| 51 | 'test/distrib/csharp', |
| 52 | 'test/distrib/node', |
| 53 | 'test/distrib/php', |
| 54 | 'test/distrib/python', |
| 55 | 'test/distrib/ruby'] |
| 56 | |
| 57 | ends_with_whitelist = ['README.md', |
| 58 | 'LICENSE'] |
| 59 | |
| 60 | # Triggers for core tests |
| 61 | core_starts_with_triggers = ['test/core'] |
| 62 | |
| 63 | # Triggers for c++ tests |
| 64 | cpp_starts_with_triggers = ['src/cpp', |
| 65 | 'test/cpp', |
| 66 | 'test/distrib/cpp'] |
| 67 | |
| 68 | # Triggers for c# tests |
| 69 | csharp_starts_with_triggers = ['src/csharp', |
| 70 | 'test/distrib/csharp'] |
| 71 | |
| 72 | # Triggers for node tests |
| 73 | node_starts_with_triggers = ['src/node', |
| 74 | 'test/distrib/node'] |
| 75 | |
| 76 | # Triggers for objective-c tests |
| 77 | objc_starts_with_triggers = ['src/objective-c'] |
| 78 | |
| 79 | # Triggers for php tests |
| 80 | php_starts_with_triggers = ['src/php', |
| 81 | 'test/distrib/php'] |
| 82 | |
| 83 | # Triggers for python tests |
| 84 | python_starts_with_triggers = ['src/python', |
| 85 | 'test/distrib/python'] |
| 86 | |
| 87 | # Triggers for ruby tests |
| 88 | ruby_starts_with_triggers = ['src/ruby', |
| 89 | 'test/distrib/ruby'] |
| 90 | |
| 91 | |
| 92 | def _filter_whitelist(whitelist, triggers): |
| 93 | """ |
| 94 | Removes triggers from whitelist |
| 95 | :param whitelist: list to remove values from |
| 96 | :param triggers: list of values to remove from whitelist |
| 97 | :return: filtered whitelist |
| 98 | """ |
| 99 | filtered_whitelist = list(whitelist) |
| 100 | for trigger in triggers: |
| 101 | if trigger in filtered_whitelist: |
| 102 | filtered_whitelist.remove(trigger) |
| 103 | else: |
| 104 | """ |
| 105 | If the trigger is not found in the whitelist, then there is likely |
| 106 | a mistake in the whitelist or trigger list, which needs to be addressed |
| 107 | to not wrongly skip tests |
| 108 | """ |
| 109 | print("ERROR: '%s' trigger not in whitelist. Please fix this!" % trigger) |
| 110 | return filtered_whitelist |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 111 | |
| 112 | |
| 113 | def _get_changed_files(): |
| 114 | """ |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame^] | 115 | 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] | 116 | """ |
| 117 | # git fetch might need to be called on Jenkins slave |
| 118 | # todo(mattkwong): remove or uncomment below after seeing if Jenkins needs this |
| 119 | # call(['git', 'fetch']) |
| 120 | # this also collects files that are changed in the repo but not updated in the branch |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame^] | 121 | # todo(mattkwong): change this to only collect changes files compared to base and not hardcode branch |
| 122 | return check_output(["git", "diff", "--name-only", "..origin/master"]).splitlines() |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 123 | |
| 124 | |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame^] | 125 | def _can_skip_tests(file_names, starts_with_whitelist=[], ends_with_whitelist=[]): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 126 | """ |
| 127 | Determines if tests are skippable based on if all file names do not match |
| 128 | any begin or end triggers |
| 129 | :param file_names: list of changed files generated by _get_changed_files() |
| 130 | :param starts_with_triggers: tuple of strings to match with beginning of file names |
| 131 | :param ends_with_triggers: tuple of strings to match with end of file names |
| 132 | :return: safe to skip tests |
| 133 | """ |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame^] | 134 | # convert lists to tuple to pass into str.startswith() and str.endswith() |
| 135 | starts_with_whitelist = tuple(starts_with_whitelist) |
| 136 | ends_with_whitelist = tuple(ends_with_whitelist) |
| 137 | print (starts_with_whitelist) |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 138 | for file_name in file_names: |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame^] | 139 | if starts_with_whitelist and not file_name.startswith(starts_with_whitelist) and \ |
| 140 | ends_with_whitelist and not file_name.endswith(ends_with_whitelist): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 141 | return False |
| 142 | return True |
| 143 | |
| 144 | |
| 145 | def _remove_irrelevant_tests(tests, tag): |
| 146 | """ |
| 147 | Filters out tests by config or language |
| 148 | :param tests: list of all tests generated by run_tests_matrix.py |
| 149 | :param tag: string representing language or config to filter - "_(language)_" or "_(config)" |
| 150 | :return: list of relevant tests |
| 151 | """ |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame^] | 152 | # todo(mattkwong): find a more reliable way to filter tests - don't use shortname |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 153 | return [test for test in tests if not tag in test.shortname] |
| 154 | |
| 155 | |
| 156 | def filter_tests(tests): |
| 157 | """ |
| 158 | Filters out tests that are safe to ignore |
| 159 | :param tests: list of all tests generated by run_tests_matrix.py |
| 160 | :return: list of relevant tests |
| 161 | """ |
| 162 | print("Finding file differences between grpc:master repo and pull request...") |
| 163 | changed_files = _get_changed_files() |
| 164 | for changed_file in changed_files: |
| 165 | print(changed_file) |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame^] | 166 | |
| 167 | changed_files = ['src/ruby/dgf'] |
| 168 | |
| 169 | # Filter core tests |
| 170 | skip_core = _can_skip_tests(changed_files, |
| 171 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, core_starts_with_triggers), |
| 172 | ends_with_whitelist=ends_with_whitelist) |
| 173 | if skip_core: |
| 174 | tests = _remove_irrelevant_tests(tests, '_c_') |
| 175 | |
| 176 | # Filter c++ tests |
| 177 | skip_cpp = _can_skip_tests(changed_files, |
| 178 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, cpp_starts_with_triggers), |
| 179 | ends_with_whitelist=ends_with_whitelist) |
| 180 | if skip_cpp: |
| 181 | tests = _remove_irrelevant_tests(tests, '_cpp_') |
| 182 | |
| 183 | # Tsan, msan, and asan tests skipped if core and c++ are skipped |
| 184 | if skip_core and skip_cpp: |
| 185 | tests = _remove_irrelevant_tests(tests, '_tsan') |
| 186 | tests = _remove_irrelevant_tests(tests, '_msan') |
| 187 | tests = _remove_irrelevant_tests(tests, '_asan') |
| 188 | |
| 189 | # Filter c# tests |
| 190 | skip_csharp = _can_skip_tests(changed_files, |
| 191 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, csharp_starts_with_triggers), |
| 192 | ends_with_whitelist=ends_with_whitelist) |
| 193 | if skip_csharp: |
| 194 | tests = _remove_irrelevant_tests(tests, '_csharp_') |
| 195 | |
| 196 | # Filter node tests |
| 197 | skip_node = _can_skip_tests(changed_files, |
| 198 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, node_starts_with_triggers), |
| 199 | ends_with_whitelist=ends_with_whitelist) |
| 200 | if skip_node: |
| 201 | tests = _remove_irrelevant_tests(tests, '_node_') |
| 202 | |
| 203 | # Filter objc tests |
| 204 | skip_objc = _can_skip_tests(changed_files, |
| 205 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, objc_starts_with_triggers), |
| 206 | ends_with_whitelist=ends_with_whitelist) |
| 207 | if skip_objc: |
| 208 | tests = _remove_irrelevant_tests(tests, '_objc_') |
| 209 | |
| 210 | # Filter php tests |
| 211 | skip_php = _can_skip_tests(changed_files, |
| 212 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, php_starts_with_triggers), |
| 213 | ends_with_whitelist=ends_with_whitelist) |
| 214 | if skip_php: |
| 215 | tests = _remove_irrelevant_tests(tests, '_php_') |
| 216 | |
| 217 | # Filter python tests |
| 218 | skip_python = _can_skip_tests(changed_files, |
| 219 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, python_starts_with_triggers), |
| 220 | ends_with_whitelist=ends_with_whitelist) |
| 221 | if skip_python: |
| 222 | tests = _remove_irrelevant_tests(tests, '_python_') |
| 223 | |
| 224 | # Filter ruby tests |
| 225 | skip_ruby = _can_skip_tests(changed_files, |
| 226 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, ruby_starts_with_triggers), |
| 227 | ends_with_whitelist=ends_with_whitelist) |
| 228 | if skip_ruby: |
| 229 | tests = _remove_irrelevant_tests(tests, '_ruby_') |
| 230 | |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 231 | return tests |