Debian Codegraph @

test/core/xds/xds_lb_policy_registry_test.cc

raw
1 //
2 //
3 // Copyright 2022 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/ext/xds/xds_lb_policy_registry.h"
20
21 #include <algorithm>
22 #include <string>
23
24 #include <google/protobuf/any.pb.h>
25 #include <google/protobuf/struct.pb.h>
26 #include <google/protobuf/wrappers.pb.h>
27
28 #include "absl/status/status.h"
29 #include "absl/status/statusor.h"
30 #include "gmock/gmock.h"
31 #include "gtest/gtest.h"
32 #include "upb/def.hpp"
33 #include "upb/upb.hpp"
34
35 #include <grpc/grpc.h>
36 #include <grpc/support/log.h>
37
38 #include "src/core/ext/xds/xds_bootstrap_grpc.h"
39 #include "src/core/lib/config/core_configuration.h"
40 #include "src/core/lib/gprpp/orphanable.h"
41 #include "src/core/lib/gprpp/ref_counted_ptr.h"
42 #include "src/core/lib/gprpp/validation_errors.h"
43 #include "src/core/lib/load_balancing/lb_policy.h"
44 #include "src/core/lib/load_balancing/lb_policy_factory.h"
45 #include "src/proto/grpc/testing/xds/v3/cluster.pb.h"
46 #include "src/proto/grpc/testing/xds/v3/extension.pb.h"
47 #include "src/proto/grpc/testing/xds/v3/ring_hash.pb.h"
48 #include "src/proto/grpc/testing/xds/v3/round_robin.pb.h"
49 #include "src/proto/grpc/testing/xds/v3/typed_struct.pb.h"
50 #include "src/proto/grpc/testing/xds/v3/wrr_locality.pb.h"
51 #include "test/core/util/test_config.h"
52
53 namespace grpc_core {
54 namespace testing {
55 namespace {
56
57 using LoadBalancingPolicyProto =
58 ::envoy::config::cluster::v3::LoadBalancingPolicy;
59 using ::envoy::extensions::load_balancing_policies::ring_hash::v3::RingHash;
60 using ::envoy::extensions::load_balancing_policies::round_robin::v3::RoundRobin;
61 using ::envoy::extensions::load_balancing_policies::wrr_locality::v3::
62 WrrLocality;
63 using ::xds::type::v3::TypedStruct;
64
65 // Uses XdsLbPolicyRegistry to convert
66 // envoy::config::cluster::v3::LoadBalancingPolicy to gRPC's JSON form.
67 absl::StatusOr<std::string> ConvertXdsPolicy(LoadBalancingPolicyProto policy) {
68 std::string serialized_policy = policy.SerializeAsString();
69 upb::Arena arena;
70 upb::SymbolTable symtab;
71 XdsResourceType::DecodeContext context = {nullptr,
72 GrpcXdsBootstrap::GrpcXdsServer(),
73 nullptr, symtab.ptr(), arena.ptr()};
74 auto* upb_policy = envoy_config_cluster_v3_LoadBalancingPolicy_parse(
75 serialized_policy.data(), serialized_policy.size(), arena.ptr());
76 ValidationErrors errors;
77 ValidationErrors::ScopedField field(&errors, ".load_balancing_policy");
78 auto config = XdsLbPolicyRegistry().ConvertXdsLbPolicyConfig(
79 context, upb_policy, &errors);
80 if (!errors.ok()) return errors.status("validation errors");
81 EXPECT_EQ(config.size(), 1);
82 return Json{config[0]}.Dump();
83 }
84
85 // A gRPC LB policy factory for a custom policy. None of the methods
86 // will actually be used; we just need it to be present in the gRPC LB
87 // policy registry.
88 class CustomLbPolicyFactory : public LoadBalancingPolicyFactory {
89 public:
90 OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy(
91 LoadBalancingPolicy::Args /*args*/) const override {
92 GPR_ASSERT(false);
93 return nullptr;
94 }
95
96 absl::string_view name() const override { return "test.CustomLb"; }
97
98 absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>>
99 ParseLoadBalancingConfig(const Json& /*json*/) const override {
100 return nullptr;
101 }
102 };
103
104 //
105 // RoundRobin
106 //
107
108 TEST(RoundRobin, Basic) {
109 LoadBalancingPolicyProto policy;
110 auto* lb_policy = policy.add_policies();
111 lb_policy->mutable_typed_extension_config()->mutable_typed_config()->PackFrom(
112 RoundRobin());
113 auto result = ConvertXdsPolicy(policy);
114 ASSERT_TRUE(result.ok()) << result.status();
115 EXPECT_EQ(*result, "{\"round_robin\":{}}");
116 }
117
118 //
119 // RingHash
120 //
121
122 TEST(RingHashConfig, DefaultConfig) {
123 LoadBalancingPolicyProto policy;
124 policy.add_policies()
125 ->mutable_typed_extension_config()
126 ->mutable_typed_config()
127 ->PackFrom(RingHash());
128 auto result = ConvertXdsPolicy(policy);
129 ASSERT_TRUE(result.ok()) << result.status();
130 EXPECT_EQ(*result,
131 "{\"ring_hash_experimental\":{"
132 "\"maxRingSize\":8388608,\"minRingSize\":1024}}");
133 }
134
135 TEST(RingHashConfig, FieldsExplicitlySet) {
136 RingHash ring_hash;
137 ring_hash.set_hash_function(RingHash::XX_HASH);
138 ring_hash.mutable_minimum_ring_size()->set_value(1234);
139 ring_hash.mutable_maximum_ring_size()->set_value(4567);
140 LoadBalancingPolicyProto policy;
141 policy.add_policies()
142 ->mutable_typed_extension_config()
143 ->mutable_typed_config()
144 ->PackFrom(ring_hash);
145 auto result = ConvertXdsPolicy(policy);
146 ASSERT_TRUE(result.ok()) << result.status();
147 EXPECT_EQ(*result,
148 "{\"ring_hash_experimental\":{"
149 "\"maxRingSize\":4567,\"minRingSize\":1234}}");
150 }
151
152 TEST(RingHashConfig, InvalidHashFunction) {
153 RingHash ring_hash;
154 ring_hash.set_hash_function(RingHash::MURMUR_HASH_2);
155 LoadBalancingPolicyProto policy;
156 policy.add_policies()
157 ->mutable_typed_extension_config()
158 ->mutable_typed_config()
159 ->PackFrom(ring_hash);
160 auto result = ConvertXdsPolicy(policy);
161 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
162 EXPECT_EQ(result.status().message(),
163 "validation errors: ["
164 "field:load_balancing_policy.policies[0].typed_extension_config"
165 ".typed_config.value[envoy.extensions.load_balancing_policies"
166 ".ring_hash.v3.RingHash].hash_function "
167 "error:unsupported value (must be XX_HASH)]")
168 << result.status();
169 }
170
171 TEST(RingHashConfig, RingSizesTooHigh) {
172 RingHash ring_hash;
173 ring_hash.mutable_minimum_ring_size()->set_value(8388609);
174 ring_hash.mutable_maximum_ring_size()->set_value(8388609);
175 LoadBalancingPolicyProto policy;
176 policy.add_policies()
177 ->mutable_typed_extension_config()
178 ->mutable_typed_config()
179 ->PackFrom(ring_hash);
180 auto result = ConvertXdsPolicy(policy);
181 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
182 EXPECT_EQ(result.status().message(),
183 "validation errors: ["
184 "field:load_balancing_policy.policies[0].typed_extension_config"
185 ".typed_config.value[envoy.extensions.load_balancing_policies"
186 ".ring_hash.v3.RingHash].maximum_ring_size "
187 "error:value must be in the range [1, 8388608]; "
188 "field:load_balancing_policy.policies[0].typed_extension_config"
189 ".typed_config.value[envoy.extensions.load_balancing_policies"
190 ".ring_hash.v3.RingHash].minimum_ring_size "
191 "error:value must be in the range [1, 8388608]]")
192 << result.status();
193 }
194
195 TEST(RingHashConfig, RingSizesTooLow) {
196 RingHash ring_hash;
197 ring_hash.mutable_minimum_ring_size()->set_value(0);
198 ring_hash.mutable_maximum_ring_size()->set_value(0);
199 LoadBalancingPolicyProto policy;
200 policy.add_policies()
201 ->mutable_typed_extension_config()
202 ->mutable_typed_config()
203 ->PackFrom(ring_hash);
204 auto result = ConvertXdsPolicy(policy);
205 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
206 EXPECT_EQ(result.status().message(),
207 "validation errors: ["
208 "field:load_balancing_policy.policies[0].typed_extension_config"
209 ".typed_config.value[envoy.extensions.load_balancing_policies"
210 ".ring_hash.v3.RingHash].maximum_ring_size "
211 "error:value must be in the range [1, 8388608]; "
212 "field:load_balancing_policy.policies[0].typed_extension_config"
213 ".typed_config.value[envoy.extensions.load_balancing_policies"
214 ".ring_hash.v3.RingHash].minimum_ring_size "
215 "error:value must be in the range [1, 8388608]]")
216 << result.status();
217 }
218
219 TEST(RingHashConfig, MinRingSizeGreaterThanMaxRingSize) {
220 RingHash ring_hash;
221 ring_hash.mutable_minimum_ring_size()->set_value(1000);
222 ring_hash.mutable_maximum_ring_size()->set_value(999);
223 LoadBalancingPolicyProto policy;
224 policy.add_policies()
225 ->mutable_typed_extension_config()
226 ->mutable_typed_config()
227 ->PackFrom(ring_hash);
228 auto result = ConvertXdsPolicy(policy);
229 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
230 EXPECT_EQ(result.status().message(),
231 "validation errors: ["
232 "field:load_balancing_policy.policies[0].typed_extension_config"
233 ".typed_config.value[envoy.extensions.load_balancing_policies"
234 ".ring_hash.v3.RingHash].minimum_ring_size "
235 "error:cannot be greater than maximum_ring_size]")
236 << result.status();
237 }
238
239 //
240 // WrrLocality
241 //
242
243 TEST(WrrLocality, RoundRobinChild) {
244 WrrLocality wrr_locality;
245 wrr_locality.mutable_endpoint_picking_policy()
246 ->add_policies()
247 ->mutable_typed_extension_config()
248 ->mutable_typed_config()
249 ->PackFrom(RoundRobin());
250 LoadBalancingPolicyProto policy;
251 auto* lb_policy = policy.add_policies();
252 lb_policy->mutable_typed_extension_config()->mutable_typed_config()->PackFrom(
253 wrr_locality);
254 auto result = ConvertXdsPolicy(policy);
255 ASSERT_TRUE(result.ok()) << result.status();
256 EXPECT_EQ(*result,
257 "{\"xds_wrr_locality_experimental\":{"
258 "\"childPolicy\":[{\"round_robin\":{}}]}}");
259 }
260
261 TEST(WrrLocality, MissingEndpointPickingPolicy) {
262 LoadBalancingPolicyProto policy;
263 auto* lb_policy = policy.add_policies();
264 lb_policy->mutable_typed_extension_config()->mutable_typed_config()->PackFrom(
265 WrrLocality());
266 auto result = ConvertXdsPolicy(policy);
267 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
268 EXPECT_EQ(result.status().message(),
269 "validation errors: ["
270 "field:load_balancing_policy.policies[0].typed_extension_config"
271 ".typed_config.value[envoy.extensions.load_balancing_policies"
272 ".wrr_locality.v3.WrrLocality].endpoint_picking_policy "
273 "error:field not present]")
274 << result.status();
275 }
276
277 TEST(WrrLocality, ChildPolicyInvalid) {
278 RingHash ring_hash;
279 ring_hash.set_hash_function(RingHash::MURMUR_HASH_2);
280 WrrLocality wrr_locality;
281 wrr_locality.mutable_endpoint_picking_policy()
282 ->add_policies()
283 ->mutable_typed_extension_config()
284 ->mutable_typed_config()
285 ->PackFrom(ring_hash);
286 LoadBalancingPolicyProto policy;
287 auto* lb_policy = policy.add_policies();
288 lb_policy->mutable_typed_extension_config()->mutable_typed_config()->PackFrom(
289 wrr_locality);
290 auto result = ConvertXdsPolicy(policy);
291 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
292 EXPECT_EQ(result.status().message(),
293 "validation errors: ["
294 "field:load_balancing_policy.policies[0].typed_extension_config"
295 ".typed_config.value[envoy.extensions.load_balancing_policies"
296 ".wrr_locality.v3.WrrLocality].endpoint_picking_policy.policies[0]"
297 ".typed_extension_config.typed_config.value["
298 "envoy.extensions.load_balancing_policies.ring_hash.v3.RingHash]"
299 ".hash_function "
300 "error:unsupported value (must be XX_HASH)]")
301 << result.status();
302 }
303
304 TEST(WrrLocality, NoSupportedChildPolicy) {
305 WrrLocality wrr_locality;
306 wrr_locality.mutable_endpoint_picking_policy()
307 ->add_policies()
308 ->mutable_typed_extension_config()
309 ->mutable_typed_config()
310 ->PackFrom(LoadBalancingPolicyProto());
311 LoadBalancingPolicyProto policy;
312 auto* lb_policy = policy.add_policies();
313 lb_policy->mutable_typed_extension_config()->mutable_typed_config()->PackFrom(
314 wrr_locality);
315 auto result = ConvertXdsPolicy(policy);
316 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
317 EXPECT_EQ(result.status().message(),
318 "validation errors: ["
319 "field:load_balancing_policy.policies[0].typed_extension_config"
320 ".typed_config.value[envoy.extensions.load_balancing_policies"
321 ".wrr_locality.v3.WrrLocality].endpoint_picking_policy "
322 "error:no supported load balancing policy config found]")
323 << result.status();
324 }
325
326 TEST(WrrLocality, UnsupportedChildPolicyTypeSkipped) {
327 // Create WrrLocality policy and add two policies to its list, an unsupported
328 // type and then a known RoundRobin type. Expect that the unsupported type is
329 // skipped and RoundRobin is selected.
330 WrrLocality wrr_locality;
331 wrr_locality.mutable_endpoint_picking_policy()
332 ->add_policies()
333 ->mutable_typed_extension_config()
334 ->mutable_typed_config()
335 ->PackFrom(LoadBalancingPolicyProto());
336 wrr_locality.mutable_endpoint_picking_policy()
337 ->add_policies()
338 ->mutable_typed_extension_config()
339 ->mutable_typed_config()
340 ->PackFrom(RoundRobin());
341 LoadBalancingPolicyProto policy;
342 auto* lb_policy = policy.add_policies();
343 lb_policy->mutable_typed_extension_config()->mutable_typed_config()->PackFrom(
344 wrr_locality);
345 auto result = ConvertXdsPolicy(policy);
346 EXPECT_EQ(*result,
347 "{\"xds_wrr_locality_experimental\":{"
348 "\"childPolicy\":[{\"round_robin\":{}}]}}");
349 }
350
351 //
352 // CustomPolicy
353 //
354
355 TEST(CustomPolicy, Basic) {
356 TypedStruct typed_struct;
357 typed_struct.set_type_url("type.googleapis.com/test.CustomLb");
358 auto* fields = typed_struct.mutable_value()->mutable_fields();
359 (*fields)["foo"].set_string_value("bar");
360 LoadBalancingPolicyProto policy;
361 auto* lb_policy = policy.add_policies();
362 lb_policy->mutable_typed_extension_config()->mutable_typed_config()->PackFrom(
363 typed_struct);
364 auto result = ConvertXdsPolicy(policy);
365 ASSERT_TRUE(result.ok()) << result.status();
366 EXPECT_EQ(*result, "{\"test.CustomLb\":{\"foo\":\"bar\"}}");
367 }
368
369 //
370 // XdsLbPolicyRegistryTest
371 //
372
373 TEST(XdsLbPolicyRegistryTest, EmptyLoadBalancingPolicy) {
374 auto result = ConvertXdsPolicy(LoadBalancingPolicyProto());
375 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
376 EXPECT_EQ(result.status().message(),
377 "validation errors: [field:load_balancing_policy "
378 "error:no supported load balancing policy config found]")
379 << result.status();
380 }
381
382 TEST(XdsLbPolicyRegistryTest, MissingTypedExtensionConfig) {
383 LoadBalancingPolicyProto policy;
384 policy.add_policies();
385 auto result = ConvertXdsPolicy(policy);
386 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
387 EXPECT_EQ(result.status().message(),
388 "validation errors: ["
389 "field:load_balancing_policy.policies[0].typed_extension_config "
390 "error:field not present]")
391 << result.status();
392 }
393
394 TEST(XdsLbPolicyRegistryTest, MissingTypedConfig) {
395 LoadBalancingPolicyProto policy;
396 policy.add_policies()->mutable_typed_extension_config();
397 auto result = ConvertXdsPolicy(policy);
398 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
399 EXPECT_EQ(result.status().message(),
400 "validation errors: ["
401 "field:load_balancing_policy.policies[0].typed_extension_config"
402 ".typed_config error:field not present]")
403 << result.status();
404 }
405
406 // This tests that we pass along errors that are generated by
407 // ExtractXdsExtension(). An exhaustive list of error cases caught by
408 // ExtractXdsExtension() are covered in xds_common_types_test.
409 TEST(XdsLbPolicyRegistryTest, ErrorExtractingExtension) {
410 TypedStruct typed_struct;
411 typed_struct.set_type_url("type.googleapis.com/");
412 LoadBalancingPolicyProto policy;
413 auto* lb_policy = policy.add_policies();
414 lb_policy->mutable_typed_extension_config()->mutable_typed_config()->PackFrom(
415 typed_struct);
416 auto result = ConvertXdsPolicy(policy);
417 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
418 EXPECT_EQ(result.status().message(),
419 "validation errors: ["
420 "field:load_balancing_policy.policies[0].typed_extension_config"
421 ".typed_config.value[xds.type.v3.TypedStruct].type_url "
422 "error:invalid value \"type.googleapis.com/\"]")
423 << result.status();
424 }
425
426 TEST(XdsLbPolicyRegistryTest, NoSupportedType) {
427 LoadBalancingPolicyProto policy;
428 // Unsupported built-in type.
429 policy.add_policies()
430 ->mutable_typed_extension_config()
431 ->mutable_typed_config()
432 ->PackFrom(LoadBalancingPolicyProto());
433 // Unsupported custom type.
434 TypedStruct typed_struct;
435 typed_struct.set_type_url("myorg/foo/bar/test.UnknownLb");
436 policy.add_policies()
437 ->mutable_typed_extension_config()
438 ->mutable_typed_config()
439 ->PackFrom(typed_struct);
440 auto result = ConvertXdsPolicy(policy);
441 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
442 EXPECT_EQ(result.status().message(),
443 "validation errors: [field:load_balancing_policy "
444 "error:no supported load balancing policy config found]")
445 << result.status();
446 }
447
448 TEST(XdsLbPolicyRegistryTest, UnsupportedTypesSkipped) {
449 LoadBalancingPolicyProto policy;
450 // Unsupported built-in type.
451 policy.add_policies()
452 ->mutable_typed_extension_config()
453 ->mutable_typed_config()
454 ->PackFrom(LoadBalancingPolicyProto());
455 // Unsupported custom type.
456 TypedStruct typed_struct;
457 typed_struct.set_type_url("myorg/foo/bar/test.UnknownLb");
458 policy.add_policies()
459 ->mutable_typed_extension_config()
460 ->mutable_typed_config()
461 ->PackFrom(typed_struct);
462 // Supported type.
463 policy.add_policies()
464 ->mutable_typed_extension_config()
465 ->mutable_typed_config()
466 ->PackFrom(RoundRobin());
467 auto result = ConvertXdsPolicy(policy);
468 ASSERT_TRUE(result.ok()) << result.status();
469 EXPECT_EQ(*result, "{\"round_robin\":{}}");
470 }
471
472 // Build a recurse load balancing policy that goes beyond the max allowable
473 // depth of 16.
474 LoadBalancingPolicyProto BuildRecursiveLoadBalancingPolicy(int depth) {
475 LoadBalancingPolicyProto policy;
476 if (depth >= 16) {
477 policy.add_policies()
478 ->mutable_typed_extension_config()
479 ->mutable_typed_config()
480 ->PackFrom(RoundRobin());
481 return policy;
482 }
483 WrrLocality wrr_locality;
484 *wrr_locality.mutable_endpoint_picking_policy() =
485 BuildRecursiveLoadBalancingPolicy(depth + 1);
486 policy.add_policies()
487 ->mutable_typed_extension_config()
488 ->mutable_typed_config()
489 ->PackFrom(wrr_locality);
490 return policy;
491 }
492
493 TEST(XdsLbPolicyRegistryTest, MaxRecursion) {
494 auto result = ConvertXdsPolicy(BuildRecursiveLoadBalancingPolicy(0));
495 EXPECT_EQ(result.status().code(), absl::StatusCode::kInvalidArgument);
496 EXPECT_THAT(std::string(result.status().message()),
497 ::testing::EndsWith("error:exceeded max recursion depth of 16]"));
498 }
499
500 } // namespace
501 } // namespace testing
502 } // namespace grpc_core
503
504 int main(int argc, char** argv) {
505 ::testing::InitGoogleTest(&argc, argv);
506 grpc::testing::TestEnvironment env(&argc, argv);
507 grpc_core::CoreConfiguration::RegisterBuilder(
508 [](grpc_core::CoreConfiguration::Builder* builder) {
509 builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory(
510 std::make_unique<grpc_core::testing::CustomLbPolicyFactory>());
511 });
512 grpc_init();
513 auto result = RUN_ALL_TESTS();
514 grpc_shutdown();
515 return result;
516 }