| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 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 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#include <cstdlib> |
| 24 |
#include <iostream> |
| 25 |
#include <memory> |
| 26 |
#include <string> |
| 27 |
#include <thread> |
| 28 |
|
| 29 |
#include "absl/strings/str_cat.h" |
| 30 |
#include "gtest/gtest.h" |
| 31 |
|
| 32 |
#include <grpc/grpc.h> |
| 33 |
#include <grpc/support/alloc.h> |
| 34 |
#include <grpcpp/channel.h> |
| 35 |
#include <grpcpp/client_context.h> |
| 36 |
#include <grpcpp/create_channel.h> |
| 37 |
#include <grpcpp/ext/channelz_service_plugin.h> |
| 38 |
#include <grpcpp/grpcpp.h> |
| 39 |
#include <grpcpp/security/credentials.h> |
| 40 |
#include <grpcpp/security/server_credentials.h> |
| 41 |
#include <grpcpp/server.h> |
| 42 |
#include <grpcpp/server_builder.h> |
| 43 |
#include <grpcpp/server_context.h> |
| 44 |
|
| 45 |
#include "src/core/lib/gprpp/env.h" |
| 46 |
#include "src/cpp/server/channelz/channelz_service.h" |
| 47 |
#include "src/proto/grpc/testing/test.grpc.pb.h" |
| 48 |
#include "test/core/util/port.h" |
| 49 |
#include "test/core/util/test_config.h" |
| 50 |
#include "test/cpp/util/subprocess.h" |
| 51 |
#include "test/cpp/util/test_credentials_provider.h" |
| 52 |
|
| 53 |
static std::string g_root; |
| 54 |
|
| 55 |
namespace { |
| 56 |
using grpc::ClientContext; |
| 57 |
using grpc::Server; |
| 58 |
using grpc::ServerBuilder; |
| 59 |
using grpc::ServerContext; |
| 60 |
using grpc::Status; |
| 61 |
} // namespace |
| 62 |
|
| 63 |
// Test variables |
| 64 |
std::string server_address("0.0.0.0:10000"); |
| 65 |
std::string custom_credentials_type("INSECURE_CREDENTIALS"); |
| 66 |
std::string sampling_times = "2"; |
| 67 |
std::string sampling_interval_seconds = "3"; |
| 68 |
std::string output_json("output.json"); |
| 69 |
|
| 70 |
// Creata an echo server |
| 71 |
class EchoServerImpl final : public grpc::testing::TestService::Service { |
| 72 |
Status EmptyCall(ServerContext* /*context*/, |
| 73 |
const grpc::testing::Empty* /*request*/, |
| 74 |
grpc::testing::Empty* /*response*/) override { |
| 75 |
return Status::OK; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
// Run client in a thread |
| 80 |
void RunClient(const std::string& client_id, gpr_event* done_ev) { |
| 81 |
grpc::ChannelArguments channel_args; |
| 82 |
std::shared_ptr<grpc::ChannelCredentials> channel_creds = |
| 83 |
grpc::testing::GetCredentialsProvider()->GetChannelCredentials( |
| 84 |
custom_credentials_type, &channel_args); |
| 85 |
std::unique_ptr<grpc::testing::TestService::Stub> stub = |
| 86 |
grpc::testing::TestService::NewStub( |
| 87 |
grpc::CreateChannel(server_address, channel_creds)); |
| 88 |
gpr_log(GPR_INFO, "Client %s is echoing!", client_id.c_str()); |
| 89 |
while (true) { |
| 90 |
if (gpr_event_wait(done_ev, grpc_timeout_seconds_to_deadline(1)) != |
| 91 |
nullptr) { |
| 92 |
return; |
| 93 |
} |
| 94 |
grpc::testing::Empty request; |
| 95 |
grpc::testing::Empty response; |
| 96 |
ClientContext context; |
| 97 |
Status status = stub->EmptyCall(&context, request, &response); |
| 98 |
if (!status.ok()) { |
| 99 |
gpr_log(GPR_ERROR, "Client echo failed."); |
| 100 |
GPR_ASSERT(0); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// Create the channelz to test the connection to the server |
| 106 |
bool WaitForConnection(int wait_server_seconds) { |
| 107 |
grpc::ChannelArguments channel_args; |
| 108 |
std::shared_ptr<grpc::ChannelCredentials> channel_creds = |
| 109 |
grpc::testing::GetCredentialsProvider()->GetChannelCredentials( |
| 110 |
custom_credentials_type, &channel_args); |
| 111 |
auto channel = grpc::CreateChannel(server_address, channel_creds); |
| 112 |
return channel->WaitForConnected( |
| 113 |
grpc_timeout_seconds_to_deadline(wait_server_seconds)); |
| 114 |
} |
| 115 |
|
| 116 |
// Test the channelz sampler |
| 117 |
TEST(ChannelzSamplerTest, SimpleTest) { |
| 118 |
// start server |
| 119 |
grpc::channelz::experimental::InitChannelzService(); |
| 120 |
EchoServerImpl service; |
| 121 |
ServerBuilder builder; |
| 122 |
auto server_creds = |
| 123 |
grpc::testing::GetCredentialsProvider()->GetServerCredentials( |
| 124 |
custom_credentials_type); |
| 125 |
builder.AddListeningPort(server_address, server_creds); |
| 126 |
builder.RegisterService(&service); |
| 127 |
std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 128 |
gpr_log(GPR_INFO, "Server listening on %s", server_address.c_str()); |
| 129 |
const int kWaitForServerSeconds = 10; |
| 130 |
ASSERT_TRUE(WaitForConnection(kWaitForServerSeconds)); |
| 131 |
// client threads |
| 132 |
gpr_event done_ev1, done_ev2; |
| 133 |
gpr_event_init(&done_ev1); |
| 134 |
gpr_event_init(&done_ev2); |
| 135 |
std::thread client_thread_1(RunClient, "1", &done_ev1); |
| 136 |
std::thread client_thread_2(RunClient, "2", &done_ev2); |
| 137 |
// Run the channelz sampler |
| 138 |
grpc::SubProcess* test_driver = new grpc::SubProcess( |
| 139 |
{g_root + "/channelz_sampler", "--server_address=" + server_address, |
| 140 |
"--custom_credentials_type=" + custom_credentials_type, |
| 141 |
"--sampling_times=" + sampling_times, |
| 142 |
"--sampling_interval_seconds=" + sampling_interval_seconds, |
| 143 |
"--output_json=" + output_json}); |
| 144 |
int status = test_driver->Join(); |
| 145 |
if (WIFEXITED(status)) { |
| 146 |
if (WEXITSTATUS(status)) { |
| 147 |
gpr_log(GPR_ERROR, |
| 148 |
"Channelz sampler test test-runner exited with code %d", |
| 149 |
WEXITSTATUS(status)); |
| 150 |
GPR_ASSERT(0); // log the line number of the assertion failure |
| 151 |
} |
| 152 |
} else if (WIFSIGNALED(status)) { |
| 153 |
gpr_log(GPR_ERROR, "Channelz sampler test test-runner ended from signal %d", |
| 154 |
WTERMSIG(status)); |
| 155 |
GPR_ASSERT(0); |
| 156 |
} else { |
| 157 |
gpr_log(GPR_ERROR, |
| 158 |
"Channelz sampler test test-runner ended with unknown status %d", |
| 159 |
status); |
| 160 |
GPR_ASSERT(0); |
| 161 |
} |
| 162 |
delete test_driver; |
| 163 |
gpr_event_set(&done_ev1, reinterpret_cast<void*>(1)); |
| 164 |
gpr_event_set(&done_ev2, reinterpret_cast<void*>(1)); |
| 165 |
client_thread_1.join(); |
| 166 |
client_thread_2.join(); |
| 167 |
} |
| 168 |
|
| 169 |
int main(int argc, char** argv) { |
| 170 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 171 |
::testing::InitGoogleTest(&argc, argv); |
| 172 |
std::string me = argv[0]; |
| 173 |
auto lslash = me.rfind('/'); |
| 174 |
if (lslash != std::string::npos) { |
| 175 |
g_root = me.substr(0, lslash); |
| 176 |
} else { |
| 177 |
g_root = "."; |
| 178 |
} |
| 179 |
|
| 180 |
/// ensures the target address is unique even if this test is run in parallel |
| 181 |
server_address = absl::StrCat("0.0.0.0:", grpc_pick_unused_port_or_die()); |
| 182 |
int ret = RUN_ALL_TESTS(); |
| 183 |
return ret; |
| 184 |
} |