istio_echo_server_test.cc
Go to the documentation of this file.
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <memory>
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/str_format.h"
24 
25 #include <grpcpp/create_channel.h>
27 #include <grpcpp/server_builder.h>
28 #include <grpcpp/support/status.h>
29 
31 #include "test/core/util/port.h"
34 
35 namespace grpc {
36 namespace testing {
37 namespace {
38 
39 using proto::EchoRequest;
40 using proto::EchoResponse;
41 using proto::EchoTestService;
42 using proto::ForwardEchoRequest;
43 using proto::ForwardEchoResponse;
44 
45 // A very simple EchoTestService implementation that just echoes back the
46 // message without handling any other expectations for ForwardEcho.
47 class SimpleEchoTestServerImpl : public proto::EchoTestService::Service {
48  public:
49  explicit SimpleEchoTestServerImpl() {}
50 
51  grpc::Status Echo(grpc::ServerContext* /* context */,
52  const proto::EchoRequest* /* request */,
53  proto::EchoResponse* /* response */) override {
54  GPR_ASSERT(false);
55  return Status(StatusCode::INVALID_ARGUMENT, "Unexpected");
56  }
57 
58  grpc::Status ForwardEcho(grpc::ServerContext* /*context*/,
59  const proto::ForwardEchoRequest* request,
60  proto::ForwardEchoResponse* response) override {
61  if (fail_rpc_) {
62  return Status(StatusCode::UNAVAILABLE, "fail rpc");
63  }
64  response->add_output(request->message());
65  return Status::OK;
66  }
67 
68  void set_fail_rpc(bool fail_rpc) { fail_rpc_ = fail_rpc; }
69 
70  private:
73  std::atomic<bool> fail_rpc_{false};
74  // The following fields are not set yet. But we may need them later.
75  // int port_;
76  // std::string version_;
77  // std::string cluster_;
78  // std::string istio_version_;
79 };
80 
81 class EchoTest : public ::testing::Test {
82  protected:
83  EchoTest() {
84  // Start the simple server which will handle protocols that
85  // EchoTestServiceImpl does not handle.
86  int forwarding_port = grpc_pick_unused_port_or_die();
87  forwarding_address_ = grpc_core::JoinHostPort("localhost", forwarding_port);
88  ServerBuilder simple_builder;
89  simple_builder.RegisterService(&simple_test_service_impl_);
90  simple_builder.AddListeningPort(forwarding_address_,
92  simple_server_ = simple_builder.BuildAndStart();
93  // Start the EchoTestServiceImpl server
94  ServerBuilder builder;
96  std::make_unique<EchoTestServiceImpl>("hostname", forwarding_address_);
97  builder.RegisterService(echo_test_service_impl_.get());
101  server_ = builder.BuildAndStart();
102 
104  stub_ = EchoTestService::NewStub(channel);
105  }
106 
108  SimpleEchoTestServerImpl simple_test_service_impl_;
109  std::unique_ptr<EchoTestServiceImpl> echo_test_service_impl_;
111  std::unique_ptr<Server> server_;
112  std::unique_ptr<Server> simple_server_;
113  std::unique_ptr<EchoTestService::Stub> stub_;
114 };
115 
116 TEST_F(EchoTest, SimpleEchoTest) {
117  ClientContext context;
118  EchoRequest request;
119  EchoResponse response;
120  request.set_message("hello");
121  auto status = stub_->Echo(&context, request, &response);
122  ASSERT_TRUE(status.ok());
123  EXPECT_THAT(response.message(),
124  ::testing::AllOf(::testing::HasSubstr("StatusCode=200\n"),
125  ::testing::HasSubstr("Hostname=hostname\n"),
126  ::testing::HasSubstr("Echo=hello\n"),
127  ::testing::HasSubstr("Host="),
128  ::testing::HasSubstr("IP=")));
129 }
130 
131 TEST_F(EchoTest, ForwardEchoTest) {
132  ClientContext context;
133  ForwardEchoRequest request;
134  ForwardEchoResponse response;
135  request.set_count(3);
136  request.set_qps(1);
137  request.set_timeout_micros(20 * 1000 * 1000); // 20 seconds
138  request.set_url(absl::StrCat("grpc://", server_address_));
139  request.set_message("hello");
140  auto status = stub_->ForwardEcho(&context, request, &response);
141  ASSERT_TRUE(status.ok());
142  for (int i = 0; i < 3; ++i) {
143  EXPECT_THAT(
144  response.output()[i],
145  ::testing::AllOf(
147  absl::StrFormat("[%d body] StatusCode=200\n", i)),
149  absl::StrFormat("[%d body] Hostname=hostname\n", i)),
150  ::testing::HasSubstr(absl::StrFormat("[%d body] Echo=hello\n", i)),
151  ::testing::HasSubstr(absl::StrFormat("[%d body] Host=", i)),
152  ::testing::HasSubstr(absl::StrFormat("[%d body] IP=", i))));
153  }
154 }
155 
156 TEST_F(EchoTest, ForwardEchoTestUnhandledProtocols) {
157  ClientContext context;
158  ForwardEchoRequest request;
159  ForwardEchoResponse response;
160  request.set_count(3);
161  request.set_qps(1);
162  request.set_timeout_micros(20 * 1000 * 1000); // 20 seconds
163  // http protocol is unhandled by EchoTestServiceImpl and should be forwarded
164  // to SimpleEchoTestServiceImpl
165  request.set_url(absl::StrCat("http://", server_address_));
166  request.set_message("hello");
167  auto status = stub_->ForwardEcho(&context, request, &response);
168  ASSERT_TRUE(status.ok()) << "Code = " << status.error_code()
169  << " Message = " << status.error_message();
170  ASSERT_FALSE(response.output().empty());
171  EXPECT_EQ(response.output()[0], "hello");
172 }
173 
174 TEST_F(EchoTest, ForwardEchoFailure) {
175  simple_test_service_impl_.set_fail_rpc(true);
176  ClientContext context;
177  ForwardEchoRequest request;
178  ForwardEchoResponse response;
179  request.set_count(3);
180  request.set_qps(1);
181  request.set_timeout_micros(20 * 1000 * 1000); // 20 seconds
182  // Use the unhandled protocol to make sure that we forward the request to
183  // SimpleEchoTestServerImpl.
184  request.set_url(absl::StrCat("http://", server_address_));
185  request.set_message("hello");
186  auto status = stub_->ForwardEcho(&context, request, &response);
188 }
189 
190 } // namespace
191 } // namespace testing
192 } // namespace grpc
193 
194 int main(int argc, char** argv) {
195  ::testing::InitGoogleTest(&argc, argv);
196  grpc::testing::TestEnvironment env(&argc, argv);
197  grpc_init();
198  auto result = RUN_ALL_TESTS();
199  grpc_shutdown();
200  return result;
201 }
grpc::EXPECT_THAT
EXPECT_THAT(status.error_message(), ::testing::HasSubstr("subject_token_type"))
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
testing
Definition: aws_request_signer_test.cc:25
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
port.h
hostname_
std::string hostname_
Definition: istio_echo_server_test.cc:71
generate.env
env
Definition: generate.py:37
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
grpc
Definition: grpcpp/alarm.h:33
stub_
std::unique_ptr< EchoTestService::Stub > stub_
Definition: istio_echo_server_test.cc:113
fail_rpc_
std::atomic< bool > fail_rpc_
Definition: istio_echo_server_test.cc:73
grpc::ASSERT_EQ
ASSERT_EQ(sizeof(valid_json), fwrite(valid_json, 1, sizeof(valid_json), creds_file))
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
server_
std::unique_ptr< Server > server_
Definition: istio_echo_server_test.cc:111
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
echo_test_service_impl_
std::unique_ptr< EchoTestServiceImpl > echo_test_service_impl_
Definition: istio_echo_server_test.cc:109
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
main
int main(int argc, char **argv)
Definition: istio_echo_server_test.cc:194
simple_test_service_impl_
SimpleEchoTestServerImpl simple_test_service_impl_
Definition: istio_echo_server_test.cc:108
grpc_core::JoinHostPort
std::string JoinHostPort(absl::string_view host, int port)
Definition: host_port.cc:32
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
host_port.h
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
istio_echo_server_lib.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
credentials.h
grpc::testing::TEST_F
TEST_F(ChannelArgumentsTest, SetInt)
Definition: channel_arguments_test.cc:134
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc.StatusCode.INVALID_ARGUMENT
tuple INVALID_ARGUMENT
Definition: src/python/grpcio/grpc/__init__.py:263
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
grpc::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
grpc.StatusCode.UNAVAILABLE
tuple UNAVAILABLE
Definition: src/python/grpcio/grpc/__init__.py:278
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
testing::AllOf
internal::AllOfResult2< M1, M2 >::type AllOf(M1 m1, M2 m2)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:13472
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
simple_server_
std::unique_ptr< Server > simple_server_
Definition: istio_echo_server_test.cc:112
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
forwarding_address_
std::string forwarding_address_
Definition: istio_echo_server_test.cc:72
testing::HasSubstr
PolymorphicMatcher< internal::HasSubstrMatcher< internal::string > > HasSubstr(const internal::string &substring)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8803
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
server_address_
std::string server_address_
Definition: istio_echo_server_test.cc:110
server_builder.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
create_channel.h
status.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:22