• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2015 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 <grpc/grpc.h>
20 #include <grpc/support/alloc.h>
21 #include <grpcpp/channel.h>
22 #include <grpcpp/client_context.h>
23 #include <grpcpp/support/channel_arguments.h>
24 
25 #include <memory>
26 #include <unordered_map>
27 
28 #include "absl/flags/flag.h"
29 #include "absl/log/log.h"
30 #include "src/core/util/crash.h"
31 #include "src/core/util/string.h"
32 #include "test/core/test_util/test_config.h"
33 #include "test/cpp/interop/client_helper.h"
34 #include "test/cpp/interop/interop_client.h"
35 #include "test/cpp/util/test_config.h"
36 
37 ABSL_FLAG(bool, use_alts, false,
38           "Whether to use alts. Enable alts will disable tls.");
39 ABSL_FLAG(bool, use_tls, false, "Whether to use tls.");
40 ABSL_FLAG(std::string, custom_credentials_type, "",
41           "User provided credentials type.");
42 ABSL_FLAG(bool, use_test_ca, false, "False to use SSL roots for google");
43 ABSL_FLAG(int32_t, server_port, 0, "Server port.");
44 ABSL_FLAG(std::string, server_host, "localhost", "Server host to connect to");
45 ABSL_FLAG(std::string, server_host_override, "",
46           "Override the server host which is sent in HTTP header");
47 ABSL_FLAG(
48     std::string, test_case, "large_unary",
49     "Configure different test cases. Valid options are:\n\n"
50     "all : all test cases;\n"
51 
52     // TODO(veblush): Replace the help message with the following full message
53     // once Abseil fixes the flag-help compiler error on Windows. (b/171659833)
54     //
55     //"cancel_after_begin : cancel stream after starting it;\n"
56     //"cancel_after_first_response: cancel on first response;\n"
57     //"channel_soak: sends 'soak_iterations' rpcs, rebuilds channel each
58     // time;\n" "client_compressed_streaming : compressed request streaming with
59     //" "client_compressed_unary : single compressed request;\n"
60     //"client_streaming : request streaming with single response;\n"
61     //"compute_engine_creds: large_unary with compute engine auth;\n"
62     //"custom_metadata: server will echo custom metadata;\n"
63     //"empty_stream : bi-di stream with no request/response;\n"
64     //"empty_unary : empty (zero bytes) request and response;\n"
65     //"google_default_credentials: large unary using GDC;\n"
66     //"half_duplex : half-duplex streaming;\n"
67     //"jwt_token_creds: large_unary with JWT token auth;\n"
68     //"large_unary : single request and (large) response;\n"
69     //"long_lived_channel: sends large_unary rpcs over a long-lived channel;\n"
70     //"oauth2_auth_token: raw oauth2 access token auth;\n"
71     //"orca_per_rpc: custom LB policy receives per-query metric reports;\n"
72     //"orca_oob: receives out-of-band metric reports from the backend;\n"
73     //"per_rpc_creds: raw oauth2 access token on a single rpc;\n"
74     //"ping_pong : full-duplex streaming;\n"
75     //"response streaming;\n"
76     //"rpc_soak: 'sends soak_iterations' large_unary rpcs;\n"
77     //"server_compressed_streaming : single request with compressed "
78     //"server_compressed_unary : single compressed response;\n"
79     //"server_streaming : single request with response streaming;\n"
80     //"slow_consumer : single request with response streaming with "
81     //"slow client consumer;\n"
82     //"special_status_message: verify Unicode and whitespace in status
83     // message;\n" "status_code_and_message: verify status code & message;\n"
84     //"timeout_on_sleeping_server: deadline exceeds on stream;\n"
85     //"unimplemented_method: client calls an unimplemented method;\n"
86     //"unimplemented_service: client calls an unimplemented service;\n"
87     //
88 );
89 ABSL_FLAG(std::string, default_service_account, "",
90           "Email of GCE default service account");
91 ABSL_FLAG(std::string, service_account_key_file, "",
92           "Path to service account json key file.");
93 ABSL_FLAG(std::string, oauth_scope, "", "Scope for OAuth tokens.");
94 ABSL_FLAG(bool, do_not_abort_on_transient_failures, false,
95           "If set to 'true', abort() is not called in case of transient "
96           "failures (i.e failures that are temporary and will likely go away "
97           "on retrying; like a temporary connection failure) and an error "
98           "message is printed instead. Note that this flag just controls "
99           "whether abort() is called or not. It does not control whether the "
100           "test is retried in case of transient failures (and currently the "
101           "interop tests are not retried even if this flag is set to true)");
102 ABSL_FLAG(int32_t, soak_iterations, 1000,
103           "The number of iterations to use for the two soak tests; rpc_soak "
104           "and channel_soak.");
105 ABSL_FLAG(int32_t, soak_max_failures, 0,
106           "The number of iterations in soak tests that are allowed to fail "
107           "(either due to non-OK status code or exceeding the "
108           "per-iteration max acceptable latency).");
109 ABSL_FLAG(int32_t, soak_per_iteration_max_acceptable_latency_ms, 0,
110           "The number of milliseconds a single iteration in the two soak "
111           "tests (rpc_soak and channel_soak) should take.");
112 ABSL_FLAG(int32_t, soak_overall_timeout_seconds, 0,
113           "The overall number of seconds after which a soak test should "
114           "stop and fail, if the desired number of iterations have not yet "
115           "completed.");
116 ABSL_FLAG(int32_t, soak_min_time_ms_between_rpcs, 0,
117           "The minimum time in milliseconds between consecutive RPCs in a "
118           "soak test (rpc_soak or channel_soak), useful for limiting QPS");
119 ABSL_FLAG(int32_t, iteration_interval, 10,
120           "The interval in seconds between rpcs. This is used by "
121           "long_connection test");
122 ABSL_FLAG(
123     int32_t, soak_request_size, 271828,
124     "The request size in a soak RPC. "
125     "The default value is set based on the interop large unary test case.");
126 ABSL_FLAG(
127     int32_t, soak_response_size, 314159,
128     "The response size in a soak RPC. "
129     "The default value is set based on the interop large unary test case.");
130 ABSL_FLAG(std::string, additional_metadata, "",
131           "Additional metadata to send in each request, as a "
132           "semicolon-separated list of key:value pairs.");
133 ABSL_FLAG(
134     bool, log_metadata_and_status, false,
135     "If set to 'true', will print received initial and trailing metadata, "
136     "grpc-status and error message to the console, in a stable format.");
137 ABSL_FLAG(std::string, service_config_json, "",
138           "Disables service config lookups and sets the provided string as the "
139           "default service config");
140 
141 using grpc::testing::CreateChannelForTestCase;
142 using grpc::testing::GetServiceAccountJsonKey;
143 using grpc::testing::UpdateActions;
144 
145 namespace {
146 
147 // Parse the contents of FLAGS_additional_metadata into a map. Allow
148 // alphanumeric characters and dashes in keys, and any character but semicolons
149 // in values. Convert keys to lowercase. On failure, log an error and return
150 // false.
151 absl::StatusOr<std::multimap<std::string, std::string>>
ParseAdditionalMetadataFlag(const std::string & flag)152 ParseAdditionalMetadataFlag(const std::string& flag) {
153   std::multimap<std::string, std::string> additional_metadata;
154   if (flag.empty()) {
155     return additional_metadata;
156   }
157   size_t start_pos = 0;
158   while (start_pos < flag.length()) {
159     size_t colon_pos = flag.find(':', start_pos);
160     if (colon_pos == std::string::npos) {
161       return absl::InvalidArgumentError(
162           "Couldn't parse metadata flag: extra characters at end of flag");
163     }
164     size_t semicolon_pos = flag.find(';', colon_pos);
165 
166     std::string key = flag.substr(start_pos, colon_pos - start_pos);
167     std::string value =
168         flag.substr(colon_pos + 1, semicolon_pos - colon_pos - 1);
169 
170     constexpr char alphanum_and_hyphen[] =
171         "-0123456789"
172         "abcdefghijklmnopqrstuvwxyz"
173         "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
174     if (key.find_first_not_of(alphanum_and_hyphen) != std::string::npos) {
175       return absl::InvalidArgumentError(absl::StrCat(
176           "Couldn't parse metadata flag: key contains characters other "
177           "than alphanumeric and hyphens: ",
178           key));
179     }
180 
181     // Convert to lowercase.
182     for (char& c : key) {
183       if (c >= 'A' && c <= 'Z') {
184         c += ('a' - 'A');
185       }
186     }
187 
188     LOG(INFO) << "Adding additional metadata with key " << key << " and value "
189               << value;
190     additional_metadata.insert({key, value});
191 
192     if (semicolon_pos == std::string::npos) {
193       break;
194     } else {
195       start_pos = semicolon_pos + 1;
196     }
197   }
198 
199   return additional_metadata;
200 }
201 
202 }  // namespace
203 
main(int argc,char ** argv)204 int main(int argc, char** argv) {
205   grpc::testing::TestEnvironment env(&argc, argv);
206   grpc::testing::InitTest(&argc, &argv, true);
207   LOG(INFO) << "Testing these cases: " << absl::GetFlag(FLAGS_test_case);
208   int ret = 0;
209 
210   std::string test_case = absl::GetFlag(FLAGS_test_case);
211   auto additional_metadata =
212       ParseAdditionalMetadataFlag(absl::GetFlag(FLAGS_additional_metadata));
213   if (!additional_metadata.ok()) {
214     LOG(ERROR) << additional_metadata.status().message();
215     return 1;
216   }
217   grpc::testing::ChannelCreationFunc channel_creation_func =
218       [test_case, &additional_metadata](grpc::ChannelArguments arguments) {
219         std::vector<std::unique_ptr<
220             grpc::experimental::ClientInterceptorFactoryInterface>>
221             factories;
222         if (!additional_metadata->empty()) {
223           factories.emplace_back(
224               new grpc::testing::AdditionalMetadataInterceptorFactory(
225                   *additional_metadata));
226         }
227         if (absl::GetFlag(FLAGS_log_metadata_and_status)) {
228           factories.emplace_back(
229               new grpc::testing::MetadataAndStatusLoggerInterceptorFactory());
230         }
231         std::string service_config_json =
232             absl::GetFlag(FLAGS_service_config_json);
233         if (!service_config_json.empty()) {
234           arguments.SetServiceConfigJSON(service_config_json);
235         }
236         return CreateChannelForTestCase(test_case, std::move(factories),
237                                         arguments);
238       };
239 
240   grpc::testing::InteropClient client(
241       channel_creation_func, true,
242       absl::GetFlag(FLAGS_do_not_abort_on_transient_failures));
243 
244   std::unordered_map<std::string, std::function<bool()>> actions;
245   actions["empty_unary"] =
246       std::bind(&grpc::testing::InteropClient::DoEmpty, &client);
247   actions["large_unary"] =
248       std::bind(&grpc::testing::InteropClient::DoLargeUnary, &client);
249   actions["server_compressed_unary"] = std::bind(
250       &grpc::testing::InteropClient::DoServerCompressedUnary, &client);
251   actions["client_compressed_unary"] = std::bind(
252       &grpc::testing::InteropClient::DoClientCompressedUnary, &client);
253   actions["client_streaming"] =
254       std::bind(&grpc::testing::InteropClient::DoRequestStreaming, &client);
255   actions["server_streaming"] =
256       std::bind(&grpc::testing::InteropClient::DoResponseStreaming, &client);
257   actions["server_compressed_streaming"] = std::bind(
258       &grpc::testing::InteropClient::DoServerCompressedStreaming, &client);
259   actions["client_compressed_streaming"] = std::bind(
260       &grpc::testing::InteropClient::DoClientCompressedStreaming, &client);
261   actions["slow_consumer"] = std::bind(
262       &grpc::testing::InteropClient::DoResponseStreamingWithSlowConsumer,
263       &client);
264   actions["half_duplex"] =
265       std::bind(&grpc::testing::InteropClient::DoHalfDuplex, &client);
266   actions["ping_pong"] =
267       std::bind(&grpc::testing::InteropClient::DoPingPong, &client);
268   actions["cancel_after_begin"] =
269       std::bind(&grpc::testing::InteropClient::DoCancelAfterBegin, &client);
270   actions["cancel_after_first_response"] = std::bind(
271       &grpc::testing::InteropClient::DoCancelAfterFirstResponse, &client);
272   actions["timeout_on_sleeping_server"] = std::bind(
273       &grpc::testing::InteropClient::DoTimeoutOnSleepingServer, &client);
274   actions["empty_stream"] =
275       std::bind(&grpc::testing::InteropClient::DoEmptyStream, &client);
276   actions["pick_first_unary"] =
277       std::bind(&grpc::testing::InteropClient::DoPickFirstUnary, &client);
278   actions["orca_per_rpc"] =
279       std::bind(&grpc::testing::InteropClient::DoOrcaPerRpc, &client);
280   actions["orca_oob"] =
281       std::bind(&grpc::testing::InteropClient::DoOrcaOob, &client);
282   if (absl::GetFlag(FLAGS_use_tls)) {
283     actions["compute_engine_creds"] =
284         std::bind(&grpc::testing::InteropClient::DoComputeEngineCreds, &client,
285                   absl::GetFlag(FLAGS_default_service_account),
286                   absl::GetFlag(FLAGS_oauth_scope));
287     actions["jwt_token_creds"] =
288         std::bind(&grpc::testing::InteropClient::DoJwtTokenCreds, &client,
289                   GetServiceAccountJsonKey());
290     actions["oauth2_auth_token"] =
291         std::bind(&grpc::testing::InteropClient::DoOauth2AuthToken, &client,
292                   absl::GetFlag(FLAGS_default_service_account),
293                   absl::GetFlag(FLAGS_oauth_scope));
294     actions["per_rpc_creds"] =
295         std::bind(&grpc::testing::InteropClient::DoPerRpcCreds, &client,
296                   GetServiceAccountJsonKey());
297   }
298   if (absl::GetFlag(FLAGS_custom_credentials_type) ==
299       "google_default_credentials") {
300     actions["google_default_credentials"] =
301         std::bind(&grpc::testing::InteropClient::DoGoogleDefaultCredentials,
302                   &client, absl::GetFlag(FLAGS_default_service_account));
303   }
304   actions["status_code_and_message"] =
305       std::bind(&grpc::testing::InteropClient::DoStatusWithMessage, &client);
306   actions["special_status_message"] =
307       std::bind(&grpc::testing::InteropClient::DoSpecialStatusMessage, &client);
308   actions["custom_metadata"] =
309       std::bind(&grpc::testing::InteropClient::DoCustomMetadata, &client);
310   actions["unimplemented_method"] =
311       std::bind(&grpc::testing::InteropClient::DoUnimplementedMethod, &client);
312   actions["unimplemented_service"] =
313       std::bind(&grpc::testing::InteropClient::DoUnimplementedService, &client);
314   actions["channel_soak"] = std::bind(
315       &grpc::testing::InteropClient::DoChannelSoakTest, &client,
316       absl::GetFlag(FLAGS_server_host), absl::GetFlag(FLAGS_soak_iterations),
317       absl::GetFlag(FLAGS_soak_max_failures),
318       absl::GetFlag(FLAGS_soak_per_iteration_max_acceptable_latency_ms),
319       absl::GetFlag(FLAGS_soak_min_time_ms_between_rpcs),
320       absl::GetFlag(FLAGS_soak_overall_timeout_seconds),
321       absl::GetFlag(FLAGS_soak_request_size),
322       absl::GetFlag(FLAGS_soak_response_size));
323   actions["rpc_soak"] = std::bind(
324       &grpc::testing::InteropClient::DoRpcSoakTest, &client,
325       absl::GetFlag(FLAGS_server_host), absl::GetFlag(FLAGS_soak_iterations),
326       absl::GetFlag(FLAGS_soak_max_failures),
327       absl::GetFlag(FLAGS_soak_per_iteration_max_acceptable_latency_ms),
328       absl::GetFlag(FLAGS_soak_min_time_ms_between_rpcs),
329       absl::GetFlag(FLAGS_soak_overall_timeout_seconds),
330       absl::GetFlag(FLAGS_soak_request_size),
331       absl::GetFlag(FLAGS_soak_response_size));
332   actions["long_lived_channel"] =
333       std::bind(&grpc::testing::InteropClient::DoLongLivedChannelTest, &client,
334                 absl::GetFlag(FLAGS_soak_iterations),
335                 absl::GetFlag(FLAGS_iteration_interval));
336 
337   UpdateActions(&actions);
338 
339   if (absl::GetFlag(FLAGS_test_case) == "all") {
340     for (const auto& action : actions) {
341       action.second();
342     }
343   } else if (actions.find(absl::GetFlag(FLAGS_test_case)) != actions.end()) {
344     actions.find(absl::GetFlag(FLAGS_test_case))->second();
345   } else {
346     std::string test_cases;
347     for (const auto& action : actions) {
348       if (!test_cases.empty()) test_cases += "\n";
349       test_cases += action.first;
350     }
351     LOG(ERROR) << "Unsupported test case " << absl::GetFlag(FLAGS_test_case)
352                << ". Valid options are\n"
353                << test_cases;
354     ret = 1;
355   }
356 
357   return ret;
358 }
359