forked from envoyproxy/nighthawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.cc
247 lines (215 loc) · 9.31 KB
/
service_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <grpc++/grpc++.h>
#include <chrono>
#include "nighthawk/common/exception.h"
#include "external/envoy/test/test_common/environment.h"
#include "external/envoy/test/test_common/network_utility.h"
#include "external/envoy/test/test_common/utility.h"
#include "api/client/service.pb.h"
#include "source/client/service_impl.h"
#include "gtest/gtest.h"
using namespace std::chrono_literals;
using namespace testing;
namespace Nighthawk {
namespace Client {
namespace {
using nighthawk::client::ExecutionRequest;
using nighthawk::client::ExecutionResponse;
class ServiceTest : public TestWithParam<Envoy::Network::Address::IpVersion> {
public:
void SetUp() override {
service_ = std::make_unique<ServiceImpl>();
grpc::ServerBuilder builder;
loopback_address_ = Envoy::Network::Test::getLoopbackAddressUrlString(GetParam());
builder.AddListeningPort(fmt::format("{}:0", loopback_address_),
grpc::InsecureServerCredentials(), &grpc_server_port_);
builder.RegisterService(service_.get());
server_ = builder.BuildAndStart();
setupGrpcClient();
setBasicRequestOptions();
}
void TearDown() override { server_->Shutdown(); }
void setupGrpcClient() {
channel_ = grpc::CreateChannel(fmt::format("{}:{}", loopback_address_, grpc_server_port_),
grpc::InsecureChannelCredentials());
stub_ = std::make_unique<nighthawk::client::NighthawkService::Stub>(channel_);
}
void singleStreamBackToBackExecution(grpc::ClientContext& context,
nighthawk::client::NighthawkService::Stub&) {
auto r = stub_->ExecutionStream(&context);
EXPECT_TRUE(r->Write(request_, {}));
EXPECT_TRUE(r->Read(&response_));
ASSERT_TRUE(response_.has_error_detail());
EXPECT_TRUE(response_.has_output());
EXPECT_TRUE(r->Write(request_, {}));
EXPECT_TRUE(r->Read(&response_));
EXPECT_TRUE(response_.has_error_detail());
EXPECT_TRUE(response_.has_output());
EXPECT_TRUE(r->WritesDone());
auto status = r->Finish();
EXPECT_TRUE(status.ok());
}
std::thread testThreadedClientRun(bool expect_success) const {
std::thread thread([this, expect_success]() {
auto channel = grpc::CreateChannel(fmt::format("{}:{}", loopback_address_, grpc_server_port_),
grpc::InsecureChannelCredentials());
auto stub = std::make_unique<nighthawk::client::NighthawkService::Stub>(channel);
grpc::ClientContext context;
auto stream = stub->ExecutionStream(&context);
EXPECT_TRUE(stream->Write(request_, {}));
EXPECT_TRUE(stream->WritesDone());
nighthawk::client::ExecutionResponse response;
EXPECT_EQ(stream->Read(&response), expect_success);
auto status = stream->Finish();
EXPECT_EQ(status.ok(), expect_success);
});
return thread;
}
void setBasicRequestOptions() {
auto options = request_.mutable_start_request()->mutable_options();
// TODO(oschaaf): this sends actual traffic, which isn't relevant for the tests
// we are about to perform. However, it would be nice to be able to mock out things
// to clean this up.
options->mutable_uri()->set_value("http://127.0.0.1:10001/");
options->mutable_duration()->set_seconds(2);
options->mutable_requests_per_second()->set_value(3);
}
void runWithFailingValidationExpectations(bool expect_output,
absl::string_view match_error = "") {
auto r = stub_->ExecutionStream(&context_);
r->Write(request_, {});
r->WritesDone();
EXPECT_TRUE(r->Read(&response_));
auto status = r->Finish();
ASSERT_FALSE(match_error.empty());
EXPECT_TRUE(response_.has_error_detail());
EXPECT_EQ(response_.has_output(), expect_output);
EXPECT_EQ(grpc::StatusCode::INTERNAL, response_.error_detail().code());
EXPECT_THAT(response_.error_detail().message(), HasSubstr(std::string(match_error)));
EXPECT_TRUE(status.ok());
}
std::unique_ptr<ServiceImpl> service_;
std::unique_ptr<grpc::Server> server_;
std::shared_ptr<grpc::Channel> channel_;
grpc::ClientContext context_;
nighthawk::client::ExecutionRequest request_;
nighthawk::client::ExecutionResponse response_;
std::unique_ptr<nighthawk::client::NighthawkService::Stub> stub_;
std::string loopback_address_;
int grpc_server_port_{0};
};
class ServiceTestWithParameterizedConstructor : public ServiceTest {
public:
void SetUp() override {
auto logging_context = std::make_unique<Envoy::Logger::Context>(
spdlog::level::info, "%L %n [%g:%#] %v", log_lock_, false);
service_ = std::make_unique<ServiceImpl>(std::move(logging_context));
grpc::ServerBuilder builder;
loopback_address_ = Envoy::Network::Test::getLoopbackAddressUrlString(GetParam());
builder.AddListeningPort(fmt::format("{}:0", loopback_address_),
grpc::InsecureServerCredentials(), &grpc_server_port_);
builder.RegisterService(service_.get());
server_ = builder.BuildAndStart();
setupGrpcClient();
setBasicRequestOptions();
}
private:
Envoy::Thread::MutexBasicLockable log_lock_;
};
INSTANTIATE_TEST_SUITE_P(IpVersions, ServiceTestWithParameterizedConstructor,
ValuesIn(Envoy::TestEnvironment::getIpVersionsForTest()),
Envoy::TestUtility::ipTestParamsToString);
TEST_P(ServiceTestWithParameterizedConstructor,
ConstructorWithLoggingContextParameterCanRespondToRequests) {
std::unique_ptr<grpc::ClientReaderWriter<ExecutionRequest, ExecutionResponse>> stream =
stub_->ExecutionStream(&context_);
stream->Write(request_, {});
stream->WritesDone();
EXPECT_TRUE(stream->Read(&response_));
ASSERT_TRUE(response_.has_error_detail());
EXPECT_THAT(response_.error_detail().message(), HasSubstr(std::string("Unknown failure")));
EXPECT_TRUE(response_.has_output());
EXPECT_GE(response_.output().results(0).counters().size(), 8);
grpc::Status status = stream->Finish();
EXPECT_TRUE(status.ok());
}
INSTANTIATE_TEST_SUITE_P(IpVersions, ServiceTest,
ValuesIn(Envoy::TestEnvironment::getIpVersionsForTest()),
Envoy::TestUtility::ipTestParamsToString);
// Test single NH run
TEST_P(ServiceTest, Basic) {
auto r = stub_->ExecutionStream(&context_);
r->Write(request_, {});
r->WritesDone();
EXPECT_TRUE(r->Read(&response_));
ASSERT_TRUE(response_.has_error_detail());
EXPECT_THAT(response_.error_detail().message(), HasSubstr(std::string("Unknown failure")));
EXPECT_TRUE(response_.has_output());
EXPECT_GE(response_.output().results(0).counters().size(), 8);
auto status = r->Finish();
EXPECT_TRUE(status.ok());
}
// Test that attempts to perform concurrent executions result in a
// failure being returned.
TEST_P(ServiceTest, NoConcurrentStart) {
auto r = stub_->ExecutionStream(&context_);
EXPECT_TRUE(r->Write(request_, {}));
EXPECT_TRUE(r->Write(request_, {}));
EXPECT_TRUE(r->WritesDone());
EXPECT_TRUE(r->Read(&response_));
ASSERT_TRUE(response_.has_error_detail());
EXPECT_THAT(response_.error_detail().message(), HasSubstr(std::string("Unknown failure")));
EXPECT_TRUE(response_.has_output());
EXPECT_FALSE(r->Read(&response_));
auto status = r->Finish();
EXPECT_FALSE(status.ok());
}
// Test we are able to perform serialized executions.
TEST_P(ServiceTest, BackToBackExecution) {
grpc::ClientContext context1;
singleStreamBackToBackExecution(context1, *stub_);
// create a new client to connect to the same server, and do it one more time.
setupGrpcClient();
grpc::ClientContext context2;
singleStreamBackToBackExecution(context2, *stub_);
}
// Test that proto validation is wired up and works.
// TODO(oschaaf): functional coverage of all the options / validations.
TEST_P(ServiceTest, InvalidRps) {
auto options = request_.mutable_start_request()->mutable_options();
options->mutable_requests_per_second()->set_value(0);
// We do not expect output, because the options proto is not valid, and can't be echoed back.
runWithFailingValidationExpectations(false, "value must be inside range");
}
// We didn't implement updates yet, ensure we indicate so.
TEST_P(ServiceTest, UpdatesNotSupported) {
request_ = nighthawk::client::ExecutionRequest();
request_.mutable_update_request();
auto r = stub_->ExecutionStream(&context_);
r->Write(request_, {});
r->WritesDone();
EXPECT_FALSE(r->Read(&response_));
auto status = r->Finish();
EXPECT_THAT(status.error_message(), HasSubstr("Request is not supported yet"));
EXPECT_FALSE(status.ok());
}
// We didn't implement cancellations yet, ensure we indicate so.
TEST_P(ServiceTest, CancelNotSupported) {
request_ = nighthawk::client::ExecutionRequest();
request_.mutable_cancellation_request();
auto r = stub_->ExecutionStream(&context_);
r->Write(request_, {});
r->WritesDone();
EXPECT_FALSE(r->Read(&response_));
auto status = r->Finish();
EXPECT_THAT(status.error_message(), HasSubstr("Request is not supported yet"));
EXPECT_FALSE(status.ok());
}
TEST_P(ServiceTest, Unresolvable) {
auto options = request_.mutable_start_request()->mutable_options();
options->mutable_uri()->set_value("http://unresolvable-host/");
// We expect output, because the options proto is valid.
runWithFailingValidationExpectations(false, "Unable to create ProcessImpl");
}
} // namespace
} // namespace Client
} // namespace Nighthawk