path_matching_test.py
Go to the documentation of this file.
1 # Copyright 2021 The gRPC Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import logging
15 from typing import Tuple
16 
17 from absl import flags
18 from absl.testing import absltest
19 
20 from framework import xds_url_map_testcase
21 from framework.helpers import skips
22 from framework.test_app import client_app
23 
24 # Type aliases
25 HostRule = xds_url_map_testcase.HostRule
26 PathMatcher = xds_url_map_testcase.PathMatcher
27 GcpResourceManager = xds_url_map_testcase.GcpResourceManager
28 DumpedXdsConfig = xds_url_map_testcase.DumpedXdsConfig
29 RpcTypeUnaryCall = xds_url_map_testcase.RpcTypeUnaryCall
30 RpcTypeEmptyCall = xds_url_map_testcase.RpcTypeEmptyCall
31 XdsTestClient = client_app.XdsTestClient
32 _Lang = skips.Lang
33 
34 logger = logging.getLogger(__name__)
35 flags.adopt_module_key_flags(xds_url_map_testcase)
36 
37 _NUM_RPCS = 150
38 
39 
40 def _is_supported(config: skips.TestConfig) -> bool:
41  if config.client_lang == _Lang.NODE:
42  return config.version_gte('v1.3.x')
43  return True
44 
45 
46 class TestFullPathMatchEmptyCall(xds_url_map_testcase.XdsUrlMapTestCase):
47 
48  @staticmethod
49  def is_supported(config: skips.TestConfig) -> bool:
50  return _is_supported(config)
51 
52  @staticmethod
54  host_rule: HostRule,
55  path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
56  path_matcher["routeRules"] = [{
57  'priority': 0,
58  # FullPath EmptyCall -> alternate_backend_service.
59  'matchRules': [{
60  'fullPathMatch': '/grpc.testing.TestService/EmptyCall'
61  }],
62  'service': GcpResourceManager().alternative_backend_service()
63  }]
64  return host_rule, path_matcher
65 
66  def xds_config_validate(self, xds_config: DumpedXdsConfig):
67  self.assertNumEndpoints(xds_config, 2)
68  self.assertEqual(
69  xds_config.rds['virtualHosts'][0]['routes'][0]['match']['path'],
70  "/grpc.testing.TestService/EmptyCall")
71 
72  def rpc_distribution_validate(self, test_client: XdsTestClient):
73  rpc_distribution = self.configure_and_send(test_client,
74  rpc_types=[RpcTypeEmptyCall],
75  num_rpcs=_NUM_RPCS)
76  self.assertEqual(
77  _NUM_RPCS,
78  rpc_distribution.empty_call_alternative_service_rpc_count)
79 
80 
81 class TestFullPathMatchUnaryCall(xds_url_map_testcase.XdsUrlMapTestCase):
82 
83  @staticmethod
84  def is_supported(config: skips.TestConfig) -> bool:
85  return _is_supported(config)
86 
87  @staticmethod
89  host_rule: HostRule,
90  path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
91  path_matcher["routeRules"] = [{
92  'priority': 0,
93  # FullPath EmptyCall -> alternate_backend_service.
94  'matchRules': [{
95  'fullPathMatch': '/grpc.testing.TestService/UnaryCall'
96  }],
97  'service': GcpResourceManager().alternative_backend_service()
98  }]
99  return host_rule, path_matcher
100 
101  def xds_config_validate(self, xds_config: DumpedXdsConfig):
102  self.assertNumEndpoints(xds_config, 2)
103  self.assertEqual(
104  xds_config.rds['virtualHosts'][0]['routes'][0]['match']['path'],
105  "/grpc.testing.TestService/UnaryCall")
106 
107  def rpc_distribution_validate(self, test_client: XdsTestClient):
108  rpc_distribution = self.configure_and_send(
109  test_client, rpc_types=(RpcTypeUnaryCall,), num_rpcs=_NUM_RPCS)
110  self.assertEqual(
111  _NUM_RPCS,
112  rpc_distribution.unary_call_alternative_service_rpc_count)
113 
114 
115 class TestTwoRoutesAndPrefixMatch(xds_url_map_testcase.XdsUrlMapTestCase):
116  """This test case is similar to the one above (but with route services
117  swapped). This test has two routes (full_path and the default) to match
118  EmptyCall, and both routes set alternative_backend_service as the action.
119  This forces the client to handle duplicate Clusters in the RDS response."""
120 
121  @staticmethod
122  def is_supported(config: skips.TestConfig) -> bool:
123  return _is_supported(config)
124 
125  @staticmethod
127  host_rule: HostRule,
128  path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
129  path_matcher["routeRules"] = [
130  {
131  'priority': 0,
132  # Prefix UnaryCall -> default_backend_service.
133  'matchRules': [{
134  'prefixMatch': '/grpc.testing.TestService/Unary'
135  }],
136  'service': GcpResourceManager().default_backend_service()
137  },
138  {
139  'priority': 1,
140  # FullPath EmptyCall -> alternate_backend_service.
141  'matchRules': [{
142  'fullPathMatch': '/grpc.testing.TestService/EmptyCall'
143  }],
144  'service': GcpResourceManager().alternative_backend_service()
145  }
146  ]
147  return host_rule, path_matcher
148 
149  def xds_config_validate(self, xds_config: DumpedXdsConfig):
150  self.assertNumEndpoints(xds_config, 2)
151  self.assertEqual(
152  xds_config.rds['virtualHosts'][0]['routes'][0]['match']['prefix'],
153  "/grpc.testing.TestService/Unary")
154  self.assertEqual(
155  xds_config.rds['virtualHosts'][0]['routes'][1]['match']['path'],
156  "/grpc.testing.TestService/EmptyCall")
157 
158  def rpc_distribution_validate(self, test_client: XdsTestClient):
159  rpc_distribution = self.configure_and_send(
160  test_client,
161  rpc_types=[RpcTypeUnaryCall, RpcTypeEmptyCall],
162  num_rpcs=_NUM_RPCS)
163  self.assertEqual(0, rpc_distribution.num_failures)
164  self.assertEqual(
165  0, rpc_distribution.unary_call_alternative_service_rpc_count)
166  self.assertEqual(0,
167  rpc_distribution.empty_call_default_service_rpc_count)
168 
169 
170 class TestRegexMatch(xds_url_map_testcase.XdsUrlMapTestCase):
171 
172  @staticmethod
173  def is_supported(config: skips.TestConfig) -> bool:
174  return _is_supported(config)
175 
176  @staticmethod
178  host_rule: HostRule,
179  path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
180  path_matcher["routeRules"] = [{
181  'priority': 0,
182  # Regex UnaryCall -> alternate_backend_service.
183  'matchRules': [{
184  'regexMatch':
185  r'^\/.*\/UnaryCall$' # Unary methods with any services.
186  }],
187  'service': GcpResourceManager().alternative_backend_service()
188  }]
189  return host_rule, path_matcher
190 
191  def xds_config_validate(self, xds_config: DumpedXdsConfig):
192  self.assertNumEndpoints(xds_config, 2)
193  self.assertEqual(
194  xds_config.rds['virtualHosts'][0]['routes'][0]['match']['safeRegex']
195  ['regex'], r'^\/.*\/UnaryCall$')
196 
197  def rpc_distribution_validate(self, test_client: XdsTestClient):
198  rpc_distribution = self.configure_and_send(
199  test_client, rpc_types=(RpcTypeUnaryCall,), num_rpcs=_NUM_RPCS)
200  self.assertEqual(
201  _NUM_RPCS,
202  rpc_distribution.unary_call_alternative_service_rpc_count)
203 
204 
205 class TestCaseInsensitiveMatch(xds_url_map_testcase.XdsUrlMapTestCase):
206 
207  @staticmethod
208  def is_supported(config: skips.TestConfig) -> bool:
209  return _is_supported(config)
210 
211  @staticmethod
213  host_rule: HostRule,
214  path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
215  path_matcher["routeRules"] = [{
216  'priority': 0,
217  # ignoreCase EmptyCall -> alternate_backend_service.
218  'matchRules': [{
219  # Case insensitive matching.
220  'fullPathMatch': '/gRpC.tEsTinG.tEstseRvice/empTycaLl',
221  'ignoreCase': True,
222  }],
223  'service': GcpResourceManager().alternative_backend_service()
224  }]
225  return host_rule, path_matcher
226 
227  def xds_config_validate(self, xds_config: DumpedXdsConfig):
228  self.assertNumEndpoints(xds_config, 2)
229  self.assertEqual(
230  xds_config.rds['virtualHosts'][0]['routes'][0]['match']['path'],
231  '/gRpC.tEsTinG.tEstseRvice/empTycaLl')
232  self.assertEqual(
233  xds_config.rds['virtualHosts'][0]['routes'][0]['match']
234  ['caseSensitive'], False)
235 
236  def rpc_distribution_validate(self, test_client: XdsTestClient):
237  rpc_distribution = self.configure_and_send(test_client,
238  rpc_types=[RpcTypeEmptyCall],
239  num_rpcs=_NUM_RPCS)
240  self.assertEqual(
241  _NUM_RPCS,
242  rpc_distribution.empty_call_alternative_service_rpc_count)
243 
244 
245 if __name__ == '__main__':
246  absltest.main()
tests.url_map.path_matching_test.TestFullPathMatchEmptyCall.url_map_change
Tuple[HostRule, PathMatcher] url_map_change(HostRule host_rule, PathMatcher path_matcher)
Definition: path_matching_test.py:53
tests.url_map.path_matching_test.TestRegexMatch.url_map_change
Tuple[HostRule, PathMatcher] url_map_change(HostRule host_rule, PathMatcher path_matcher)
Definition: path_matching_test.py:177
tests.url_map.path_matching_test.TestCaseInsensitiveMatch.xds_config_validate
def xds_config_validate(self, DumpedXdsConfig xds_config)
Definition: path_matching_test.py:227
tests.url_map.path_matching_test.TestTwoRoutesAndPrefixMatch.rpc_distribution_validate
def rpc_distribution_validate(self, XdsTestClient test_client)
Definition: path_matching_test.py:158
tests.url_map.path_matching_test.GcpResourceManager
GcpResourceManager
Definition: path_matching_test.py:27
tests.url_map.path_matching_test.TestCaseInsensitiveMatch.rpc_distribution_validate
def rpc_distribution_validate(self, XdsTestClient test_client)
Definition: path_matching_test.py:236
tests.url_map.path_matching_test.TestCaseInsensitiveMatch
Definition: path_matching_test.py:205
tests.url_map.path_matching_test.TestRegexMatch.xds_config_validate
def xds_config_validate(self, DumpedXdsConfig xds_config)
Definition: path_matching_test.py:191
tests.url_map.path_matching_test.TestTwoRoutesAndPrefixMatch.is_supported
bool is_supported(skips.TestConfig config)
Definition: path_matching_test.py:122
tests.url_map.path_matching_test.TestFullPathMatchUnaryCall.is_supported
bool is_supported(skips.TestConfig config)
Definition: path_matching_test.py:84
framework.helpers
Definition: tools/run_tests/xds_k8s_test_driver/framework/helpers/__init__.py:1
framework.test_app
Definition: tools/run_tests/xds_k8s_test_driver/framework/test_app/__init__.py:1
tests.url_map.path_matching_test.TestRegexMatch.rpc_distribution_validate
def rpc_distribution_validate(self, XdsTestClient test_client)
Definition: path_matching_test.py:197
tests.url_map.path_matching_test.TestFullPathMatchUnaryCall.xds_config_validate
def xds_config_validate(self, DumpedXdsConfig xds_config)
Definition: path_matching_test.py:101
tests.url_map.path_matching_test.TestFullPathMatchEmptyCall.is_supported
bool is_supported(skips.TestConfig config)
Definition: path_matching_test.py:49
tests.url_map.path_matching_test.TestCaseInsensitiveMatch.is_supported
bool is_supported(skips.TestConfig config)
Definition: path_matching_test.py:208
tests.url_map.path_matching_test.TestRegexMatch.is_supported
bool is_supported(skips.TestConfig config)
Definition: path_matching_test.py:173
tests.url_map.path_matching_test.TestTwoRoutesAndPrefixMatch.xds_config_validate
def xds_config_validate(self, DumpedXdsConfig xds_config)
Definition: path_matching_test.py:149
tests.url_map.path_matching_test.TestFullPathMatchEmptyCall.rpc_distribution_validate
def rpc_distribution_validate(self, XdsTestClient test_client)
Definition: path_matching_test.py:72
tests.url_map.path_matching_test.TestRegexMatch
Definition: path_matching_test.py:170
tests.url_map.path_matching_test.TestTwoRoutesAndPrefixMatch.url_map_change
Tuple[HostRule, PathMatcher] url_map_change(HostRule host_rule, PathMatcher path_matcher)
Definition: path_matching_test.py:126
tests.url_map.path_matching_test.TestFullPathMatchUnaryCall.url_map_change
Tuple[HostRule, PathMatcher] url_map_change(HostRule host_rule, PathMatcher path_matcher)
Definition: path_matching_test.py:88
tests.url_map.path_matching_test._is_supported
bool _is_supported(skips.TestConfig config)
Definition: path_matching_test.py:40
tests.url_map.path_matching_test.TestCaseInsensitiveMatch.url_map_change
Tuple[HostRule, PathMatcher] url_map_change(HostRule host_rule, PathMatcher path_matcher)
Definition: path_matching_test.py:212
tests.url_map.path_matching_test.TestFullPathMatchUnaryCall
Definition: path_matching_test.py:81
tests.url_map.path_matching_test.TestTwoRoutesAndPrefixMatch
Definition: path_matching_test.py:115
tests.url_map.path_matching_test.TestFullPathMatchEmptyCall
Definition: path_matching_test.py:46
tests.url_map.path_matching_test.TestFullPathMatchEmptyCall.xds_config_validate
def xds_config_validate(self, DumpedXdsConfig xds_config)
Definition: path_matching_test.py:66
tests.url_map.path_matching_test.TestFullPathMatchUnaryCall.rpc_distribution_validate
def rpc_distribution_validate(self, XdsTestClient test_client)
Definition: path_matching_test.py:107


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:50