Debian Codegraph @

test/cpp/interop/istio_echo_server_test.cc

raw
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>
26 #include <grpcpp/security/credentials.h>
27 #include <grpcpp/server_builder.h>
28 #include <grpcpp/support/status.h>
29
30 #include "src/core/lib/gprpp/host_port.h"
31 #include "test/core/util/port.h"
32 #include "test/core/util/test_config.h"
33 #include "test/cpp/interop/istio_echo_server_lib.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:
71 std::string hostname_;
72 std::string forwarding_address_;
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_,
91 InsecureServerCredentials());
92 simple_server_ = simple_builder.BuildAndStart();
93 // Start the EchoTestServiceImpl server
94 ServerBuilder builder;
95 echo_test_service_impl_ = std::make_unique<EchoTestServiceImpl>(
96 "hostname", "v1", forwarding_address_);
97 builder.RegisterService(echo_test_service_impl_.get());
98 int port = grpc_pick_unused_port_or_die();
99 server_address_ = grpc_core::JoinHostPort("localhost", port);
100 builder.AddListeningPort(server_address_, InsecureServerCredentials());
101 server_ = builder.BuildAndStart();
102
103 auto channel = CreateChannel(server_address_, InsecureChannelCredentials());
104 stub_ = EchoTestService::NewStub(channel);
105 }
106
107 std::string forwarding_address_;
108 SimpleEchoTestServerImpl simple_test_service_impl_;
109 std::unique_ptr<EchoTestServiceImpl> echo_test_service_impl_;
110 std::string server_address_;
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 ::testing::HasSubstr("ServiceVersion=v1")));
130 }
131
132 TEST_F(EchoTest, ForwardEchoTest) {
133 ClientContext context;
134 ForwardEchoRequest request;
135 ForwardEchoResponse response;
136 request.set_count(3);
137 request.set_qps(1);
138 request.set_timeout_micros(20 * 1000 * 1000); // 20 seconds
139 request.set_url(absl::StrCat("grpc://", server_address_));
140 request.set_message("hello");
141 auto status = stub_->ForwardEcho(&context, request, &response);
142 ASSERT_TRUE(status.ok());
143 for (int i = 0; i < 3; ++i) {
144 EXPECT_THAT(
145 response.output()[i],
146 ::testing::AllOf(
147 ::testing::HasSubstr(
148 absl::StrFormat("[%d body] StatusCode=200\n", i)),
149 ::testing::HasSubstr(
150 absl::StrFormat("[%d body] Hostname=hostname\n", i)),
151 ::testing::HasSubstr(absl::StrFormat("[%d body] Echo=hello\n", i)),
152 ::testing::HasSubstr(absl::StrFormat("[%d body] Host=", i)),
153 ::testing::HasSubstr(
154 absl::StrFormat("[%d body] ServiceVersion=v1", i))));
155 }
156 }
157
158 TEST_F(EchoTest, ForwardEchoTestUnhandledProtocols) {
159 ClientContext context;
160 ForwardEchoRequest request;
161 ForwardEchoResponse response;
162 request.set_count(3);
163 request.set_qps(1);
164 request.set_timeout_micros(20 * 1000 * 1000); // 20 seconds
165 // http protocol is unhandled by EchoTestServiceImpl and should be forwarded
166 // to SimpleEchoTestServiceImpl
167 request.set_url(absl::StrCat("http://", server_address_));
168 request.set_message("hello");
169 auto status = stub_->ForwardEcho(&context, request, &response);
170 ASSERT_TRUE(status.ok()) << "Code = " << status.error_code()
171 << " Message = " << status.error_message();
172 ASSERT_FALSE(response.output().empty());
173 EXPECT_EQ(response.output()[0], "hello");
174 }
175
176 TEST_F(EchoTest, ForwardEchoFailure) {
177 simple_test_service_impl_.set_fail_rpc(true);
178 ClientContext context;
179 ForwardEchoRequest request;
180 ForwardEchoResponse response;
181 request.set_count(3);
182 request.set_qps(1);
183 request.set_timeout_micros(20 * 1000 * 1000); // 20 seconds
184 // Use the unhandled protocol to make sure that we forward the request to
185 // SimpleEchoTestServerImpl.
186 request.set_url(absl::StrCat("http://", server_address_));
187 request.set_message("hello");
188 auto status = stub_->ForwardEcho(&context, request, &response);
189 ASSERT_EQ(status.error_code(), StatusCode::UNAVAILABLE);
190 }
191
192 } // namespace
193 } // namespace testing
194 } // namespace grpc
195
196 int main(int argc, char** argv) {
197 ::testing::InitGoogleTest(&argc, argv);
198 grpc::testing::TestEnvironment env(&argc, argv);
199 grpc_init();
200 auto result = RUN_ALL_TESTS();
201 grpc_shutdown();
202 return result;
203 }