| 1 |
// |
| 2 |
// Copyright 2019 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include "src/core/ext/filters/client_channel/client_channel_service_config.h" |
| 18 |
|
| 19 |
#include "absl/status/status.h" |
| 20 |
#include "absl/status/statusor.h" |
| 21 |
#include "gtest/gtest.h" |
| 22 |
|
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpc/slice.h> |
| 25 |
|
| 26 |
#include "src/core/lib/channel/channel_args.h" |
| 27 |
#include "src/core/lib/config/core_configuration.h" |
| 28 |
#include "src/core/lib/gprpp/time.h" |
| 29 |
#include "src/core/lib/service_config/service_config.h" |
| 30 |
#include "src/core/lib/service_config/service_config_impl.h" |
| 31 |
#include "src/core/lib/service_config/service_config_parser.h" |
| 32 |
#include "test/core/util/test_config.h" |
| 33 |
|
| 34 |
namespace grpc_core { |
| 35 |
namespace testing { |
| 36 |
|
| 37 |
// A regular expression to enter referenced or child errors. |
| 38 |
#define CHILD_ERROR_TAG ".*children.*" |
| 39 |
|
| 40 |
class ClientChannelParserTest : public ::testing::Test { |
| 41 |
protected: |
| 42 |
void SetUp() override { |
| 43 |
parser_index_ = |
| 44 |
CoreConfiguration::Get().service_config_parser().GetParserIndex( |
| 45 |
"client_channel"); |
| 46 |
} |
| 47 |
|
| 48 |
size_t parser_index_; |
| 49 |
}; |
| 50 |
|
| 51 |
TEST_F(ClientChannelParserTest, ValidLoadBalancingConfigPickFirst) { |
| 52 |
const char* test_json = "{\"loadBalancingConfig\": [{\"pick_first\":{}}]}"; |
| 53 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 54 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 55 |
const auto* parsed_config = |
| 56 |
static_cast<internal::ClientChannelGlobalParsedConfig*>( |
| 57 |
(*service_config)->GetGlobalParsedConfig(parser_index_)); |
| 58 |
auto lb_config = parsed_config->parsed_lb_config(); |
| 59 |
EXPECT_EQ(lb_config->name(), "pick_first"); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_F(ClientChannelParserTest, ValidLoadBalancingConfigRoundRobin) { |
| 63 |
const char* test_json = |
| 64 |
"{\"loadBalancingConfig\": [{\"round_robin\":{}}, {}]}"; |
| 65 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 66 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 67 |
auto parsed_config = static_cast<internal::ClientChannelGlobalParsedConfig*>( |
| 68 |
(*service_config)->GetGlobalParsedConfig(parser_index_)); |
| 69 |
auto lb_config = parsed_config->parsed_lb_config(); |
| 70 |
EXPECT_EQ(lb_config->name(), "round_robin"); |
| 71 |
} |
| 72 |
|
| 73 |
TEST_F(ClientChannelParserTest, ValidLoadBalancingConfigGrpclb) { |
| 74 |
const char* test_json = |
| 75 |
"{\"loadBalancingConfig\": " |
| 76 |
"[{\"grpclb\":{\"childPolicy\":[{\"pick_first\":{}}]}}]}"; |
| 77 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 78 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 79 |
const auto* parsed_config = |
| 80 |
static_cast<internal::ClientChannelGlobalParsedConfig*>( |
| 81 |
(*service_config)->GetGlobalParsedConfig(parser_index_)); |
| 82 |
auto lb_config = parsed_config->parsed_lb_config(); |
| 83 |
EXPECT_EQ(lb_config->name(), "grpclb"); |
| 84 |
} |
| 85 |
|
| 86 |
TEST_F(ClientChannelParserTest, ValidLoadBalancingConfigXds) { |
| 87 |
const char* test_json = |
| 88 |
"{\n" |
| 89 |
" \"loadBalancingConfig\":[\n" |
| 90 |
" { \"does_not_exist\":{} },\n" |
| 91 |
" { \"xds_cluster_resolver_experimental\":{\n" |
| 92 |
" \"discoveryMechanisms\": [\n" |
| 93 |
" { \"clusterName\": \"foo\",\n" |
| 94 |
" \"type\": \"EDS\"\n" |
| 95 |
" } ],\n" |
| 96 |
" \"xdsLbPolicy\": [{\"round_robin\":{}}]\n" |
| 97 |
" } }\n" |
| 98 |
" ]\n" |
| 99 |
"}"; |
| 100 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 101 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 102 |
const auto* parsed_config = |
| 103 |
static_cast<internal::ClientChannelGlobalParsedConfig*>( |
| 104 |
(*service_config)->GetGlobalParsedConfig(parser_index_)); |
| 105 |
auto lb_config = parsed_config->parsed_lb_config(); |
| 106 |
EXPECT_EQ(lb_config->name(), "xds_cluster_resolver_experimental"); |
| 107 |
} |
| 108 |
|
| 109 |
TEST_F(ClientChannelParserTest, UnknownLoadBalancingConfig) { |
| 110 |
const char* test_json = "{\"loadBalancingConfig\": [{\"unknown\":{}}]}"; |
| 111 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 112 |
EXPECT_EQ(service_config.status().code(), absl::StatusCode::kInvalidArgument); |
| 113 |
EXPECT_EQ(service_config.status().message(), |
| 114 |
"errors validating service config: [" |
| 115 |
"field:loadBalancingConfig error:" |
| 116 |
"No known policies in list: unknown]") |
| 117 |
<< service_config.status(); |
| 118 |
} |
| 119 |
|
| 120 |
TEST_F(ClientChannelParserTest, InvalidGrpclbLoadBalancingConfig) { |
| 121 |
const char* test_json = |
| 122 |
"{\"loadBalancingConfig\": [" |
| 123 |
" {\"grpclb\":{\"childPolicy\":1}}," |
| 124 |
" {\"round_robin\":{}}" |
| 125 |
"]}"; |
| 126 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 127 |
EXPECT_EQ(service_config.status().code(), absl::StatusCode::kInvalidArgument); |
| 128 |
EXPECT_EQ(service_config.status().message(), |
| 129 |
"errors validating service config: [" |
| 130 |
"field:loadBalancingConfig error:" |
| 131 |
"errors validating grpclb LB policy config: [" |
| 132 |
"field:childPolicy error:type should be array]]") |
| 133 |
<< service_config.status(); |
| 134 |
} |
| 135 |
|
| 136 |
TEST_F(ClientChannelParserTest, ValidLoadBalancingPolicy) { |
| 137 |
const char* test_json = "{\"loadBalancingPolicy\":\"pick_first\"}"; |
| 138 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 139 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 140 |
const auto* parsed_config = |
| 141 |
static_cast<internal::ClientChannelGlobalParsedConfig*>( |
| 142 |
(*service_config)->GetGlobalParsedConfig(parser_index_)); |
| 143 |
EXPECT_EQ(parsed_config->parsed_deprecated_lb_policy(), "pick_first"); |
| 144 |
} |
| 145 |
|
| 146 |
TEST_F(ClientChannelParserTest, ValidLoadBalancingPolicyAllCaps) { |
| 147 |
const char* test_json = "{\"loadBalancingPolicy\":\"PICK_FIRST\"}"; |
| 148 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 149 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 150 |
const auto* parsed_config = |
| 151 |
static_cast<internal::ClientChannelGlobalParsedConfig*>( |
| 152 |
(*service_config)->GetGlobalParsedConfig(parser_index_)); |
| 153 |
EXPECT_EQ(parsed_config->parsed_deprecated_lb_policy(), "pick_first"); |
| 154 |
} |
| 155 |
|
| 156 |
TEST_F(ClientChannelParserTest, UnknownLoadBalancingPolicy) { |
| 157 |
const char* test_json = "{\"loadBalancingPolicy\":\"unknown\"}"; |
| 158 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 159 |
EXPECT_EQ(service_config.status().code(), absl::StatusCode::kInvalidArgument); |
| 160 |
EXPECT_EQ(service_config.status().message(), |
| 161 |
"errors validating service config: [" |
| 162 |
"field:loadBalancingPolicy error:unknown LB policy \"unknown\"]") |
| 163 |
<< service_config.status(); |
| 164 |
} |
| 165 |
|
| 166 |
TEST_F(ClientChannelParserTest, LoadBalancingPolicyXdsNotAllowed) { |
| 167 |
const char* test_json = |
| 168 |
"{\"loadBalancingPolicy\":\"xds_cluster_resolver_experimental\"}"; |
| 169 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 170 |
EXPECT_EQ(service_config.status().code(), absl::StatusCode::kInvalidArgument); |
| 171 |
EXPECT_EQ(service_config.status().message(), |
| 172 |
"errors validating service config: [" |
| 173 |
"field:loadBalancingPolicy error:LB policy " |
| 174 |
"\"xds_cluster_resolver_experimental\" requires a config. Please " |
| 175 |
"use loadBalancingConfig instead.]") |
| 176 |
<< service_config.status(); |
| 177 |
} |
| 178 |
|
| 179 |
TEST_F(ClientChannelParserTest, ValidTimeout) { |
| 180 |
const char* test_json = |
| 181 |
"{\n" |
| 182 |
" \"methodConfig\": [ {\n" |
| 183 |
" \"name\": [\n" |
| 184 |
" { \"service\": \"TestServ\", \"method\": \"TestMethod\" }\n" |
| 185 |
" ],\n" |
| 186 |
" \"timeout\": \"5s\"\n" |
| 187 |
" } ]\n" |
| 188 |
"}"; |
| 189 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 190 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 191 |
const auto* vector_ptr = |
| 192 |
(*service_config) |
| 193 |
->GetMethodParsedConfigVector( |
| 194 |
grpc_slice_from_static_string("/TestServ/TestMethod")); |
| 195 |
ASSERT_NE(vector_ptr, nullptr); |
| 196 |
auto parsed_config = ((*vector_ptr)[parser_index_]).get(); |
| 197 |
EXPECT_EQ( |
| 198 |
(static_cast<internal::ClientChannelMethodParsedConfig*>(parsed_config)) |
| 199 |
->timeout(), |
| 200 |
Duration::Seconds(5)); |
| 201 |
} |
| 202 |
|
| 203 |
TEST_F(ClientChannelParserTest, InvalidTimeout) { |
| 204 |
const char* test_json = |
| 205 |
"{\n" |
| 206 |
" \"methodConfig\": [ {\n" |
| 207 |
" \"name\": [\n" |
| 208 |
" { \"service\": \"service\", \"method\": \"method\" }\n" |
| 209 |
" ],\n" |
| 210 |
" \"timeout\": \"5sec\"\n" |
| 211 |
" } ]\n" |
| 212 |
"}"; |
| 213 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 214 |
EXPECT_EQ(service_config.status().code(), absl::StatusCode::kInvalidArgument); |
| 215 |
EXPECT_EQ(service_config.status().message(), |
| 216 |
"errors validating service config: [" |
| 217 |
"field:methodConfig[0].timeout " |
| 218 |
"error:Not a duration (no s suffix)]") |
| 219 |
<< service_config.status(); |
| 220 |
} |
| 221 |
|
| 222 |
TEST_F(ClientChannelParserTest, ValidWaitForReady) { |
| 223 |
const char* test_json = |
| 224 |
"{\n" |
| 225 |
" \"methodConfig\": [ {\n" |
| 226 |
" \"name\": [\n" |
| 227 |
" { \"service\": \"TestServ\", \"method\": \"TestMethod\" }\n" |
| 228 |
" ],\n" |
| 229 |
" \"waitForReady\": true\n" |
| 230 |
" } ]\n" |
| 231 |
"}"; |
| 232 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 233 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 234 |
const auto* vector_ptr = |
| 235 |
(*service_config) |
| 236 |
->GetMethodParsedConfigVector( |
| 237 |
grpc_slice_from_static_string("/TestServ/TestMethod")); |
| 238 |
ASSERT_NE(vector_ptr, nullptr); |
| 239 |
auto parsed_config = ((*vector_ptr)[parser_index_]).get(); |
| 240 |
ASSERT_TRUE( |
| 241 |
(static_cast<internal::ClientChannelMethodParsedConfig*>(parsed_config)) |
| 242 |
->wait_for_ready() |
| 243 |
.has_value()); |
| 244 |
EXPECT_TRUE( |
| 245 |
(static_cast<internal::ClientChannelMethodParsedConfig*>(parsed_config)) |
| 246 |
->wait_for_ready() |
| 247 |
.value()); |
| 248 |
} |
| 249 |
|
| 250 |
TEST_F(ClientChannelParserTest, InvalidWaitForReady) { |
| 251 |
const char* test_json = |
| 252 |
"{\n" |
| 253 |
" \"methodConfig\": [ {\n" |
| 254 |
" \"name\": [\n" |
| 255 |
" { \"service\": \"service\", \"method\": \"method\" }\n" |
| 256 |
" ],\n" |
| 257 |
" \"waitForReady\": \"true\"\n" |
| 258 |
" } ]\n" |
| 259 |
"}"; |
| 260 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 261 |
EXPECT_EQ(service_config.status().code(), absl::StatusCode::kInvalidArgument); |
| 262 |
EXPECT_EQ(service_config.status().message(), |
| 263 |
"errors validating service config: [" |
| 264 |
"field:methodConfig[0].waitForReady error:is not a boolean]") |
| 265 |
<< service_config.status(); |
| 266 |
} |
| 267 |
|
| 268 |
TEST_F(ClientChannelParserTest, ValidHealthCheck) { |
| 269 |
const char* test_json = |
| 270 |
"{\n" |
| 271 |
" \"healthCheckConfig\": {\n" |
| 272 |
" \"serviceName\": \"health_check_service_name\"\n" |
| 273 |
" }\n" |
| 274 |
"}"; |
| 275 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 276 |
ASSERT_TRUE(service_config.ok()) << service_config.status(); |
| 277 |
const auto* parsed_config = |
| 278 |
static_cast<internal::ClientChannelGlobalParsedConfig*>( |
| 279 |
(*service_config)->GetGlobalParsedConfig(parser_index_)); |
| 280 |
ASSERT_NE(parsed_config, nullptr); |
| 281 |
EXPECT_EQ(parsed_config->health_check_service_name(), |
| 282 |
"health_check_service_name"); |
| 283 |
} |
| 284 |
|
| 285 |
TEST_F(ClientChannelParserTest, InvalidHealthCheckMultipleEntries) { |
| 286 |
const char* test_json = |
| 287 |
"{\n" |
| 288 |
" \"healthCheckConfig\": {\n" |
| 289 |
" \"serviceName\": \"health_check_service_name\"\n" |
| 290 |
" },\n" |
| 291 |
" \"healthCheckConfig\": {\n" |
| 292 |
" \"serviceName\": \"health_check_service_name1\"\n" |
| 293 |
" }\n" |
| 294 |
"}"; |
| 295 |
auto service_config = ServiceConfigImpl::Create(ChannelArgs(), test_json); |
| 296 |
EXPECT_EQ(service_config.status().code(), absl::StatusCode::kInvalidArgument); |
| 297 |
EXPECT_EQ(service_config.status().message(), |
| 298 |
"JSON parsing failed: [" |
| 299 |
"duplicate key \"healthCheckConfig\" at index 104]") |
| 300 |
<< service_config.status(); |
| 301 |
} |
| 302 |
|
| 303 |
} // namespace testing |
| 304 |
} // namespace grpc_core |
| 305 |
|
| 306 |
int main(int argc, char** argv) { |
| 307 |
::testing::InitGoogleTest(&argc, argv); |
| 308 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 309 |
grpc_init(); |
| 310 |
int ret = RUN_ALL_TESTS(); |
| 311 |
grpc_shutdown(); |
| 312 |
return ret; |
| 313 |
} |