15 from typing
import Tuple
17 from absl
import flags
18 from absl.testing
import absltest
20 from framework
import xds_url_map_testcase
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
34 logger = logging.getLogger(__name__)
35 flags.adopt_module_key_flags(xds_url_map_testcase)
38 _TEST_METADATA_KEY =
'xds_md'
39 _TEST_METADATA_VALUE_UNARY =
'unary_yranu'
40 _TEST_METADATA_VALUE_EMPTY =
'empty_ytpme'
41 _TEST_METADATA_NUMERIC_KEY =
'xds_md_numeric'
42 _TEST_METADATA_NUMERIC_VALUE =
'159'
45 (RpcTypeUnaryCall, _TEST_METADATA_KEY, _TEST_METADATA_VALUE_UNARY),
46 (RpcTypeEmptyCall, _TEST_METADATA_KEY, _TEST_METADATA_VALUE_EMPTY),
47 (RpcTypeUnaryCall, _TEST_METADATA_NUMERIC_KEY,
48 _TEST_METADATA_NUMERIC_VALUE),
53 if config.client_lang == _Lang.NODE:
54 return config.version_gte(
'v1.3.x')
67 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
68 path_matcher[
"routeRules"] = [{
76 'headerName': _TEST_METADATA_KEY,
77 'exactMatch': _TEST_METADATA_VALUE_EMPTY
82 return host_rule, path_matcher
85 self.assertNumEndpoints(xds_config, 2)
87 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
88 [0][
'name'], _TEST_METADATA_KEY)
90 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
91 [0][
'exactMatch'], _TEST_METADATA_VALUE_EMPTY)
94 rpc_distribution = self.configure_and_send(test_client,
95 rpc_types=[RpcTypeEmptyCall],
96 metadata=_TEST_METADATA,
100 rpc_distribution.empty_call_alternative_service_rpc_count)
103 @absltest.skip(
'the xDS config is good, but distribution is wrong.')
113 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
114 path_matcher[
"routeRules"] = [{
122 'headerName': _TEST_METADATA_KEY,
123 'prefixMatch': _TEST_METADATA_VALUE_UNARY[:2]
128 return host_rule, path_matcher
131 self.assertNumEndpoints(xds_config, 2)
133 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
134 [0][
'name'], _TEST_METADATA_KEY)
136 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
137 [0][
'prefixMatch'], _TEST_METADATA_VALUE_UNARY[:2])
140 rpc_distribution = self.configure_and_send(
142 rpc_types=(RpcTypeUnaryCall,),
143 metadata=_TEST_METADATA,
147 rpc_distribution.unary_call_alternative_service_rpc_count)
159 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
160 path_matcher[
"routeRules"] = [{
168 'headerName': _TEST_METADATA_KEY,
169 'suffixMatch': _TEST_METADATA_VALUE_EMPTY[-2:]
174 return host_rule, path_matcher
177 self.assertNumEndpoints(xds_config, 2)
179 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
180 [0][
'name'], _TEST_METADATA_KEY)
182 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
183 [0][
'suffixMatch'], _TEST_METADATA_VALUE_EMPTY[-2:])
186 rpc_distribution = self.configure_and_send(test_client,
187 rpc_types=[RpcTypeEmptyCall],
188 metadata=_TEST_METADATA,
192 rpc_distribution.empty_call_alternative_service_rpc_count)
204 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
205 path_matcher[
"routeRules"] = [{
213 'headerName': _TEST_METADATA_NUMERIC_KEY,
219 return host_rule, path_matcher
222 self.assertNumEndpoints(xds_config, 2)
224 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
225 [0][
'name'], _TEST_METADATA_NUMERIC_KEY)
227 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
228 [0][
'presentMatch'],
True)
231 rpc_distribution = self.configure_and_send(
233 rpc_types=(RpcTypeUnaryCall,),
234 metadata=_TEST_METADATA,
238 rpc_distribution.unary_call_alternative_service_rpc_count)
250 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
251 path_matcher[
"routeRules"] = [{
260 'headerName': _TEST_METADATA_KEY,
261 'exactMatch': _TEST_METADATA_VALUE_UNARY,
267 return host_rule, path_matcher
270 self.assertNumEndpoints(xds_config, 2)
272 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
273 [0][
'name'], _TEST_METADATA_KEY)
275 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
276 [0][
'invertMatch'],
True)
279 rpc_distribution = self.configure_and_send(
281 rpc_types=[RpcTypeUnaryCall, RpcTypeEmptyCall],
282 metadata=_TEST_METADATA,
284 self.assertEqual(_NUM_RPCS, rpc_distribution.num_oks)
286 0, rpc_distribution.unary_call_alternative_service_rpc_count)
288 rpc_distribution.empty_call_default_service_rpc_count)
300 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
301 path_matcher[
"routeRules"] = [{
309 'headerName': _TEST_METADATA_NUMERIC_KEY,
318 return host_rule, path_matcher
321 self.assertNumEndpoints(xds_config, 2)
323 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
324 [0][
'name'], _TEST_METADATA_NUMERIC_KEY)
326 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
327 [0][
'rangeMatch'][
'start'],
'100')
329 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
330 [0][
'rangeMatch'][
'end'],
'200')
333 rpc_distribution = self.configure_and_send(
335 rpc_types=[RpcTypeUnaryCall, RpcTypeEmptyCall],
336 metadata=_TEST_METADATA,
338 self.assertEqual(_NUM_RPCS, rpc_distribution.num_oks)
340 rpc_distribution.unary_call_default_service_rpc_count)
342 0, rpc_distribution.empty_call_alternative_service_rpc_count)
354 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
355 path_matcher[
"routeRules"] = [{
366 "^%s.*%s$" % (_TEST_METADATA_VALUE_EMPTY[:2],
367 _TEST_METADATA_VALUE_EMPTY[-2:])
372 return host_rule, path_matcher
375 self.assertNumEndpoints(xds_config, 2)
377 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
378 [0][
'name'], _TEST_METADATA_KEY)
380 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'match'][
'headers']
381 [0][
'safeRegexMatch'][
'regex'],
"^%s.*%s$" %
382 (_TEST_METADATA_VALUE_EMPTY[:2], _TEST_METADATA_VALUE_EMPTY[-2:]))
385 rpc_distribution = self.configure_and_send(test_client,
386 rpc_types=[RpcTypeEmptyCall],
387 metadata=_TEST_METADATA,
391 rpc_distribution.empty_call_alternative_service_rpc_count)
394 if __name__ ==
'__main__':