| 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 |
from framework.helpers import skips |
| 23 |
|
| 24 |
logger = logging.getLogger(__name__) |
| 25 |
flags.adopt_module_key_flags(xds_k8s_testcase) |
| 26 |
|
| 27 |
# Type aliases |
| 28 |
_XdsTestServer = xds_k8s_testcase.XdsTestServer |
| 29 |
_XdsTestClient = xds_k8s_testcase.XdsTestClient |
| 30 |
_Lang = skips.Lang |
| 31 |
|
| 32 |
_EXPECTED_STATUS = grpc.StatusCode.DATA_LOSS |
| 33 |
|
| 34 |
|
| 35 |
class CustomLbTest(xds_k8s_testcase.RegularXdsKubernetesTestCase): |
| 36 |
|
| 37 |
# As of 2022-07-06 custom load balancer configuration via xDS is only supported by |
| 38 |
# Java clients v1.47.x and above. |
| 39 |
@staticmethod |
| 40 |
def is_supported(config: skips.TestConfig) -> bool: |
| 41 |
if config.client_lang == _Lang.JAVA: |
| 42 |
return config.version_gte('v1.47.x') |
| 43 |
return False |
| 44 |
|
| 45 |
def test_custom_lb_config(self): |
| 46 |
with self.subTest('0_create_health_check'): |
| 47 |
self.td.create_health_check() |
| 48 |
|
| 49 |
# Configures a custom, test LB on the client to instruct the servers |
| 50 |
# to always respond with a specific error code. |
| 51 |
# |
| 52 |
# The first policy in the list is a non-existent one to verify that |
| 53 |
# the gRPC client can gracefully move down the list to the valid one |
| 54 |
# once it determines the first one is not available. |
| 55 |
with self.subTest('1_create_backend_service'): |
| 56 |
self.td.create_backend_service(locality_lb_policies=[{ |
| 57 |
'customPolicy': { |
| 58 |
'name': 'test.ThisLoadBalancerDoesNotExist', |
| 59 |
'data': '{ "foo": "bar" }' |
| 60 |
}, |
| 61 |
}, { |
| 62 |
'customPolicy': { |
| 63 |
'name': |
| 64 |
'test.RpcBehaviorLoadBalancer', |
| 65 |
'data': |
| 66 |
f'{{ "rpcBehavior": "error-code-{_EXPECTED_STATUS.value[0]}" }}' |
| 67 |
} |
| 68 |
}]) |
| 69 |
|
| 70 |
with self.subTest('2_create_url_map'): |
| 71 |
self.td.create_url_map(self.server_xds_host, self.server_xds_port) |
| 72 |
|
| 73 |
with self.subTest('3_create_target_proxy'): |
| 74 |
self.td.create_target_proxy() |
| 75 |
|
| 76 |
with self.subTest('4_create_forwarding_rule'): |
| 77 |
self.td.create_forwarding_rule(self.server_xds_port) |
| 78 |
|
| 79 |
with self.subTest('5_start_test_server'): |
| 80 |
test_server: _XdsTestServer = self.startTestServers()[0] |
| 81 |
|
| 82 |
with self.subTest('6_add_server_backends_to_backend_service'): |
| 83 |
self.setupServerBackends() |
| 84 |
|
| 85 |
with self.subTest('7_start_test_client'): |
| 86 |
test_client: _XdsTestClient = self.startTestClient(test_server) |
| 87 |
|
| 88 |
with self.subTest('8_test_client_xds_config_exists'): |
| 89 |
self.assertXdsConfigExists(test_client) |
| 90 |
|
| 91 |
# Verify status codes from the servers have the configured one. |
| 92 |
with self.subTest('9_test_server_returned_configured_status_code'): |
| 93 |
self.assertRpcStatusCodes(test_client, |
| 94 |
status_code=_EXPECTED_STATUS, |
| 95 |
duration=datetime.timedelta(seconds=10), |
| 96 |
method='UNARY_CALL') |
| 97 |
|
| 98 |
|
| 99 |
if __name__ == '__main__': |
| 100 |
absltest.main(failfast=True) |