blob: ebbb1a93b38b42bd066460a563b0321b1fcdc031 [file] [log] [blame]
Siddharth Shukla8e64d902017-03-12 19:50:18 +01001#!/usr/bin/env python
Matt Kwong7e9bd6c2016-10-24 17:30:25 -07002
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# Copyright 2016 gRPC authors.
Matt Kwong7e9bd6c2016-10-24 17:30:25 -07004#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
Matt Kwong7e9bd6c2016-10-24 17:30:25 -07008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# http://www.apache.org/licenses/LICENSE-2.0
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070010#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070016
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070017import os
18import sys
19import unittest
20import re
21
22# hack import paths to pick up extra code
23sys.path.insert(0, os.path.abspath('tools/run_tests/'))
24from run_tests_matrix import _create_test_jobs, _create_portability_test_jobs
Jan Tattermusch5c79a312016-12-20 11:02:50 +010025import python_utils.filter_pull_request_tests as filter_pull_request_tests
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070026
ncteisen0cd6cfe2017-12-11 16:56:44 -080027_LIST_OF_LANGUAGE_LABELS = [
28 'c', 'c++', 'csharp', 'grpc-node', 'objc', 'php', 'php7', 'python', 'ruby'
29]
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070030_LIST_OF_PLATFORM_LABELS = ['linux', 'macos', 'windows']
31
ncteisen0cd6cfe2017-12-11 16:56:44 -080032
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070033class TestFilteringTest(unittest.TestCase):
34
ncteisen0cd6cfe2017-12-11 16:56:44 -080035 def generate_all_tests(self):
36 all_jobs = _create_test_jobs() + _create_portability_test_jobs()
37 self.assertIsNotNone(all_jobs)
38 return all_jobs
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070039
ncteisen0cd6cfe2017-12-11 16:56:44 -080040 def test_filtering(self, changed_files=[], labels=_LIST_OF_LANGUAGE_LABELS):
41 """
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070042 Default args should filter no tests because changed_files is empty and
43 default labels should be able to match all jobs
44 :param changed_files: mock list of changed_files from pull request
45 :param labels: list of job labels that should be skipped
46 """
ncteisen0cd6cfe2017-12-11 16:56:44 -080047 all_jobs = self.generate_all_tests()
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070048
ncteisen0cd6cfe2017-12-11 16:56:44 -080049 # Replacing _get_changed_files function to allow specifying changed files in filter_tests function
50 def _get_changed_files(foo):
51 return changed_files
Matt Kwongfe1bcd92016-11-07 14:07:06 -080052
ncteisen0cd6cfe2017-12-11 16:56:44 -080053 filter_pull_request_tests._get_changed_files = _get_changed_files
54 print()
55 filtered_jobs = filter_pull_request_tests.filter_tests(all_jobs, "test")
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070056
ncteisen0cd6cfe2017-12-11 16:56:44 -080057 # Make sure sanity tests aren't being filtered out
58 sanity_tests_in_all_jobs = 0
59 sanity_tests_in_filtered_jobs = 0
60 for job in all_jobs:
61 if "sanity" in job.labels:
62 sanity_tests_in_all_jobs += 1
63 all_jobs = [job for job in all_jobs if "sanity" not in job.labels]
64 for job in filtered_jobs:
65 if "sanity" in job.labels:
66 sanity_tests_in_filtered_jobs += 1
67 filtered_jobs = [
68 job for job in filtered_jobs if "sanity" not in job.labels
69 ]
70 self.assertEquals(sanity_tests_in_all_jobs,
71 sanity_tests_in_filtered_jobs)
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070072
ncteisen0cd6cfe2017-12-11 16:56:44 -080073 for label in labels:
74 for job in filtered_jobs:
75 self.assertNotIn(label, job.labels)
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070076
ncteisen0cd6cfe2017-12-11 16:56:44 -080077 jobs_matching_labels = 0
78 for label in labels:
79 for job in all_jobs:
80 if (label in job.labels):
81 jobs_matching_labels += 1
82 self.assertEquals(
Mehrdad Afshari87cd9942018-01-02 14:40:00 -080083 len(filtered_jobs),
84 len(all_jobs) - jobs_matching_labels)
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070085
ncteisen0cd6cfe2017-12-11 16:56:44 -080086 def test_individual_language_filters(self):
87 # Changing unlisted file should trigger all languages
88 self.test_filtering(['ffffoo/bar.baz'], [_LIST_OF_LANGUAGE_LABELS])
89 # Changing core should trigger all tests
90 self.test_filtering(['src/core/foo.bar'], [_LIST_OF_LANGUAGE_LABELS])
91 # Testing individual languages
92 self.test_filtering(['test/core/foo.bar'], [
93 label for label in _LIST_OF_LANGUAGE_LABELS
94 if label not in filter_pull_request_tests._CORE_TEST_SUITE.labels +
95 filter_pull_request_tests._CPP_TEST_SUITE.labels
96 ])
97 self.test_filtering(['src/cpp/foo.bar'], [
98 label for label in _LIST_OF_LANGUAGE_LABELS
99 if label not in filter_pull_request_tests._CPP_TEST_SUITE.labels
100 ])
101 self.test_filtering(['src/csharp/foo.bar'], [
102 label for label in _LIST_OF_LANGUAGE_LABELS
103 if label not in filter_pull_request_tests._CSHARP_TEST_SUITE.labels
104 ])
105 self.test_filtering(['src/objective-c/foo.bar'], [
106 label for label in _LIST_OF_LANGUAGE_LABELS
107 if label not in filter_pull_request_tests._OBJC_TEST_SUITE.labels
108 ])
109 self.test_filtering(['src/php/foo.bar'], [
110 label for label in _LIST_OF_LANGUAGE_LABELS
111 if label not in filter_pull_request_tests._PHP_TEST_SUITE.labels
112 ])
113 self.test_filtering(['src/python/foo.bar'], [
114 label for label in _LIST_OF_LANGUAGE_LABELS
115 if label not in filter_pull_request_tests._PYTHON_TEST_SUITE.labels
116 ])
117 self.test_filtering(['src/ruby/foo.bar'], [
118 label for label in _LIST_OF_LANGUAGE_LABELS
119 if label not in filter_pull_request_tests._RUBY_TEST_SUITE.labels
120 ])
Matt Kwong7e9bd6c2016-10-24 17:30:25 -0700121
ncteisen0cd6cfe2017-12-11 16:56:44 -0800122 def test_combined_language_filters(self):
123 self.test_filtering(['src/cpp/foo.bar', 'test/core/foo.bar'], [
124 label for label in _LIST_OF_LANGUAGE_LABELS
125 if label not in filter_pull_request_tests._CPP_TEST_SUITE.labels and
126 label not in filter_pull_request_tests._CORE_TEST_SUITE.labels
127 ])
128 self.test_filtering(['src/cpp/foo.bar', "src/csharp/foo.bar"], [
129 label for label in _LIST_OF_LANGUAGE_LABELS
130 if label not in filter_pull_request_tests._CPP_TEST_SUITE.labels and
131 label not in filter_pull_request_tests._CSHARP_TEST_SUITE.labels
132 ])
133 self.test_filtering([
134 'src/objective-c/foo.bar', 'src/php/foo.bar', "src/python/foo.bar",
135 "src/ruby/foo.bar"
136 ], [
137 label for label in _LIST_OF_LANGUAGE_LABELS
138 if label not in filter_pull_request_tests._OBJC_TEST_SUITE.labels
139 and label not in filter_pull_request_tests._PHP_TEST_SUITE.labels
140 and label not in filter_pull_request_tests._PYTHON_TEST_SUITE.labels
141 and label not in filter_pull_request_tests._RUBY_TEST_SUITE.labels
142 ])
143
144 def test_platform_filter(self):
145 self.test_filtering(['vsprojects/foo.bar'], [
146 label for label in _LIST_OF_PLATFORM_LABELS
147 if label not in filter_pull_request_tests._WINDOWS_TEST_SUITE.labels
148 ])
149
150 def test_whitelist(self):
151 whitelist = filter_pull_request_tests._WHITELIST_DICT
152 files_that_should_trigger_all_tests = [
153 'src/core/foo.bar', 'some_file_not_on_the_white_list', 'BUILD',
154 'etc/roots.pem', 'Makefile', 'tools/foo'
155 ]
156 for key in whitelist.keys():
157 for file_name in files_that_should_trigger_all_tests:
158 self.assertFalse(re.match(key, file_name))
159
Matt Kwong7e9bd6c2016-10-24 17:30:25 -0700160
161if __name__ == '__main__':
ncteisen0cd6cfe2017-12-11 16:56:44 -0800162 unittest.main(verbosity=2)