blob: 128370e1914e6224290b96f2833104883f7e96fe [file] [log] [blame]
Matt Kwonge9163f02016-10-05 11:42:55 -07001#!/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 Kwong037704d2016-10-06 18:18:11 -070031"""Filter out tests based on file differences compared to merge target branch"""
Matt Kwonge9163f02016-10-05 11:42:55 -070032
Matt Kwongf01122c2016-10-12 18:24:53 -070033import re
Matt Kwonge9163f02016-10-05 11:42:55 -070034from subprocess import call, check_output
35
Matt Kwong037704d2016-10-06 18:18:11 -070036
Matt Kwongf01122c2016-10-12 18:24:53 -070037class TestSuite:
Matt Kwong037704d2016-10-06 18:18:11 -070038 """
Matt Kwongf01122c2016-10-12 18:24:53 -070039 Contains tag to identify job as belonging to this test suite and
40 triggers to identify if changed files are relevant
Matt Kwong037704d2016-10-06 18:18:11 -070041 """
Matt Kwongf01122c2016-10-12 18:24:53 -070042 def __init__(self, tags):
43 """
44 Build TestSuite to group tests by their tags
45 :param tag: string used to identify if a job belongs to this TestSuite
46 todo(mattkwong): Change the use of tag because do not want to depend on
47 job.shortname to identify what suite a test belongs to
48 """
49 self.triggers = []
50 self.tags = tags
51
52 def add_trigger(self, trigger):
53 """
54 Add a regex to list of triggers that determine if a changed file should run tests
55 :param trigger: regex matching file relevant to tests
56 """
57 self.triggers.append(trigger)
58
59# Create test suites
Matt Kwong1fb0e472016-10-17 12:16:14 -070060_CORE_TEST_SUITE = TestSuite(['_c_'])
61_CPP_TEST_SUITE = TestSuite(['_c++_'])
62_CSHARP_TEST_SUITE = TestSuite(['_csharp_'])
63_NODE_TEST_SUITE = TestSuite(['_node_'])
64_OBJC_TEST_SUITE = TestSuite(['_objc_'])
65_PHP_TEST_SUITE = TestSuite(['_php_', '_php7_'])
66_PYTHON_TEST_SUITE = TestSuite(['_python_'])
67_RUBY_TEST_SUITE = TestSuite(['_ruby'])
68_ALL_TEST_SUITES = [_CORE_TEST_SUITE, _CPP_TEST_SUITE, _CSHARP_TEST_SUITE,
69 _NODE_TEST_SUITE, _OBJC_TEST_SUITE, _PHP_TEST_SUITE,
70 _PYTHON_TEST_SUITE, _RUBY_TEST_SUITE]
Matt Kwongf01122c2016-10-12 18:24:53 -070071
72# Dictionary of whitelistable files where the key is a regex matching changed files
73# and the value is a list of tests that should be run. An empty list means that
74# the changed files should not trigger any tests. Any changed file that does not
75# match any of these regexes will trigger all tests
76_WHITELIST_DICT = {
Matt Kwong1fb0e472016-10-17 12:16:14 -070077 #'^templates/.*': [_sanity_test_suite],
78 # todo(mattkwong): add sanity test suite
79 '^doc/': [],
80 '^examples/': [],
81 '^summerofcode/': [],
82 'README\.md$': [],
83 'CONTRIBUTING\.md$': [],
84 'LICENSE$': [],
85 'INSTALL\.md$': [],
86 'MANIFEST\.md$': [],
87 'PATENTS$': [],
88 'binding\.grp$': [_NODE_TEST_SUITE],
89 'gRPC\-Core\.podspec$': [_OBJC_TEST_SUITE],
90 'gRPC\-ProtoRPC\.podspec$': [_OBJC_TEST_SUITE],
91 'gRPC\-RxLibrary\.podspec$': [_OBJC_TEST_SUITE],
92 'gRPC\.podspec$': [_OBJC_TEST_SUITE],
93 'composer\.json$': [_PHP_TEST_SUITE],
94 'config\.m4$': [_PHP_TEST_SUITE],
95 'package\.json$': [_PHP_TEST_SUITE],
96 'package\.xml$': [_PHP_TEST_SUITE],
97 'PYTHON\-MANIFEST\.in$': [_PYTHON_TEST_SUITE],
98 'requirements\.txt$': [_PYTHON_TEST_SUITE],
99 'setup\.cfg$': [_PYTHON_TEST_SUITE],
100 'setup\.py$': [_PYTHON_TEST_SUITE],
101 'grpc\.gemspec$': [_RUBY_TEST_SUITE],
102 'Gemfile$': [_RUBY_TEST_SUITE],
103 # 'grpc.def$': [_WINDOWS_TEST_SUITE],
104 '^src/cpp/': [_CPP_TEST_SUITE],
105 '^src/csharp/': [_CSHARP_TEST_SUITE],
106 '^src/node/': [_NODE_TEST_SUITE],
107 '^src/objective\-c/': [_OBJC_TEST_SUITE],
108 '^src/php/': [_PHP_TEST_SUITE],
109 '^src/python/': [_PYTHON_TEST_SUITE],
110 '^src/ruby/': [_RUBY_TEST_SUITE],
111 '^test/core/': [_CORE_TEST_SUITE],
112 '^test/cpp/': [_CPP_TEST_SUITE],
113 '^test/distrib/cpp/': [_CPP_TEST_SUITE],
114 '^test/distrib/csharp/': [_CSHARP_TEST_SUITE],
115 '^test/distrib/node/': [_NODE_TEST_SUITE],
116 '^test/distrib/php/': [_PHP_TEST_SUITE],
117 '^test/distrib/python/': [_PYTHON_TEST_SUITE],
118 '^test/distrib/ruby/': [_RUBY_TEST_SUITE],
119 '^include/grpc\+\+/': [_CPP_TEST_SUITE]
120 #'^vsprojects/': [_WINDOWS_TEST_SUITE]
121 # todo(mattkwong): add windows test suite
Matt Kwongf01122c2016-10-12 18:24:53 -0700122}
123# Add all triggers to their respective test suites
124for trigger, test_suites in _WHITELIST_DICT.iteritems():
125 for test_suite in test_suites:
126 test_suite.add_trigger(trigger)
Matt Kwonge9163f02016-10-05 11:42:55 -0700127
128
Matt Kwongf3f28722016-10-07 15:25:56 -0700129def _get_changed_files(base_branch):
Matt Kwonge9163f02016-10-05 11:42:55 -0700130 """
Matt Kwong037704d2016-10-06 18:18:11 -0700131 Get list of changed files between current branch and base of target merge branch
Matt Kwonge9163f02016-10-05 11:42:55 -0700132 """
133 # git fetch might need to be called on Jenkins slave
134 # todo(mattkwong): remove or uncomment below after seeing if Jenkins needs this
Matt Kwong1fb0e472016-10-17 12:16:14 -0700135 call(['git', 'fetch'])
Matt Kwongf3f28722016-10-07 15:25:56 -0700136
Matt Kwongf01122c2016-10-12 18:24:53 -0700137 # Get file changes between branch and merge-base of specified branch
138 # Not combined to be Windows friendly
Matt Kwongf3f28722016-10-07 15:25:56 -0700139 base_commit = check_output(["git", "merge-base", base_branch, "HEAD"]).rstrip()
140 return check_output(["git", "diff", base_commit, "--name-only"]).splitlines()
Matt Kwonge9163f02016-10-05 11:42:55 -0700141
142
Matt Kwongf01122c2016-10-12 18:24:53 -0700143def _can_skip_tests(file_names, triggers):
Matt Kwonge9163f02016-10-05 11:42:55 -0700144 """
Matt Kwongf01122c2016-10-12 18:24:53 -0700145 Determines if tests are skippable based on if all files do not match list of regexes
Matt Kwonge9163f02016-10-05 11:42:55 -0700146 :param file_names: list of changed files generated by _get_changed_files()
Matt Kwongf01122c2016-10-12 18:24:53 -0700147 :param triggers: list of regexes matching file name that indicates tests should be run
Matt Kwonge9163f02016-10-05 11:42:55 -0700148 :return: safe to skip tests
149 """
150 for file_name in file_names:
Matt Kwongf01122c2016-10-12 18:24:53 -0700151 if any(re.match(trigger, file_name) for trigger in triggers):
152 return False
Matt Kwonge9163f02016-10-05 11:42:55 -0700153 return True
154
155
156def _remove_irrelevant_tests(tests, tag):
157 """
Matt Kwongf3f28722016-10-07 15:25:56 -0700158 Filters out tests by config or language - will not remove sanitizer tests
Matt Kwonge9163f02016-10-05 11:42:55 -0700159 :param tests: list of all tests generated by run_tests_matrix.py
160 :param tag: string representing language or config to filter - "_(language)_" or "_(config)"
161 :return: list of relevant tests
162 """
Matt Kwong037704d2016-10-06 18:18:11 -0700163 # todo(mattkwong): find a more reliable way to filter tests - don't use shortname
Matt Kwong1fb0e472016-10-17 12:16:14 -0700164 return [test for test in tests if tag not in test.shortname]
Matt Kwongf01122c2016-10-12 18:24:53 -0700165
Matt Kwongf3f28722016-10-07 15:25:56 -0700166
167def filter_tests(tests, base_branch):
Matt Kwonge9163f02016-10-05 11:42:55 -0700168 """
169 Filters out tests that are safe to ignore
170 :param tests: list of all tests generated by run_tests_matrix.py
171 :return: list of relevant tests
172 """
Matt Kwongf01122c2016-10-12 18:24:53 -0700173 print("Finding file differences between %s repo and current branch...\n" % base_branch)
Matt Kwongf3f28722016-10-07 15:25:56 -0700174 changed_files = _get_changed_files(base_branch)
Matt Kwonge9163f02016-10-05 11:42:55 -0700175 for changed_file in changed_files:
176 print(changed_file)
Matt Kwongf01122c2016-10-12 18:24:53 -0700177 print
Matt Kwong037704d2016-10-06 18:18:11 -0700178
Matt Kwongf01122c2016-10-12 18:24:53 -0700179 # Regex that combines all keys in _WHITELIST_DICT
180 all_triggers = "(" + ")|(".join(_WHITELIST_DICT.keys()) + ")"
181 # Check if all tests have to be run
182 for changed_file in changed_files:
183 if not re.match(all_triggers, changed_file):
184 return(tests)
185 # Filter out tests by language
Matt Kwong1fb0e472016-10-17 12:16:14 -0700186 for test_suite in _ALL_TEST_SUITES:
Matt Kwongf01122c2016-10-12 18:24:53 -0700187 if _can_skip_tests(changed_files, test_suite.triggers):
188 for tag in test_suite.tags:
189 print(" Filtering %s tests" % tag)
190 tests = _remove_irrelevant_tests(tests, tag)
Matt Kwong037704d2016-10-06 18:18:11 -0700191
Matt Kwonge9163f02016-10-05 11:42:55 -0700192 return tests