Debian Codegraph @

tools/run_tests/xds_k8s_test_driver/tests/url_map/timeout_test.py

raw
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 import unittest
17
18 from absl import flags
19 from absl.testing import absltest
20 import grpc
21
22 from framework import xds_url_map_testcase
23 from framework.helpers import skips
24 from framework.test_app import client_app
25
26 # Type aliases
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 ExpectedResult = xds_url_map_testcase.ExpectedResult
34 XdsTestClient = client_app.XdsTestClient
35 XdsUrlMapTestCase = xds_url_map_testcase.XdsUrlMapTestCase
36
37 logger = logging.getLogger(__name__)
38 flags.adopt_module_key_flags(xds_url_map_testcase)
39
40 # The first batch of RPCs don't count towards the result of test case. They are
41 # meant to prove the communication between driver and client is fine.
42 _NUM_RPCS = 25
43 _LENGTH_OF_RPC_SENDING_SEC = 10
44 _ERROR_TOLERANCE = 0.1
45
46
47 class _BaseXdsTimeOutTestCase(XdsUrlMapTestCase):
48
49 @staticmethod
50 def url_map_change(
51 host_rule: HostRule,
52 path_matcher: PathMatcher) -> Tuple[HostRule, PathMatcher]:
53 path_matcher['routeRules'] = [{
54 'priority': 0,
55 'matchRules': [{
56 'fullPathMatch': '/grpc.testing.TestService/UnaryCall'
57 }],
58 'service': GcpResourceManager().default_backend_service(),
59 'routeAction': {
60 'maxStreamDuration': {
61 'seconds': 3,
62 },
63 },
64 }]
65 return host_rule, path_matcher
66
67 def xds_config_validate(self, xds_config: DumpedXdsConfig):
68 self.assertNumEndpoints(xds_config, 1)
69 self.assertEqual(
70 xds_config.rds['virtualHosts'][0]['routes'][0]['route']
71 ['maxStreamDuration']['maxStreamDuration'], '3s')
72 self.assertEqual(
73 xds_config.rds['virtualHosts'][0]['routes'][0]['route']
74 ['maxStreamDuration']['grpcTimeoutHeaderMax'], '3s')
75
76 def rpc_distribution_validate(self, unused_test_client):
77 raise NotImplementedError()
78
79
80 class TestTimeoutInRouteRule(_BaseXdsTimeOutTestCase):
81
82 @staticmethod
83 def is_supported(config: skips.TestConfig) -> bool:
84 # TODO(lidiz) either add support for rpc-behavior to other languages, or we
85 # should always use Java server as backend.
86 if config.server_lang != 'java':
87 return False
88 if config.client_lang == skips.Lang.NODE:
89 return config.version_gte('v1.4.x')
90 return True
91
92 def rpc_distribution_validate(self, test_client: XdsTestClient):
93 self.configure_and_send(
94 test_client,
95 rpc_types=[RpcTypeUnaryCall, RpcTypeEmptyCall],
96 # UnaryCall and EmptyCall both sleep-4.
97 # UnaryCall timeouts, EmptyCall succeeds.
98 metadata=(
99 (RpcTypeUnaryCall, 'rpc-behavior', 'sleep-4'),
100 (RpcTypeEmptyCall, 'rpc-behavior', 'sleep-4'),
101 ),
102 num_rpcs=_NUM_RPCS)
103 self.assertRpcStatusCode(
104 test_client,
105 expected=(
106 ExpectedResult(rpc_type=RpcTypeUnaryCall,
107 status_code=grpc.StatusCode.DEADLINE_EXCEEDED),
108 ExpectedResult(rpc_type=RpcTypeEmptyCall,
109 status_code=grpc.StatusCode.OK),
110 ),
111 length=_LENGTH_OF_RPC_SENDING_SEC,
112 tolerance=_ERROR_TOLERANCE)
113
114
115 class TestTimeoutInApplication(_BaseXdsTimeOutTestCase):
116
117 @staticmethod
118 def is_supported(config: skips.TestConfig) -> bool:
119 # TODO(lidiz) either add support for rpc-behavior to other languages, or we
120 # should always use Java server as backend.
121 if config.server_lang != 'java':
122 return False
123 if config.client_lang == skips.Lang.NODE:
124 return config.version_gte('v1.4.x')
125 return True
126
127 def rpc_distribution_validate(self, test_client: XdsTestClient):
128 self.configure_and_send(
129 test_client,
130 rpc_types=(RpcTypeUnaryCall,),
131 # UnaryCall only with sleep-2; timeout=1s; calls timeout.
132 metadata=((RpcTypeUnaryCall, 'rpc-behavior', 'sleep-2'),),
133 app_timeout=1,
134 num_rpcs=_NUM_RPCS)
135 self.assertRpcStatusCode(
136 test_client,
137 expected=(ExpectedResult(
138 rpc_type=RpcTypeUnaryCall,
139 status_code=grpc.StatusCode.DEADLINE_EXCEEDED),),
140 length=_LENGTH_OF_RPC_SENDING_SEC,
141 tolerance=_ERROR_TOLERANCE)
142
143
144 class TestTimeoutNotExceeded(_BaseXdsTimeOutTestCase):
145
146 @staticmethod
147 def is_supported(config: skips.TestConfig) -> bool:
148 if config.client_lang == skips.Lang.NODE:
149 return config.version_gte('v1.4.x')
150 return True
151
152 def rpc_distribution_validate(self, test_client: XdsTestClient):
153 self.configure_and_send(
154 test_client,
155 # UnaryCall only with no sleep; calls succeed.
156 rpc_types=(RpcTypeUnaryCall,),
157 num_rpcs=_NUM_RPCS)
158 self.assertRpcStatusCode(test_client,
159 expected=(ExpectedResult(
160 rpc_type=RpcTypeUnaryCall,
161 status_code=grpc.StatusCode.OK),),
162 length=_LENGTH_OF_RPC_SENDING_SEC,
163 tolerance=_ERROR_TOLERANCE)
164
165
166 def load_tests(loader: absltest.TestLoader, unused_tests, unused_pattern):
167 suite = unittest.TestSuite()
168 test_cases = [
169 TestTimeoutInRouteRule, TestTimeoutInApplication, TestTimeoutNotExceeded
170 ]
171 for test_class in test_cases:
172 tests = loader.loadTestsFromTestCase(test_class)
173 suite.addTests(tests)
174 return suite
175
176
177 if __name__ == '__main__':
178 absltest.main()