forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.cc
675 lines (579 loc) · 28.2 KB
/
server_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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#include <memory>
#include "common/common/assert.h"
#include "common/common/version.h"
#include "common/network/address_impl.h"
#include "common/thread_local/thread_local_impl.h"
#include "server/process_context_impl.h"
#include "server/server.h"
#include "test/integration/server.h"
#include "test/mocks/server/mocks.h"
#include "test/mocks/stats/mocks.h"
#include "test/test_common/environment.h"
#include "test/test_common/test_time.h"
#include "test/test_common/utility.h"
#include "absl/synchronization/notification.h"
#include "gtest/gtest.h"
using testing::_;
using testing::Assign;
using testing::HasSubstr;
using testing::InSequence;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::Property;
using testing::Ref;
using testing::Return;
using testing::SaveArg;
using testing::StrictMock;
namespace Envoy {
namespace Server {
namespace {
TEST(ServerInstanceUtil, flushHelper) {
InSequence s;
Stats::IsolatedStoreImpl store;
Stats::Counter& c = store.counter("hello");
c.inc();
store.gauge("world", Stats::Gauge::ImportMode::Accumulate).set(5);
store.histogram("histogram");
std::list<Stats::SinkPtr> sinks;
InstanceUtil::flushMetricsToSinks(sinks, store);
// Make sure that counters have been latched even if there are no sinks.
EXPECT_EQ(1UL, c.value());
EXPECT_EQ(0, c.latch());
Stats::MockSink* sink = new StrictMock<Stats::MockSink>();
sinks.emplace_back(sink);
EXPECT_CALL(*sink, flush(_)).WillOnce(Invoke([](Stats::MetricSnapshot& snapshot) {
ASSERT_EQ(snapshot.counters().size(), 1);
EXPECT_EQ(snapshot.counters()[0].counter_.get().name(), "hello");
EXPECT_EQ(snapshot.counters()[0].delta_, 1);
ASSERT_EQ(snapshot.gauges().size(), 1);
EXPECT_EQ(snapshot.gauges()[0].get().name(), "world");
EXPECT_EQ(snapshot.gauges()[0].get().value(), 5);
}));
c.inc();
InstanceUtil::flushMetricsToSinks(sinks, store);
// Histograms don't currently work with the isolated store so test those with a mock store.
NiceMock<Stats::MockStore> mock_store;
Stats::ParentHistogramSharedPtr parent_histogram(new Stats::MockParentHistogram());
std::vector<Stats::ParentHistogramSharedPtr> parent_histograms = {parent_histogram};
ON_CALL(mock_store, histograms).WillByDefault(Return(parent_histograms));
EXPECT_CALL(*sink, flush(_)).WillOnce(Invoke([](Stats::MetricSnapshot& snapshot) {
EXPECT_TRUE(snapshot.counters().empty());
EXPECT_TRUE(snapshot.gauges().empty());
EXPECT_EQ(snapshot.histograms().size(), 1);
}));
InstanceUtil::flushMetricsToSinks(sinks, mock_store);
}
class RunHelperTest : public testing::Test {
public:
RunHelperTest() {
InSequence s;
sigterm_ = new Event::MockSignalEvent(&dispatcher_);
sigint_ = new Event::MockSignalEvent(&dispatcher_);
sigusr1_ = new Event::MockSignalEvent(&dispatcher_);
sighup_ = new Event::MockSignalEvent(&dispatcher_);
EXPECT_CALL(overload_manager_, start());
EXPECT_CALL(cm_, setInitializedCb(_)).WillOnce(SaveArg<0>(&cm_init_callback_));
ON_CALL(server_, shutdown()).WillByDefault(Assign(&shutdown_, true));
helper_ = std::make_unique<RunHelper>(server_, options_, dispatcher_, cm_, access_log_manager_,
init_manager_, overload_manager_,
[this] { start_workers_.ready(); });
}
NiceMock<MockInstance> server_;
testing::NiceMock<MockOptions> options_;
NiceMock<Event::MockDispatcher> dispatcher_;
NiceMock<Upstream::MockClusterManager> cm_;
NiceMock<AccessLog::MockAccessLogManager> access_log_manager_;
NiceMock<MockOverloadManager> overload_manager_;
Init::ManagerImpl init_manager_{""};
ReadyWatcher start_workers_;
std::unique_ptr<RunHelper> helper_;
std::function<void()> cm_init_callback_;
Event::MockSignalEvent* sigterm_;
Event::MockSignalEvent* sigint_;
Event::MockSignalEvent* sigusr1_;
Event::MockSignalEvent* sighup_;
bool shutdown_ = false;
};
TEST_F(RunHelperTest, Normal) {
EXPECT_CALL(start_workers_, ready());
cm_init_callback_();
}
TEST_F(RunHelperTest, ShutdownBeforeCmInitialize) {
EXPECT_CALL(start_workers_, ready()).Times(0);
sigterm_->callback_();
EXPECT_CALL(server_, isShutdown()).WillOnce(Return(shutdown_));
cm_init_callback_();
}
TEST_F(RunHelperTest, ShutdownBeforeInitManagerInit) {
EXPECT_CALL(start_workers_, ready()).Times(0);
Init::ExpectableTargetImpl target;
init_manager_.add(target);
EXPECT_CALL(target, initialize());
cm_init_callback_();
sigterm_->callback_();
EXPECT_CALL(server_, isShutdown()).WillOnce(Return(shutdown_));
target.ready();
}
class InitializingInitManager : public Init::ManagerImpl {
public:
InitializingInitManager(absl::string_view name) : Init::ManagerImpl(name) {}
State state() const override { return State::Initializing; }
};
class InitializingInstanceImpl : public InstanceImpl {
private:
InitializingInitManager init_manager_{"Server"};
public:
InitializingInstanceImpl(const Options& options, Event::TimeSystem& time_system,
Network::Address::InstanceConstSharedPtr local_address,
ListenerHooks& hooks, HotRestart& restarter, Stats::StoreRoot& store,
Thread::BasicLockable& access_log_lock,
ComponentFactory& component_factory,
Runtime::RandomGeneratorPtr&& random_generator,
ThreadLocal::Instance& tls, Thread::ThreadFactory& thread_factory,
Filesystem::Instance& file_system,
std::unique_ptr<ProcessContext> process_context)
: InstanceImpl(options, time_system, local_address, hooks, restarter, store, access_log_lock,
component_factory, std::move(random_generator), tls, thread_factory,
file_system, std::move(process_context)) {}
Init::Manager& initManager() override { return init_manager_; }
};
// Class creates minimally viable server instance for testing.
class ServerInstanceImplTest : public testing::TestWithParam<Network::Address::IpVersion> {
protected:
ServerInstanceImplTest() : version_(GetParam()) {}
void initialize(const std::string& bootstrap_path) { initialize(bootstrap_path, false); }
void initialize(const std::string& bootstrap_path, const bool use_intializing_instance) {
if (bootstrap_path.empty()) {
options_.config_path_ = TestEnvironment::temporaryFileSubstitute(
"test/config/integration/server.json", {{"upstream_0", 0}, {"upstream_1", 0}}, version_);
} else {
options_.config_path_ = TestEnvironment::temporaryFileSubstitute(
bootstrap_path, {{"upstream_0", 0}, {"upstream_1", 0}}, version_);
}
thread_local_ = std::make_unique<ThreadLocal::InstanceImpl>();
if (process_object_ != nullptr) {
process_context_ = std::make_unique<ProcessContextImpl>(*process_object_);
}
if (use_intializing_instance) {
server_ = std::make_unique<InitializingInstanceImpl>(
options_, test_time_.timeSystem(),
Network::Address::InstanceConstSharedPtr(new Network::Address::Ipv4Instance("127.0.0.1")),
hooks_, restart_, stats_store_, fakelock_, component_factory_,
std::make_unique<NiceMock<Runtime::MockRandomGenerator>>(), *thread_local_,
Thread::threadFactoryForTest(), Filesystem::fileSystemForTest(),
std::move(process_context_));
} else {
server_ = std::make_unique<InstanceImpl>(
options_, test_time_.timeSystem(),
Network::Address::InstanceConstSharedPtr(new Network::Address::Ipv4Instance("127.0.0.1")),
hooks_, restart_, stats_store_, fakelock_, component_factory_,
std::make_unique<NiceMock<Runtime::MockRandomGenerator>>(), *thread_local_,
Thread::threadFactoryForTest(), Filesystem::fileSystemForTest(),
std::move(process_context_));
}
EXPECT_TRUE(server_->api().fileSystem().fileExists("/dev/null"));
}
void initializeWithHealthCheckParams(const std::string& bootstrap_path, const double timeout,
const double interval) {
options_.config_path_ = TestEnvironment::temporaryFileSubstitute(
bootstrap_path,
{{"health_check_timeout", fmt::format("{}", timeout).c_str()},
{"health_check_interval", fmt::format("{}", interval).c_str()}},
TestEnvironment::PortMap{}, version_);
thread_local_ = std::make_unique<ThreadLocal::InstanceImpl>();
server_ = std::make_unique<InstanceImpl>(
options_, test_time_.timeSystem(),
Network::Address::InstanceConstSharedPtr(new Network::Address::Ipv4Instance("127.0.0.1")),
hooks_, restart_, stats_store_, fakelock_, component_factory_,
std::make_unique<NiceMock<Runtime::MockRandomGenerator>>(), *thread_local_,
Thread::threadFactoryForTest(), Filesystem::fileSystemForTest(), nullptr);
EXPECT_TRUE(server_->api().fileSystem().fileExists("/dev/null"));
}
Thread::ThreadPtr startTestServer(const std::string& bootstrap_path,
const bool use_intializing_instance) {
absl::Notification started;
auto server_thread = Thread::threadFactoryForTest().createThread([&] {
initialize(bootstrap_path, use_intializing_instance);
auto startup_handle = server_->registerCallback(ServerLifecycleNotifier::Stage::Startup,
[&] { started.Notify(); });
auto shutdown_handle = server_->registerCallback(ServerLifecycleNotifier::Stage::ShutdownExit,
[&](Event::PostCb) { FAIL(); });
shutdown_handle = nullptr; // unregister callback
server_->run();
startup_handle = nullptr;
server_ = nullptr;
thread_local_ = nullptr;
});
started.WaitForNotification();
return server_thread;
}
// Returns the server's tracer as a pointer, for use in dynamic_cast tests.
Tracing::HttpTracer* tracer() { return &server_->httpContext().tracer(); };
Network::Address::IpVersion version_;
testing::NiceMock<MockOptions> options_;
DefaultListenerHooks hooks_;
testing::NiceMock<MockHotRestart> restart_;
std::unique_ptr<ThreadLocal::InstanceImpl> thread_local_;
Stats::TestIsolatedStoreImpl stats_store_;
Thread::MutexBasicLockable fakelock_;
TestComponentFactory component_factory_;
DangerousDeprecatedTestTime test_time_;
ProcessObject* process_object_ = nullptr;
std::unique_ptr<ProcessContextImpl> process_context_;
std::unique_ptr<InstanceImpl> server_;
};
// Custom StatsSink that just increments a counter when flush is called.
class CustomStatsSink : public Stats::Sink {
public:
CustomStatsSink(Stats::Scope& scope) : stats_flushed_(scope.counter("stats.flushed")) {}
// Stats::Sink
void flush(Stats::MetricSnapshot&) override { stats_flushed_.inc(); }
void onHistogramComplete(const Stats::Histogram&, uint64_t) override {}
private:
Stats::Counter& stats_flushed_;
};
// Custom StatsSinFactory that creates CustomStatsSink.
class CustomStatsSinkFactory : public Server::Configuration::StatsSinkFactory {
public:
// StatsSinkFactory
Stats::SinkPtr createStatsSink(const Protobuf::Message&, Server::Instance& server) override {
return std::make_unique<CustomStatsSink>(server.stats());
}
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return ProtobufTypes::MessagePtr{new Envoy::ProtobufWkt::Empty()};
}
std::string name() override { return "envoy.custom_stats_sink"; }
};
INSTANTIATE_TEST_SUITE_P(IpVersions, ServerInstanceImplTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
/**
* Static registration for the custom sink factory. @see RegisterFactory.
*/
REGISTER_FACTORY(CustomStatsSinkFactory, Server::Configuration::StatsSinkFactory);
// Validates that server stats are flushed even when server is stuck with initialization.
TEST_P(ServerInstanceImplTest, StatsFlushWhenServerIsStillInitializing) {
auto server_thread = startTestServer("test/server/stats_sink_bootstrap.yaml", true);
// Wait till stats are flushed to custom sink and validate that the actual flush happens.
TestUtility::waitForCounterEq(stats_store_, "stats.flushed", 1, test_time_.timeSystem());
EXPECT_EQ(3L, TestUtility::findGauge(stats_store_, "server.state")->value());
EXPECT_EQ(Init::Manager::State::Initializing, server_->initManager().state());
server_->dispatcher().post([&] { server_->shutdown(); });
server_thread->join();
}
TEST_P(ServerInstanceImplTest, EmptyShutdownLifecycleNotifications) {
auto server_thread = startTestServer("test/server/node_bootstrap.yaml", false);
server_->dispatcher().post([&] { server_->shutdown(); });
server_thread->join();
}
TEST_P(ServerInstanceImplTest, LifecycleNotifications) {
bool startup = false, shutdown = false, shutdown_with_completion = false;
absl::Notification started, shutdown_begin, completion_block, completion_done;
// Run the server in a separate thread so we can test different lifecycle stages.
auto server_thread = Thread::threadFactoryForTest().createThread([&] {
initialize("test/server/node_bootstrap.yaml");
auto handle1 = server_->registerCallback(ServerLifecycleNotifier::Stage::Startup, [&] {
startup = true;
started.Notify();
});
auto handle2 = server_->registerCallback(ServerLifecycleNotifier::Stage::ShutdownExit, [&] {
shutdown = true;
shutdown_begin.Notify();
});
auto handle3 = server_->registerCallback(ServerLifecycleNotifier::Stage::ShutdownExit,
[&](Event::PostCb completion_cb) {
// Block till we're told to complete
completion_block.WaitForNotification();
shutdown_with_completion = true;
server_->dispatcher().post(completion_cb);
completion_done.Notify();
});
auto handle4 =
server_->registerCallback(ServerLifecycleNotifier::Stage::Startup, [&] { FAIL(); });
handle4 = server_->registerCallback(ServerLifecycleNotifier::Stage::ShutdownExit,
[&](Event::PostCb) { FAIL(); });
handle4 = nullptr;
server_->run();
handle1 = nullptr;
handle2 = nullptr;
handle3 = nullptr;
server_ = nullptr;
thread_local_ = nullptr;
});
started.WaitForNotification();
EXPECT_TRUE(startup);
EXPECT_FALSE(shutdown);
server_->dispatcher().post([&] { server_->shutdown(); });
shutdown_begin.WaitForNotification();
EXPECT_TRUE(shutdown);
// Expect the server to block waiting for the completion callback to be invoked
EXPECT_FALSE(completion_done.WaitForNotificationWithTimeout(absl::Seconds(1)));
completion_block.Notify();
completion_done.WaitForNotification();
EXPECT_TRUE(shutdown_with_completion);
server_thread->join();
}
TEST_P(ServerInstanceImplTest, V2ConfigOnly) {
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
try {
initialize(std::string());
FAIL();
} catch (const EnvoyException& e) {
EXPECT_THAT(e.what(), HasSubstr("Unable to parse JSON as proto"));
}
}
TEST_P(ServerInstanceImplTest, Stats) {
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
options_.concurrency_ = 2;
options_.hot_restart_epoch_ = 3;
EXPECT_NO_THROW(initialize("test/server/empty_bootstrap.yaml"));
EXPECT_NE(nullptr, TestUtility::findCounter(stats_store_, "server.watchdog_miss"));
EXPECT_EQ(2L, TestUtility::findGauge(stats_store_, "server.concurrency")->value());
EXPECT_EQ(3L, TestUtility::findGauge(stats_store_, "server.hot_restart_epoch")->value());
// This stat only works in this configuration.
#if defined(NDEBUG) && defined(ENVOY_LOG_DEBUG_ASSERT_IN_RELEASE)
ASSERT(false, "Testing debug assertion failure detection in release build.");
EXPECT_EQ(1L, TestUtility::findCounter(stats_store_, "server.debug_assertion_failures")->value());
#else
EXPECT_EQ(0L, TestUtility::findCounter(stats_store_, "server.debug_assertion_failures")->value());
#endif
}
// Validate server localInfo() from bootstrap Node.
TEST_P(ServerInstanceImplTest, BootstrapNode) {
initialize("test/server/node_bootstrap.yaml");
EXPECT_EQ("bootstrap_zone", server_->localInfo().zoneName());
EXPECT_EQ("bootstrap_cluster", server_->localInfo().clusterName());
EXPECT_EQ("bootstrap_id", server_->localInfo().nodeName());
EXPECT_EQ("bootstrap_sub_zone", server_->localInfo().node().locality().sub_zone());
EXPECT_EQ(VersionInfo::version(), server_->localInfo().node().build_version());
}
// Validate server localInfo() from bootstrap Node with CLI overrides.
TEST_P(ServerInstanceImplTest, BootstrapNodeWithOptionsOverride) {
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
options_.service_zone_name_ = "some_zone_name";
initialize("test/server/node_bootstrap.yaml");
EXPECT_EQ("some_zone_name", server_->localInfo().zoneName());
EXPECT_EQ("some_cluster_name", server_->localInfo().clusterName());
EXPECT_EQ("some_node_name", server_->localInfo().nodeName());
EXPECT_EQ("bootstrap_sub_zone", server_->localInfo().node().locality().sub_zone());
EXPECT_EQ(VersionInfo::version(), server_->localInfo().node().build_version());
}
// Validate server runtime is parsed from bootstrap and that we can read from
// service cluster specified disk-based overrides.
TEST_P(ServerInstanceImplTest, BootstrapRuntime) {
options_.service_cluster_name_ = "some_service";
initialize("test/server/runtime_bootstrap.yaml");
EXPECT_EQ("bar", server_->runtime().snapshot().get("foo"));
// This should access via the override/some_service overlay.
EXPECT_EQ("fozz", server_->runtime().snapshot().get("fizz"));
}
// Validate that a runtime absent an admin layer will fail mutating operations
// but still support inspection of runtime values.
TEST_P(ServerInstanceImplTest, RuntimeNoAdminLayer) {
options_.service_cluster_name_ = "some_service";
initialize("test/server/runtime_bootstrap.yaml");
Http::TestHeaderMapImpl response_headers;
std::string response_body;
EXPECT_EQ(Http::Code::OK,
server_->admin().request("/runtime", "GET", response_headers, response_body));
EXPECT_THAT(response_body, HasSubstr("fozz"));
EXPECT_EQ(
Http::Code::ServiceUnavailable,
server_->admin().request("/runtime_modify?foo=bar", "POST", response_headers, response_body));
EXPECT_EQ("No admin layer specified", response_body);
}
// Validate invalid runtime in bootstrap is rejected.
TEST_P(ServerInstanceImplTest, InvalidBootstrapRuntime) {
EXPECT_THROW_WITH_MESSAGE(initialize("test/server/invalid_runtime_bootstrap.yaml"),
EnvoyException, "Invalid runtime entry value for foo");
}
// Validate invalid layered runtime missing a name is rejected.
TEST_P(ServerInstanceImplTest, InvalidLayeredBootstrapMissingName) {
EXPECT_THROW_WITH_REGEX(initialize("test/server/invalid_layered_runtime_missing_name.yaml"),
EnvoyException,
"RuntimeLayerValidationError.Name: \\[\"value length must be at least");
}
// Validate invalid layered runtime with duplicate names is rejected.
TEST_P(ServerInstanceImplTest, InvalidLayeredBootstrapDuplicateName) {
EXPECT_THROW_WITH_REGEX(initialize("test/server/invalid_layered_runtime_duplicate_name.yaml"),
EnvoyException, "Duplicate layer name: some_static_laye");
}
// Regression test for segfault when server initialization fails prior to
// ClusterManager initialization.
TEST_P(ServerInstanceImplTest, BootstrapClusterManagerInitializationFail) {
EXPECT_THROW_WITH_MESSAGE(initialize("test/server/cluster_dupe_bootstrap.yaml"), EnvoyException,
"cluster manager: duplicate cluster 'service_google'");
}
// Test for protoc-gen-validate constraint on invalid timeout entry of a health check config entry.
TEST_P(ServerInstanceImplTest, BootstrapClusterHealthCheckInvalidTimeout) {
EXPECT_THROW_WITH_REGEX(
initializeWithHealthCheckParams("test/server/cluster_health_check_bootstrap.yaml", 0, 0.25),
EnvoyException,
"HealthCheckValidationError.Timeout: \\[\"value must be greater than \" \"0s\"\\]");
}
// Test for protoc-gen-validate constraint on invalid interval entry of a health check config entry.
TEST_P(ServerInstanceImplTest, BootstrapClusterHealthCheckInvalidInterval) {
EXPECT_THROW_WITH_REGEX(
initializeWithHealthCheckParams("test/server/cluster_health_check_bootstrap.yaml", 0.5, 0),
EnvoyException,
"HealthCheckValidationError.Interval: \\[\"value must be greater than \" \"0s\"\\]");
}
// Test for protoc-gen-validate constraint on invalid timeout and interval entry of a health check
// config entry.
TEST_P(ServerInstanceImplTest, BootstrapClusterHealthCheckInvalidTimeoutAndInterval) {
EXPECT_THROW_WITH_REGEX(
initializeWithHealthCheckParams("test/server/cluster_health_check_bootstrap.yaml", 0, 0),
EnvoyException,
"HealthCheckValidationError.Timeout: \\[\"value must be greater than \" \"0s\"\\]");
}
// Test for protoc-gen-validate constraint on valid interval entry of a health check config entry.
TEST_P(ServerInstanceImplTest, BootstrapClusterHealthCheckValidTimeoutAndInterval) {
EXPECT_NO_THROW(initializeWithHealthCheckParams("test/server/cluster_health_check_bootstrap.yaml",
0.25, 0.5));
}
// Test that a Bootstrap proto with no address specified in its Admin field can go through
// initialization properly, but without starting an admin listener.
TEST_P(ServerInstanceImplTest, BootstrapNodeNoAdmin) {
EXPECT_NO_THROW(initialize("test/server/node_bootstrap_no_admin_port.yaml"));
// Admin::addListenerToHandler() calls one of handler's methods after checking that the Admin
// has a listener. So, the fact that passing a nullptr doesn't cause a segfault establishes
// that there is no listener.
server_->admin().addListenerToHandler(/*handler=*/nullptr);
}
// Validate that an admin config with a server address but no access log path is rejected.
TEST_P(ServerInstanceImplTest, BootstrapNodeWithoutAccessLog) {
EXPECT_THROW_WITH_MESSAGE(initialize("test/server/node_bootstrap_without_access_log.yaml"),
EnvoyException,
"An admin access log path is required for a listening server.");
}
// Empty bootstrap succeeds.
TEST_P(ServerInstanceImplTest, EmptyBootstrap) {
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
EXPECT_NO_THROW(initialize("test/server/empty_bootstrap.yaml"));
}
// Negative test for protoc-gen-validate constraints.
TEST_P(ServerInstanceImplTest, ValidateFail) {
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
try {
initialize("test/server/invalid_bootstrap.yaml");
FAIL();
} catch (const EnvoyException& e) {
EXPECT_THAT(e.what(), HasSubstr("Proto constraint validation failed"));
}
}
TEST_P(ServerInstanceImplTest, LogToFile) {
const std::string path =
TestEnvironment::temporaryPath("ServerInstanceImplTest_LogToFile_Test.log");
options_.log_path_ = path;
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
EXPECT_NO_THROW(initialize("test/server/empty_bootstrap.yaml"));
EXPECT_TRUE(server_->api().fileSystem().fileExists(path));
GET_MISC_LOGGER().set_level(spdlog::level::info);
ENVOY_LOG_MISC(warn, "LogToFile test string");
Logger::Registry::getSink()->flush();
std::string log = server_->api().fileSystem().fileReadToEnd(path);
EXPECT_GT(log.size(), 0);
EXPECT_TRUE(log.find("LogToFile test string") != std::string::npos);
// Test that critical messages get immediately flushed
ENVOY_LOG_MISC(critical, "LogToFile second test string");
log = server_->api().fileSystem().fileReadToEnd(path);
EXPECT_TRUE(log.find("LogToFile second test string") != std::string::npos);
}
TEST_P(ServerInstanceImplTest, LogToFileError) {
options_.log_path_ = "/this/path/does/not/exist";
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
try {
initialize(std::string());
FAIL();
} catch (const EnvoyException& e) {
EXPECT_THAT(e.what(), HasSubstr("Failed to open log-file"));
}
}
// When there are no bootstrap CLI options, either for content or path, we can load the server with
// an empty config.
TEST_P(ServerInstanceImplTest, NoOptionsPassed) {
thread_local_ = std::make_unique<ThreadLocal::InstanceImpl>();
EXPECT_THROW_WITH_MESSAGE(
server_.reset(new InstanceImpl(
options_, test_time_.timeSystem(),
Network::Address::InstanceConstSharedPtr(new Network::Address::Ipv4Instance("127.0.0.1")),
hooks_, restart_, stats_store_, fakelock_, component_factory_,
std::make_unique<NiceMock<Runtime::MockRandomGenerator>>(), *thread_local_,
Thread::threadFactoryForTest(), Filesystem::fileSystemForTest(), nullptr)),
EnvoyException, "At least one of --config-path and --config-yaml should be non-empty");
}
// Validate that when std::exception is unexpectedly thrown, we exit safely.
// This is a regression test for when we used to crash.
TEST_P(ServerInstanceImplTest, StdExceptionThrowInConstructor) {
EXPECT_CALL(restart_, initialize(_, _)).WillOnce(InvokeWithoutArgs([] {
throw(std::runtime_error("foobar"));
}));
EXPECT_THROW_WITH_MESSAGE(initialize("test/server/node_bootstrap.yaml"), std::runtime_error,
"foobar");
}
// Neither EnvoyException nor std::exception derived.
class FakeException {
public:
FakeException(const std::string& what) : what_(what) {}
const std::string& what() const { return what_; }
const std::string what_;
};
// Validate that when a totally unknown exception is unexpectedly thrown, we
// exit safely. This is a regression test for when we used to crash.
TEST_P(ServerInstanceImplTest, UnknownExceptionThrowInConstructor) {
EXPECT_CALL(restart_, initialize(_, _)).WillOnce(InvokeWithoutArgs([] {
throw(FakeException("foobar"));
}));
EXPECT_THROW_WITH_MESSAGE(initialize("test/server/node_bootstrap.yaml"), FakeException, "foobar");
}
TEST_P(ServerInstanceImplTest, MutexContentionEnabled) {
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
options_.mutex_tracing_enabled_ = true;
EXPECT_NO_THROW(initialize("test/server/empty_bootstrap.yaml"));
}
TEST_P(ServerInstanceImplTest, NoHttpTracing) {
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
EXPECT_NO_THROW(initialize("test/server/empty_bootstrap.yaml"));
EXPECT_NE(nullptr, dynamic_cast<Tracing::HttpNullTracer*>(tracer()));
EXPECT_EQ(nullptr, dynamic_cast<Tracing::HttpTracerImpl*>(tracer()));
}
TEST_P(ServerInstanceImplTest, ZipkinHttpTracingEnabled) {
options_.service_cluster_name_ = "some_cluster_name";
options_.service_node_name_ = "some_node_name";
EXPECT_NO_THROW(initialize("test/server/zipkin_tracing.yaml"));
EXPECT_EQ(nullptr, dynamic_cast<Tracing::HttpNullTracer*>(tracer()));
// Note: there is no ZipkinTracerImpl object;
// source/extensions/tracers/zipkin/config.cc instantiates the tracer with
// std::make_unique<Tracing::HttpTracerImpl>(std::move(zipkin_driver), server.localInfo());
// so we look for a successful dynamic cast to HttpTracerImpl, rather
// than HttpNullTracer.
EXPECT_NE(nullptr, dynamic_cast<Tracing::HttpTracerImpl*>(tracer()));
}
class TestObject : public ProcessObject {
public:
void setFlag(bool value) { boolean_flag_ = value; }
bool boolean_flag_ = true;
};
TEST_P(ServerInstanceImplTest, WithProcessContext) {
TestObject object;
process_object_ = &object;
EXPECT_NO_THROW(initialize("test/server/empty_bootstrap.yaml"));
ProcessContext& context = server_->processContext();
auto& object_from_context = dynamic_cast<TestObject&>(context.get());
EXPECT_EQ(&object_from_context, &object);
EXPECT_TRUE(object_from_context.boolean_flag_);
object.boolean_flag_ = false;
EXPECT_FALSE(object_from_context.boolean_flag_);
}
} // namespace
} // namespace Server
} // namespace Envoy