Debian Codegraph @

test/cpp/end2end/xds/xds_rls_end2end_test.cc

raw
1 // Copyright 2017 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 //
15
16 #include <memory>
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include "absl/memory/memory.h"
22 #include "absl/strings/str_cat.h"
23
24 #include "src/core/ext/filters/client_channel/backup_poller.h"
25 #include "src/core/lib/gprpp/env.h"
26 #include "src/proto/grpc/lookup/v1/rls.grpc.pb.h"
27 #include "src/proto/grpc/lookup/v1/rls.pb.h"
28 #include "src/proto/grpc/lookup/v1/rls_config.pb.h"
29 #include "test/core/util/scoped_env_var.h"
30 #include "test/cpp/end2end/rls_server.h"
31 #include "test/cpp/end2end/xds/xds_end2end_test_lib.h"
32
33 namespace grpc {
34 namespace testing {
35 namespace {
36
37 using ::grpc::lookup::v1::RouteLookupClusterSpecifier;
38 using ::grpc::lookup::v1::RouteLookupConfig;
39
40 using ::grpc_core::testing::ScopedExperimentalEnvVar;
41
42 constexpr char kRlsTestKey[] = "test_key";
43 constexpr char kRlsTestKey1[] = "key1";
44 constexpr char kRlsTestValue[] = "test_value";
45 constexpr char kRlsHostKey[] = "host_key";
46 constexpr char kRlsServiceKey[] = "service_key";
47 constexpr char kRlsServiceValue[] = "grpc.testing.EchoTestService";
48 constexpr char kRlsMethodKey[] = "method_key";
49 constexpr char kRlsMethodValue[] = "Echo";
50 constexpr char kRlsConstantKey[] = "constant_key";
51 constexpr char kRlsConstantValue[] = "constant_value";
52 constexpr char kRlsClusterSpecifierPluginInstanceName[] = "rls_plugin_instance";
53
54 class RlsTest : public XdsEnd2endTest {
55 protected:
56 class RlsServerThread : public ServerThread {
57 public:
58 explicit RlsServerThread(XdsEnd2endTest* test_obj)
59 : ServerThread(test_obj, /*use_xds_enabled_server=*/false),
60 rls_service_(new RlsServiceImpl()) {}
61
62 RlsServiceImpl* rls_service() { return rls_service_.get(); }
63
64 private:
65 const char* Type() override { return "Rls"; }
66
67 void RegisterAllServices(ServerBuilder* builder) override {
68 builder->RegisterService(rls_service_.get());
69 }
70
71 void StartAllServices() override { rls_service_->Start(); }
72
73 void ShutdownAllServices() override { rls_service_->Shutdown(); }
74
75 std::shared_ptr<RlsServiceImpl> rls_service_;
76 };
77
78 RlsTest() {
79 rls_server_ = std::make_unique<RlsServerThread>(this);
80 rls_server_->Start();
81 }
82
83 void TearDown() override {
84 rls_server_->Shutdown();
85 XdsEnd2endTest::TearDown();
86 }
87
88 std::unique_ptr<RlsServerThread> rls_server_;
89 };
90
91 // Test both with and without RDS.
92 INSTANTIATE_TEST_SUITE_P(
93 XdsTest, RlsTest,
94 ::testing::Values(XdsTestType(), XdsTestType().set_enable_rds_testing()),
95 &XdsTestType::Name);
96
97 TEST_P(RlsTest, XdsRoutingClusterSpecifierPlugin) {
98 ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_RLS_LB");
99 CreateAndStartBackends(2);
100 const char* kNewClusterName = "new_cluster";
101 const char* kNewEdsServiceName = "new_eds_service_name";
102 const size_t kNumEchoRpcs = 5;
103 // Populate new EDS resources.
104 EdsResourceArgs args({
105 {"locality0", CreateEndpointsForBackends(0, 1)},
106 });
107 EdsResourceArgs args1({
108 {"locality0", CreateEndpointsForBackends(1, 2)},
109 });
110 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
111 balancer_->ads_service()->SetEdsResource(
112 BuildEdsResource(args1, kNewEdsServiceName));
113 // Populate new CDS resources.
114 Cluster new_cluster = default_cluster_;
115 new_cluster.set_name(kNewClusterName);
116 new_cluster.mutable_eds_cluster_config()->set_service_name(
117 kNewEdsServiceName);
118 balancer_->ads_service()->SetCdsResource(new_cluster);
119 // Prepare the RLSLookupConfig and configure all the keys; change route
120 // configurations to use cluster specifier plugin.
121 rls_server_->rls_service()->SetResponse(
122 BuildRlsRequest({{kRlsTestKey, kRlsTestValue},
123 {kRlsHostKey, kServerName},
124 {kRlsServiceKey, kRlsServiceValue},
125 {kRlsMethodKey, kRlsMethodValue},
126 {kRlsConstantKey, kRlsConstantValue}}),
127 BuildRlsResponse({kNewClusterName}));
128 RouteLookupConfig route_lookup_config;
129 auto* key_builder = route_lookup_config.add_grpc_keybuilders();
130 auto* name = key_builder->add_names();
131 name->set_service(kRlsServiceValue);
132 name->set_method(kRlsMethodValue);
133 auto* header = key_builder->add_headers();
134 header->set_key(kRlsTestKey);
135 header->add_names(kRlsTestKey1);
136 header->add_names("key2");
137 auto* extra_keys = key_builder->mutable_extra_keys();
138 extra_keys->set_host(kRlsHostKey);
139 extra_keys->set_service(kRlsServiceKey);
140 extra_keys->set_method(kRlsMethodKey);
141 (*key_builder->mutable_constant_keys())[kRlsConstantKey] = kRlsConstantValue;
142 route_lookup_config.set_lookup_service(
143 absl::StrCat("localhost:", rls_server_->port()));
144 route_lookup_config.set_cache_size_bytes(5000);
145 RouteLookupClusterSpecifier rls;
146 *rls.mutable_route_lookup_config() = std::move(route_lookup_config);
147 RouteConfiguration new_route_config = default_route_config_;
148 auto* plugin = new_route_config.add_cluster_specifier_plugins();
149 plugin->mutable_extension()->set_name(kRlsClusterSpecifierPluginInstanceName);
150 plugin->mutable_extension()->mutable_typed_config()->PackFrom(rls);
151 auto* default_route =
152 new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
153 default_route->mutable_route()->set_cluster_specifier_plugin(
154 kRlsClusterSpecifierPluginInstanceName);
155 SetRouteConfiguration(balancer_.get(), new_route_config);
156 auto rpc_options = RpcOptions().set_metadata({{kRlsTestKey1, kRlsTestValue}});
157 WaitForAllBackends(DEBUG_LOCATION, 1, 2, /*check_status=*/nullptr,
158 WaitForBackendOptions(), rpc_options);
159 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs, rpc_options);
160 // Make sure RPCs all go to the correct backend.
161 EXPECT_EQ(kNumEchoRpcs, backends_[1]->backend_service()->request_count());
162 }
163
164 TEST_P(RlsTest, XdsRoutingClusterSpecifierPluginNacksUndefinedSpecifier) {
165 ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_RLS_LB");
166 RouteConfiguration new_route_config = default_route_config_;
167 auto* default_route =
168 new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
169 // Set Cluster Specifier Plugin to something that does not exist.
170 default_route->mutable_route()->set_cluster_specifier_plugin(
171 kRlsClusterSpecifierPluginInstanceName);
172 SetRouteConfiguration(balancer_.get(), new_route_config);
173 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
174 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
175 EXPECT_THAT(response_state->error_message,
176 ::testing::HasSubstr(absl::StrCat(
177 "RouteAction cluster contains cluster specifier plugin "
178 "name not configured: ",
179 kRlsClusterSpecifierPluginInstanceName)));
180 }
181
182 TEST_P(RlsTest, XdsRoutingClusterSpecifierPluginNacksDuplicateSpecifier) {
183 ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_RLS_LB");
184 // Prepare the RLSLookupConfig: change route configurations to use cluster
185 // specifier plugin.
186 RouteLookupConfig route_lookup_config;
187 auto* key_builder = route_lookup_config.add_grpc_keybuilders();
188 auto* name = key_builder->add_names();
189 name->set_service(kRlsServiceValue);
190 name->set_method(kRlsMethodValue);
191 auto* header = key_builder->add_headers();
192 header->set_key(kRlsTestKey);
193 header->add_names(kRlsTestKey1);
194 route_lookup_config.set_lookup_service(
195 absl::StrCat("localhost:", rls_server_->port()));
196 route_lookup_config.set_cache_size_bytes(5000);
197 RouteLookupClusterSpecifier rls;
198 *rls.mutable_route_lookup_config() = std::move(route_lookup_config);
199 RouteConfiguration new_route_config = default_route_config_;
200 auto* plugin = new_route_config.add_cluster_specifier_plugins();
201 plugin->mutable_extension()->set_name(kRlsClusterSpecifierPluginInstanceName);
202 plugin->mutable_extension()->mutable_typed_config()->PackFrom(rls);
203 auto* duplicate_plugin = new_route_config.add_cluster_specifier_plugins();
204 duplicate_plugin->mutable_extension()->set_name(
205 kRlsClusterSpecifierPluginInstanceName);
206 duplicate_plugin->mutable_extension()->mutable_typed_config()->PackFrom(rls);
207 auto* default_route =
208 new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
209 default_route->mutable_route()->set_cluster_specifier_plugin(
210 kRlsClusterSpecifierPluginInstanceName);
211 SetRouteConfiguration(balancer_.get(), new_route_config);
212 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
213 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
214 EXPECT_THAT(response_state->error_message,
215 ::testing::HasSubstr(absl::StrCat(
216 "Duplicated definition of cluster_specifier_plugin ",
217 kRlsClusterSpecifierPluginInstanceName)));
218 }
219
220 TEST_P(RlsTest,
221 XdsRoutingClusterSpecifierPluginNacksUnknownSpecifierProtoNotOptional) {
222 ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_RLS_LB");
223 // Prepare the RLSLookupConfig: change route configurations to use cluster
224 // specifier plugin.
225 RouteLookupConfig route_lookup_config;
226 RouteConfiguration new_route_config = default_route_config_;
227 auto* plugin = new_route_config.add_cluster_specifier_plugins();
228 plugin->mutable_extension()->set_name(kRlsClusterSpecifierPluginInstanceName);
229 // Instead of grpc.lookup.v1.RouteLookupClusterSpecifier, let's say we
230 // mistakenly packed the inner RouteLookupConfig instead.
231 plugin->mutable_extension()->mutable_typed_config()->PackFrom(
232 route_lookup_config);
233 auto* default_route =
234 new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
235 default_route->mutable_route()->set_cluster_specifier_plugin(
236 kRlsClusterSpecifierPluginInstanceName);
237 SetRouteConfiguration(balancer_.get(), new_route_config);
238 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
239 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
240 EXPECT_THAT(response_state->error_message,
241 ::testing::HasSubstr("Unknown ClusterSpecifierPlugin type "
242 "grpc.lookup.v1.RouteLookupConfig"));
243 }
244
245 TEST_P(RlsTest,
246 XdsRoutingClusterSpecifierPluginIgnoreUnknownSpecifierProtoOptional) {
247 ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_RLS_LB");
248 CreateAndStartBackends(1);
249 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
250 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
251 // Prepare the RLSLookupConfig: change route configurations to use cluster
252 // specifier plugin.
253 RouteLookupConfig route_lookup_config;
254 RouteConfiguration new_route_config = default_route_config_;
255 auto* plugin = new_route_config.add_cluster_specifier_plugins();
256 plugin->mutable_extension()->set_name(kRlsClusterSpecifierPluginInstanceName);
257 // Instead of grpc.lookup.v1.RouteLookupClusterSpecifier, let's say we
258 // mistakenly packed the inner RouteLookupConfig instead.
259 plugin->mutable_extension()->mutable_typed_config()->PackFrom(
260 route_lookup_config);
261 plugin->set_is_optional(true);
262 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
263 route->mutable_route()->set_cluster_specifier_plugin(
264 kRlsClusterSpecifierPluginInstanceName);
265 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
266 default_route->mutable_match()->set_prefix("");
267 default_route->mutable_route()->set_cluster(kDefaultClusterName);
268 SetRouteConfiguration(balancer_.get(), new_route_config);
269 // Ensure we ignore the cluster specifier plugin and send traffic according to
270 // the default route.
271 WaitForAllBackends(DEBUG_LOCATION);
272 }
273
274 TEST_P(RlsTest, XdsRoutingRlsClusterSpecifierPluginNacksRequiredMatch) {
275 ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_RLS_LB");
276 // Prepare the RLSLookupConfig and configure all the keys; add required_match
277 // field which should not be there.
278 RouteLookupConfig route_lookup_config;
279 auto* key_builder = route_lookup_config.add_grpc_keybuilders();
280 auto* name = key_builder->add_names();
281 name->set_service(kRlsServiceValue);
282 name->set_method(kRlsMethodValue);
283 auto* header = key_builder->add_headers();
284 header->set_key(kRlsTestKey);
285 header->add_names(kRlsTestKey1);
286 header->set_required_match(true);
287 route_lookup_config.set_lookup_service(
288 absl::StrCat("localhost:", rls_server_->port()));
289 route_lookup_config.set_cache_size_bytes(5000);
290 RouteLookupClusterSpecifier rls;
291 *rls.mutable_route_lookup_config() = std::move(route_lookup_config);
292 RouteConfiguration new_route_config = default_route_config_;
293 auto* plugin = new_route_config.add_cluster_specifier_plugins();
294 plugin->mutable_extension()->set_name(kRlsClusterSpecifierPluginInstanceName);
295 plugin->mutable_extension()->mutable_typed_config()->PackFrom(rls);
296 auto* default_route =
297 new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
298 default_route->mutable_route()->set_cluster_specifier_plugin(
299 kRlsClusterSpecifierPluginInstanceName);
300 SetRouteConfiguration(balancer_.get(), new_route_config);
301 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
302 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
303 EXPECT_THAT(
304 response_state->error_message,
305 ::testing::HasSubstr(
306 "field:routeLookupConfig.grpcKeybuilders[0].headers[0].requiredMatch "
307 "error:must not be present"));
308 }
309
310 TEST_P(RlsTest, XdsRoutingClusterSpecifierPluginDisabled) {
311 CreateAndStartBackends(1);
312 // Populate new EDS resources.
313 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
314 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
315 // Prepare the RLSLookupConfig and configure all the keys; change route
316 // configurations to use cluster specifier plugin.
317 RouteLookupConfig route_lookup_config;
318 auto* key_builder = route_lookup_config.add_grpc_keybuilders();
319 auto* name = key_builder->add_names();
320 name->set_service(kRlsServiceValue);
321 name->set_method(kRlsMethodValue);
322 auto* header = key_builder->add_headers();
323 header->set_key(kRlsTestKey);
324 header->add_names(kRlsTestKey1);
325 route_lookup_config.set_lookup_service(
326 absl::StrCat("localhost:", rls_server_->port()));
327 route_lookup_config.set_cache_size_bytes(5000);
328 RouteLookupClusterSpecifier rls;
329 *rls.mutable_route_lookup_config() = std::move(route_lookup_config);
330 RouteConfiguration new_route_config = default_route_config_;
331 auto* plugin = new_route_config.add_cluster_specifier_plugins();
332 plugin->mutable_extension()->set_name(kRlsClusterSpecifierPluginInstanceName);
333 plugin->mutable_extension()->mutable_typed_config()->PackFrom(rls);
334 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
335 route->mutable_route()->set_cluster_specifier_plugin(
336 kRlsClusterSpecifierPluginInstanceName);
337 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
338 default_route->mutable_match()->set_prefix("");
339 default_route->mutable_route()->set_cluster(kDefaultClusterName);
340 SetRouteConfiguration(balancer_.get(), new_route_config);
341 // Ensure we ignore the cluster specifier plugin and send traffic according to
342 // the default route.
343 auto rpc_options = RpcOptions().set_metadata({{kRlsTestKey1, kRlsTestValue}});
344 WaitForAllBackends(DEBUG_LOCATION, 0, 1, /*check_status=*/nullptr,
345 WaitForBackendOptions(), rpc_options);
346 }
347
348 } // namespace
349 } // namespace testing
350 } // namespace grpc
351
352 int main(int argc, char** argv) {
353 grpc::testing::TestEnvironment env(&argc, argv);
354 ::testing::InitGoogleTest(&argc, argv);
355 // Make the backup poller poll very frequently in order to pick up
356 // updates from all the subchannels's FDs.
357 GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1);
358 #if TARGET_OS_IPHONE
359 // Workaround Apple CFStream bug
360 grpc_core::SetEnv("grpc_cfstream", "0");
361 #endif
362 grpc_init();
363 const auto result = RUN_ALL_TESTS();
364 grpc_shutdown();
365 return result;
366 }