diff options
Diffstat (limited to 'test/cpp')
-rw-r--r-- | test/cpp/end2end/generic_end2end_test.cc | 2 | ||||
-rw-r--r-- | test/cpp/end2end/server_builder_plugin_test.cc | 1 | ||||
-rw-r--r-- | test/cpp/qps/client_async.cc | 1 | ||||
-rw-r--r-- | test/cpp/qps/client_sync.cc | 1 | ||||
-rw-r--r-- | test/cpp/qps/driver.cc | 1 | ||||
-rw-r--r-- | test/cpp/qps/parse_json.cc | 2 | ||||
-rw-r--r-- | test/cpp/qps/parse_json.h | 2 | ||||
-rw-r--r-- | test/cpp/qps/perf_db_client.cc | 140 | ||||
-rw-r--r-- | test/cpp/qps/perf_db_client.h | 113 | ||||
-rw-r--r-- | test/cpp/qps/qps_json_driver.cc | 2 | ||||
-rw-r--r-- | test/cpp/qps/report.h | 1 | ||||
-rw-r--r-- | test/cpp/qps/server_async.cc | 2 | ||||
-rw-r--r-- | test/cpp/qps/server_sync.cc | 1 | ||||
-rw-r--r-- | test/cpp/util/byte_buffer_proto_helper.h | 2 | ||||
-rw-r--r-- | test/cpp/util/cli_call.cc | 2 | ||||
-rw-r--r-- | test/cpp/util/grpc_cli.cc | 129 | ||||
-rw-r--r-- | test/cpp/util/proto_file_parser.cc | 178 | ||||
-rw-r--r-- | test/cpp/util/proto_file_parser.h | 85 |
18 files changed, 351 insertions, 314 deletions
diff --git a/test/cpp/end2end/generic_end2end_test.cc b/test/cpp/end2end/generic_end2end_test.cc index d0cf6aea9d..57efa5fa17 100644 --- a/test/cpp/end2end/generic_end2end_test.cc +++ b/test/cpp/end2end/generic_end2end_test.cc @@ -38,7 +38,7 @@ #include <grpc++/create_channel.h> #include <grpc++/generic/async_generic_service.h> #include <grpc++/generic/generic_stub.h> -#include <grpc++/impl/proto_utils.h> +#include <grpc++/impl/codegen/proto_utils.h> #include <grpc++/server.h> #include <grpc++/server_builder.h> #include <grpc++/server_context.h> diff --git a/test/cpp/end2end/server_builder_plugin_test.cc b/test/cpp/end2end/server_builder_plugin_test.cc index 8a74621e5a..1c1095087a 100644 --- a/test/cpp/end2end/server_builder_plugin_test.cc +++ b/test/cpp/end2end/server_builder_plugin_test.cc @@ -61,6 +61,7 @@ class TestServerBuilderPlugin : public ServerBuilderPlugin { init_server_is_called_ = false; finish_is_called_ = false; change_arguments_is_called_ = false; + register_service_ = false; } grpc::string name() GRPC_OVERRIDE { return PLUGIN_NAME; } diff --git a/test/cpp/qps/client_async.cc b/test/cpp/qps/client_async.cc index c32160a7d4..6ad4c320b5 100644 --- a/test/cpp/qps/client_async.cc +++ b/test/cpp/qps/client_async.cc @@ -42,7 +42,6 @@ #include <thread> #include <vector> -#include <gflags/gflags.h> #include <grpc++/alarm.h> #include <grpc++/channel.h> #include <grpc++/client_context.h> diff --git a/test/cpp/qps/client_sync.cc b/test/cpp/qps/client_sync.cc index fb161f70ee..c88e95b80e 100644 --- a/test/cpp/qps/client_sync.cc +++ b/test/cpp/qps/client_sync.cc @@ -40,7 +40,6 @@ #include <thread> #include <vector> -#include <gflags/gflags.h> #include <grpc++/channel.h> #include <grpc++/client_context.h> #include <grpc++/server.h> diff --git a/test/cpp/qps/driver.cc b/test/cpp/qps/driver.cc index 04b2b453f9..57d8c22a95 100644 --- a/test/cpp/qps/driver.cc +++ b/test/cpp/qps/driver.cc @@ -43,7 +43,6 @@ #include <grpc/support/alloc.h> #include <grpc/support/host_port.h> #include <grpc/support/log.h> -#include <gtest/gtest.h> #include "src/core/lib/support/env.h" #include "src/proto/grpc/testing/services.grpc.pb.h" diff --git a/test/cpp/qps/parse_json.cc b/test/cpp/qps/parse_json.cc index df7a62f0a0..a90bf6153c 100644 --- a/test/cpp/qps/parse_json.cc +++ b/test/cpp/qps/parse_json.cc @@ -31,8 +31,6 @@ * */ -#include <grpc++/support/config_protobuf.h> - #include "test/cpp/qps/parse_json.h" #include <string> diff --git a/test/cpp/qps/parse_json.h b/test/cpp/qps/parse_json.h index 4b8ca79f21..42d7d22c53 100644 --- a/test/cpp/qps/parse_json.h +++ b/test/cpp/qps/parse_json.h @@ -34,8 +34,8 @@ #ifndef TEST_QPS_PARSE_JSON_H #define TEST_QPS_PARSE_JSON_H +#include <grpc++/impl/codegen/config_protobuf.h> #include <grpc++/support/config.h> -#include <grpc++/support/config_protobuf.h> namespace grpc { namespace testing { diff --git a/test/cpp/qps/perf_db_client.cc b/test/cpp/qps/perf_db_client.cc deleted file mode 100644 index 98efd8c3e3..0000000000 --- a/test/cpp/qps/perf_db_client.cc +++ /dev/null @@ -1,140 +0,0 @@ -/* - * - * Copyright 2015, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include "test/cpp/qps/perf_db_client.h" - -namespace grpc { -namespace testing { - -// sets the client and server config information -void PerfDbClient::setConfigs(const ClientConfig& client_config, - const ServerConfig& server_config) { - client_config_ = client_config; - server_config_ = server_config; -} - -// sets the QPS -void PerfDbClient::setQps(double qps) { qps_ = qps; } - -// sets the QPS per core -void PerfDbClient::setQpsPerCore(double qps_per_core) { - qps_per_core_ = qps_per_core; -} - -// sets the 50th, 90th, 95th, 99th and 99.9th percentile latency -void PerfDbClient::setLatencies(double perc_lat_50, double perc_lat_90, - double perc_lat_95, double perc_lat_99, - double perc_lat_99_point_9) { - perc_lat_50_ = perc_lat_50; - perc_lat_90_ = perc_lat_90; - perc_lat_95_ = perc_lat_95; - perc_lat_99_ = perc_lat_99; - perc_lat_99_point_9_ = perc_lat_99_point_9; -} - -// sets the server and client, user and system times -void PerfDbClient::setTimes(double server_system_time, double server_user_time, - double client_system_time, - double client_user_time) { - server_system_time_ = server_system_time; - server_user_time_ = server_user_time; - client_system_time_ = client_system_time; - client_user_time_ = client_user_time; -} - -// sends the data to the performance database server -bool PerfDbClient::sendData(std::string hashed_id, std::string test_name, - std::string sys_info, std::string tag) { - // Data record request object - SingleUserRecordRequest single_user_record_request; - - // setting access token, name of the test and the system information - single_user_record_request.set_hashed_id(hashed_id); - single_user_record_request.set_test_name(test_name); - single_user_record_request.set_sys_info(sys_info); - single_user_record_request.set_tag(tag); - - // setting configs - *(single_user_record_request.mutable_client_config()) = client_config_; - *(single_user_record_request.mutable_server_config()) = server_config_; - - Metrics* metrics = single_user_record_request.mutable_metrics(); - - // setting metrcs in data record request - if (qps_ != DBL_MIN) { - metrics->set_qps(qps_); - } - if (qps_per_core_ != DBL_MIN) { - metrics->set_qps_per_core(qps_per_core_); - } - if (perc_lat_50_ != DBL_MIN) { - metrics->set_perc_lat_50(perc_lat_50_); - } - if (perc_lat_90_ != DBL_MIN) { - metrics->set_perc_lat_90(perc_lat_90_); - } - if (perc_lat_95_ != DBL_MIN) { - metrics->set_perc_lat_95(perc_lat_95_); - } - if (perc_lat_99_ != DBL_MIN) { - metrics->set_perc_lat_99(perc_lat_99_); - } - if (perc_lat_99_point_9_ != DBL_MIN) { - metrics->set_perc_lat_99_point_9(perc_lat_99_point_9_); - } - if (server_system_time_ != DBL_MIN) { - metrics->set_server_system_time(server_system_time_); - } - if (server_user_time_ != DBL_MIN) { - metrics->set_server_user_time(server_user_time_); - } - if (client_system_time_ != DBL_MIN) { - metrics->set_client_system_time(client_system_time_); - } - if (client_user_time_ != DBL_MIN) { - metrics->set_client_user_time(client_user_time_); - } - - SingleUserRecordReply single_user_record_reply; - ClientContext context; - - Status status = stub_->RecordSingleClientData( - &context, single_user_record_request, &single_user_record_reply); - if (status.ok()) { - return true; // data sent to database successfully - } else { - return false; // error in data sending - } -} -} // testing -} // grpc diff --git a/test/cpp/qps/perf_db_client.h b/test/cpp/qps/perf_db_client.h deleted file mode 100644 index b74c70d86b..0000000000 --- a/test/cpp/qps/perf_db_client.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * - * Copyright 2015, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include <cfloat> -#include <iostream> -#include <memory> -#include <string> - -#include <grpc++/channel.h> -#include <grpc++/client_context.h> -#include <grpc++/create_channel.h> -#include <grpc++/security/credentials.h> -#include <grpc++/support/channel_arguments.h> -#include <grpc/grpc.h> -#include "src/proto/grpc/testing/perf_db.grpc.pb.h" - -namespace grpc { -namespace testing { - -// Manages data sending to performance database server -class PerfDbClient { - public: - PerfDbClient() { - qps_ = DBL_MIN; - qps_per_core_ = DBL_MIN; - perc_lat_50_ = DBL_MIN; - perc_lat_90_ = DBL_MIN; - perc_lat_95_ = DBL_MIN; - perc_lat_99_ = DBL_MIN; - perc_lat_99_point_9_ = DBL_MIN; - server_system_time_ = DBL_MIN; - server_user_time_ = DBL_MIN; - client_system_time_ = DBL_MIN; - client_user_time_ = DBL_MIN; - } - - void init(std::shared_ptr<Channel> channel) { - stub_ = PerfDbTransfer::NewStub(channel); - } - - ~PerfDbClient() {} - - // sets the client and server config information - void setConfigs(const ClientConfig& client_config, - const ServerConfig& server_config); - - // sets the qps - void setQps(double qps); - - // sets the qps per core - void setQpsPerCore(double qps_per_core); - - // sets the 50th, 90th, 95th, 99th and 99.9th percentile latency - void setLatencies(double perc_lat_50, double perc_lat_90, double perc_lat_95, - double perc_lat_99, double perc_lat_99_point_9); - - // sets the server and client, user and system times - void setTimes(double server_system_time, double server_user_time, - double client_system_time, double client_user_time); - - // sends the data to the performance database server - bool sendData(std::string hashed_id, std::string test_name, - std::string sys_info, std::string tag); - - private: - std::unique_ptr<PerfDbTransfer::Stub> stub_; - ClientConfig client_config_; - ServerConfig server_config_; - double qps_; - double qps_per_core_; - double perc_lat_50_; - double perc_lat_90_; - double perc_lat_95_; - double perc_lat_99_; - double perc_lat_99_point_9_; - double server_system_time_; - double server_user_time_; - double client_system_time_; - double client_user_time_; -}; - -} // namespace testing -} // namespace grpc diff --git a/test/cpp/qps/qps_json_driver.cc b/test/cpp/qps/qps_json_driver.cc index d7642f0e1e..f5d739f893 100644 --- a/test/cpp/qps/qps_json_driver.cc +++ b/test/cpp/qps/qps_json_driver.cc @@ -34,7 +34,7 @@ #include <memory> #include <set> -#include <grpc++/support/config_protobuf.h> +#include <grpc++/impl/codegen/config_protobuf.h> #include <gflags/gflags.h> #include <grpc/support/log.h> diff --git a/test/cpp/qps/report.h b/test/cpp/qps/report.h index 8f04d84124..39cf498e7b 100644 --- a/test/cpp/qps/report.h +++ b/test/cpp/qps/report.h @@ -41,7 +41,6 @@ #include <grpc++/support/config.h> #include "test/cpp/qps/driver.h" -#include "test/cpp/qps/perf_db_client.h" namespace grpc { namespace testing { diff --git a/test/cpp/qps/server_async.cc b/test/cpp/qps/server_async.cc index 1eddb1dc1d..c9954d0d02 100644 --- a/test/cpp/qps/server_async.cc +++ b/test/cpp/qps/server_async.cc @@ -37,7 +37,6 @@ #include <mutex> #include <thread> -#include <gflags/gflags.h> #include <grpc++/generic/async_generic_service.h> #include <grpc++/security/server_credentials.h> #include <grpc++/server.h> @@ -48,7 +47,6 @@ #include <grpc/support/alloc.h> #include <grpc/support/host_port.h> #include <grpc/support/log.h> -#include <gtest/gtest.h> #include "src/proto/grpc/testing/services.grpc.pb.h" #include "test/core/util/test_config.h" diff --git a/test/cpp/qps/server_sync.cc b/test/cpp/qps/server_sync.cc index 9e64f470bf..c774985bfa 100644 --- a/test/cpp/qps/server_sync.cc +++ b/test/cpp/qps/server_sync.cc @@ -33,7 +33,6 @@ #include <thread> -#include <gflags/gflags.h> #include <grpc++/security/server_credentials.h> #include <grpc++/server.h> #include <grpc++/server_builder.h> diff --git a/test/cpp/util/byte_buffer_proto_helper.h b/test/cpp/util/byte_buffer_proto_helper.h index 42cea59e33..007723c691 100644 --- a/test/cpp/util/byte_buffer_proto_helper.h +++ b/test/cpp/util/byte_buffer_proto_helper.h @@ -36,8 +36,8 @@ #include <memory> +#include <grpc++/impl/codegen/config_protobuf.h> #include <grpc++/support/byte_buffer.h> -#include <grpc++/support/config_protobuf.h> namespace grpc { namespace testing { diff --git a/test/cpp/util/cli_call.cc b/test/cpp/util/cli_call.cc index 99fad7f2fe..98b9d930d6 100644 --- a/test/cpp/util/cli_call.cc +++ b/test/cpp/util/cli_call.cc @@ -86,7 +86,6 @@ Status CliCall::Call(std::shared_ptr<grpc::Channel> channel, cq.Next(&got_tag, &ok); if (!ok) { std::cout << "Failed to read response." << std::endl; - return Status(StatusCode::INTERNAL, "Failed to read response"); } grpc::Status status; call->Finish(&status, tag(5)); @@ -103,6 +102,7 @@ Status CliCall::Call(std::shared_ptr<grpc::Channel> channel, slices[i].size()); } } + *server_initial_metadata = ctx.GetServerInitialMetadata(); *server_trailing_metadata = ctx.GetServerTrailingMetadata(); return status; diff --git a/test/cpp/util/grpc_cli.cc b/test/cpp/util/grpc_cli.cc index 68cf4114a8..c52e48bae6 100644 --- a/test/cpp/util/grpc_cli.cc +++ b/test/cpp/util/grpc_cli.cc @@ -32,32 +32,33 @@ */ /* - A command line tool to talk to any grpc server. + A command line tool to talk to a grpc server. Example of talking to grpc interop server: - 1. Prepare request binary file: - a. create a text file input.txt, containing the following: - response_size: 10 - payload: { - body: "hello world" - } - b. under grpc/ run - protoc --proto_path=src/proto/grpc/testing/ \ - --encode=grpc.testing.SimpleRequest - src/proto/grpc/testing/messages.proto \ - < input.txt > input.bin - 2. Start a server - make interop_server && bins/opt/interop_server --port=50051 - 3. Run the tool - make grpc_cli && bins/opt/grpc_cli call localhost:50051 \ - /grpc.testing.TestService/UnaryCall --enable_ssl=false \ - --input_binary_file=input.bin --output_binary_file=output.bin - 4. Decode response - protoc --proto_path=src/proto/grpc/testing/ \ - --decode=grpc.testing.SimpleResponse src/proto/grpc/testing/messages.proto \ - < output.bin > output.txt - 5. Now the text form of response should be in output.txt - Optionally, metadata can be passed to server via flag --metadata, e.g. - --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2" + grpc_cli call localhost:50051 UnaryCall src/proto/grpc/testing/test.proto \ + "response_size:10" --enable_ssl=false + + Options: + 1. --proto_path, if your proto file is not under current working directory, + use this flag to provide a search root. It should work similar to the + counterpart in protoc. + 2. --metadata specifies metadata to be sent to the server, such as: + --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2" + 3. --enable_ssl, whether to use tls. + 4. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call + 3. --input_binary_file, a file containing the serialized request. The file + can be generated by calling something like: + protoc --proto_path=src/proto/grpc/testing/ \ + --encode=grpc.testing.SimpleRequest \ + src/proto/grpc/testing/messages.proto \ + < input.txt > input.bin + If this is used and no proto file is provided in the argument list, the + method string has to be exact in the form of /package.service/method. + 4. --output_binary_file, a file to write binary format response into, it can + be later decoded using protoc: + protoc --proto_path=src/proto/grpc/testing/ \ + --decode=grpc.testing.SimpleResponse \ + src/proto/grpc/testing/messages.proto \ + < output.bin > output.txt */ #include <fstream> @@ -72,6 +73,7 @@ #include <grpc/grpc.h> #include "test/cpp/util/cli_call.h" +#include "test/cpp/util/proto_file_parser.h" #include "test/cpp/util/string_ref_helper.h" #include "test/cpp/util/test_config.h" @@ -79,10 +81,11 @@ DEFINE_bool(enable_ssl, true, "Whether to use ssl/tls."); DEFINE_bool(use_auth, false, "Whether to create default google credentials."); DEFINE_string(input_binary_file, "", "Path to input file containing serialized request."); -DEFINE_string(output_binary_file, "output.bin", +DEFINE_string(output_binary_file, "", "Path to output file to write serialized response."); DEFINE_string(metadata, "", "Metadata to send to server, in the form of key1:val1:key2:val2"); +DEFINE_string(proto_path, ".", "Path to look for the proto file."); void ParseMetadataFlag( std::multimap<grpc::string, grpc::string>* client_metadata) { @@ -126,28 +129,51 @@ void PrintMetadata(const T& m, const grpc::string& message) { int main(int argc, char** argv) { grpc::testing::InitTest(&argc, &argv, true); - if (argc < 4 || grpc::string(argv[1]) != "call") { - std::cout << "Usage: grpc_cli call server_host:port full_method_string\n" - << "Example: grpc_cli call service.googleapis.com " - << "/grpc.testing.TestService/UnaryCall " - << "--input_binary_file=input.bin --output_binary_file=output.bin" - << std::endl; + if (argc < 4 || argc == 5 || grpc::string(argv[1]) != "call") { + std::cout << "Usage: grpc_cli call server_host:port method_name " + << "[proto file] [text format request] [<options>]" << std::endl; } + + grpc::string file_name; + grpc::string request_text; grpc::string server_address(argv[2]); - // TODO(yangg) basic check of method string - grpc::string method(argv[3]); + grpc::string method_name(argv[3]); + std::unique_ptr<grpc::testing::ProtoFileParser> parser; + grpc::string serialized_request_proto; - if (FLAGS_input_binary_file.empty()) { - std::cout << "Missing --input_binary_file for serialized request." - << std::endl; + if (argc == 6) { + file_name = argv[4]; + // TODO(yangg) read from stdin as well? + request_text = argv[5]; + } + + if (request_text.empty() && FLAGS_input_binary_file.empty()) { + std::cout << "Missing input. Use text format input or " + << "--input_binary_file for serialized request" << std::endl; return 1; + } else if (!request_text.empty()) { + parser.reset(new grpc::testing::ProtoFileParser(FLAGS_proto_path, file_name, + method_name)); + method_name = parser->GetFullMethodName(); + if (parser->HasError()) { + return 1; + } } - std::cout << "connecting to " << server_address << std::endl; - std::ifstream input_file(FLAGS_input_binary_file, - std::ios::in | std::ios::binary); - std::stringstream input_stream; - input_stream << input_file.rdbuf(); + if (parser) { + serialized_request_proto = + parser->GetSerializedProto(request_text, true /* is_request */); + if (parser->HasError()) { + return 1; + } + } else if (!FLAGS_input_binary_file.empty()) { + std::ifstream input_file(FLAGS_input_binary_file, + std::ios::in | std::ios::binary); + std::stringstream input_stream; + input_stream << input_file.rdbuf(); + serialized_request_proto = input_stream.str(); + } + std::cout << "connecting to " << server_address << std::endl; std::shared_ptr<grpc::ChannelCredentials> creds; if (!FLAGS_enable_ssl) { @@ -162,25 +188,34 @@ int main(int argc, char** argv) { std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(server_address, creds); - grpc::string response; + grpc::string serialized_response_proto; std::multimap<grpc::string, grpc::string> client_metadata; std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata, server_trailing_metadata; ParseMetadataFlag(&client_metadata); PrintMetadata(client_metadata, "Sending client initial metadata:"); grpc::Status s = grpc::testing::CliCall::Call( - channel, method, input_stream.str(), &response, client_metadata, - &server_initial_metadata, &server_trailing_metadata); + channel, method_name, serialized_request_proto, + &serialized_response_proto, client_metadata, &server_initial_metadata, + &server_trailing_metadata); PrintMetadata(server_initial_metadata, "Received initial metadata from server:"); PrintMetadata(server_trailing_metadata, "Received trailing metadata from server:"); if (s.ok()) { std::cout << "Rpc succeeded with OK status" << std::endl; - if (!response.empty()) { + if (parser) { + grpc::string response_text = parser->GetTextFormat( + serialized_response_proto, false /* is_request */); + if (parser->HasError()) { + return 1; + } + std::cout << "Response: \n " << response_text << std::endl; + } + if (!FLAGS_output_binary_file.empty()) { std::ofstream output_file(FLAGS_output_binary_file, std::ios::trunc | std::ios::binary); - output_file << response; + output_file << serialized_response_proto; } } else { std::cout << "Rpc failed with status code " << s.error_code() diff --git a/test/cpp/util/proto_file_parser.cc b/test/cpp/util/proto_file_parser.cc new file mode 100644 index 0000000000..25aec329eb --- /dev/null +++ b/test/cpp/util/proto_file_parser.cc @@ -0,0 +1,178 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "test/cpp/util/proto_file_parser.h" + +#include <algorithm> +#include <iostream> +#include <sstream> + +#include <google/protobuf/text_format.h> +#include <grpc++/support/config.h> + +namespace grpc { +namespace testing { +namespace { + +// Match the user input method string to the full_name from method descriptor. +bool MethodNameMatch(const grpc::string& full_name, const grpc::string& input) { + grpc::string clean_input = input; + std::replace(clean_input.begin(), clean_input.end(), '/', '.'); + if (clean_input.size() > full_name.size()) { + return false; + } + return full_name.compare(full_name.size() - clean_input.size(), + clean_input.size(), clean_input) == 0; +} +} // namespace + +class ErrorPrinter + : public google::protobuf::compiler::MultiFileErrorCollector { + public: + explicit ErrorPrinter(ProtoFileParser* parser) : parser_(parser) {} + + void AddError(const grpc::string& filename, int line, int column, + const grpc::string& message) GRPC_OVERRIDE { + std::ostringstream oss; + oss << "error " << filename << " " << line << " " << column << " " + << message << "\n"; + parser_->LogError(oss.str()); + } + + void AddWarning(const grpc::string& filename, int line, int column, + const grpc::string& message) GRPC_OVERRIDE { + std::cout << "warning " << filename << " " << line << " " << column << " " + << message << std::endl; + } + + private: + ProtoFileParser* parser_; // not owned +}; + +ProtoFileParser::ProtoFileParser(const grpc::string& proto_path, + const grpc::string& file_name, + const grpc::string& method) + : has_error_(false) { + source_tree_.MapPath("", proto_path); + error_printer_.reset(new ErrorPrinter(this)); + importer_.reset(new google::protobuf::compiler::Importer( + &source_tree_, error_printer_.get())); + const auto* file_desc = importer_->Import(file_name); + if (!file_desc) { + LogError(""); + return; + } + dynamic_factory_.reset( + new google::protobuf::DynamicMessageFactory(importer_->pool())); + + const google::protobuf::MethodDescriptor* method_descriptor = nullptr; + for (int i = 0; !method_descriptor && i < file_desc->service_count(); i++) { + const auto* service_desc = file_desc->service(i); + for (int j = 0; j < service_desc->method_count(); j++) { + const auto* method_desc = service_desc->method(j); + if (MethodNameMatch(method_desc->full_name(), method)) { + if (method_descriptor) { + std::ostringstream error_stream("Ambiguous method names: "); + error_stream << method_descriptor->full_name() << " "; + error_stream << method_desc->full_name(); + LogError(error_stream.str()); + } + method_descriptor = method_desc; + } + } + } + if (!method_descriptor) { + LogError("Method name not found"); + } + if (has_error_) { + return; + } + full_method_name_ = method_descriptor->full_name(); + size_t last_dot = full_method_name_.find_last_of('.'); + if (last_dot != grpc::string::npos) { + full_method_name_[last_dot] = '/'; + } + full_method_name_.insert(full_method_name_.begin(), '/'); + + request_prototype_.reset( + dynamic_factory_->GetPrototype(method_descriptor->input_type())->New()); + response_prototype_.reset( + dynamic_factory_->GetPrototype(method_descriptor->output_type())->New()); +} + +ProtoFileParser::~ProtoFileParser() {} + +grpc::string ProtoFileParser::GetSerializedProto( + const grpc::string& text_format_proto, bool is_request) { + grpc::string serialized; + grpc::protobuf::Message* msg = + is_request ? request_prototype_.get() : response_prototype_.get(); + bool ok = + google::protobuf::TextFormat::ParseFromString(text_format_proto, msg); + if (!ok) { + LogError("Failed to parse text format to proto."); + return ""; + } + ok = request_prototype_->SerializeToString(&serialized); + if (!ok) { + LogError("Failed to serialize proto."); + return ""; + } + return serialized; +} + +grpc::string ProtoFileParser::GetTextFormat( + const grpc::string& serialized_proto, bool is_request) { + grpc::protobuf::Message* msg = + is_request ? request_prototype_.get() : response_prototype_.get(); + if (!msg->ParseFromString(serialized_proto)) { + LogError("Failed to deserialize proto."); + return ""; + } + grpc::string text_format; + if (!google::protobuf::TextFormat::PrintToString(*msg, &text_format)) { + LogError("Failed to print proto message to text format"); + return ""; + } + return text_format; +} + +void ProtoFileParser::LogError(const grpc::string& error_msg) { + if (!error_msg.empty()) { + std::cout << error_msg << std::endl; + } + has_error_ = true; +} + +} // namespace testing +} // namespace grpc diff --git a/test/cpp/util/proto_file_parser.h b/test/cpp/util/proto_file_parser.h new file mode 100644 index 0000000000..46cdd66503 --- /dev/null +++ b/test/cpp/util/proto_file_parser.h @@ -0,0 +1,85 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPC_TEST_CPP_UTIL_PROTO_FILE_PARSER_H +#define GRPC_TEST_CPP_UTIL_PROTO_FILE_PARSER_H + +#include <memory> + +#include <google/protobuf/compiler/importer.h> +#include <google/protobuf/dynamic_message.h> + +#include "src/compiler/config.h" + +namespace grpc { +namespace testing { +class ErrorPrinter; + +// Find method and associated request/response types. +class ProtoFileParser { + public: + // The given proto file_name will be searched in a source tree rooted from + // proto_path. The method could be a partial string such as Service.Method or + // even just Method. It will log an error if there is ambiguity. + ProtoFileParser(const grpc::string& proto_path, const grpc::string& file_name, + const grpc::string& method); + ~ProtoFileParser(); + + grpc::string GetFullMethodName() const { return full_method_name_; } + + grpc::string GetSerializedProto(const grpc::string& text_format_proto, + bool is_request); + + grpc::string GetTextFormat(const grpc::string& serialized_proto, + bool is_request); + + bool HasError() const { return has_error_; } + + void LogError(const grpc::string& error_msg); + + private: + bool has_error_; + grpc::string request_text_; + grpc::string full_method_name_; + google::protobuf::compiler::DiskSourceTree source_tree_; + std::unique_ptr<ErrorPrinter> error_printer_; + std::unique_ptr<google::protobuf::compiler::Importer> importer_; + std::unique_ptr<google::protobuf::DynamicMessageFactory> dynamic_factory_; + std::unique_ptr<grpc::protobuf::Message> request_prototype_; + std::unique_ptr<grpc::protobuf::Message> response_prototype_; +}; + +} // namespace testing +} // namespace grpc + +#endif // GRPC_TEST_CPP_UTIL_PROTO_FILE_PARSER_H |