blob: 55dab42f8a7a6afba1488a6e25cdecc7550bcf02 [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
60_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]
71
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 = {
77 '^templates/.*': [],
78 '^doc/.*': [],
79 '^examples/.*': [],
80 '^summerofcode/.*': [],
81 '.*README.md$': [],
82 '.*LICENSE$': [],
83 '^src/cpp.*': [_cpp_test_suite],
84 '^src/csharp.*': [_csharp_test_suite],
85 '^src/node.*': [_node_test_suite],
86 '^src/objective-c.*': [_objc_test_suite],
87 '^src/php.*': [_php_test_suite],
88 '^src/python.*': [_python_test_suite],
89 '^src/ruby.*': [_ruby_test_suite],
90 '^test/core.*': [_core_test_suite],
91 '^test/cpp.*': [_cpp_test_suite],
92 '^test/distrib/cpp.*': [_cpp_test_suite],
93 '^test/distrib/csharp.*': [_csharp_test_suite],
94 '^test/distrib/node.*': [_node_test_suite],
95 '^test/distrib/php.*': [_php_test_suite],
96 '^test/distrib/python.*': [_python_test_suite],
97 '^test/distrib/ruby.*': [_ruby_test_suite]
98}
99# Add all triggers to their respective test suites
100for trigger, test_suites in _WHITELIST_DICT.iteritems():
101 for test_suite in test_suites:
102 test_suite.add_trigger(trigger)
Matt Kwonge9163f02016-10-05 11:42:55 -0700103
104
Matt Kwongf3f28722016-10-07 15:25:56 -0700105def _get_changed_files(base_branch):
Matt Kwonge9163f02016-10-05 11:42:55 -0700106 """
Matt Kwong037704d2016-10-06 18:18:11 -0700107 Get list of changed files between current branch and base of target merge branch
Matt Kwonge9163f02016-10-05 11:42:55 -0700108 """
109 # git fetch might need to be called on Jenkins slave
110 # todo(mattkwong): remove or uncomment below after seeing if Jenkins needs this
111 # call(['git', 'fetch'])
Matt Kwongf3f28722016-10-07 15:25:56 -0700112
Matt Kwongf01122c2016-10-12 18:24:53 -0700113 # Get file changes between branch and merge-base of specified branch
114 # Not combined to be Windows friendly
Matt Kwongf3f28722016-10-07 15:25:56 -0700115 base_commit = check_output(["git", "merge-base", base_branch, "HEAD"]).rstrip()
116 return check_output(["git", "diff", base_commit, "--name-only"]).splitlines()
Matt Kwonge9163f02016-10-05 11:42:55 -0700117
118
Matt Kwongf01122c2016-10-12 18:24:53 -0700119def _can_skip_tests(file_names, triggers):
Matt Kwonge9163f02016-10-05 11:42:55 -0700120 """
Matt Kwongf01122c2016-10-12 18:24:53 -0700121 Determines if tests are skippable based on if all files do not match list of regexes
Matt Kwonge9163f02016-10-05 11:42:55 -0700122 :param file_names: list of changed files generated by _get_changed_files()
Matt Kwongf01122c2016-10-12 18:24:53 -0700123 :param triggers: list of regexes matching file name that indicates tests should be run
Matt Kwonge9163f02016-10-05 11:42:55 -0700124 :return: safe to skip tests
125 """
126 for file_name in file_names:
Matt Kwongf01122c2016-10-12 18:24:53 -0700127 if any(re.match(trigger, file_name) for trigger in triggers):
128 return False
Matt Kwonge9163f02016-10-05 11:42:55 -0700129 return True
130
131
132def _remove_irrelevant_tests(tests, tag):
133 """
Matt Kwongf3f28722016-10-07 15:25:56 -0700134 Filters out tests by config or language - will not remove sanitizer tests
Matt Kwonge9163f02016-10-05 11:42:55 -0700135 :param tests: list of all tests generated by run_tests_matrix.py
136 :param tag: string representing language or config to filter - "_(language)_" or "_(config)"
137 :return: list of relevant tests
138 """
Matt Kwong037704d2016-10-06 18:18:11 -0700139 # todo(mattkwong): find a more reliable way to filter tests - don't use shortname
Matt Kwongf01122c2016-10-12 18:24:53 -0700140 return [test for test in tests if tag not in test.shortname or
141 any(san_tag in test.shortname for san_tag in ['_asan', '_tsan', '_msan'])]
Matt Kwonge9163f02016-10-05 11:42:55 -0700142
143
Matt Kwongf01122c2016-10-12 18:24:53 -0700144def _remove_sanitizer_tests(tests):
Matt Kwongf3f28722016-10-07 15:25:56 -0700145 """
Matt Kwongf01122c2016-10-12 18:24:53 -0700146 Filters out sanitizer tests
Matt Kwongf3f28722016-10-07 15:25:56 -0700147 :param tests: list of all tests generated by run_tests_matrix.py
Matt Kwongf3f28722016-10-07 15:25:56 -0700148 :return: list of relevant tests
149 """
Matt Kwongf01122c2016-10-12 18:24:53 -0700150 # todo(mattkwong): find a more reliable way to filter tests - don't use shortname
151 return [test for test in tests if
152 all(san_tag not in test.shortname for san_tag in ['_asan', '_tsan', '_msan'])]
153
Matt Kwongf3f28722016-10-07 15:25:56 -0700154
155def filter_tests(tests, base_branch):
Matt Kwonge9163f02016-10-05 11:42:55 -0700156 """
157 Filters out tests that are safe to ignore
158 :param tests: list of all tests generated by run_tests_matrix.py
159 :return: list of relevant tests
160 """
Matt Kwongf01122c2016-10-12 18:24:53 -0700161 print("Finding file differences between %s repo and current branch...\n" % base_branch)
Matt Kwongf3f28722016-10-07 15:25:56 -0700162 changed_files = _get_changed_files(base_branch)
Matt Kwonge9163f02016-10-05 11:42:55 -0700163 for changed_file in changed_files:
164 print(changed_file)
Matt Kwongf01122c2016-10-12 18:24:53 -0700165 print
Matt Kwong037704d2016-10-06 18:18:11 -0700166
Matt Kwongf01122c2016-10-12 18:24:53 -0700167 # Regex that combines all keys in _WHITELIST_DICT
168 all_triggers = "(" + ")|(".join(_WHITELIST_DICT.keys()) + ")"
169 # Check if all tests have to be run
170 for changed_file in changed_files:
171 if not re.match(all_triggers, changed_file):
172 return(tests)
173 # Filter out tests by language
174 for test_suite in _all_test_suites:
175 if _can_skip_tests(changed_files, test_suite.triggers):
176 for tag in test_suite.tags:
177 print(" Filtering %s tests" % tag)
178 tests = _remove_irrelevant_tests(tests, tag)
Matt Kwongf3f28722016-10-07 15:25:56 -0700179 # Sanitizer tests skipped if core and c++ are skipped
Matt Kwongf01122c2016-10-12 18:24:53 -0700180 if _can_skip_tests(changed_files, _cpp_test_suite.triggers + _core_test_suite.triggers):
181 print(" Filtering Sanitizer tests")
182 tests = _remove_sanitizer_tests(tests)
Matt Kwong037704d2016-10-06 18:18:11 -0700183
Matt Kwonge9163f02016-10-05 11:42:55 -0700184 return tests