forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hot_restarting_parent_test.cc
347 lines (304 loc) · 15.3 KB
/
hot_restarting_parent_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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <memory>
#include "source/common/network/address_impl.h"
#include "source/server/hot_restarting_child.h"
#include "source/server/hot_restarting_parent.h"
#include "test/mocks/network/mocks.h"
#include "test/mocks/server/instance.h"
#include "test/mocks/server/listener_manager.h"
#include "test/server/utility.h"
#include "gtest/gtest.h"
using testing::InSequence;
using testing::Return;
using testing::ReturnRef;
namespace Envoy {
namespace Server {
namespace {
using HotRestartMessage = envoy::HotRestartMessage;
class MockHotRestartMessageSender : public HotRestartMessageSender {
public:
MOCK_METHOD(void, sendHotRestartMessage, (envoy::HotRestartMessage && msg));
};
class HotRestartingParentTest : public testing::Test {
public:
Network::Address::InstanceConstSharedPtr ipv4_test_addr_1_ =
Network::Utility::parseInternetAddressAndPortNoThrow("127.0.0.1:12345");
Network::Address::InstanceConstSharedPtr ipv4_test_addr_2_ =
Network::Utility::parseInternetAddressAndPortNoThrow("127.0.0.1:54321");
NiceMock<MockInstance> server_;
MockHotRestartMessageSender message_sender_;
HotRestartingParent::Internal hot_restarting_parent_{&server_, message_sender_};
};
TEST_F(HotRestartingParentTest, ShutdownAdmin) {
EXPECT_CALL(server_, shutdownAdmin());
EXPECT_CALL(server_, startTimeFirstEpoch()).WillOnce(Return(12345));
HotRestartMessage message = hot_restarting_parent_.shutdownAdmin();
EXPECT_EQ(12345, message.reply().shutdown_admin().original_start_time_unix_seconds());
}
TEST_F(HotRestartingParentTest, GetListenSocketsForChildNotFound) {
MockListenerManager listener_manager;
std::vector<std::reference_wrapper<Network::ListenerConfig>> listeners;
EXPECT_CALL(server_, listenerManager()).WillOnce(ReturnRef(listener_manager));
EXPECT_CALL(listener_manager, listeners(ListenerManager::ListenerState::ACTIVE))
.WillOnce(Return(listeners));
HotRestartMessage::Request request;
request.mutable_pass_listen_socket()->set_address("tcp://127.0.0.1:80");
HotRestartMessage message = hot_restarting_parent_.getListenSocketsForChild(request);
EXPECT_EQ(-1, message.reply().pass_listen_socket().fd());
}
TEST_F(HotRestartingParentTest, GetListenSocketsForChildNotBindPort) {
MockListenerManager listener_manager;
Network::MockListenerConfig listener_config;
std::vector<std::reference_wrapper<Network::ListenerConfig>> listeners;
InSequence s;
listeners.push_back(std::ref(*static_cast<Network::ListenerConfig*>(&listener_config)));
EXPECT_CALL(server_, listenerManager()).WillOnce(ReturnRef(listener_manager));
EXPECT_CALL(listener_manager, listeners(ListenerManager::ListenerState::ACTIVE))
.WillOnce(Return(listeners));
EXPECT_CALL(listener_config, listenSocketFactories());
Network::Address::InstanceConstSharedPtr address(
new Network::Address::Ipv4Instance("0.0.0.0", 80));
EXPECT_CALL(
*static_cast<Network::MockListenSocketFactory*>(listener_config.socket_factories_[0].get()),
localAddress())
.WillOnce(ReturnRef(address));
EXPECT_CALL(listener_config, bindToPort()).WillOnce(Return(false));
HotRestartMessage::Request request;
request.mutable_pass_listen_socket()->set_address("tcp://0.0.0.0:80");
HotRestartMessage message = hot_restarting_parent_.getListenSocketsForChild(request);
EXPECT_EQ(-1, message.reply().pass_listen_socket().fd());
}
TEST_F(HotRestartingParentTest, GetListenSocketsForChildSocketType) {
MockListenerManager listener_manager;
Network::MockListenerConfig tcp_listener_config;
Network::MockListenerConfig udp_listener_config;
MockOptions options;
std::vector<std::reference_wrapper<Network::ListenerConfig>> listeners;
InSequence s;
listeners.push_back(std::ref(*static_cast<Network::ListenerConfig*>(&tcp_listener_config)));
listeners.push_back(std::ref(*static_cast<Network::ListenerConfig*>(&udp_listener_config)));
EXPECT_CALL(server_, listenerManager()).WillOnce(ReturnRef(listener_manager));
EXPECT_CALL(listener_manager, listeners(ListenerManager::ListenerState::ACTIVE))
.WillOnce(Return(listeners));
Network::Address::InstanceConstSharedPtr address(
new Network::Address::Ipv4Instance("0.0.0.0", 80));
EXPECT_CALL(tcp_listener_config, listenSocketFactories());
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
tcp_listener_config.socket_factories_[0].get()),
localAddress())
.WillOnce(ReturnRef(address));
EXPECT_CALL(tcp_listener_config, bindToPort()).WillOnce(Return(true));
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
tcp_listener_config.socket_factories_[0].get()),
socketType())
.WillOnce(Return(Network::Socket::Type::Stream));
EXPECT_CALL(udp_listener_config, listenSocketFactories());
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
udp_listener_config.socket_factories_[0].get()),
localAddress())
.WillOnce(ReturnRef(address));
EXPECT_CALL(udp_listener_config, bindToPort()).WillOnce(Return(true));
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
udp_listener_config.socket_factories_[0].get()),
socketType())
.WillOnce(Return(Network::Socket::Type::Datagram));
EXPECT_CALL(server_, options()).WillOnce(ReturnRef(options));
EXPECT_CALL(options, concurrency()).WillOnce(Return(1));
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
udp_listener_config.socket_factories_[0].get()),
getListenSocket(_));
HotRestartMessage::Request request;
request.mutable_pass_listen_socket()->set_address("udp://0.0.0.0:80");
HotRestartMessage message = hot_restarting_parent_.getListenSocketsForChild(request);
EXPECT_EQ(0, message.reply().pass_listen_socket().fd());
}
TEST_F(HotRestartingParentTest, GetListenSocketsWithMultipleAddresses) {
Network::SocketSharedPtr socket = std::make_shared<NiceMock<Network::MockListenSocket>>();
MockListenerManager listener_manager;
Network::MockListenerConfig udp_listener_config;
MockOptions options;
std::vector<std::reference_wrapper<Network::ListenerConfig>> listeners;
InSequence s;
// Add one more socket factory to mimic two addresses in the listener.
udp_listener_config.socket_factories_.emplace_back(
std::make_unique<Network::MockListenSocketFactory>());
listeners.push_back(std::ref(*static_cast<Network::ListenerConfig*>(&udp_listener_config)));
EXPECT_CALL(server_, listenerManager()).WillOnce(ReturnRef(listener_manager));
EXPECT_CALL(listener_manager, listeners(ListenerManager::ListenerState::ACTIVE))
.WillOnce(Return(listeners));
Network::Address::InstanceConstSharedPtr address(
new Network::Address::Ipv4Instance("0.0.0.0", 80));
Network::Address::InstanceConstSharedPtr alt_address(
new Network::Address::Ipv4Instance("0.0.0.0", 8080));
EXPECT_CALL(udp_listener_config, listenSocketFactories());
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
udp_listener_config.socket_factories_[0].get()),
localAddress())
.WillOnce(ReturnRef(alt_address));
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
udp_listener_config.socket_factories_[1].get()),
localAddress())
.WillOnce(ReturnRef(address));
EXPECT_CALL(udp_listener_config, bindToPort()).WillOnce(Return(true));
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
udp_listener_config.socket_factories_[1].get()),
socketType())
.WillOnce(Return(Network::Socket::Type::Datagram));
EXPECT_CALL(server_, options()).WillOnce(ReturnRef(options));
EXPECT_CALL(options, concurrency()).WillOnce(Return(1));
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(
udp_listener_config.socket_factories_[1].get()),
getListenSocket(_))
.WillOnce(Return(socket));
HotRestartMessage::Request request;
request.mutable_pass_listen_socket()->set_address("udp://0.0.0.0:80");
HotRestartMessage message = hot_restarting_parent_.getListenSocketsForChild(request);
EXPECT_EQ(0, message.reply().pass_listen_socket().fd());
}
TEST_F(HotRestartingParentTest, GetListenSocketsForChildUnixDomainSocket) {
MockListenerManager listener_manager;
Network::MockListenerConfig listener_config;
std::vector<std::reference_wrapper<Network::ListenerConfig>> listeners;
Network::Address::InstanceConstSharedPtr local_address =
*Network::Address::PipeInstance::create("domain.socket");
MockOptions options;
InSequence s;
listeners.push_back(std::ref(*static_cast<Network::ListenerConfig*>(&listener_config)));
EXPECT_CALL(server_, listenerManager()).WillOnce(ReturnRef(listener_manager));
EXPECT_CALL(listener_manager, listeners(ListenerManager::ListenerState::ACTIVE))
.WillOnce(Return(listeners));
EXPECT_CALL(listener_config, listenSocketFactories());
EXPECT_CALL(
*static_cast<Network::MockListenSocketFactory*>(listener_config.socket_factories_[0].get()),
localAddress())
.WillOnce(ReturnRef(local_address));
EXPECT_CALL(listener_config, bindToPort()).WillOnce(Return(true));
EXPECT_CALL(
*static_cast<Network::MockListenSocketFactory*>(listener_config.socket_factories_[0].get()),
socketType())
.WillOnce(Return(Network::Socket::Type::Stream));
EXPECT_CALL(server_, options()).WillOnce(ReturnRef(options));
EXPECT_CALL(options, concurrency()).WillOnce(Return(1));
EXPECT_CALL(
*static_cast<Network::MockListenSocketFactory*>(listener_config.socket_factories_[0].get()),
getListenSocket(_));
HotRestartMessage::Request request;
request.mutable_pass_listen_socket()->set_address("unix://domain.socket");
HotRestartMessage message = hot_restarting_parent_.getListenSocketsForChild(request);
EXPECT_EQ(0, message.reply().pass_listen_socket().fd());
}
TEST_F(HotRestartingParentTest, ExportStatsToChild) {
Stats::TestUtil::TestStore store;
MockListenerManager listener_manager;
EXPECT_CALL(server_, listenerManager()).WillRepeatedly(ReturnRef(listener_manager));
EXPECT_CALL(listener_manager, numConnections()).WillRepeatedly(Return(0));
EXPECT_CALL(server_, stats()).WillRepeatedly(ReturnRef(store));
{
store.counter("c1").inc();
store.counter("c2").add(2);
store.gauge("g0", Stats::Gauge::ImportMode::Accumulate).set(0);
store.gauge("g1", Stats::Gauge::ImportMode::Accumulate).set(123);
store.gauge("g2", Stats::Gauge::ImportMode::Accumulate).set(456);
HotRestartMessage::Reply::Stats stats;
hot_restarting_parent_.exportStatsToChild(&stats);
EXPECT_EQ(1, stats.counter_deltas().at("c1"));
EXPECT_EQ(2, stats.counter_deltas().at("c2"));
EXPECT_EQ(0, stats.gauges().at("g0"));
EXPECT_EQ(123, stats.gauges().at("g1"));
EXPECT_EQ(456, stats.gauges().at("g2"));
}
// When a counter has not changed since its last export, it should not be included in the message.
{
store.counter("c2").add(2);
store.gauge("g1", Stats::Gauge::ImportMode::Accumulate).add(1);
store.gauge("g2", Stats::Gauge::ImportMode::Accumulate).sub(1);
HotRestartMessage::Reply::Stats stats;
hot_restarting_parent_.exportStatsToChild(&stats);
EXPECT_EQ(stats.counter_deltas().end(), stats.counter_deltas().find("c1"));
EXPECT_EQ(2, stats.counter_deltas().at("c2")); // 4 is the value, but 2 is the delta
EXPECT_EQ(0, stats.gauges().at("g0"));
EXPECT_EQ(124, stats.gauges().at("g1"));
EXPECT_EQ(455, stats.gauges().at("g2"));
}
// When a counter and gauge are not used, they should not be included in the message.
{
store.counter("unused_counter");
store.counter("used_counter").inc();
store.gauge("unused_gauge", Stats::Gauge::ImportMode::Accumulate);
store.gauge("used_gauge", Stats::Gauge::ImportMode::Accumulate).add(1);
HotRestartMessage::Reply::Stats stats;
hot_restarting_parent_.exportStatsToChild(&stats);
EXPECT_EQ(stats.counter_deltas().end(), stats.counter_deltas().find("unused_counter"));
EXPECT_EQ(1, stats.counter_deltas().at("used_counter"));
EXPECT_EQ(stats.gauges().end(), stats.counter_deltas().find("unused_gauge"));
EXPECT_EQ(1, stats.gauges().at("used_gauge"));
}
}
TEST_F(HotRestartingParentTest, RetainDynamicStats) {
MockListenerManager listener_manager;
Stats::SymbolTableImpl parent_symbol_table;
Stats::TestUtil::TestStore parent_store(parent_symbol_table);
EXPECT_CALL(server_, listenerManager()).WillRepeatedly(ReturnRef(listener_manager));
EXPECT_CALL(listener_manager, numConnections()).WillRepeatedly(Return(0));
EXPECT_CALL(server_, stats()).WillRepeatedly(ReturnRef(parent_store));
HotRestartMessage::Reply::Stats stats_proto;
{
Stats::StatNameDynamicPool dynamic(parent_store.symbolTable());
parent_store.counter("c1").inc();
parent_store.rootScope()->counterFromStatName(dynamic.add("c2")).inc();
parent_store.gauge("g1", Stats::Gauge::ImportMode::Accumulate).set(123);
parent_store.rootScope()
->gaugeFromStatName(dynamic.add("g2"), Stats::Gauge::ImportMode::Accumulate)
.set(42);
hot_restarting_parent_.exportStatsToChild(&stats_proto);
}
{
Stats::SymbolTableImpl child_symbol_table;
Stats::TestUtil::TestStore child_store(child_symbol_table);
Stats::StatNameDynamicPool dynamic(child_store.symbolTable());
Stats::Counter& c1 = child_store.counter("c1");
Stats::Counter& c2 = child_store.rootScope()->counterFromStatName(dynamic.add("c2"));
Stats::Gauge& g1 = child_store.gauge("g1", Stats::Gauge::ImportMode::Accumulate);
Stats::Gauge& g2 = child_store.rootScope()->gaugeFromStatName(
dynamic.add("g2"), Stats::Gauge::ImportMode::Accumulate);
HotRestartingChild hot_restarting_child(0, 0, testDomainSocketName(), 0, false, false);
hot_restarting_child.mergeParentStats(child_store, stats_proto);
EXPECT_EQ(1, c1.value());
EXPECT_EQ(1, c2.value());
EXPECT_EQ(123, g1.value());
EXPECT_EQ(42, g2.value());
}
}
MATCHER_P(UdpPacketHandlerPtrIs, expected_handler, "") {
bool matched = arg->non_dispatched_udp_packet_handler_.ptr() == expected_handler;
if (!matched) {
*result_listener << "\n&non_dispatched_udp_packet_handler_ == "
<< arg->non_dispatched_udp_packet_handler_.ptr()
<< "\nexpected_handler == " << expected_handler;
}
return matched;
}
TEST_F(HotRestartingParentTest, DrainListeners) {
EXPECT_CALL(server_, drainListeners(UdpPacketHandlerPtrIs(&hot_restarting_parent_)));
hot_restarting_parent_.drainListeners();
}
TEST_F(HotRestartingParentTest, UdpPacketIsForwarded) {
uint32_t worker_index = 12; // arbitrary index
Network::UdpRecvData packet;
std::string msg = "hello";
packet.addresses_.local_ = ipv4_test_addr_1_;
packet.addresses_.peer_ = ipv4_test_addr_2_;
packet.buffer_ = std::make_unique<Buffer::OwnedImpl>(msg);
packet.receive_time_ = MonotonicTime(std::chrono::microseconds(1234567890));
envoy::HotRestartMessage expected_msg;
auto* expected_packet = expected_msg.mutable_request()->mutable_forwarded_udp_packet();
expected_packet->set_local_addr("udp://127.0.0.1:12345");
expected_packet->set_peer_addr("udp://127.0.0.1:54321");
expected_packet->set_payload(msg);
expected_packet->set_receive_time_epoch_microseconds(1234567890);
expected_packet->set_worker_index(worker_index);
EXPECT_CALL(message_sender_, sendHotRestartMessage(ProtoEq(expected_msg)));
hot_restarting_parent_.handle(worker_index, packet);
}
} // namespace
} // namespace Server
} // namespace Envoy