15 from typing
import Tuple
17 from absl
import flags
18 from absl.testing
import absltest
20 from framework
import xds_url_map_testcase
27 HostRule = xds_url_map_testcase.HostRule
28 PathMatcher = xds_url_map_testcase.PathMatcher
29 GcpResourceManager = xds_url_map_testcase.GcpResourceManager
30 DumpedXdsConfig = xds_url_map_testcase.DumpedXdsConfig
31 RpcTypeUnaryCall = xds_url_map_testcase.RpcTypeUnaryCall
32 RpcTypeEmptyCall = xds_url_map_testcase.RpcTypeEmptyCall
33 XdsTestClient = client_app.XdsTestClient
36 logger = logging.getLogger(__name__)
37 flags.adopt_module_key_flags(xds_url_map_testcase)
40 _TEST_METADATA_KEY = traffic_director.TEST_AFFINITY_METADATA_KEY
41 _TEST_METADATA_VALUE_UNARY =
'unary_yranu'
42 _TEST_METADATA_VALUE_EMPTY =
'empty_ytpme'
43 _TEST_METADATA_NUMERIC_KEY =
'xds_md_numeric'
44 _TEST_METADATA_NUMERIC_VALUE =
'159'
47 (RpcTypeUnaryCall, _TEST_METADATA_KEY, _TEST_METADATA_VALUE_UNARY),
48 (RpcTypeEmptyCall, _TEST_METADATA_KEY, _TEST_METADATA_VALUE_EMPTY),
49 (RpcTypeUnaryCall, _TEST_METADATA_NUMERIC_KEY,
50 _TEST_METADATA_NUMERIC_VALUE),
53 _ChannelzChannelState = grpc_channelz.ChannelState
59 if config.client_lang
in _Lang.CPP | _Lang.JAVA:
60 return config.version_gte(
'v1.40.x')
61 elif config.client_lang == _Lang.GO:
62 return config.version_gte(
'v1.41.x')
63 elif config.client_lang == _Lang.PYTHON:
67 elif config.client_lang == _Lang.NODE:
85 return 'EmptyCall',
'EmptyCall:%s:%s' % (_TEST_METADATA_KEY,
86 _TEST_METADATA_VALUE_EMPTY)
91 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
94 ).affinity_backend_service()
95 return host_rule, path_matcher
99 self.assertNumEndpoints(xds_config, 3)
101 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'route']
102 [
'hashPolicy'][0][
'header'][
'headerName'], _TEST_METADATA_KEY)
103 self.assertEqual(xds_config.cds[0][
'lbPolicy'],
'RING_HASH')
106 rpc_distribution = self.configure_and_send(test_client,
107 rpc_types=[RpcTypeEmptyCall],
108 metadata=_TEST_METADATA,
112 self.assertEqual(1, rpc_distribution.num_peers)
114 test_client.find_subchannels_with_state(
115 _ChannelzChannelState.READY),
119 test_client.find_subchannels_with_state(_ChannelzChannelState.IDLE),
124 rpc_distribution = self.configure_and_send(
126 rpc_types=[RpcTypeEmptyCall, RpcTypeUnaryCall],
128 self.assertEqual(3, rpc_distribution.num_peers)
130 test_client.find_subchannels_with_state(
131 _ChannelzChannelState.READY),
137 xds_url_map_testcase.XdsUrlMapTestCase):
150 return 'EmptyCall',
'EmptyCall:%s:%s' % (_TEST_METADATA_KEY,
151 _TEST_METADATA_VALUE_EMPTY)
156 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
159 ).affinity_backend_service()
160 return host_rule, path_matcher
164 self.assertNumEndpoints(xds_config, 3)
166 xds_config.rds[
'virtualHosts'][0][
'routes'][0][
'route']
167 [
'hashPolicy'][0][
'header'][
'headerName'], _TEST_METADATA_KEY)
168 self.assertEqual(xds_config.cds[0][
'lbPolicy'],
'RING_HASH')
171 rpc_distribution = self.configure_and_send(test_client,
172 rpc_types=[RpcTypeEmptyCall],
173 metadata=_TEST_METADATA,
177 self.assertEqual(1, rpc_distribution.num_peers)
179 test_client.find_subchannels_with_state(
180 _ChannelzChannelState.READY),
184 test_client.find_subchannels_with_state(_ChannelzChannelState.IDLE),
187 empty_call_peer = list(rpc_distribution.raw[
'rpcsByMethod'][
'EmptyCall']
188 [
'rpcsByPeer'].
keys())[0]
201 different_peer_picked =
False
204 (RpcTypeEmptyCall, _TEST_METADATA_KEY,
205 _TEST_METADATA_VALUE_EMPTY),
206 (RpcTypeUnaryCall, _TEST_METADATA_KEY,
str(i)),
208 rpc_distribution = self.configure_and_send(
210 rpc_types=[RpcTypeEmptyCall, RpcTypeUnaryCall],
211 metadata=new_metadata,
213 unary_call_peer = list(rpc_distribution.raw[
'rpcsByMethod']
214 [
'UnaryCall'][
'rpcsByPeer'].
keys())[0]
215 if unary_call_peer != empty_call_peer:
216 different_peer_picked =
True
219 different_peer_picked,
220 (
"the same endpoint was picked for all the headers, expect a "
221 "different endpoint to be picked"))
223 test_client.find_subchannels_with_state(
224 _ChannelzChannelState.READY),
228 test_client.find_subchannels_with_state(_ChannelzChannelState.IDLE),
237 if __name__ ==
'__main__':