custom_lb_test.py
Go to the documentation of this file.
1 # Copyright 2022 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 datetime
15 import logging
16 
17 from absl import flags
18 from absl.testing import absltest
19 import grpc
20 
21 from framework import xds_k8s_testcase
22 
23 logger = logging.getLogger(__name__)
24 flags.adopt_module_key_flags(xds_k8s_testcase)
25 
26 # Type aliases
27 _XdsTestServer = xds_k8s_testcase.XdsTestServer
28 _XdsTestClient = xds_k8s_testcase.XdsTestClient
29 
30 _EXPECTED_STATUS = grpc.StatusCode.DATA_LOSS
31 
32 
33 class CustomLbTest(xds_k8s_testcase.RegularXdsKubernetesTestCase):
34 
36  with self.subTest('0_create_health_check'):
37  self.td.create_health_check()
38 
39  # Configures a custom, test LB on the client to instruct the servers
40  # to always respond with a specific error code.
41  with self.subTest('1_create_backend_service'):
42  self.td.create_backend_service(locality_lb_policies=[{
43  'customPolicy': {
44  'name':
45  'test.RpcBehaviorLoadBalancer',
46  'data':
47  f'{{ "rpcBehavior": "error-code-{_EXPECTED_STATUS.value[0]}" }}'
48  }
49  }])
50 
51  with self.subTest('2_create_url_map'):
52  self.td.create_url_map(self.server_xds_host, self.server_xds_port)
53 
54  with self.subTest('3_create_target_proxy'):
55  self.td.create_target_proxy()
56 
57  with self.subTest('4_create_forwarding_rule'):
58  self.td.create_forwarding_rule(self.server_xds_port)
59 
60  with self.subTest('5_start_test_server'):
61  test_server: _XdsTestServer = self.startTestServers()[0]
62 
63  with self.subTest('6_add_server_backends_to_backend_service'):
64  self.setupServerBackends()
65 
66  with self.subTest('7_start_test_client'):
67  test_client: _XdsTestClient = self.startTestClient(test_server)
68 
69  with self.subTest('8_test_client_xds_config_exists'):
70  self.assertXdsConfigExists(test_client)
71 
72  # Verify status codes from the servers have the configured one.
73  with self.subTest('9_test_server_returned_configured_status_code'):
74  self.assertRpcStatusCodes(test_client,
75  status_code=_EXPECTED_STATUS,
76  duration=datetime.timedelta(seconds=10),
77  method='UNARY_CALL')
78 
79 
80 if __name__ == '__main__':
81  absltest.main(failfast=True)
tests.custom_lb_test.CustomLbTest.test_custom_lb_config
def test_custom_lb_config(self)
Definition: custom_lb_test.py:35
run_xds_tests.create_target_proxy
def create_target_proxy(gcp, name, validate_for_proxyless=True, url_map=None)
Definition: run_xds_tests.py:2618
run_xds_tests.create_url_map
def create_url_map(gcp, name, backend_service, host_name)
Definition: run_xds_tests.py:2582
tests.custom_lb_test.CustomLbTest
Definition: custom_lb_test.py:33
run_xds_tests.create_health_check
def create_health_check(gcp, name)
Definition: run_xds_tests.py:2515


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:02