blob: c2a6399ae8e7ea4bbd3f82141558d4efc99c5af5 [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(
83 len(filtered_jobs), len(all_jobs) - jobs_matching_labels)
Matt Kwong7e9bd6c2016-10-24 17:30:25 -070084
ncteisen0cd6cfe2017-12-11 16:56:44 -080085 def test_individual_language_filters(self):
86 # Changing unlisted file should trigger all languages
87 self.test_filtering(['ffffoo/bar.baz'], [_LIST_OF_LANGUAGE_LABELS])
88 # Changing core should trigger all tests
89 self.test_filtering(['src/core/foo.bar'], [_LIST_OF_LANGUAGE_LABELS])
90 # Testing individual languages
91 self.test_filtering(['test/core/foo.bar'], [
92 label for label in _LIST_OF_LANGUAGE_LABELS
93 if label not in filter_pull_request_tests._CORE_TEST_SUITE.labels +
94 filter_pull_request_tests._CPP_TEST_SUITE.labels
95 ])
96 self.test_filtering(['src/cpp/foo.bar'], [
97 label for label in _LIST_OF_LANGUAGE_LABELS
98 if label not in filter_pull_request_tests._CPP_TEST_SUITE.labels
99 ])
100 self.test_filtering(['src/csharp/foo.bar'], [
101 label for label in _LIST_OF_LANGUAGE_LABELS
102 if label not in filter_pull_request_tests._CSHARP_TEST_SUITE.labels
103 ])
104 self.test_filtering(['src/objective-c/foo.bar'], [
105 label for label in _LIST_OF_LANGUAGE_LABELS
106 if label not in filter_pull_request_tests._OBJC_TEST_SUITE.labels
107 ])
108 self.test_filtering(['src/php/foo.bar'], [
109 label for label in _LIST_OF_LANGUAGE_LABELS
110 if label not in filter_pull_request_tests._PHP_TEST_SUITE.labels
111 ])
112 self.test_filtering(['src/python/foo.bar'], [
113 label for label in _LIST_OF_LANGUAGE_LABELS
114 if label not in filter_pull_request_tests._PYTHON_TEST_SUITE.labels
115 ])
116 self.test_filtering(['src/ruby/foo.bar'], [
117 label for label in _LIST_OF_LANGUAGE_LABELS
118 if label not in filter_pull_request_tests._RUBY_TEST_SUITE.labels
119 ])
Matt Kwong7e9bd6c2016-10-24 17:30:25 -0700120
ncteisen0cd6cfe2017-12-11 16:56:44 -0800121 def test_combined_language_filters(self):
122 self.test_filtering(['src/cpp/foo.bar', 'test/core/foo.bar'], [
123 label for label in _LIST_OF_LANGUAGE_LABELS
124 if label not in filter_pull_request_tests._CPP_TEST_SUITE.labels and
125 label not in filter_pull_request_tests._CORE_TEST_SUITE.labels
126 ])
127 self.test_filtering(['src/cpp/foo.bar', "src/csharp/foo.bar"], [
128 label for label in _LIST_OF_LANGUAGE_LABELS
129 if label not in filter_pull_request_tests._CPP_TEST_SUITE.labels and
130 label not in filter_pull_request_tests._CSHARP_TEST_SUITE.labels
131 ])
132 self.test_filtering([
133 'src/objective-c/foo.bar', 'src/php/foo.bar', "src/python/foo.bar",
134 "src/ruby/foo.bar"
135 ], [
136 label for label in _LIST_OF_LANGUAGE_LABELS
137 if label not in filter_pull_request_tests._OBJC_TEST_SUITE.labels
138 and label not in filter_pull_request_tests._PHP_TEST_SUITE.labels
139 and label not in filter_pull_request_tests._PYTHON_TEST_SUITE.labels
140 and label not in filter_pull_request_tests._RUBY_TEST_SUITE.labels
141 ])
142
143 def test_platform_filter(self):
144 self.test_filtering(['vsprojects/foo.bar'], [
145 label for label in _LIST_OF_PLATFORM_LABELS
146 if label not in filter_pull_request_tests._WINDOWS_TEST_SUITE.labels
147 ])
148
149 def test_whitelist(self):
150 whitelist = filter_pull_request_tests._WHITELIST_DICT
151 files_that_should_trigger_all_tests = [
152 'src/core/foo.bar', 'some_file_not_on_the_white_list', 'BUILD',
153 'etc/roots.pem', 'Makefile', 'tools/foo'
154 ]
155 for key in whitelist.keys():
156 for file_name in files_that_should_trigger_all_tests:
157 self.assertFalse(re.match(key, file_name))
158
Matt Kwong7e9bd6c2016-10-24 17:30:25 -0700159
160if __name__ == '__main__':
ncteisen0cd6cfe2017-12-11 16:56:44 -0800161 unittest.main(verbosity=2)