forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhot_restart_impl_test.cc
More file actions
300 lines (268 loc) · 13.7 KB
/
Copy pathhot_restart_impl_test.cc
File metadata and controls
300 lines (268 loc) · 13.7 KB
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
#include <memory>
#include "source/common/api/os_sys_calls_impl.h"
#include "source/common/api/os_sys_calls_impl_hot_restart.h"
#include "source/common/common/hex.h"
#include "source/common/network/utility.h"
#include "source/server/hot_restart_impl.h"
#include "test/mocks/api/hot_restart.h"
#include "test/mocks/api/mocks.h"
#include "test/mocks/network/mocks.h"
#include "test/mocks/server/hot_restart.h"
#include "test/server/hot_restart_udp_forwarding_test_helper.h"
#include "test/server/utility.h"
#include "test/test_common/logging.h"
#include "test/test_common/test_random_generator.h"
#include "test/test_common/threadsafe_singleton_injector.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
using testing::_;
using testing::AnyNumber;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::WithArg;
namespace Envoy {
namespace Server {
struct TestAddresses {
Network::Address::InstanceConstSharedPtr ipv4_test_addr_ =
Network::Utility::parseInternetAddressAndPortNoThrow("127.0.0.5:12345");
Network::Address::InstanceConstSharedPtr ipv4_test_addr_different_ip_ =
Network::Utility::parseInternetAddressAndPortNoThrow("127.0.0.6:12345");
Network::Address::InstanceConstSharedPtr ipv4_test_addr_different_port_ =
Network::Utility::parseInternetAddressAndPortNoThrow("127.0.0.5:12346");
Network::Address::InstanceConstSharedPtr ipv4_default_ =
Network::Utility::parseInternetAddressAndPortNoThrow("0.0.0.0:12345");
Network::Address::InstanceConstSharedPtr ipv6_test_addr_ =
Network::Utility::parseInternetAddressAndPortNoThrow("[::1]:12345");
Network::Address::InstanceConstSharedPtr ipv6_test_addr_different_ip_ =
Network::Utility::parseInternetAddressAndPortNoThrow("[::2]:12345");
Network::Address::InstanceConstSharedPtr ipv6_test_addr_different_port_ =
Network::Utility::parseInternetAddressAndPortNoThrow("[::1]:12346");
Network::Address::InstanceConstSharedPtr ipv6_default_ =
Network::Utility::parseInternetAddressAndPortNoThrow("[::]:12345");
Network::Address::InstanceConstSharedPtr ipv6_default_with_ipv4_support_ =
Network::Utility::parseInternetAddressAndPortNoThrow("[::]:12345", false);
};
class HotRestartImplTest : public testing::Test {
public:
void setup() {
EXPECT_CALL(hot_restart_os_sys_calls_, shmUnlink(_)).Times(AnyNumber());
EXPECT_CALL(hot_restart_os_sys_calls_, shmOpen(_, _, _));
EXPECT_CALL(os_sys_calls_, ftruncate(_, _)).WillOnce(WithArg<1>(Invoke([this](off_t size) {
buffer_.resize(size);
return Api::SysCallIntResult{0, 0};
})));
EXPECT_CALL(os_sys_calls_, mmap(_, _, _, _, _, _)).WillOnce(InvokeWithoutArgs([this]() {
return Api::SysCallPtrResult{buffer_.data(), 0};
}));
// We bind two sockets, from both ends (parent and child), totaling four sockets to be bound.
// The socket for child->parent RPCs, and the socket for parent->child UDP forwarding
// in support of QUIC during hot restart.
EXPECT_CALL(os_sys_calls_, bind(_, _, _)).Times(4);
// Test we match the correct stat with empty-slots before, after, or both.
hot_restart_ = std::make_unique<HotRestartImpl>(0, 0, socket_addr_, 0, false, false);
hot_restart_->drainParentListeners();
// We close both sockets, both ends, totaling 4.
EXPECT_CALL(os_sys_calls_, close(_)).Times(4);
}
std::string socket_addr_ = testDomainSocketName();
// test_addresses_ must be initialized before os_sys_calls_ sets us mocking, as
// parseInternetAddress uses several os system calls.
TestAddresses test_addresses_;
Api::MockOsSysCalls os_sys_calls_;
TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls{&os_sys_calls_};
Api::MockHotRestartOsSysCalls hot_restart_os_sys_calls_;
TestThreadsafeSingletonInjector<Api::HotRestartOsSysCallsImpl> hot_restart_os_calls{
&hot_restart_os_sys_calls_};
std::vector<uint8_t> buffer_;
std::unique_ptr<HotRestartImpl> hot_restart_;
};
TEST_F(HotRestartImplTest, ParentDrainedCallbackRegistrarIsSetAndCanBeCalled) {
setup();
OptRef<Network::ParentDrainedCallbackRegistrar> registrar =
hot_restart_->parentDrainedCallbackRegistrar();
ASSERT_TRUE(registrar.has_value());
registrar->registerParentDrainedCallback(test_addresses_.ipv4_test_addr_, []() {});
}
TEST_F(HotRestartImplTest, VersionString) {
// Tests that the version-string will be consistent and HOT_RESTART_VERSION,
// between multiple instantiations.
std::string version;
// The mocking infrastructure requires a test setup and teardown every time we
// want to re-instantiate HotRestartImpl.
{
setup();
version = hot_restart_->version();
EXPECT_TRUE(absl::StartsWith(version, fmt::format("{}.", HOT_RESTART_VERSION))) << version;
TearDown();
}
{
setup();
EXPECT_EQ(version, hot_restart_->version()) << "Version string deterministic from options";
TearDown();
}
}
class DomainSocketErrorTest : public HotRestartImplTest, public testing::WithParamInterface<int> {};
// The parameter is the number of sockets that bind including the one that errors.
INSTANTIATE_TEST_CASE_P(SocketIndex, DomainSocketErrorTest, ::testing::Values(1, 2, 3, 4));
// Test that HotRestartDomainSocketInUseException is thrown when any of the domain sockets is
// already in use.
TEST_P(DomainSocketErrorTest, DomainSocketAlreadyInUse) {
int i = 0;
EXPECT_CALL(os_sys_calls_, bind(_, _, _)).Times(GetParam()).WillRepeatedly([&i]() {
if (++i == GetParam()) {
return Api::SysCallIntResult{-1, SOCKET_ERROR_ADDR_IN_USE};
}
return Api::SysCallIntResult{0, 0};
});
EXPECT_CALL(os_sys_calls_, close(_)).Times(GetParam());
EXPECT_THROW(std::make_unique<HotRestartImpl>(0, 0, socket_addr_, 0, false, false),
Server::HotRestartDomainSocketInUseException);
}
// Test that EnvoyException is thrown when any of the the domain socket bind fails
// for reasons other than being in use.
TEST_P(DomainSocketErrorTest, DomainSocketError) {
int i = 0;
EXPECT_CALL(os_sys_calls_, bind(_, _, _)).Times(GetParam()).WillRepeatedly([&i]() {
if (++i == GetParam()) {
return Api::SysCallIntResult{-1, SOCKET_ERROR_ACCESS};
}
return Api::SysCallIntResult{0, 0};
});
EXPECT_CALL(os_sys_calls_, close(_)).Times(GetParam());
EXPECT_THROW(std::make_unique<HotRestartImpl>(0, 0, socket_addr_, 0, false, false),
EnvoyException);
}
class HotRestartUdpForwardingContextTest : public HotRestartImplTest {
public:
void SetUp() override {
setup();
helper_ = std::make_unique<HotRestartUdpForwardingTestHelper>(*hot_restart_);
}
std::unique_ptr<HotRestartUdpForwardingTestHelper> helper_;
};
// Test that registering a forwarding listener results in a UdpForwardingContext which
// returns the correct listener, for IPv4 addresses.
TEST_F(HotRestartUdpForwardingContextTest, RegisterUdpForwardingListenerFindsIpv4Address) {
auto config_1 = std::make_shared<Network::MockUdpListenerConfig>();
auto config_any = std::make_shared<Network::MockUdpListenerConfig>();
helper_->registerUdpForwardingListener(test_addresses_.ipv4_test_addr_, config_1);
helper_->registerUdpForwardingListener(test_addresses_.ipv4_default_, config_any);
// Try a request to the specified address and port.
auto result = helper_->getListenerForDestination(*test_addresses_.ipv4_test_addr_);
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->first->asStringView(), test_addresses_.ipv4_test_addr_->asStringView());
EXPECT_EQ(result->second, config_1);
// Try with mismatched port, should be no result.
result = helper_->getListenerForDestination(*test_addresses_.ipv4_test_addr_different_port_);
EXPECT_FALSE(result.has_value());
// Try with mismatched address, should be default route.
result = helper_->getListenerForDestination(*test_addresses_.ipv4_test_addr_different_ip_);
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->first->asStringView(), test_addresses_.ipv4_default_->asStringView());
EXPECT_EQ(result->second, config_any);
// If there's an IPv6 request and only an IPv4 default route, no match.
result = helper_->getListenerForDestination(*test_addresses_.ipv6_test_addr_);
EXPECT_FALSE(result.has_value());
}
// Test that registering a forwarding listener results in a UdpForwardingContext which
// returns the correct listener, for IPv6 addresses.
TEST_F(HotRestartUdpForwardingContextTest, RegisterUdpForwardingListenerFindsIpv6Address) {
auto config_1 = std::make_shared<Network::MockUdpListenerConfig>();
auto config_any = std::make_shared<Network::MockUdpListenerConfig>();
helper_->registerUdpForwardingListener(test_addresses_.ipv6_test_addr_, config_1);
helper_->registerUdpForwardingListener(test_addresses_.ipv6_default_, config_any);
// Try a request to the specified address and port.
auto result = helper_->getListenerForDestination(*test_addresses_.ipv6_test_addr_);
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->first->asStringView(), test_addresses_.ipv6_test_addr_->asStringView());
EXPECT_EQ(result->second, config_1);
// Try with mismatched port, should be no result.
result = helper_->getListenerForDestination(*test_addresses_.ipv6_test_addr_different_port_);
EXPECT_FALSE(result.has_value());
// Try with mismatched address, should be default route.
result = helper_->getListenerForDestination(*test_addresses_.ipv6_test_addr_different_ip_);
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->first->asStringView(), test_addresses_.ipv6_default_->asStringView());
EXPECT_EQ(result->second, config_any);
// If there's an IPv4 request and only an IPv6 default route, no match.
result = helper_->getListenerForDestination(*test_addresses_.ipv4_test_addr_);
EXPECT_FALSE(result.has_value());
}
TEST_F(HotRestartUdpForwardingContextTest,
RegisterUdpForwardingListenerIpv6DefaultRouteCanMatchIpv4) {
auto config_any = std::make_shared<Network::MockUdpListenerConfig>();
helper_->registerUdpForwardingListener(test_addresses_.ipv6_default_with_ipv4_support_,
config_any);
auto result = helper_->getListenerForDestination(*test_addresses_.ipv4_test_addr_);
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->first->asStringView(), test_addresses_.ipv6_default_->asStringView());
EXPECT_EQ(result->second, config_any);
}
// Test that registering a udp forwarding listener default route for IPv6 and
// IPv6 separately prefers the one that matches the type of the request.
TEST_F(HotRestartUdpForwardingContextTest,
RegisterUdpForwardingListenerPrefersSameTypeDefaultRoute) {
auto config_ip4 = std::make_shared<Network::MockUdpListenerConfig>();
auto config_ip6 = std::make_shared<Network::MockUdpListenerConfig>();
helper_->registerUdpForwardingListener(test_addresses_.ipv4_default_, config_ip4);
helper_->registerUdpForwardingListener(test_addresses_.ipv6_default_, config_ip6);
// Request to an IPv6 address should use the ip6 config.
auto result = helper_->getListenerForDestination(*test_addresses_.ipv6_test_addr_);
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->first->asStringView(), test_addresses_.ipv6_default_->asStringView());
EXPECT_EQ(result->second, config_ip6);
// Request to an IPv4 address should use the ip4 config.
result = helper_->getListenerForDestination(*test_addresses_.ipv4_test_addr_);
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->first->asStringView(), test_addresses_.ipv4_default_->asStringView());
EXPECT_EQ(result->second, config_ip4);
// Request to a different port should be not matched.
result = helper_->getListenerForDestination(*test_addresses_.ipv4_test_addr_different_port_);
EXPECT_FALSE(result.has_value());
result = helper_->getListenerForDestination(*test_addresses_.ipv6_test_addr_different_port_);
EXPECT_FALSE(result.has_value());
}
TEST_F(HotRestartImplTest, IsInitializingReflectsLifecyclePhases) {
const mode_t mode = S_IRUSR | S_IWUSR;
const std::string socket_path = testDomainSocketName();
std::vector<uint8_t> buffer;
EXPECT_CALL(hot_restart_os_sys_calls_, shmOpen(_, _, _)).Times(2).WillRepeatedly([]() {
return Api::SysCallIntResult{1, 0};
});
EXPECT_CALL(os_sys_calls_, ftruncate(_, _)).WillOnce(WithArg<1>(Invoke([&buffer](off_t size) {
buffer.resize(size);
return Api::SysCallIntResult{0, 0};
})));
EXPECT_CALL(os_sys_calls_, mmap(_, _, _, _, _, _))
.Times(2)
.WillRepeatedly(
InvokeWithoutArgs([&buffer]() { return Api::SysCallPtrResult{buffer.data(), 0}; }));
EXPECT_CALL(os_sys_calls_, bind(_, _, _)).Times(8);
EXPECT_CALL(os_sys_calls_, close(_)).Times(8);
EXPECT_CALL(hot_restart_os_sys_calls_, shmUnlink(_));
EXPECT_CALL(os_sys_calls_, sendmsg(_, _, _)).WillOnce([](int, const msghdr* msg, int) {
return Api::SysCallSizeResult{static_cast<ssize_t>(msg->msg_iov[0].iov_len), 0};
});
// Create the parent process first (restart_epoch == 0), and it should start in initializing
// state.
std::unique_ptr<HotRestartImpl> hri_epoch0;
ASSERT_NO_THROW(
{ hri_epoch0 = std::make_unique<HotRestartImpl>(1, 0, socket_path, mode, false, false); });
EXPECT_TRUE(hri_epoch0->isInitializing());
// Simulate the parent finishing its initialization by clearing the flag.
ASSERT_FALSE(buffer.empty());
auto* shmem = reinterpret_cast<SharedMemory*>(buffer.data());
shmem->flags_.store(shmem->flags_.load() & ~SHMEM_FLAGS_INITIALIZING);
EXPECT_FALSE(hri_epoch0->isInitializing());
// Create the child process (restart_epoch > 0).
std::unique_ptr<HotRestartImpl> hri_epoch1;
ASSERT_NO_THROW(
{ hri_epoch1 = std::make_unique<HotRestartImpl>(0, 1, socket_path, mode, false, false); });
EXPECT_TRUE(hri_epoch1->isInitializing());
// Ensure that the child's flag is cleared after call to drain parent listeners.
ASSERT_NO_THROW({ hri_epoch1->drainParentListeners(); });
EXPECT_FALSE(hri_epoch1->isInitializing());
}
} // namespace Server
} // namespace Envoy