[gRPC CLI] Add a --channel_args string flag (a=b,c=d).

PiperOrigin-RevId: 625783208
pull/36350/head
Alisha Nanda 10 months ago committed by Copybara-Service
parent fceacd7a2c
commit f7962578c1
  1. 22
      test/cpp/util/grpc_tool.cc
  2. 42
      test/cpp/util/grpc_tool_test.cc

@ -25,9 +25,12 @@
#include <sstream>
#include <string>
#include <thread>
#include <utility>
#include "absl/flags/flag.h"
#include "absl/memory/memory.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include <grpc/grpc.h>
#include <grpc/support/port_platform.h>
@ -82,6 +85,9 @@ ABSL_FLAG(
int, max_recv_msg_size, 0,
"Specify the max receive message size in bytes for all RPCs. -1 indicates "
"unlimited. The default value of 0 means to use the gRPC default.");
ABSL_FLAG(std::string, channel_args, "",
"Comma-separated list of key=value gRPC ChannelArgs to apply "
"(a=b,c=d,...). Values may be integers or strings.");
namespace grpc {
namespace testing {
@ -244,6 +250,20 @@ std::shared_ptr<grpc::Channel> CreateCliChannel(
// See |GRPC_ARG_MAX_METADATA_SIZE| in |grpc_types.h|.
// Set to large enough size (10M) that should work for most use cases.
args.SetInt(GRPC_ARG_MAX_METADATA_SIZE, 10 * 1024 * 1024);
// Extend channel args according to flag --channel_args.
const auto flag = absl::GetFlag(FLAGS_channel_args);
for (absl::string_view arg :
absl::StrSplit(flag, ',', absl::SkipWhitespace())) {
std::pair<std::string, std::string> kv =
absl::StrSplit(arg, absl::MaxSplits('=', 1), absl::SkipWhitespace());
int ival;
if (absl::SimpleAtoi(kv.second, &ival)) {
args.SetInt(kv.first, ival);
} else if (!kv.second.empty()) {
args.SetString(kv.first, kv.second);
}
}
return grpc::CreateCustomChannel(server_address, cred.GetCredentials(), args);
}

@ -133,6 +133,7 @@ ABSL_DECLARE_FLAG(std::string, proto_path);
ABSL_DECLARE_FLAG(std::string, default_service_config);
ABSL_DECLARE_FLAG(double, timeout);
ABSL_DECLARE_FLAG(int, max_recv_msg_size);
ABSL_DECLARE_FLAG(std::string, channel_args);
namespace grpc {
namespace testing {
@ -1299,11 +1300,11 @@ TEST_F(GrpcToolTest, CallMaxRecvMessageSizeSmall) {
// Set max_recv_msg_size to 4 which is not enough.
absl::SetFlag(&FLAGS_max_recv_msg_size, 4);
// This should fail.
EXPECT_FALSE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
absl::SetFlag(&FLAGS_max_recv_msg_size, 0);
// No output expected.
EXPECT_TRUE(0 == output_stream.tellp());
ShutdownServer();
@ -1320,10 +1321,11 @@ TEST_F(GrpcToolTest, CallMaxRecvMessageSizeEnough) {
// Set max_recv_msg_size to a large enough number.
absl::SetFlag(&FLAGS_max_recv_msg_size, 1024 * 1024);
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
absl::SetFlag(&FLAGS_max_recv_msg_size, 0);
// Expected output: "message: \"Hello\""
EXPECT_TRUE(nullptr !=
strstr(output_stream.str().c_str(), "message: \"Hello\""));
@ -1341,10 +1343,10 @@ TEST_F(GrpcToolTest, CallMaxRecvMessageSizeDefault) {
// Set max_recv_msg_size to gRPC default, which should suffice.
absl::SetFlag(&FLAGS_max_recv_msg_size, 0);
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
// Expected output: "message: \"Hello\""
EXPECT_TRUE(nullptr !=
strstr(output_stream.str().c_str(), "message: \"Hello\""));
@ -1362,16 +1364,46 @@ TEST_F(GrpcToolTest, CallMaxRecvMessageSizeUnlimited) {
// Set max_recv_msg_size to unlimited (-1), which should work.
absl::SetFlag(&FLAGS_max_recv_msg_size, -1);
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
absl::SetFlag(&FLAGS_max_recv_msg_size, 0);
// Expected output: "message: \"Hello\""
EXPECT_TRUE(nullptr !=
strstr(output_stream.str().c_str(), "message: \"Hello\""));
ShutdownServer();
}
// This duplicates CallMaxRecvMessageSizeSmall, but using --channel_args.
TEST_F(GrpcToolTest, CallWithChannelArgs) {
std::stringstream output_stream;
const std::string server_address = SetUpServer();
// Test input "grpc_cli call localhost:10000 Echo "message: 'Hello'
// --channel_args=grpc.max_receive_message_length=4"
const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
"grpc.testing.EchoTestService.Echo",
"message: 'Hello'"};
// Set max receive size to 4 bytes which is not enough.
absl::SetFlag(&FLAGS_channel_args,
"x=y,grpc.max_receive_message_length=4,z=1");
EXPECT_FALSE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
absl::SetFlag(&FLAGS_channel_args, "");
// Then try again with large enough size, which should succeed.
absl::SetFlag(&FLAGS_channel_args,
",grpc.max_receive_message_length=1000000,");
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
absl::SetFlag(&FLAGS_channel_args, "");
ShutdownServer();
}
TEST_F(GrpcToolTest, ListCommandOverrideSslHostName) {
const std::string server_address = SetUpServer(true);

Loading…
Cancel
Save