From 7d5e12cb31bce4750838fa3f02a1e4e26261c067 Mon Sep 17 00:00:00 2001 From: foreseeable Date: Mon, 6 Jul 2020 22:36:11 +0000 Subject: [PATCH] test: Break down huge monolith mock header to improve test compilation performance (#11797) Commit Message: breakdown `test/mocks/server/mocks.h` into different mock classes Additional Description: `test/mocks/server/mocks.h` is a wide-used mock header included by various test files. However it's very huge and most test files only used a small portion of it. Splitting it up into different mock classes will be helpful to reduce compilation time. Risk Level: low Testing: existing tests Docs Changes: N/A Release Notes: no Related Issues: #10917 Signed-off-by: Muge Chen --- test/mocks/server/BUILD | 275 ++++++- test/mocks/server/admin.cc | 3 - test/mocks/server/admin.h | 1 - test/mocks/server/admin_stream.cc | 15 + test/mocks/server/admin_stream.h | 25 + .../server/bootstrap_extension_factory.cc | 12 + .../server/bootstrap_extension_factory.h | 22 + test/mocks/server/config_tracker.h | 1 - test/mocks/server/drain_manager.cc | 21 + test/mocks/server/drain_manager.h | 28 + test/mocks/server/factory_context.cc | 47 ++ test/mocks/server/factory_context.h | 75 ++ .../server/filter_chain_factory_context.cc | 17 + .../server/filter_chain_factory_context.h | 17 + test/mocks/server/guard_dog.cc | 20 + test/mocks/server/guard_dog.h | 23 + .../server/health_checker_factory_context.cc | 30 + .../server/health_checker_factory_context.h | 43 ++ test/mocks/server/hot_restart.cc | 22 + test/mocks/server/hot_restart.h | 38 + test/mocks/server/instance.cc | 81 +++ test/mocks/server/instance.h | 170 +++++ .../server/listener_component_factory.cc | 31 + .../mocks/server/listener_component_factory.h | 47 ++ test/mocks/server/listener_factory_context.cc | 15 + test/mocks/server/listener_factory_context.h | 23 + test/mocks/server/listener_manager.cc | 16 + test/mocks/server/listener_manager.h | 29 + test/mocks/server/main.cc | 24 + test/mocks/server/main.h | 37 + test/mocks/server/mocks.cc | 295 -------- test/mocks/server/mocks.h | 679 +----------------- test/mocks/server/options.cc | 51 ++ test/mocks/server/options.h | 76 ++ test/mocks/server/overload_manager.cc | 19 + test/mocks/server/overload_manager.h | 26 + .../mocks/server/server_lifecycle_notifier.cc | 16 + test/mocks/server/server_lifecycle_notifier.h | 20 + test/mocks/server/tracer_factory.cc | 24 + test/mocks/server/tracer_factory.h | 27 + test/mocks/server/tracer_factory_context.cc | 24 + test/mocks/server/tracer_factory_context.h | 25 + .../transport_socket_factory_context.cc | 26 + .../server/transport_socket_factory_context.h | 44 ++ test/mocks/server/watch_dog.cc | 16 + test/mocks/server/watch_dog.h | 21 + test/mocks/server/worker.cc | 50 ++ test/mocks/server/worker.h | 54 ++ test/mocks/server/worker_factory.cc | 14 + test/mocks/server/worker_factory.h | 23 + 50 files changed, 1763 insertions(+), 975 deletions(-) create mode 100644 test/mocks/server/admin_stream.cc create mode 100644 test/mocks/server/admin_stream.h create mode 100644 test/mocks/server/bootstrap_extension_factory.cc create mode 100644 test/mocks/server/bootstrap_extension_factory.h create mode 100644 test/mocks/server/drain_manager.cc create mode 100644 test/mocks/server/drain_manager.h create mode 100644 test/mocks/server/factory_context.cc create mode 100644 test/mocks/server/factory_context.h create mode 100644 test/mocks/server/filter_chain_factory_context.cc create mode 100644 test/mocks/server/filter_chain_factory_context.h create mode 100644 test/mocks/server/guard_dog.cc create mode 100644 test/mocks/server/guard_dog.h create mode 100644 test/mocks/server/health_checker_factory_context.cc create mode 100644 test/mocks/server/health_checker_factory_context.h create mode 100644 test/mocks/server/hot_restart.cc create mode 100644 test/mocks/server/hot_restart.h create mode 100644 test/mocks/server/instance.cc create mode 100644 test/mocks/server/instance.h create mode 100644 test/mocks/server/listener_component_factory.cc create mode 100644 test/mocks/server/listener_component_factory.h create mode 100644 test/mocks/server/listener_factory_context.cc create mode 100644 test/mocks/server/listener_factory_context.h create mode 100644 test/mocks/server/listener_manager.cc create mode 100644 test/mocks/server/listener_manager.h create mode 100644 test/mocks/server/main.cc create mode 100644 test/mocks/server/main.h delete mode 100644 test/mocks/server/mocks.cc create mode 100644 test/mocks/server/options.cc create mode 100644 test/mocks/server/options.h create mode 100644 test/mocks/server/overload_manager.cc create mode 100644 test/mocks/server/overload_manager.h create mode 100644 test/mocks/server/server_lifecycle_notifier.cc create mode 100644 test/mocks/server/server_lifecycle_notifier.h create mode 100644 test/mocks/server/tracer_factory.cc create mode 100644 test/mocks/server/tracer_factory.h create mode 100644 test/mocks/server/tracer_factory_context.cc create mode 100644 test/mocks/server/tracer_factory_context.h create mode 100644 test/mocks/server/transport_socket_factory_context.cc create mode 100644 test/mocks/server/transport_socket_factory_context.h create mode 100644 test/mocks/server/watch_dog.cc create mode 100644 test/mocks/server/watch_dog.h create mode 100644 test/mocks/server/worker.cc create mode 100644 test/mocks/server/worker.h create mode 100644 test/mocks/server/worker_factory.cc create mode 100644 test/mocks/server/worker_factory.h diff --git a/test/mocks/server/BUILD b/test/mocks/server/BUILD index a907ca21feb7..35a1ac14b80a 100644 --- a/test/mocks/server/BUILD +++ b/test/mocks/server/BUILD @@ -28,25 +28,138 @@ envoy_cc_mock( ) envoy_cc_mock( - name = "server_mocks", - srcs = ["mocks.cc"], - hdrs = ["mocks.h"], + name = "bootstrap_extension_factory_mocks", + srcs = ["bootstrap_extension_factory.cc"], + hdrs = ["bootstrap_extension_factory.h"], deps = [ - "//include/envoy/secret:secret_manager_interface", - "//include/envoy/server:admin_interface", "//include/envoy/server:bootstrap_extension_config_interface", - "//include/envoy/server:configuration_interface", + ], +) + +envoy_cc_mock( + name = "options_mocks", + srcs = ["options.cc"], + hdrs = ["options.h"], + deps = [ + "//include/envoy/server:options_interface", + "@envoy_api//envoy/admin/v3:pkg_cc_proto", + "@envoy_api//envoy/config/bootstrap/v3:pkg_cc_proto", + ], +) + +envoy_cc_mock( + name = "admin_stream_mocks", + srcs = ["admin_stream.cc"], + hdrs = ["admin_stream.h"], + deps = [ + "//include/envoy/server:admin_interface", + "//test/mocks/http:http_mocks", + ], +) + +envoy_cc_mock( + name = "drain_manager_mocks", + srcs = ["drain_manager.cc"], + hdrs = ["drain_manager.h"], + deps = [ "//include/envoy/server:drain_manager_interface", - "//include/envoy/server:filter_config_interface", + ], +) + +envoy_cc_mock( + name = "watch_dog_mocks", + srcs = ["watch_dog.cc"], + hdrs = ["watch_dog.h"], + deps = [ + "//include/envoy/server:watchdog_interface", + ], +) + +envoy_cc_mock( + name = "guard_dog_mocks", + srcs = ["guard_dog.cc"], + hdrs = ["guard_dog.h"], + deps = [ "//include/envoy/server:guarddog_interface", - "//include/envoy/server:health_checker_config_interface", + "//test/mocks/server:watch_dog_mocks", + ], +) + +envoy_cc_mock( + name = "hot_restart_mocks", + srcs = ["hot_restart.cc"], + hdrs = ["hot_restart.h"], + deps = [ "//include/envoy/server:instance_interface", - "//include/envoy/server:options_interface", - "//include/envoy/server:overload_manager_interface", - "//include/envoy/server:tracer_config_interface", + "//test/mocks/stats:stats_mocks", + ], +) + +envoy_cc_mock( + name = "listener_component_factory_mocks", + srcs = ["listener_component_factory.cc"], + hdrs = ["listener_component_factory.h"], + deps = [ + "//include/envoy/server:drain_manager_interface", + "//include/envoy/server:listener_manager_interface", + "//test/mocks/network:network_mocks", + "@envoy_api//envoy/config/core/v3:pkg_cc_proto", + "@envoy_api//envoy/config/listener/v3:pkg_cc_proto", + ], +) + +envoy_cc_mock( + name = "listener_manager_mocks", + srcs = ["listener_manager.cc"], + hdrs = ["listener_manager.h"], + deps = [ + "//include/envoy/server:listener_manager_interface", + ], +) + +envoy_cc_mock( + name = "server_lifecycle_notifier_mocks", + srcs = ["server_lifecycle_notifier.cc"], + hdrs = ["server_lifecycle_notifier.h"], + deps = [ + "//include/envoy/server:lifecycle_notifier_interface", + ], +) + +envoy_cc_mock( + name = "worker_factory_mocks", + srcs = ["worker_factory.cc"], + hdrs = ["worker_factory.h"], + deps = [ "//include/envoy/server:worker_interface", - "//include/envoy/ssl:context_manager_interface", - "//include/envoy/upstream:health_checker_interface", + "//test/mocks/server:worker_mocks", + ], +) + +envoy_cc_mock( + name = "worker_mocks", + srcs = ["worker.cc"], + hdrs = ["worker.h"], + deps = [ + "//include/envoy/server:worker_interface", + ], +) + +envoy_cc_mock( + name = "overload_manager_mocks", + srcs = ["overload_manager.cc"], + hdrs = ["overload_manager.h"], + deps = [ + "//include/envoy/server:overload_manager_interface", + ], +) + +envoy_cc_mock( + name = "instance_mocks", + srcs = ["instance.cc"], + hdrs = ["instance.h"], + deps = [ + "//include/envoy/server:instance_interface", "//source/common/grpc:context_lib", "//source/common/http:context_lib", "//source/common/secret:secret_manager_impl_lib", @@ -64,14 +177,138 @@ envoy_cc_mock( "//test/mocks/runtime:runtime_mocks", "//test/mocks/secret:secret_mocks", "//test/mocks/server:admin_mocks", - "//test/mocks/server:config_tracker_mocks", + "//test/mocks/server:drain_manager_mocks", + "//test/mocks/server:hot_restart_mocks", + "//test/mocks/server:listener_manager_mocks", + "//test/mocks/server:options_mocks", + "//test/mocks/server:overload_manager_mocks", + "//test/mocks/server:server_lifecycle_notifier_mocks", + "//test/mocks/server:transport_socket_factory_context_mocks", "//test/mocks/thread_local:thread_local_mocks", "//test/mocks/tracing:tracing_mocks", "//test/mocks/upstream:upstream_mocks", - "//test/test_common:test_time_lib", - "@envoy_api//envoy/admin/v3:pkg_cc_proto", - "@envoy_api//envoy/config/bootstrap/v3:pkg_cc_proto", - "@envoy_api//envoy/config/core/v3:pkg_cc_proto", - "@envoy_api//envoy/config/listener/v3:pkg_cc_proto", + ], +) + +envoy_cc_mock( + name = "main_mocks", + srcs = ["main.cc"], + hdrs = ["main.h"], + deps = [ + "//include/envoy/server:configuration_interface", + "//include/envoy/server:overload_manager_interface", + ], +) + +envoy_cc_mock( + name = "factory_context_mocks", + srcs = ["factory_context.cc"], + hdrs = ["factory_context.h"], + deps = [ + "//test/mocks/server:drain_manager_mocks", + "//test/mocks/server:instance_mocks", + "//test/mocks/server:overload_manager_mocks", + "//test/mocks/server:server_lifecycle_notifier_mocks", + ], +) + +envoy_cc_mock( + name = "transport_socket_factory_context_mocks", + srcs = ["transport_socket_factory_context.cc"], + hdrs = ["transport_socket_factory_context.h"], + deps = [ + "//include/envoy/server:tracer_config_interface", + "//source/common/secret:secret_manager_impl_lib", + "//test/mocks/api:api_mocks", + "//test/mocks/server:config_tracker_mocks", + "//test/mocks/upstream:upstream_mocks", + ], +) + +envoy_cc_mock( + name = "listener_factory_context_mocks", + srcs = ["listener_factory_context.cc"], + hdrs = ["listener_factory_context.h"], + deps = [ + "//include/envoy/server:listener_manager_interface", + "//test/mocks/server:factory_context_mocks", + ], +) + +envoy_cc_mock( + name = "health_checker_factory_context_mocks", + srcs = ["health_checker_factory_context.cc"], + hdrs = ["health_checker_factory_context.h"], + deps = [ + "//include/envoy/server:health_checker_config_interface", + "//test/mocks/api:api_mocks", + "//test/mocks/event:event_mocks", + "//test/mocks/protobuf:protobuf_mocks", + "//test/mocks/router:router_mocks", + "//test/mocks/runtime:runtime_mocks", + "//test/mocks/upstream:upstream_mocks", + ], +) + +envoy_cc_mock( + name = "filter_chain_factory_context_mocks", + srcs = ["filter_chain_factory_context.cc"], + hdrs = ["filter_chain_factory_context.h"], + deps = [ + "//include/envoy/server:filter_config_interface", + "//test/mocks/server:factory_context_mocks", + ], +) + +envoy_cc_mock( + name = "tracer_factory_mocks", + srcs = ["tracer_factory.cc"], + hdrs = ["tracer_factory.h"], + deps = [ + "//include/envoy/protobuf:message_validator_interface", + "//include/envoy/server:tracer_config_interface", + ], +) + +envoy_cc_mock( + name = "tracer_factory_context_mocks", + srcs = ["tracer_factory_context.cc"], + hdrs = ["tracer_factory_context.h"], + deps = [ + "//include/envoy/server:configuration_interface", + "//test/mocks/server:instance_mocks", + "//test/mocks/server:tracer_factory_mocks", + ], +) + +envoy_cc_mock( + name = "server_mocks", + srcs = [], + hdrs = ["mocks.h"], + deps = [ + "//test/mocks/server:admin_mocks", + "//test/mocks/server:admin_stream_mocks", + "//test/mocks/server:bootstrap_extension_factory_mocks", + "//test/mocks/server:config_tracker_mocks", + "//test/mocks/server:drain_manager_mocks", + "//test/mocks/server:factory_context_mocks", + "//test/mocks/server:filter_chain_factory_context_mocks", + "//test/mocks/server:guard_dog_mocks", + "//test/mocks/server:health_checker_factory_context_mocks", + "//test/mocks/server:hot_restart_mocks", + "//test/mocks/server:instance_mocks", + "//test/mocks/server:listener_component_factory_mocks", + "//test/mocks/server:listener_factory_context_mocks", + "//test/mocks/server:listener_manager_mocks", + "//test/mocks/server:main_mocks", + "//test/mocks/server:options_mocks", + "//test/mocks/server:overload_manager_mocks", + "//test/mocks/server:server_lifecycle_notifier_mocks", + "//test/mocks/server:tracer_factory_context_mocks", + "//test/mocks/server:tracer_factory_mocks", + "//test/mocks/server:transport_socket_factory_context_mocks", + "//test/mocks/server:watch_dog_mocks", + "//test/mocks/server:worker_factory_mocks", + "//test/mocks/server:worker_mocks", ], ) diff --git a/test/mocks/server/admin.cc b/test/mocks/server/admin.cc index 2411ef375f00..435c14f6e973 100644 --- a/test/mocks/server/admin.cc +++ b/test/mocks/server/admin.cc @@ -1,7 +1,5 @@ #include "admin.h" -#include - #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -14,5 +12,4 @@ MockAdmin::MockAdmin() { MockAdmin::~MockAdmin() = default; } // namespace Server - } // namespace Envoy diff --git a/test/mocks/server/admin.h b/test/mocks/server/admin.h index 2a82d23859c6..512e3286bfca 100644 --- a/test/mocks/server/admin.h +++ b/test/mocks/server/admin.h @@ -35,5 +35,4 @@ class MockAdmin : public Admin { ::testing::NiceMock config_tracker_; }; } // namespace Server - } // namespace Envoy diff --git a/test/mocks/server/admin_stream.cc b/test/mocks/server/admin_stream.cc new file mode 100644 index 000000000000..9ed778013b18 --- /dev/null +++ b/test/mocks/server/admin_stream.cc @@ -0,0 +1,15 @@ +#include "admin_stream.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +MockAdminStream::MockAdminStream() = default; + +MockAdminStream::~MockAdminStream() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/admin_stream.h b/test/mocks/server/admin_stream.h new file mode 100644 index 000000000000..d7972b4ae27c --- /dev/null +++ b/test/mocks/server/admin_stream.h @@ -0,0 +1,25 @@ +#pragma once + +#include "envoy/server/admin.h" + +#include "test/mocks/http/mocks.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockAdminStream : public AdminStream { +public: + MockAdminStream(); + ~MockAdminStream() override; + + MOCK_METHOD(void, setEndStreamOnComplete, (bool)); + MOCK_METHOD(void, addOnDestroyCallback, (std::function)); + MOCK_METHOD(const Buffer::Instance*, getRequestBody, (), (const)); + MOCK_METHOD(Http::RequestHeaderMap&, getRequestHeaders, (), (const)); + MOCK_METHOD(NiceMock&, getDecoderFilterCallbacks, (), + (const)); + MOCK_METHOD(Http::Http1StreamEncoderOptionsOptRef, http1StreamEncoderOptions, ()); +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/bootstrap_extension_factory.cc b/test/mocks/server/bootstrap_extension_factory.cc new file mode 100644 index 000000000000..80984ea4093d --- /dev/null +++ b/test/mocks/server/bootstrap_extension_factory.cc @@ -0,0 +1,12 @@ +#include "bootstrap_extension_factory.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +MockBootstrapExtensionFactory::MockBootstrapExtensionFactory() = default; + +MockBootstrapExtensionFactory::~MockBootstrapExtensionFactory() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/bootstrap_extension_factory.h b/test/mocks/server/bootstrap_extension_factory.h new file mode 100644 index 000000000000..f6421f788788 --- /dev/null +++ b/test/mocks/server/bootstrap_extension_factory.h @@ -0,0 +1,22 @@ +#pragma once + +#include "envoy/server/bootstrap_extension_config.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockBootstrapExtensionFactory : public BootstrapExtensionFactory { +public: + MockBootstrapExtensionFactory(); + ~MockBootstrapExtensionFactory() override; + + MOCK_METHOD(BootstrapExtensionPtr, createBootstrapExtension, + (const Protobuf::Message&, Configuration::ServerFactoryContext&), (override)); + MOCK_METHOD(ProtobufTypes::MessagePtr, createEmptyConfigProto, (), (override)); + MOCK_METHOD(std::string, name, (), (const, override)); +}; +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/config_tracker.h b/test/mocks/server/config_tracker.h index a84b87c7feb0..09f516f0e03c 100644 --- a/test/mocks/server/config_tracker.h +++ b/test/mocks/server/config_tracker.h @@ -26,5 +26,4 @@ class MockConfigTracker : public ConfigTracker { std::unordered_map config_tracker_callbacks_; }; } // namespace Server - } // namespace Envoy diff --git a/test/mocks/server/drain_manager.cc b/test/mocks/server/drain_manager.cc new file mode 100644 index 000000000000..15735d57834c --- /dev/null +++ b/test/mocks/server/drain_manager.cc @@ -0,0 +1,21 @@ +#include "drain_manager.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::_; +using ::testing::SaveArg; + +MockDrainManager::MockDrainManager() { + ON_CALL(*this, startDrainSequence(_)).WillByDefault(SaveArg<0>(&drain_sequence_completion_)); +} + +MockDrainManager::~MockDrainManager() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/drain_manager.h b/test/mocks/server/drain_manager.h new file mode 100644 index 000000000000..dc0331b05876 --- /dev/null +++ b/test/mocks/server/drain_manager.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include +#include + +#include "envoy/server/drain_manager.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockDrainManager : public DrainManager { +public: + MockDrainManager(); + ~MockDrainManager() override; + + // Server::DrainManager + MOCK_METHOD(bool, drainClose, (), (const)); + MOCK_METHOD(bool, draining, (), (const)); + MOCK_METHOD(void, startDrainSequence, (std::function completion)); + MOCK_METHOD(void, startParentShutdownSequence, ()); + + std::function drain_sequence_completion_; +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/factory_context.cc b/test/mocks/server/factory_context.cc new file mode 100644 index 000000000000..974371a1d3b5 --- /dev/null +++ b/test/mocks/server/factory_context.cc @@ -0,0 +1,47 @@ +#include "factory_context.h" + +#include + +#include "common/singleton/manager_impl.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +namespace Configuration { + +using ::testing::ReturnRef; + +MockFactoryContext::MockFactoryContext() + : singleton_manager_(new Singleton::ManagerImpl(Thread::threadFactoryForTest())), + grpc_context_(scope_.symbolTable()), http_context_(scope_.symbolTable()) { + ON_CALL(*this, getServerFactoryContext()).WillByDefault(ReturnRef(server_factory_context_)); + ON_CALL(*this, accessLogManager()).WillByDefault(ReturnRef(access_log_manager_)); + ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_)); + ON_CALL(*this, dispatcher()).WillByDefault(ReturnRef(dispatcher_)); + ON_CALL(*this, drainDecision()).WillByDefault(ReturnRef(drain_manager_)); + ON_CALL(*this, initManager()).WillByDefault(ReturnRef(init_manager_)); + ON_CALL(*this, lifecycleNotifier()).WillByDefault(ReturnRef(lifecycle_notifier_)); + ON_CALL(*this, localInfo()).WillByDefault(ReturnRef(local_info_)); + ON_CALL(*this, random()).WillByDefault(ReturnRef(random_)); + ON_CALL(*this, runtime()).WillByDefault(ReturnRef(runtime_loader_)); + ON_CALL(*this, scope()).WillByDefault(ReturnRef(scope_)); + ON_CALL(*this, singletonManager()).WillByDefault(ReturnRef(*singleton_manager_)); + ON_CALL(*this, threadLocal()).WillByDefault(ReturnRef(thread_local_)); + ON_CALL(*this, admin()).WillByDefault(ReturnRef(admin_)); + ON_CALL(*this, listenerScope()).WillByDefault(ReturnRef(listener_scope_)); + ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); + ON_CALL(*this, timeSource()).WillByDefault(ReturnRef(time_system_)); + ON_CALL(*this, overloadManager()).WillByDefault(ReturnRef(overload_manager_)); + ON_CALL(*this, messageValidationContext()).WillByDefault(ReturnRef(validation_context_)); + ON_CALL(*this, messageValidationVisitor()) + .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); + ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); +} + +MockFactoryContext::~MockFactoryContext() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/factory_context.h b/test/mocks/server/factory_context.h new file mode 100644 index 000000000000..cd1c70d4ecd6 --- /dev/null +++ b/test/mocks/server/factory_context.h @@ -0,0 +1,75 @@ +#pragma once + +#include "envoy/server/configuration.h" + +#include "extensions/transport_sockets/tls/context_manager_impl.h" + +#include "admin.h" +#include "drain_manager.h" +#include "gmock/gmock.h" +#include "instance.h" +#include "overload_manager.h" +#include "server_lifecycle_notifier.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockFactoryContext : public virtual FactoryContext { +public: + MockFactoryContext(); + ~MockFactoryContext() override; + + MOCK_METHOD(ServerFactoryContext&, getServerFactoryContext, (), (const)); + MOCK_METHOD(TransportSocketFactoryContext&, getTransportSocketFactoryContext, (), (const)); + MOCK_METHOD(AccessLog::AccessLogManager&, accessLogManager, ()); + MOCK_METHOD(Upstream::ClusterManager&, clusterManager, ()); + MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); + MOCK_METHOD(const Network::DrainDecision&, drainDecision, ()); + MOCK_METHOD(bool, healthCheckFailed, ()); + MOCK_METHOD(Init::Manager&, initManager, ()); + MOCK_METHOD(ServerLifecycleNotifier&, lifecycleNotifier, ()); + MOCK_METHOD(Envoy::Runtime::RandomGenerator&, random, ()); + MOCK_METHOD(Envoy::Runtime::Loader&, runtime, ()); + MOCK_METHOD(Stats::Scope&, scope, ()); + MOCK_METHOD(Singleton::Manager&, singletonManager, ()); + MOCK_METHOD(OverloadManager&, overloadManager, ()); + MOCK_METHOD(ThreadLocal::Instance&, threadLocal, ()); + MOCK_METHOD(Server::Admin&, admin, ()); + MOCK_METHOD(Stats::Scope&, listenerScope, ()); + MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const)); + MOCK_METHOD(const envoy::config::core::v3::Metadata&, listenerMetadata, (), (const)); + MOCK_METHOD(envoy::config::core::v3::TrafficDirection, direction, (), (const)); + MOCK_METHOD(TimeSource&, timeSource, ()); + Event::TestTimeSystem& timeSystem() { return time_system_; } + Grpc::Context& grpcContext() override { return grpc_context_; } + Http::Context& httpContext() override { return http_context_; } + MOCK_METHOD(ProcessContextOptRef, processContext, ()); + MOCK_METHOD(ProtobufMessage::ValidationContext&, messageValidationContext, ()); + MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); + MOCK_METHOD(Api::Api&, api, ()); + + testing::NiceMock server_factory_context_; + testing::NiceMock access_log_manager_; + testing::NiceMock cluster_manager_; + testing::NiceMock dispatcher_; + testing::NiceMock drain_manager_; + testing::NiceMock init_manager_; + testing::NiceMock lifecycle_notifier_; + testing::NiceMock local_info_; + testing::NiceMock random_; + testing::NiceMock runtime_loader_; + testing::NiceMock scope_; + testing::NiceMock thread_local_; + Singleton::ManagerPtr singleton_manager_; + testing::NiceMock admin_; + Stats::IsolatedStoreImpl listener_scope_; + Event::GlobalTimeSystem time_system_; + testing::NiceMock validation_context_; + testing::NiceMock overload_manager_; + Grpc::ContextImpl grpc_context_; + Http::ContextImpl http_context_; + testing::NiceMock api_; +}; +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/filter_chain_factory_context.cc b/test/mocks/server/filter_chain_factory_context.cc new file mode 100644 index 000000000000..674f95f4a558 --- /dev/null +++ b/test/mocks/server/filter_chain_factory_context.cc @@ -0,0 +1,17 @@ +#include "filter_chain_factory_context.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +MockFilterChainFactoryContext::MockFilterChainFactoryContext() = default; + +MockFilterChainFactoryContext::~MockFilterChainFactoryContext() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/filter_chain_factory_context.h b/test/mocks/server/filter_chain_factory_context.h new file mode 100644 index 000000000000..f09e7a565722 --- /dev/null +++ b/test/mocks/server/filter_chain_factory_context.h @@ -0,0 +1,17 @@ +#pragma once + +#include "envoy/server/filter_config.h" + +#include "factory_context.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockFilterChainFactoryContext : public MockFactoryContext, public FilterChainFactoryContext { +public: + MockFilterChainFactoryContext(); + ~MockFilterChainFactoryContext() override; +}; +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/guard_dog.cc b/test/mocks/server/guard_dog.cc new file mode 100644 index 000000000000..e5e552c234f5 --- /dev/null +++ b/test/mocks/server/guard_dog.cc @@ -0,0 +1,20 @@ +#include "guard_dog.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::_; +using ::testing::NiceMock; +using ::testing::Return; + +MockGuardDog::MockGuardDog() : watch_dog_(new NiceMock()) { + ON_CALL(*this, createWatchDog(_, _)).WillByDefault(Return(watch_dog_)); +} + +MockGuardDog::~MockGuardDog() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/guard_dog.h b/test/mocks/server/guard_dog.h new file mode 100644 index 000000000000..fed29041db3e --- /dev/null +++ b/test/mocks/server/guard_dog.h @@ -0,0 +1,23 @@ +#pragma once + +#include "envoy/server/guarddog.h" + +#include "gmock/gmock.h" +#include "watch_dog.h" + +namespace Envoy { +namespace Server { +class MockGuardDog : public GuardDog { +public: + MockGuardDog(); + ~MockGuardDog() override; + + // Server::GuardDog + MOCK_METHOD(WatchDogSharedPtr, createWatchDog, + (Thread::ThreadId thread_id, const std::string& thread_name)); + MOCK_METHOD(void, stopWatching, (WatchDogSharedPtr wd)); + + std::shared_ptr watch_dog_; +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/health_checker_factory_context.cc b/test/mocks/server/health_checker_factory_context.cc new file mode 100644 index 000000000000..f6a17d962e2a --- /dev/null +++ b/test/mocks/server/health_checker_factory_context.cc @@ -0,0 +1,30 @@ +#include "health_checker_factory_context.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +namespace Configuration { + +using ::testing::ReturnRef; + +MockHealthCheckerFactoryContext::MockHealthCheckerFactoryContext() { + event_logger_ = new testing::NiceMock(); + ON_CALL(*this, cluster()).WillByDefault(ReturnRef(cluster_)); + ON_CALL(*this, dispatcher()).WillByDefault(ReturnRef(dispatcher_)); + ON_CALL(*this, random()).WillByDefault(ReturnRef(random_)); + ON_CALL(*this, runtime()).WillByDefault(ReturnRef(runtime_)); + ON_CALL(*this, eventLogger_()).WillByDefault(Return(event_logger_)); + ON_CALL(*this, messageValidationVisitor()) + .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); + ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); +} + +MockHealthCheckerFactoryContext::~MockHealthCheckerFactoryContext() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/health_checker_factory_context.h b/test/mocks/server/health_checker_factory_context.h new file mode 100644 index 000000000000..1d49abffb481 --- /dev/null +++ b/test/mocks/server/health_checker_factory_context.h @@ -0,0 +1,43 @@ +#pragma once + +#include "envoy/server/health_checker_config.h" + +#include "test/mocks/api/mocks.h" +#include "test/mocks/event/mocks.h" +#include "test/mocks/protobuf/mocks.h" +#include "test/mocks/router/mocks.h" +#include "test/mocks/runtime/mocks.h" +#include "test/mocks/upstream/mocks.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockHealthCheckerFactoryContext : public virtual HealthCheckerFactoryContext { +public: + MockHealthCheckerFactoryContext(); + ~MockHealthCheckerFactoryContext() override; + + MOCK_METHOD(Upstream::Cluster&, cluster, ()); + MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); + MOCK_METHOD(Envoy::Runtime::RandomGenerator&, random, ()); + MOCK_METHOD(Envoy::Runtime::Loader&, runtime, ()); + MOCK_METHOD(Upstream::HealthCheckEventLogger*, eventLogger_, ()); + MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); + MOCK_METHOD(Api::Api&, api, ()); + Upstream::HealthCheckEventLoggerPtr eventLogger() override { + return Upstream::HealthCheckEventLoggerPtr(eventLogger_()); + } + + testing::NiceMock cluster_; + testing::NiceMock dispatcher_; + testing::NiceMock random_; + testing::NiceMock runtime_; + testing::NiceMock* event_logger_{}; + testing::NiceMock api_{}; +}; +} // namespace Configuration + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/hot_restart.cc b/test/mocks/server/hot_restart.cc new file mode 100644 index 000000000000..8a11dbe8011d --- /dev/null +++ b/test/mocks/server/hot_restart.cc @@ -0,0 +1,22 @@ +#include "hot_restart.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::ReturnRef; + +MockHotRestart::MockHotRestart() : stats_allocator_(*symbol_table_) { + ON_CALL(*this, logLock()).WillByDefault(ReturnRef(log_lock_)); + ON_CALL(*this, accessLogLock()).WillByDefault(ReturnRef(access_log_lock_)); + ON_CALL(*this, statsAllocator()).WillByDefault(ReturnRef(stats_allocator_)); +} + +MockHotRestart::~MockHotRestart() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/hot_restart.h b/test/mocks/server/hot_restart.h new file mode 100644 index 000000000000..c6edd13d8905 --- /dev/null +++ b/test/mocks/server/hot_restart.h @@ -0,0 +1,38 @@ +#pragma once + +#include "envoy/server/instance.h" + +#include "test/mocks/stats/mocks.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockHotRestart : public HotRestart { +public: + MockHotRestart(); + ~MockHotRestart() override; + + // Server::HotRestart + MOCK_METHOD(void, drainParentListeners, ()); + MOCK_METHOD(int, duplicateParentListenSocket, (const std::string& address)); + MOCK_METHOD(std::unique_ptr, getParentStats, ()); + MOCK_METHOD(void, initialize, (Event::Dispatcher & dispatcher, Server::Instance& server)); + MOCK_METHOD(void, sendParentAdminShutdownRequest, (time_t & original_start_time)); + MOCK_METHOD(void, sendParentTerminateRequest, ()); + MOCK_METHOD(ServerStatsFromParent, mergeParentStatsIfAny, (Stats::StoreRoot & stats_store)); + MOCK_METHOD(void, shutdown, ()); + MOCK_METHOD(uint32_t, baseId, ()); + MOCK_METHOD(std::string, version, ()); + MOCK_METHOD(Thread::BasicLockable&, logLock, ()); + MOCK_METHOD(Thread::BasicLockable&, accessLogLock, ()); + MOCK_METHOD(Stats::Allocator&, statsAllocator, ()); + +private: + Stats::TestSymbolTable symbol_table_; + Thread::MutexBasicLockable log_lock_; + Thread::MutexBasicLockable access_log_lock_; + Stats::AllocatorImpl stats_allocator_; +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/instance.cc b/test/mocks/server/instance.cc new file mode 100644 index 000000000000..91c102c45267 --- /dev/null +++ b/test/mocks/server/instance.cc @@ -0,0 +1,81 @@ +#include "instance.h" + +#include "common/singleton/manager_impl.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::Return; +using ::testing::ReturnRef; + +MockInstance::MockInstance() + : secret_manager_(std::make_unique(admin_.getConfigTracker())), + cluster_manager_(timeSource()), ssl_context_manager_(timeSource()), + singleton_manager_(new Singleton::ManagerImpl(Thread::threadFactoryForTest())), + grpc_context_(stats_store_.symbolTable()), http_context_(stats_store_.symbolTable()), + server_factory_context_( + std::make_shared>()), + transport_socket_factory_context_( + std::make_shared>()) { + ON_CALL(*this, threadLocal()).WillByDefault(ReturnRef(thread_local_)); + ON_CALL(*this, stats()).WillByDefault(ReturnRef(stats_store_)); + ON_CALL(*this, grpcContext()).WillByDefault(ReturnRef(grpc_context_)); + ON_CALL(*this, httpContext()).WillByDefault(ReturnRef(http_context_)); + ON_CALL(*this, dnsResolver()).WillByDefault(Return(dns_resolver_)); + ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); + ON_CALL(*this, admin()).WillByDefault(ReturnRef(admin_)); + ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_)); + ON_CALL(*this, sslContextManager()).WillByDefault(ReturnRef(ssl_context_manager_)); + ON_CALL(*this, accessLogManager()).WillByDefault(ReturnRef(access_log_manager_)); + ON_CALL(*this, runtime()).WillByDefault(ReturnRef(runtime_loader_)); + ON_CALL(*this, dispatcher()).WillByDefault(ReturnRef(dispatcher_)); + ON_CALL(*this, hotRestart()).WillByDefault(ReturnRef(hot_restart_)); + ON_CALL(*this, random()).WillByDefault(ReturnRef(random_)); + ON_CALL(*this, lifecycleNotifier()).WillByDefault(ReturnRef(lifecycle_notifier_)); + ON_CALL(*this, localInfo()).WillByDefault(ReturnRef(local_info_)); + ON_CALL(*this, options()).WillByDefault(ReturnRef(options_)); + ON_CALL(*this, drainManager()).WillByDefault(ReturnRef(drain_manager_)); + ON_CALL(*this, initManager()).WillByDefault(ReturnRef(init_manager_)); + ON_CALL(*this, listenerManager()).WillByDefault(ReturnRef(listener_manager_)); + ON_CALL(*this, mutexTracer()).WillByDefault(Return(nullptr)); + ON_CALL(*this, singletonManager()).WillByDefault(ReturnRef(*singleton_manager_)); + ON_CALL(*this, overloadManager()).WillByDefault(ReturnRef(overload_manager_)); + ON_CALL(*this, messageValidationContext()).WillByDefault(ReturnRef(validation_context_)); + ON_CALL(*this, serverFactoryContext()).WillByDefault(ReturnRef(*server_factory_context_)); + ON_CALL(*this, transportSocketFactoryContext()) + .WillByDefault(ReturnRef(*transport_socket_factory_context_)); +} + +MockInstance::~MockInstance() = default; + +namespace Configuration { + +MockServerFactoryContext::MockServerFactoryContext() + : singleton_manager_(new Singleton::ManagerImpl(Thread::threadFactoryForTest())), + grpc_context_(scope_.symbolTable()) { + ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_)); + ON_CALL(*this, dispatcher()).WillByDefault(ReturnRef(dispatcher_)); + ON_CALL(*this, drainDecision()).WillByDefault(ReturnRef(drain_manager_)); + ON_CALL(*this, localInfo()).WillByDefault(ReturnRef(local_info_)); + ON_CALL(*this, random()).WillByDefault(ReturnRef(random_)); + ON_CALL(*this, runtime()).WillByDefault(ReturnRef(runtime_loader_)); + ON_CALL(*this, scope()).WillByDefault(ReturnRef(scope_)); + ON_CALL(*this, singletonManager()).WillByDefault(ReturnRef(*singleton_manager_)); + ON_CALL(*this, threadLocal()).WillByDefault(ReturnRef(thread_local_)); + ON_CALL(*this, admin()).WillByDefault(ReturnRef(admin_)); + ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); + ON_CALL(*this, timeSource()).WillByDefault(ReturnRef(time_system_)); + ON_CALL(*this, messageValidationContext()).WillByDefault(ReturnRef(validation_context_)); + ON_CALL(*this, messageValidationVisitor()) + .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); + ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); + ON_CALL(*this, drainManager()).WillByDefault(ReturnRef(drain_manager_)); +} +MockServerFactoryContext::~MockServerFactoryContext() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/instance.h b/test/mocks/server/instance.h new file mode 100644 index 000000000000..67da77794772 --- /dev/null +++ b/test/mocks/server/instance.h @@ -0,0 +1,170 @@ +#pragma once + +#include "envoy/server/instance.h" + +#include "common/grpc/context_impl.h" +#include "common/http/context_impl.h" +#include "common/stats/fake_symbol_table_impl.h" + +#include "extensions/transport_sockets/tls/context_manager_impl.h" + +#include "test/mocks/access_log/mocks.h" +#include "test/mocks/api/mocks.h" +#include "test/mocks/event/mocks.h" +#include "test/mocks/http/mocks.h" +#include "test/mocks/init/mocks.h" +#include "test/mocks/local_info/mocks.h" +#include "test/mocks/network/mocks.h" +#include "test/mocks/protobuf/mocks.h" +#include "test/mocks/runtime/mocks.h" +#include "test/mocks/secret/mocks.h" +#include "test/mocks/stats/mocks.h" +#include "test/mocks/thread_local/mocks.h" +#include "test/mocks/tracing/mocks.h" +#include "test/mocks/upstream/mocks.h" + +#include "admin.h" +#include "drain_manager.h" +#include "gmock/gmock.h" +#include "hot_restart.h" +#include "listener_manager.h" +#include "options.h" +#include "overload_manager.h" +#include "server_lifecycle_notifier.h" +#include "transport_socket_factory_context.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockServerFactoryContext; +} // namespace Configuration + +class MockInstance : public Instance { +public: + MockInstance(); + ~MockInstance() override; + + Secret::SecretManager& secretManager() override { return *(secret_manager_.get()); } + + MOCK_METHOD(Admin&, admin, ()); + MOCK_METHOD(Api::Api&, api, ()); + MOCK_METHOD(Upstream::ClusterManager&, clusterManager, ()); + MOCK_METHOD(Ssl::ContextManager&, sslContextManager, ()); + MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); + MOCK_METHOD(Network::DnsResolverSharedPtr, dnsResolver, ()); + MOCK_METHOD(void, drainListeners, ()); + MOCK_METHOD(DrainManager&, drainManager, ()); + MOCK_METHOD(AccessLog::AccessLogManager&, accessLogManager, ()); + MOCK_METHOD(void, failHealthcheck, (bool fail)); + MOCK_METHOD(void, exportStatsToChild, (envoy::HotRestartMessage::Reply::Stats*)); + MOCK_METHOD(bool, healthCheckFailed, ()); + MOCK_METHOD(HotRestart&, hotRestart, ()); + MOCK_METHOD(Init::Manager&, initManager, ()); + MOCK_METHOD(ServerLifecycleNotifier&, lifecycleNotifier, ()); + MOCK_METHOD(ListenerManager&, listenerManager, ()); + MOCK_METHOD(Envoy::MutexTracer*, mutexTracer, ()); + MOCK_METHOD(const Options&, options, ()); + MOCK_METHOD(OverloadManager&, overloadManager, ()); + MOCK_METHOD(Runtime::RandomGenerator&, random, ()); + MOCK_METHOD(Runtime::Loader&, runtime, ()); + MOCK_METHOD(void, shutdown, ()); + MOCK_METHOD(bool, isShutdown, ()); + MOCK_METHOD(void, shutdownAdmin, ()); + MOCK_METHOD(Singleton::Manager&, singletonManager, ()); + MOCK_METHOD(time_t, startTimeCurrentEpoch, ()); + MOCK_METHOD(time_t, startTimeFirstEpoch, ()); + MOCK_METHOD(Stats::Store&, stats, ()); + MOCK_METHOD(Grpc::Context&, grpcContext, ()); + MOCK_METHOD(Http::Context&, httpContext, ()); + MOCK_METHOD(ProcessContextOptRef, processContext, ()); + MOCK_METHOD(ThreadLocal::Instance&, threadLocal, ()); + MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const)); + MOCK_METHOD(std::chrono::milliseconds, statsFlushInterval, (), (const)); + MOCK_METHOD(void, flushStats, ()); + MOCK_METHOD(ProtobufMessage::ValidationContext&, messageValidationContext, ()); + MOCK_METHOD(Configuration::ServerFactoryContext&, serverFactoryContext, ()); + MOCK_METHOD(Configuration::TransportSocketFactoryContext&, transportSocketFactoryContext, ()); + + void setDefaultTracingConfig(const envoy::config::trace::v3::Tracing& tracing_config) override { + http_context_.setDefaultTracingConfig(tracing_config); + } + + TimeSource& timeSource() override { return time_system_; } + + NiceMock stats_store_; + testing::NiceMock thread_local_; + std::shared_ptr> dns_resolver_{ + new testing::NiceMock()}; + testing::NiceMock api_; + testing::NiceMock admin_; + Event::GlobalTimeSystem time_system_; + std::unique_ptr secret_manager_; + testing::NiceMock cluster_manager_; + Thread::MutexBasicLockable access_log_lock_; + testing::NiceMock runtime_loader_; + Extensions::TransportSockets::Tls::ContextManagerImpl ssl_context_manager_; + testing::NiceMock dispatcher_; + testing::NiceMock drain_manager_; + testing::NiceMock access_log_manager_; + testing::NiceMock hot_restart_; + testing::NiceMock options_; + testing::NiceMock random_; + testing::NiceMock lifecycle_notifier_; + testing::NiceMock local_info_; + testing::NiceMock init_manager_; + testing::NiceMock listener_manager_; + testing::NiceMock overload_manager_; + Singleton::ManagerPtr singleton_manager_; + Grpc::ContextImpl grpc_context_; + Http::ContextImpl http_context_; + testing::NiceMock validation_context_; + std::shared_ptr> + server_factory_context_; + std::shared_ptr> + transport_socket_factory_context_; +}; + +namespace Configuration { +class MockServerFactoryContext : public virtual ServerFactoryContext { +public: + MockServerFactoryContext(); + ~MockServerFactoryContext() override; + + MOCK_METHOD(Upstream::ClusterManager&, clusterManager, ()); + MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); + MOCK_METHOD(const Network::DrainDecision&, drainDecision, ()); + MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const)); + MOCK_METHOD(Envoy::Runtime::RandomGenerator&, random, ()); + MOCK_METHOD(Envoy::Runtime::Loader&, runtime, ()); + MOCK_METHOD(Stats::Scope&, scope, ()); + MOCK_METHOD(Singleton::Manager&, singletonManager, ()); + MOCK_METHOD(ThreadLocal::Instance&, threadLocal, ()); + MOCK_METHOD(Server::Admin&, admin, ()); + MOCK_METHOD(TimeSource&, timeSource, ()); + Event::TestTimeSystem& timeSystem() { return time_system_; } + MOCK_METHOD(ProtobufMessage::ValidationContext&, messageValidationContext, ()); + MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); + MOCK_METHOD(Api::Api&, api, ()); + Grpc::Context& grpcContext() override { return grpc_context_; } + MOCK_METHOD(Server::DrainManager&, drainManager, ()); + MOCK_METHOD(Init::Manager&, initManager, ()); + MOCK_METHOD(ServerLifecycleNotifier&, lifecycleNotifier, ()); + + testing::NiceMock cluster_manager_; + testing::NiceMock dispatcher_; + testing::NiceMock drain_manager_; + testing::NiceMock local_info_; + testing::NiceMock random_; + testing::NiceMock runtime_loader_; + testing::NiceMock scope_; + testing::NiceMock thread_local_; + testing::NiceMock validation_context_; + Singleton::ManagerPtr singleton_manager_; + testing::NiceMock admin_; + Event::GlobalTimeSystem time_system_; + testing::NiceMock api_; + Grpc::ContextImpl grpc_context_; +}; +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/listener_component_factory.cc b/test/mocks/server/listener_component_factory.cc new file mode 100644 index 000000000000..a0b00b7bc756 --- /dev/null +++ b/test/mocks/server/listener_component_factory.cc @@ -0,0 +1,31 @@ +#include "listener_component_factory.h" + +#include "envoy/config/core/v3/base.pb.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::_; +using ::testing::Invoke; + +MockListenerComponentFactory::MockListenerComponentFactory() + : socket_(std::make_shared>()) { + ON_CALL(*this, createListenSocket(_, _, _, _)) + .WillByDefault(Invoke([&](Network::Address::InstanceConstSharedPtr, Network::Socket::Type, + const Network::Socket::OptionsSharedPtr& options, + const ListenSocketCreationParams&) -> Network::SocketSharedPtr { + if (!Network::Socket::applyOptions(options, *socket_, + envoy::config::core::v3::SocketOption::STATE_PREBIND)) { + throw EnvoyException("MockListenerComponentFactory: Setting socket options failed"); + } + return socket_; + })); +} + +MockListenerComponentFactory::~MockListenerComponentFactory() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/listener_component_factory.h b/test/mocks/server/listener_component_factory.h new file mode 100644 index 000000000000..84d73dd4bfd5 --- /dev/null +++ b/test/mocks/server/listener_component_factory.h @@ -0,0 +1,47 @@ +#pragma once + +#include "envoy/config/listener/v3/listener_components.pb.h" +#include "envoy/server/drain_manager.h" +#include "envoy/server/listener_manager.h" + +#include "test/mocks/network/mocks.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockListenerComponentFactory : public ListenerComponentFactory { +public: + MockListenerComponentFactory(); + ~MockListenerComponentFactory() override; + + DrainManagerPtr + createDrainManager(envoy::config::listener::v3::Listener::DrainType drain_type) override { + return DrainManagerPtr{createDrainManager_(drain_type)}; + } + LdsApiPtr createLdsApi(const envoy::config::core::v3::ConfigSource& lds_config) override { + return LdsApiPtr{createLdsApi_(lds_config)}; + } + + MOCK_METHOD(LdsApi*, createLdsApi_, (const envoy::config::core::v3::ConfigSource& lds_config)); + MOCK_METHOD(std::vector, createNetworkFilterFactoryList, + (const Protobuf::RepeatedPtrField& filters, + Configuration::FilterChainFactoryContext& filter_chain_factory_context)); + MOCK_METHOD(std::vector, createListenerFilterFactoryList, + (const Protobuf::RepeatedPtrField&, + Configuration::ListenerFactoryContext& context)); + MOCK_METHOD(std::vector, createUdpListenerFilterFactoryList, + (const Protobuf::RepeatedPtrField&, + Configuration::ListenerFactoryContext& context)); + MOCK_METHOD(Network::SocketSharedPtr, createListenSocket, + (Network::Address::InstanceConstSharedPtr address, Network::Socket::Type socket_type, + const Network::Socket::OptionsSharedPtr& options, + const ListenSocketCreationParams& params)); + MOCK_METHOD(DrainManager*, createDrainManager_, + (envoy::config::listener::v3::Listener::DrainType drain_type)); + MOCK_METHOD(uint64_t, nextListenerTag, ()); + + std::shared_ptr socket_; +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/listener_factory_context.cc b/test/mocks/server/listener_factory_context.cc new file mode 100644 index 000000000000..99a7678c1a27 --- /dev/null +++ b/test/mocks/server/listener_factory_context.cc @@ -0,0 +1,15 @@ +#include "listener_factory_context.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +MockListenerFactoryContext::MockListenerFactoryContext() = default; + +MockListenerFactoryContext::~MockListenerFactoryContext() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/listener_factory_context.h b/test/mocks/server/listener_factory_context.h new file mode 100644 index 000000000000..8d322735b577 --- /dev/null +++ b/test/mocks/server/listener_factory_context.h @@ -0,0 +1,23 @@ +#pragma once + +#include "envoy/server/listener_manager.h" + +#include "factory_context.h" +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockListenerFactoryContext : public MockFactoryContext, public ListenerFactoryContext { +public: + MockListenerFactoryContext(); + ~MockListenerFactoryContext() override; + + const Network::ListenerConfig& listenerConfig() const override { return listener_config_; } + MOCK_METHOD(const Network::ListenerConfig&, listenerConfig_, (), (const)); + + Network::MockListenerConfig listener_config_; +}; +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/listener_manager.cc b/test/mocks/server/listener_manager.cc new file mode 100644 index 000000000000..0448ff4e7122 --- /dev/null +++ b/test/mocks/server/listener_manager.cc @@ -0,0 +1,16 @@ +#include "listener_manager.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +MockListenerManager::MockListenerManager() = default; + +MockListenerManager::~MockListenerManager() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/listener_manager.h b/test/mocks/server/listener_manager.h new file mode 100644 index 000000000000..889dfa1f521f --- /dev/null +++ b/test/mocks/server/listener_manager.h @@ -0,0 +1,29 @@ +#pragma once + +#include "envoy/server/listener_manager.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockListenerManager : public ListenerManager { +public: + MockListenerManager(); + ~MockListenerManager() override; + + MOCK_METHOD(bool, addOrUpdateListener, + (const envoy::config::listener::v3::Listener& config, const std::string& version_info, + bool modifiable)); + MOCK_METHOD(void, createLdsApi, (const envoy::config::core::v3::ConfigSource& lds_config)); + MOCK_METHOD(std::vector>, listeners, ()); + MOCK_METHOD(uint64_t, numConnections, (), (const)); + MOCK_METHOD(bool, removeListener, (const std::string& listener_name)); + MOCK_METHOD(void, startWorkers, (GuardDog & guard_dog)); + MOCK_METHOD(void, stopListeners, (StopListenersType listeners_type)); + MOCK_METHOD(void, stopWorkers, ()); + MOCK_METHOD(void, beginListenerUpdate, ()); + MOCK_METHOD(void, endListenerUpdate, (ListenerManager::FailureStates &&)); + MOCK_METHOD(ApiListenerOptRef, apiListener, ()); +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/main.cc b/test/mocks/server/main.cc new file mode 100644 index 000000000000..2cc3e8dfeec4 --- /dev/null +++ b/test/mocks/server/main.cc @@ -0,0 +1,24 @@ +#include "main.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +namespace Configuration { + +using ::testing::Return; + +MockMain::MockMain(int wd_miss, int wd_megamiss, int wd_kill, int wd_multikill) + : wd_miss_(wd_miss), wd_megamiss_(wd_megamiss), wd_kill_(wd_kill), wd_multikill_(wd_multikill) { + ON_CALL(*this, wdMissTimeout()).WillByDefault(Return(wd_miss_)); + ON_CALL(*this, wdMegaMissTimeout()).WillByDefault(Return(wd_megamiss_)); + ON_CALL(*this, wdKillTimeout()).WillByDefault(Return(wd_kill_)); + ON_CALL(*this, wdMultiKillTimeout()).WillByDefault(Return(wd_multikill_)); +} + +MockMain::~MockMain() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/main.h b/test/mocks/server/main.h new file mode 100644 index 000000000000..573cda3df1f0 --- /dev/null +++ b/test/mocks/server/main.h @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include +#include + +#include "envoy/server/configuration.h" +#include "envoy/server/overload_manager.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockMain : public Main { +public: + MockMain() : MockMain(0, 0, 0, 0) {} + MockMain(int wd_miss, int wd_megamiss, int wd_kill, int wd_multikill); + ~MockMain() override; + + MOCK_METHOD(Upstream::ClusterManager*, clusterManager, ()); + MOCK_METHOD(std::list&, statsSinks, ()); + MOCK_METHOD(std::chrono::milliseconds, statsFlushInterval, (), (const)); + MOCK_METHOD(std::chrono::milliseconds, wdMissTimeout, (), (const)); + MOCK_METHOD(std::chrono::milliseconds, wdMegaMissTimeout, (), (const)); + MOCK_METHOD(std::chrono::milliseconds, wdKillTimeout, (), (const)); + MOCK_METHOD(std::chrono::milliseconds, wdMultiKillTimeout, (), (const)); + + std::chrono::milliseconds wd_miss_; + std::chrono::milliseconds wd_megamiss_; + std::chrono::milliseconds wd_kill_; + std::chrono::milliseconds wd_multikill_; +}; +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/mocks.cc b/test/mocks/server/mocks.cc deleted file mode 100644 index e9fc293558c5..000000000000 --- a/test/mocks/server/mocks.cc +++ /dev/null @@ -1,295 +0,0 @@ -#include "mocks.h" - -#include - -#include "envoy/admin/v3/server_info.pb.h" -#include "envoy/config/core/v3/base.pb.h" - -#include "common/singleton/manager_impl.h" - -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using testing::_; -using testing::Invoke; -using testing::Return; -using testing::ReturnPointee; -using testing::ReturnRef; -using testing::SaveArg; - -namespace Envoy { -namespace Server { - -MockOptions::MockOptions(const std::string& config_path) : config_path_(config_path) { - ON_CALL(*this, concurrency()).WillByDefault(ReturnPointee(&concurrency_)); - ON_CALL(*this, configPath()).WillByDefault(ReturnRef(config_path_)); - ON_CALL(*this, configProto()).WillByDefault(ReturnRef(config_proto_)); - ON_CALL(*this, configYaml()).WillByDefault(ReturnRef(config_yaml_)); - ON_CALL(*this, bootstrapVersion()).WillByDefault(ReturnRef(bootstrap_version_)); - ON_CALL(*this, allowUnknownStaticFields()).WillByDefault(Invoke([this] { - return allow_unknown_static_fields_; - })); - ON_CALL(*this, rejectUnknownDynamicFields()).WillByDefault(Invoke([this] { - return reject_unknown_dynamic_fields_; - })); - ON_CALL(*this, ignoreUnknownDynamicFields()).WillByDefault(Invoke([this] { - return ignore_unknown_dynamic_fields_; - })); - ON_CALL(*this, adminAddressPath()).WillByDefault(ReturnRef(admin_address_path_)); - ON_CALL(*this, serviceClusterName()).WillByDefault(ReturnRef(service_cluster_name_)); - ON_CALL(*this, serviceNodeName()).WillByDefault(ReturnRef(service_node_name_)); - ON_CALL(*this, serviceZone()).WillByDefault(ReturnRef(service_zone_name_)); - ON_CALL(*this, logLevel()).WillByDefault(Return(log_level_)); - ON_CALL(*this, logPath()).WillByDefault(ReturnRef(log_path_)); - ON_CALL(*this, restartEpoch()).WillByDefault(ReturnPointee(&hot_restart_epoch_)); - ON_CALL(*this, hotRestartDisabled()).WillByDefault(ReturnPointee(&hot_restart_disabled_)); - ON_CALL(*this, signalHandlingEnabled()).WillByDefault(ReturnPointee(&signal_handling_enabled_)); - ON_CALL(*this, mutexTracingEnabled()).WillByDefault(ReturnPointee(&mutex_tracing_enabled_)); - ON_CALL(*this, cpusetThreadsEnabled()).WillByDefault(ReturnPointee(&cpuset_threads_enabled_)); - ON_CALL(*this, disabledExtensions()).WillByDefault(ReturnRef(disabled_extensions_)); - ON_CALL(*this, toCommandLineOptions()).WillByDefault(Invoke([] { - return std::make_unique(); - })); -} -MockOptions::~MockOptions() = default; - -MockAdminStream::MockAdminStream() = default; -MockAdminStream::~MockAdminStream() = default; - -MockDrainManager::MockDrainManager() { - ON_CALL(*this, startDrainSequence(_)).WillByDefault(SaveArg<0>(&drain_sequence_completion_)); -} -MockDrainManager::~MockDrainManager() = default; - -MockWatchDog::MockWatchDog() = default; -MockWatchDog::~MockWatchDog() = default; - -MockGuardDog::MockGuardDog() : watch_dog_(new NiceMock()) { - ON_CALL(*this, createWatchDog(_, _)).WillByDefault(Return(watch_dog_)); -} -MockGuardDog::~MockGuardDog() = default; - -MockHotRestart::MockHotRestart() : stats_allocator_(*symbol_table_) { - ON_CALL(*this, logLock()).WillByDefault(ReturnRef(log_lock_)); - ON_CALL(*this, accessLogLock()).WillByDefault(ReturnRef(access_log_lock_)); - ON_CALL(*this, statsAllocator()).WillByDefault(ReturnRef(stats_allocator_)); -} -MockHotRestart::~MockHotRestart() = default; - -MockOverloadManager::MockOverloadManager() { - ON_CALL(*this, getThreadLocalOverloadState()).WillByDefault(ReturnRef(overload_state_)); -} -MockOverloadManager::~MockOverloadManager() = default; - -MockListenerComponentFactory::MockListenerComponentFactory() - : socket_(std::make_shared>()) { - ON_CALL(*this, createListenSocket(_, _, _, _)) - .WillByDefault(Invoke([&](Network::Address::InstanceConstSharedPtr, Network::Socket::Type, - const Network::Socket::OptionsSharedPtr& options, - const ListenSocketCreationParams&) -> Network::SocketSharedPtr { - if (!Network::Socket::applyOptions(options, *socket_, - envoy::config::core::v3::SocketOption::STATE_PREBIND)) { - throw EnvoyException("MockListenerComponentFactory: Setting socket options failed"); - } - return socket_; - })); -} -MockListenerComponentFactory::~MockListenerComponentFactory() = default; - -MockServerLifecycleNotifier::MockServerLifecycleNotifier() = default; -MockServerLifecycleNotifier::~MockServerLifecycleNotifier() = default; - -MockListenerManager::MockListenerManager() = default; -MockListenerManager::~MockListenerManager() = default; - -MockWorkerFactory::MockWorkerFactory() = default; -MockWorkerFactory::~MockWorkerFactory() = default; - -MockWorker::MockWorker() { - ON_CALL(*this, addListener(_, _, _)) - .WillByDefault( - Invoke([this](absl::optional overridden_listener, - Network::ListenerConfig& config, AddListenerCompletion completion) -> void { - UNREFERENCED_PARAMETER(overridden_listener); - config.listenSocketFactory().getListenSocket(); - EXPECT_EQ(nullptr, add_listener_completion_); - add_listener_completion_ = completion; - })); - - ON_CALL(*this, removeListener(_, _)) - .WillByDefault( - Invoke([this](Network::ListenerConfig&, std::function completion) -> void { - EXPECT_EQ(nullptr, remove_listener_completion_); - remove_listener_completion_ = completion; - })); - - ON_CALL(*this, stopListener(_, _)) - .WillByDefault(Invoke([](Network::ListenerConfig&, std::function completion) -> void { - if (completion != nullptr) { - completion(); - } - })); - - ON_CALL(*this, removeFilterChains(_, _, _)) - .WillByDefault(Invoke([this](uint64_t, const std::list&, - std::function completion) -> void { - EXPECT_EQ(nullptr, remove_filter_chains_completion_); - remove_filter_chains_completion_ = completion; - })); -} -MockWorker::~MockWorker() = default; - -MockInstance::MockInstance() - : secret_manager_(std::make_unique(admin_.getConfigTracker())), - cluster_manager_(timeSource()), ssl_context_manager_(timeSource()), - singleton_manager_(new Singleton::ManagerImpl(Thread::threadFactoryForTest())), - grpc_context_(stats_store_.symbolTable()), http_context_(stats_store_.symbolTable()), - server_factory_context_( - std::make_shared>()), - transport_socket_factory_context_( - std::make_shared>()) { - ON_CALL(*this, threadLocal()).WillByDefault(ReturnRef(thread_local_)); - ON_CALL(*this, stats()).WillByDefault(ReturnRef(stats_store_)); - ON_CALL(*this, grpcContext()).WillByDefault(ReturnRef(grpc_context_)); - ON_CALL(*this, httpContext()).WillByDefault(ReturnRef(http_context_)); - ON_CALL(*this, dnsResolver()).WillByDefault(Return(dns_resolver_)); - ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); - ON_CALL(*this, admin()).WillByDefault(ReturnRef(admin_)); - ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_)); - ON_CALL(*this, sslContextManager()).WillByDefault(ReturnRef(ssl_context_manager_)); - ON_CALL(*this, accessLogManager()).WillByDefault(ReturnRef(access_log_manager_)); - ON_CALL(*this, runtime()).WillByDefault(ReturnRef(runtime_loader_)); - ON_CALL(*this, dispatcher()).WillByDefault(ReturnRef(dispatcher_)); - ON_CALL(*this, hotRestart()).WillByDefault(ReturnRef(hot_restart_)); - ON_CALL(*this, random()).WillByDefault(ReturnRef(random_)); - ON_CALL(*this, lifecycleNotifier()).WillByDefault(ReturnRef(lifecycle_notifier_)); - ON_CALL(*this, localInfo()).WillByDefault(ReturnRef(local_info_)); - ON_CALL(*this, options()).WillByDefault(ReturnRef(options_)); - ON_CALL(*this, drainManager()).WillByDefault(ReturnRef(drain_manager_)); - ON_CALL(*this, initManager()).WillByDefault(ReturnRef(init_manager_)); - ON_CALL(*this, listenerManager()).WillByDefault(ReturnRef(listener_manager_)); - ON_CALL(*this, mutexTracer()).WillByDefault(Return(nullptr)); - ON_CALL(*this, singletonManager()).WillByDefault(ReturnRef(*singleton_manager_)); - ON_CALL(*this, overloadManager()).WillByDefault(ReturnRef(overload_manager_)); - ON_CALL(*this, messageValidationContext()).WillByDefault(ReturnRef(validation_context_)); - ON_CALL(*this, serverFactoryContext()).WillByDefault(ReturnRef(*server_factory_context_)); - ON_CALL(*this, transportSocketFactoryContext()) - .WillByDefault(ReturnRef(*transport_socket_factory_context_)); -} - -MockInstance::~MockInstance() = default; - -namespace Configuration { - -MockMain::MockMain(int wd_miss, int wd_megamiss, int wd_kill, int wd_multikill) - : wd_miss_(wd_miss), wd_megamiss_(wd_megamiss), wd_kill_(wd_kill), wd_multikill_(wd_multikill) { - ON_CALL(*this, wdMissTimeout()).WillByDefault(Return(wd_miss_)); - ON_CALL(*this, wdMegaMissTimeout()).WillByDefault(Return(wd_megamiss_)); - ON_CALL(*this, wdKillTimeout()).WillByDefault(Return(wd_kill_)); - ON_CALL(*this, wdMultiKillTimeout()).WillByDefault(Return(wd_multikill_)); -} - -MockMain::~MockMain() = default; - -MockServerFactoryContext::MockServerFactoryContext() - : singleton_manager_(new Singleton::ManagerImpl(Thread::threadFactoryForTest())), - grpc_context_(scope_.symbolTable()) { - ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_)); - ON_CALL(*this, dispatcher()).WillByDefault(ReturnRef(dispatcher_)); - ON_CALL(*this, drainDecision()).WillByDefault(ReturnRef(drain_manager_)); - ON_CALL(*this, localInfo()).WillByDefault(ReturnRef(local_info_)); - ON_CALL(*this, random()).WillByDefault(ReturnRef(random_)); - ON_CALL(*this, runtime()).WillByDefault(ReturnRef(runtime_loader_)); - ON_CALL(*this, scope()).WillByDefault(ReturnRef(scope_)); - ON_CALL(*this, singletonManager()).WillByDefault(ReturnRef(*singleton_manager_)); - ON_CALL(*this, threadLocal()).WillByDefault(ReturnRef(thread_local_)); - ON_CALL(*this, admin()).WillByDefault(ReturnRef(admin_)); - ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); - ON_CALL(*this, timeSource()).WillByDefault(ReturnRef(time_system_)); - ON_CALL(*this, messageValidationContext()).WillByDefault(ReturnRef(validation_context_)); - ON_CALL(*this, messageValidationVisitor()) - .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); - ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); - ON_CALL(*this, drainManager()).WillByDefault(ReturnRef(drain_manager_)); -} -MockServerFactoryContext::~MockServerFactoryContext() = default; - -MockFactoryContext::MockFactoryContext() - : singleton_manager_(new Singleton::ManagerImpl(Thread::threadFactoryForTest())), - grpc_context_(scope_.symbolTable()), http_context_(scope_.symbolTable()) { - ON_CALL(*this, getServerFactoryContext()).WillByDefault(ReturnRef(server_factory_context_)); - ON_CALL(*this, accessLogManager()).WillByDefault(ReturnRef(access_log_manager_)); - ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_)); - ON_CALL(*this, dispatcher()).WillByDefault(ReturnRef(dispatcher_)); - ON_CALL(*this, drainDecision()).WillByDefault(ReturnRef(drain_manager_)); - ON_CALL(*this, initManager()).WillByDefault(ReturnRef(init_manager_)); - ON_CALL(*this, lifecycleNotifier()).WillByDefault(ReturnRef(lifecycle_notifier_)); - ON_CALL(*this, localInfo()).WillByDefault(ReturnRef(local_info_)); - ON_CALL(*this, random()).WillByDefault(ReturnRef(random_)); - ON_CALL(*this, runtime()).WillByDefault(ReturnRef(runtime_loader_)); - ON_CALL(*this, scope()).WillByDefault(ReturnRef(scope_)); - ON_CALL(*this, singletonManager()).WillByDefault(ReturnRef(*singleton_manager_)); - ON_CALL(*this, threadLocal()).WillByDefault(ReturnRef(thread_local_)); - ON_CALL(*this, admin()).WillByDefault(ReturnRef(admin_)); - ON_CALL(*this, listenerScope()).WillByDefault(ReturnRef(listener_scope_)); - ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); - ON_CALL(*this, timeSource()).WillByDefault(ReturnRef(time_system_)); - ON_CALL(*this, overloadManager()).WillByDefault(ReturnRef(overload_manager_)); - ON_CALL(*this, messageValidationContext()).WillByDefault(ReturnRef(validation_context_)); - ON_CALL(*this, messageValidationVisitor()) - .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); - ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); -} - -MockFactoryContext::~MockFactoryContext() = default; - -MockTransportSocketFactoryContext::MockTransportSocketFactoryContext() - : secret_manager_(std::make_unique(config_tracker_)) { - ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_)); - ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); - ON_CALL(*this, messageValidationVisitor()) - .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); -} - -MockTransportSocketFactoryContext::~MockTransportSocketFactoryContext() = default; - -MockListenerFactoryContext::MockListenerFactoryContext() = default; -MockListenerFactoryContext::~MockListenerFactoryContext() = default; - -MockHealthCheckerFactoryContext::MockHealthCheckerFactoryContext() { - event_logger_ = new NiceMock(); - ON_CALL(*this, cluster()).WillByDefault(ReturnRef(cluster_)); - ON_CALL(*this, dispatcher()).WillByDefault(ReturnRef(dispatcher_)); - ON_CALL(*this, random()).WillByDefault(ReturnRef(random_)); - ON_CALL(*this, runtime()).WillByDefault(ReturnRef(runtime_)); - ON_CALL(*this, eventLogger_()).WillByDefault(Return(event_logger_)); - ON_CALL(*this, messageValidationVisitor()) - .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); - ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); -} - -MockHealthCheckerFactoryContext::~MockHealthCheckerFactoryContext() = default; - -MockFilterChainFactoryContext::MockFilterChainFactoryContext() = default; -MockFilterChainFactoryContext::~MockFilterChainFactoryContext() = default; - -MockTracerFactory::MockTracerFactory(const std::string& name) : name_(name) { - ON_CALL(*this, createEmptyConfigProto()).WillByDefault(Invoke([] { - return std::make_unique(); - })); -} -MockTracerFactory::~MockTracerFactory() = default; - -MockTracerFactoryContext::MockTracerFactoryContext() { - ON_CALL(*this, serverFactoryContext()).WillByDefault(ReturnRef(server_factory_context_)); - ON_CALL(*this, messageValidationVisitor()) - .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); -} - -MockTracerFactoryContext::~MockTracerFactoryContext() = default; - -MockBootstrapExtensionFactory::MockBootstrapExtensionFactory() = default; -MockBootstrapExtensionFactory::~MockBootstrapExtensionFactory() = default; -} // namespace Configuration -} // namespace Server -} // namespace Envoy diff --git a/test/mocks/server/mocks.h b/test/mocks/server/mocks.h index 1685d0f706d2..d041412d01d1 100644 --- a/test/mocks/server/mocks.h +++ b/test/mocks/server/mocks.h @@ -1,661 +1,28 @@ #pragma once -#include -#include -#include -#include +// NOLINT(namespace-envoy) -#include "envoy/common/mutex_tracer.h" -#include "envoy/config/bootstrap/v3/bootstrap.pb.h" -#include "envoy/config/core/v3/base.pb.h" -#include "envoy/config/core/v3/config_source.pb.h" -#include "envoy/config/listener/v3/listener.pb.h" -#include "envoy/config/listener/v3/listener_components.pb.h" -#include "envoy/protobuf/message_validator.h" -#include "envoy/server/admin.h" -#include "envoy/server/bootstrap_extension_config.h" -#include "envoy/server/configuration.h" -#include "envoy/server/drain_manager.h" -#include "envoy/server/filter_config.h" -#include "envoy/server/health_checker_config.h" -#include "envoy/server/instance.h" -#include "envoy/server/options.h" -#include "envoy/server/overload_manager.h" -#include "envoy/server/tracer_config.h" -#include "envoy/server/transport_socket_config.h" -#include "envoy/server/worker.h" -#include "envoy/ssl/context_manager.h" -#include "envoy/stats/scope.h" -#include "envoy/thread/thread.h" - -#include "common/grpc/context_impl.h" -#include "common/http/context_impl.h" -#include "common/secret/secret_manager_impl.h" -#include "common/stats/fake_symbol_table_impl.h" - -#include "extensions/transport_sockets/tls/context_manager_impl.h" - -#include "test/mocks/access_log/mocks.h" -#include "test/mocks/api/mocks.h" -#include "test/mocks/event/mocks.h" -#include "test/mocks/http/mocks.h" -#include "test/mocks/init/mocks.h" -#include "test/mocks/local_info/mocks.h" -#include "test/mocks/network/mocks.h" -#include "test/mocks/protobuf/mocks.h" -#include "test/mocks/router/mocks.h" -#include "test/mocks/runtime/mocks.h" -#include "test/mocks/secret/mocks.h" -#include "test/mocks/stats/mocks.h" -#include "test/mocks/thread_local/mocks.h" -#include "test/mocks/tracing/mocks.h" -#include "test/mocks/upstream/mocks.h" -#include "test/test_common/test_time_system.h" - -#include "absl/strings/string_view.h" #include "admin.h" +#include "admin_stream.h" +#include "bootstrap_extension_factory.h" #include "config_tracker.h" -#include "gmock/gmock.h" -#include "spdlog/spdlog.h" - -namespace Envoy { -namespace Server { - -namespace Configuration { -class MockServerFactoryContext; -class MockTransportSocketFactoryContext; -} // namespace Configuration - -class MockOptions : public Options { -public: - MockOptions() : MockOptions(std::string()) {} - MockOptions(const std::string& config_path); - ~MockOptions() override; - - MOCK_METHOD(uint64_t, baseId, (), (const)); - MOCK_METHOD(bool, useDynamicBaseId, (), (const)); - MOCK_METHOD(const std::string&, baseIdPath, (), (const)); - MOCK_METHOD(uint32_t, concurrency, (), (const)); - MOCK_METHOD(const std::string&, configPath, (), (const)); - MOCK_METHOD(const envoy::config::bootstrap::v3::Bootstrap&, configProto, (), (const)); - MOCK_METHOD(const std::string&, configYaml, (), (const)); - MOCK_METHOD(const absl::optional&, bootstrapVersion, (), (const)); - MOCK_METHOD(bool, allowUnknownStaticFields, (), (const)); - MOCK_METHOD(bool, rejectUnknownDynamicFields, (), (const)); - MOCK_METHOD(bool, ignoreUnknownDynamicFields, (), (const)); - MOCK_METHOD(const std::string&, adminAddressPath, (), (const)); - MOCK_METHOD(Network::Address::IpVersion, localAddressIpVersion, (), (const)); - MOCK_METHOD(std::chrono::seconds, drainTime, (), (const)); - MOCK_METHOD(std::chrono::seconds, parentShutdownTime, (), (const)); - MOCK_METHOD(Server::DrainStrategy, drainStrategy, (), (const)); - MOCK_METHOD(spdlog::level::level_enum, logLevel, (), (const)); - MOCK_METHOD((const std::vector>&), - componentLogLevels, (), (const)); - MOCK_METHOD(const std::string&, logFormat, (), (const)); - MOCK_METHOD(bool, logFormatEscaped, (), (const)); - MOCK_METHOD(const std::string&, logPath, (), (const)); - MOCK_METHOD(uint64_t, restartEpoch, (), (const)); - MOCK_METHOD(std::chrono::milliseconds, fileFlushIntervalMsec, (), (const)); - MOCK_METHOD(Mode, mode, (), (const)); - MOCK_METHOD(const std::string&, serviceClusterName, (), (const)); - MOCK_METHOD(const std::string&, serviceNodeName, (), (const)); - MOCK_METHOD(const std::string&, serviceZone, (), (const)); - MOCK_METHOD(bool, hotRestartDisabled, (), (const)); - MOCK_METHOD(bool, signalHandlingEnabled, (), (const)); - MOCK_METHOD(bool, mutexTracingEnabled, (), (const)); - MOCK_METHOD(bool, fakeSymbolTableEnabled, (), (const)); - MOCK_METHOD(bool, cpusetThreadsEnabled, (), (const)); - MOCK_METHOD(const std::vector&, disabledExtensions, (), (const)); - MOCK_METHOD(Server::CommandLineOptionsPtr, toCommandLineOptions, (), (const)); - - std::string config_path_; - envoy::config::bootstrap::v3::Bootstrap config_proto_; - std::string config_yaml_; - absl::optional bootstrap_version_; - bool allow_unknown_static_fields_{}; - bool reject_unknown_dynamic_fields_{}; - bool ignore_unknown_dynamic_fields_{}; - std::string admin_address_path_; - std::string service_cluster_name_; - std::string service_node_name_; - std::string service_zone_name_; - spdlog::level::level_enum log_level_{spdlog::level::trace}; - std::string log_path_; - uint32_t concurrency_{1}; - uint64_t hot_restart_epoch_{}; - bool hot_restart_disabled_{}; - bool signal_handling_enabled_{true}; - bool mutex_tracing_enabled_{}; - bool cpuset_threads_enabled_{}; - std::vector disabled_extensions_; -}; - -class MockAdminStream : public AdminStream { -public: - MockAdminStream(); - ~MockAdminStream() override; - - MOCK_METHOD(void, setEndStreamOnComplete, (bool)); - MOCK_METHOD(void, addOnDestroyCallback, (std::function)); - MOCK_METHOD(const Buffer::Instance*, getRequestBody, (), (const)); - MOCK_METHOD(Http::RequestHeaderMap&, getRequestHeaders, (), (const)); - MOCK_METHOD(NiceMock&, getDecoderFilterCallbacks, (), - (const)); - MOCK_METHOD(Http::Http1StreamEncoderOptionsOptRef, http1StreamEncoderOptions, ()); -}; - -class MockDrainManager : public DrainManager { -public: - MockDrainManager(); - ~MockDrainManager() override; - - // Server::DrainManager - MOCK_METHOD(bool, drainClose, (), (const)); - MOCK_METHOD(bool, draining, (), (const)); - MOCK_METHOD(void, startDrainSequence, (std::function completion)); - MOCK_METHOD(void, startParentShutdownSequence, ()); - - std::function drain_sequence_completion_; -}; - -class MockWatchDog : public WatchDog { -public: - MockWatchDog(); - ~MockWatchDog() override; - - // Server::WatchDog - MOCK_METHOD(void, startWatchdog, (Event::Dispatcher & dispatcher)); - MOCK_METHOD(void, touch, ()); - MOCK_METHOD(Thread::ThreadId, threadId, (), (const)); - MOCK_METHOD(MonotonicTime, lastTouchTime, (), (const)); -}; - -class MockGuardDog : public GuardDog { -public: - MockGuardDog(); - ~MockGuardDog() override; - - // Server::GuardDog - MOCK_METHOD(WatchDogSharedPtr, createWatchDog, - (Thread::ThreadId thread_id, const std::string& thread_name)); - MOCK_METHOD(void, stopWatching, (WatchDogSharedPtr wd)); - - std::shared_ptr watch_dog_; -}; - -class MockHotRestart : public HotRestart { -public: - MockHotRestart(); - ~MockHotRestart() override; - - // Server::HotRestart - MOCK_METHOD(void, drainParentListeners, ()); - MOCK_METHOD(int, duplicateParentListenSocket, (const std::string& address)); - MOCK_METHOD(std::unique_ptr, getParentStats, ()); - MOCK_METHOD(void, initialize, (Event::Dispatcher & dispatcher, Server::Instance& server)); - MOCK_METHOD(void, sendParentAdminShutdownRequest, (time_t & original_start_time)); - MOCK_METHOD(void, sendParentTerminateRequest, ()); - MOCK_METHOD(ServerStatsFromParent, mergeParentStatsIfAny, (Stats::StoreRoot & stats_store)); - MOCK_METHOD(void, shutdown, ()); - MOCK_METHOD(uint32_t, baseId, ()); - MOCK_METHOD(std::string, version, ()); - MOCK_METHOD(Thread::BasicLockable&, logLock, ()); - MOCK_METHOD(Thread::BasicLockable&, accessLogLock, ()); - MOCK_METHOD(Stats::Allocator&, statsAllocator, ()); - -private: - Stats::TestSymbolTable symbol_table_; - Thread::MutexBasicLockable log_lock_; - Thread::MutexBasicLockable access_log_lock_; - Stats::AllocatorImpl stats_allocator_; -}; - -class MockListenerComponentFactory : public ListenerComponentFactory { -public: - MockListenerComponentFactory(); - ~MockListenerComponentFactory() override; - - DrainManagerPtr - createDrainManager(envoy::config::listener::v3::Listener::DrainType drain_type) override { - return DrainManagerPtr{createDrainManager_(drain_type)}; - } - LdsApiPtr createLdsApi(const envoy::config::core::v3::ConfigSource& lds_config) override { - return LdsApiPtr{createLdsApi_(lds_config)}; - } - - MOCK_METHOD(LdsApi*, createLdsApi_, (const envoy::config::core::v3::ConfigSource& lds_config)); - MOCK_METHOD(std::vector, createNetworkFilterFactoryList, - (const Protobuf::RepeatedPtrField& filters, - Configuration::FilterChainFactoryContext& filter_chain_factory_context)); - MOCK_METHOD(std::vector, createListenerFilterFactoryList, - (const Protobuf::RepeatedPtrField&, - Configuration::ListenerFactoryContext& context)); - MOCK_METHOD(std::vector, createUdpListenerFilterFactoryList, - (const Protobuf::RepeatedPtrField&, - Configuration::ListenerFactoryContext& context)); - MOCK_METHOD(Network::SocketSharedPtr, createListenSocket, - (Network::Address::InstanceConstSharedPtr address, Network::Socket::Type socket_type, - const Network::Socket::OptionsSharedPtr& options, - const ListenSocketCreationParams& params)); - MOCK_METHOD(DrainManager*, createDrainManager_, - (envoy::config::listener::v3::Listener::DrainType drain_type)); - MOCK_METHOD(uint64_t, nextListenerTag, ()); - - std::shared_ptr socket_; -}; - -class MockListenerManager : public ListenerManager { -public: - MockListenerManager(); - ~MockListenerManager() override; - - MOCK_METHOD(bool, addOrUpdateListener, - (const envoy::config::listener::v3::Listener& config, const std::string& version_info, - bool modifiable)); - MOCK_METHOD(void, createLdsApi, (const envoy::config::core::v3::ConfigSource& lds_config)); - MOCK_METHOD(std::vector>, listeners, ()); - MOCK_METHOD(uint64_t, numConnections, (), (const)); - MOCK_METHOD(bool, removeListener, (const std::string& listener_name)); - MOCK_METHOD(void, startWorkers, (GuardDog & guard_dog)); - MOCK_METHOD(void, stopListeners, (StopListenersType listeners_type)); - MOCK_METHOD(void, stopWorkers, ()); - MOCK_METHOD(void, beginListenerUpdate, ()); - MOCK_METHOD(void, endListenerUpdate, (ListenerManager::FailureStates &&)); - MOCK_METHOD(ApiListenerOptRef, apiListener, ()); -}; - -class MockServerLifecycleNotifier : public ServerLifecycleNotifier { -public: - MockServerLifecycleNotifier(); - ~MockServerLifecycleNotifier() override; - - MOCK_METHOD(ServerLifecycleNotifier::HandlePtr, registerCallback, (Stage, StageCallback)); - MOCK_METHOD(ServerLifecycleNotifier::HandlePtr, registerCallback, - (Stage, StageCallbackWithCompletion)); -}; - -class MockWorkerFactory : public WorkerFactory { -public: - MockWorkerFactory(); - ~MockWorkerFactory() override; - - // Server::WorkerFactory - WorkerPtr createWorker(OverloadManager&, const std::string&) override { - return WorkerPtr{createWorker_()}; - } - - MOCK_METHOD(Worker*, createWorker_, ()); -}; - -class MockWorker : public Worker { -public: - MockWorker(); - ~MockWorker() override; - - void callAddCompletion(bool success) { - EXPECT_NE(nullptr, add_listener_completion_); - add_listener_completion_(success); - add_listener_completion_ = nullptr; - } - - void callRemovalCompletion() { - EXPECT_NE(nullptr, remove_listener_completion_); - remove_listener_completion_(); - remove_listener_completion_ = nullptr; - } - - void callDrainFilterChainsComplete() { - EXPECT_NE(nullptr, remove_filter_chains_completion_); - remove_filter_chains_completion_(); - remove_filter_chains_completion_ = nullptr; - } - - // Server::Worker - MOCK_METHOD(void, addListener, - (absl::optional overridden_listener, Network::ListenerConfig& listener, - AddListenerCompletion completion)); - MOCK_METHOD(uint64_t, numConnections, (), (const)); - MOCK_METHOD(void, removeListener, - (Network::ListenerConfig & listener, std::function completion)); - MOCK_METHOD(void, start, (GuardDog & guard_dog)); - MOCK_METHOD(void, initializeStats, (Stats::Scope & scope)); - MOCK_METHOD(void, stop, ()); - MOCK_METHOD(void, stopListener, - (Network::ListenerConfig & listener, std::function completion)); - MOCK_METHOD(void, removeFilterChains, - (uint64_t listener_tag, const std::list& filter_chains, - std::function completion)); - - AddListenerCompletion add_listener_completion_; - std::function remove_listener_completion_; - std::function remove_filter_chains_completion_; -}; - -class MockOverloadManager : public OverloadManager { -public: - MockOverloadManager(); - ~MockOverloadManager() override; - - // OverloadManager - MOCK_METHOD(void, start, ()); - MOCK_METHOD(bool, registerForAction, - (const std::string& action, Event::Dispatcher& dispatcher, - OverloadActionCb callback)); - MOCK_METHOD(ThreadLocalOverloadState&, getThreadLocalOverloadState, ()); - - ThreadLocalOverloadState overload_state_; -}; - -class MockInstance : public Instance { -public: - MockInstance(); - ~MockInstance() override; - - Secret::SecretManager& secretManager() override { return *(secret_manager_.get()); } - - MOCK_METHOD(Admin&, admin, ()); - MOCK_METHOD(Api::Api&, api, ()); - MOCK_METHOD(Upstream::ClusterManager&, clusterManager, ()); - MOCK_METHOD(Ssl::ContextManager&, sslContextManager, ()); - MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); - MOCK_METHOD(Network::DnsResolverSharedPtr, dnsResolver, ()); - MOCK_METHOD(void, drainListeners, ()); - MOCK_METHOD(DrainManager&, drainManager, ()); - MOCK_METHOD(AccessLog::AccessLogManager&, accessLogManager, ()); - MOCK_METHOD(void, failHealthcheck, (bool fail)); - MOCK_METHOD(void, exportStatsToChild, (envoy::HotRestartMessage::Reply::Stats*)); - MOCK_METHOD(bool, healthCheckFailed, ()); - MOCK_METHOD(HotRestart&, hotRestart, ()); - MOCK_METHOD(Init::Manager&, initManager, ()); - MOCK_METHOD(ServerLifecycleNotifier&, lifecycleNotifier, ()); - MOCK_METHOD(ListenerManager&, listenerManager, ()); - MOCK_METHOD(Envoy::MutexTracer*, mutexTracer, ()); - MOCK_METHOD(const Options&, options, ()); - MOCK_METHOD(OverloadManager&, overloadManager, ()); - MOCK_METHOD(Runtime::RandomGenerator&, random, ()); - MOCK_METHOD(Runtime::Loader&, runtime, ()); - MOCK_METHOD(void, shutdown, ()); - MOCK_METHOD(bool, isShutdown, ()); - MOCK_METHOD(void, shutdownAdmin, ()); - MOCK_METHOD(Singleton::Manager&, singletonManager, ()); - MOCK_METHOD(time_t, startTimeCurrentEpoch, ()); - MOCK_METHOD(time_t, startTimeFirstEpoch, ()); - MOCK_METHOD(Stats::Store&, stats, ()); - MOCK_METHOD(Grpc::Context&, grpcContext, ()); - MOCK_METHOD(Http::Context&, httpContext, ()); - MOCK_METHOD(ProcessContextOptRef, processContext, ()); - MOCK_METHOD(ThreadLocal::Instance&, threadLocal, ()); - MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const)); - MOCK_METHOD(std::chrono::milliseconds, statsFlushInterval, (), (const)); - MOCK_METHOD(void, flushStats, ()); - MOCK_METHOD(ProtobufMessage::ValidationContext&, messageValidationContext, ()); - MOCK_METHOD(Configuration::ServerFactoryContext&, serverFactoryContext, ()); - MOCK_METHOD(Configuration::TransportSocketFactoryContext&, transportSocketFactoryContext, ()); - - void setDefaultTracingConfig(const envoy::config::trace::v3::Tracing& tracing_config) override { - http_context_.setDefaultTracingConfig(tracing_config); - } - - TimeSource& timeSource() override { return time_system_; } - - NiceMock stats_store_; - testing::NiceMock thread_local_; - std::shared_ptr> dns_resolver_{ - new testing::NiceMock()}; - testing::NiceMock api_; - testing::NiceMock admin_; - Event::GlobalTimeSystem time_system_; - std::unique_ptr secret_manager_; - testing::NiceMock cluster_manager_; - Thread::MutexBasicLockable access_log_lock_; - testing::NiceMock runtime_loader_; - Extensions::TransportSockets::Tls::ContextManagerImpl ssl_context_manager_; - testing::NiceMock dispatcher_; - testing::NiceMock drain_manager_; - testing::NiceMock access_log_manager_; - testing::NiceMock hot_restart_; - testing::NiceMock options_; - testing::NiceMock random_; - testing::NiceMock lifecycle_notifier_; - testing::NiceMock local_info_; - testing::NiceMock init_manager_; - testing::NiceMock listener_manager_; - testing::NiceMock overload_manager_; - Singleton::ManagerPtr singleton_manager_; - Grpc::ContextImpl grpc_context_; - Http::ContextImpl http_context_; - testing::NiceMock validation_context_; - std::shared_ptr> - server_factory_context_; - std::shared_ptr> - transport_socket_factory_context_; -}; - -namespace Configuration { - -class MockMain : public Main { -public: - MockMain() : MockMain(0, 0, 0, 0) {} - MockMain(int wd_miss, int wd_megamiss, int wd_kill, int wd_multikill); - ~MockMain() override; - - MOCK_METHOD(Upstream::ClusterManager*, clusterManager, ()); - MOCK_METHOD(std::list&, statsSinks, ()); - MOCK_METHOD(std::chrono::milliseconds, statsFlushInterval, (), (const)); - MOCK_METHOD(std::chrono::milliseconds, wdMissTimeout, (), (const)); - MOCK_METHOD(std::chrono::milliseconds, wdMegaMissTimeout, (), (const)); - MOCK_METHOD(std::chrono::milliseconds, wdKillTimeout, (), (const)); - MOCK_METHOD(std::chrono::milliseconds, wdMultiKillTimeout, (), (const)); - - std::chrono::milliseconds wd_miss_; - std::chrono::milliseconds wd_megamiss_; - std::chrono::milliseconds wd_kill_; - std::chrono::milliseconds wd_multikill_; -}; - -class MockServerFactoryContext : public virtual ServerFactoryContext { -public: - MockServerFactoryContext(); - ~MockServerFactoryContext() override; - - MOCK_METHOD(Upstream::ClusterManager&, clusterManager, ()); - MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); - MOCK_METHOD(const Network::DrainDecision&, drainDecision, ()); - MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const)); - MOCK_METHOD(Envoy::Runtime::RandomGenerator&, random, ()); - MOCK_METHOD(Envoy::Runtime::Loader&, runtime, ()); - MOCK_METHOD(Stats::Scope&, scope, ()); - MOCK_METHOD(Singleton::Manager&, singletonManager, ()); - MOCK_METHOD(ThreadLocal::Instance&, threadLocal, ()); - MOCK_METHOD(Server::Admin&, admin, ()); - MOCK_METHOD(TimeSource&, timeSource, ()); - Event::TestTimeSystem& timeSystem() { return time_system_; } - MOCK_METHOD(ProtobufMessage::ValidationContext&, messageValidationContext, ()); - MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); - MOCK_METHOD(Api::Api&, api, ()); - Grpc::Context& grpcContext() override { return grpc_context_; } - MOCK_METHOD(Server::DrainManager&, drainManager, ()); - MOCK_METHOD(Init::Manager&, initManager, ()); - MOCK_METHOD(ServerLifecycleNotifier&, lifecycleNotifier, ()); - - testing::NiceMock cluster_manager_; - testing::NiceMock dispatcher_; - testing::NiceMock drain_manager_; - testing::NiceMock local_info_; - testing::NiceMock random_; - testing::NiceMock runtime_loader_; - testing::NiceMock scope_; - testing::NiceMock thread_local_; - testing::NiceMock validation_context_; - Singleton::ManagerPtr singleton_manager_; - testing::NiceMock admin_; - Event::GlobalTimeSystem time_system_; - testing::NiceMock api_; - Grpc::ContextImpl grpc_context_; -}; - -class MockFactoryContext : public virtual FactoryContext { -public: - MockFactoryContext(); - ~MockFactoryContext() override; - - MOCK_METHOD(ServerFactoryContext&, getServerFactoryContext, (), (const)); - MOCK_METHOD(TransportSocketFactoryContext&, getTransportSocketFactoryContext, (), (const)); - MOCK_METHOD(AccessLog::AccessLogManager&, accessLogManager, ()); - MOCK_METHOD(Upstream::ClusterManager&, clusterManager, ()); - MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); - MOCK_METHOD(const Network::DrainDecision&, drainDecision, ()); - MOCK_METHOD(bool, healthCheckFailed, ()); - MOCK_METHOD(Init::Manager&, initManager, ()); - MOCK_METHOD(ServerLifecycleNotifier&, lifecycleNotifier, ()); - MOCK_METHOD(Envoy::Runtime::RandomGenerator&, random, ()); - MOCK_METHOD(Envoy::Runtime::Loader&, runtime, ()); - MOCK_METHOD(Stats::Scope&, scope, ()); - MOCK_METHOD(Singleton::Manager&, singletonManager, ()); - MOCK_METHOD(OverloadManager&, overloadManager, ()); - MOCK_METHOD(ThreadLocal::Instance&, threadLocal, ()); - MOCK_METHOD(Server::Admin&, admin, ()); - MOCK_METHOD(Stats::Scope&, listenerScope, ()); - MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const)); - MOCK_METHOD(const envoy::config::core::v3::Metadata&, listenerMetadata, (), (const)); - MOCK_METHOD(envoy::config::core::v3::TrafficDirection, direction, (), (const)); - MOCK_METHOD(TimeSource&, timeSource, ()); - Event::TestTimeSystem& timeSystem() { return time_system_; } - Grpc::Context& grpcContext() override { return grpc_context_; } - Http::Context& httpContext() override { return http_context_; } - MOCK_METHOD(ProcessContextOptRef, processContext, ()); - MOCK_METHOD(ProtobufMessage::ValidationContext&, messageValidationContext, ()); - MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); - MOCK_METHOD(Api::Api&, api, ()); - - testing::NiceMock server_factory_context_; - testing::NiceMock access_log_manager_; - testing::NiceMock cluster_manager_; - testing::NiceMock dispatcher_; - testing::NiceMock drain_manager_; - testing::NiceMock init_manager_; - testing::NiceMock lifecycle_notifier_; - testing::NiceMock local_info_; - testing::NiceMock random_; - testing::NiceMock runtime_loader_; - testing::NiceMock scope_; - testing::NiceMock thread_local_; - Singleton::ManagerPtr singleton_manager_; - testing::NiceMock admin_; - Stats::IsolatedStoreImpl listener_scope_; - Event::GlobalTimeSystem time_system_; - testing::NiceMock validation_context_; - testing::NiceMock overload_manager_; - Grpc::ContextImpl grpc_context_; - Http::ContextImpl http_context_; - testing::NiceMock api_; -}; - -class MockTransportSocketFactoryContext : public TransportSocketFactoryContext { -public: - MockTransportSocketFactoryContext(); - ~MockTransportSocketFactoryContext() override; - - Secret::SecretManager& secretManager() override { return *(secret_manager_.get()); } - - MOCK_METHOD(Server::Admin&, admin, ()); - MOCK_METHOD(Ssl::ContextManager&, sslContextManager, ()); - MOCK_METHOD(Stats::Scope&, scope, ()); - MOCK_METHOD(Upstream::ClusterManager&, clusterManager, ()); - MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const)); - MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); - MOCK_METHOD(Envoy::Runtime::RandomGenerator&, random, ()); - MOCK_METHOD(Stats::Store&, stats, ()); - MOCK_METHOD(Init::Manager&, initManager, ()); - MOCK_METHOD(Singleton::Manager&, singletonManager, ()); - MOCK_METHOD(ThreadLocal::SlotAllocator&, threadLocal, ()); - MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); - MOCK_METHOD(Api::Api&, api, ()); - - testing::NiceMock cluster_manager_; - testing::NiceMock api_; - testing::NiceMock config_tracker_; - std::unique_ptr secret_manager_; -}; - -class MockListenerFactoryContext : public MockFactoryContext, public ListenerFactoryContext { -public: - MockListenerFactoryContext(); - ~MockListenerFactoryContext() override; - - const Network::ListenerConfig& listenerConfig() const override { return listener_config_; } - MOCK_METHOD(const Network::ListenerConfig&, listenerConfig_, (), (const)); - - Network::MockListenerConfig listener_config_; -}; - -class MockHealthCheckerFactoryContext : public virtual HealthCheckerFactoryContext { -public: - MockHealthCheckerFactoryContext(); - ~MockHealthCheckerFactoryContext() override; - - MOCK_METHOD(Upstream::Cluster&, cluster, ()); - MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); - MOCK_METHOD(Envoy::Runtime::RandomGenerator&, random, ()); - MOCK_METHOD(Envoy::Runtime::Loader&, runtime, ()); - MOCK_METHOD(Upstream::HealthCheckEventLogger*, eventLogger_, ()); - MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); - MOCK_METHOD(Api::Api&, api, ()); - Upstream::HealthCheckEventLoggerPtr eventLogger() override { - return Upstream::HealthCheckEventLoggerPtr(eventLogger_()); - } - - testing::NiceMock cluster_; - testing::NiceMock dispatcher_; - testing::NiceMock random_; - testing::NiceMock runtime_; - testing::NiceMock* event_logger_{}; - testing::NiceMock api_{}; -}; - -class MockFilterChainFactoryContext : public MockFactoryContext, public FilterChainFactoryContext { -public: - MockFilterChainFactoryContext(); - ~MockFilterChainFactoryContext() override; -}; - -class MockTracerFactory : public TracerFactory { -public: - explicit MockTracerFactory(const std::string& name); - ~MockTracerFactory() override; - - std::string name() const override { return name_; } - - MOCK_METHOD(ProtobufTypes::MessagePtr, createEmptyConfigProto, ()); - MOCK_METHOD(Tracing::HttpTracerSharedPtr, createHttpTracer, - (const Protobuf::Message& config, TracerFactoryContext& context)); - -private: - std::string name_; -}; - -class MockTracerFactoryContext : public TracerFactoryContext { -public: - MockTracerFactoryContext(); - ~MockTracerFactoryContext() override; - - MOCK_METHOD(ServerFactoryContext&, serverFactoryContext, ()); - MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); - - testing::NiceMock server_factory_context_; -}; - -class MockBootstrapExtensionFactory : public BootstrapExtensionFactory { -public: - MockBootstrapExtensionFactory(); - ~MockBootstrapExtensionFactory() override; - - MOCK_METHOD(BootstrapExtensionPtr, createBootstrapExtension, - (const Protobuf::Message&, Configuration::ServerFactoryContext&), (override)); - MOCK_METHOD(ProtobufTypes::MessagePtr, createEmptyConfigProto, (), (override)); - MOCK_METHOD(std::string, name, (), (const, override)); -}; - -} // namespace Configuration -} // namespace Server -} // namespace Envoy +#include "drain_manager.h" +#include "factory_context.h" +#include "filter_chain_factory_context.h" +#include "guard_dog.h" +#include "health_checker_factory_context.h" +#include "hot_restart.h" +#include "instance.h" +#include "listener_component_factory.h" +#include "listener_factory_context.h" +#include "listener_manager.h" +#include "main.h" +#include "options.h" +#include "overload_manager.h" +#include "server_lifecycle_notifier.h" +#include "tracer_factory.h" +#include "tracer_factory_context.h" +#include "transport_socket_factory_context.h" +#include "watch_dog.h" +#include "worker.h" +#include "worker_factory.h" diff --git a/test/mocks/server/options.cc b/test/mocks/server/options.cc new file mode 100644 index 000000000000..c407ab6ef907 --- /dev/null +++ b/test/mocks/server/options.cc @@ -0,0 +1,51 @@ +#include "options.h" + +#include "envoy/admin/v3/server_info.pb.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::Invoke; +using ::testing::Return; +using ::testing::ReturnPointee; +using ::testing::ReturnRef; + +MockOptions::MockOptions(const std::string& config_path) : config_path_(config_path) { + ON_CALL(*this, concurrency()).WillByDefault(ReturnPointee(&concurrency_)); + ON_CALL(*this, configPath()).WillByDefault(ReturnRef(config_path_)); + ON_CALL(*this, configProto()).WillByDefault(ReturnRef(config_proto_)); + ON_CALL(*this, configYaml()).WillByDefault(ReturnRef(config_yaml_)); + ON_CALL(*this, bootstrapVersion()).WillByDefault(ReturnRef(bootstrap_version_)); + ON_CALL(*this, allowUnknownStaticFields()).WillByDefault(Invoke([this] { + return allow_unknown_static_fields_; + })); + ON_CALL(*this, rejectUnknownDynamicFields()).WillByDefault(Invoke([this] { + return reject_unknown_dynamic_fields_; + })); + ON_CALL(*this, ignoreUnknownDynamicFields()).WillByDefault(Invoke([this] { + return ignore_unknown_dynamic_fields_; + })); + ON_CALL(*this, adminAddressPath()).WillByDefault(ReturnRef(admin_address_path_)); + ON_CALL(*this, serviceClusterName()).WillByDefault(ReturnRef(service_cluster_name_)); + ON_CALL(*this, serviceNodeName()).WillByDefault(ReturnRef(service_node_name_)); + ON_CALL(*this, serviceZone()).WillByDefault(ReturnRef(service_zone_name_)); + ON_CALL(*this, logLevel()).WillByDefault(Return(log_level_)); + ON_CALL(*this, logPath()).WillByDefault(ReturnRef(log_path_)); + ON_CALL(*this, restartEpoch()).WillByDefault(ReturnPointee(&hot_restart_epoch_)); + ON_CALL(*this, hotRestartDisabled()).WillByDefault(ReturnPointee(&hot_restart_disabled_)); + ON_CALL(*this, signalHandlingEnabled()).WillByDefault(ReturnPointee(&signal_handling_enabled_)); + ON_CALL(*this, mutexTracingEnabled()).WillByDefault(ReturnPointee(&mutex_tracing_enabled_)); + ON_CALL(*this, cpusetThreadsEnabled()).WillByDefault(ReturnPointee(&cpuset_threads_enabled_)); + ON_CALL(*this, disabledExtensions()).WillByDefault(ReturnRef(disabled_extensions_)); + ON_CALL(*this, toCommandLineOptions()).WillByDefault(Invoke([] { + return std::make_unique(); + })); +} + +MockOptions::~MockOptions() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/options.h b/test/mocks/server/options.h new file mode 100644 index 000000000000..31a6112dca35 --- /dev/null +++ b/test/mocks/server/options.h @@ -0,0 +1,76 @@ +#pragma once + +#include "envoy/config/bootstrap/v3/bootstrap.pb.h" +#include "envoy/server/options.h" + +#include "absl/strings/string_view.h" +#include "gmock/gmock.h" +#include "spdlog/spdlog.h" + +namespace Envoy { +namespace Server { +class MockOptions : public Options { +public: + MockOptions() : MockOptions(std::string()) {} + MockOptions(const std::string& config_path); + ~MockOptions() override; + + MOCK_METHOD(uint64_t, baseId, (), (const)); + MOCK_METHOD(bool, useDynamicBaseId, (), (const)); + MOCK_METHOD(const std::string&, baseIdPath, (), (const)); + MOCK_METHOD(uint32_t, concurrency, (), (const)); + MOCK_METHOD(const std::string&, configPath, (), (const)); + MOCK_METHOD(const envoy::config::bootstrap::v3::Bootstrap&, configProto, (), (const)); + MOCK_METHOD(const std::string&, configYaml, (), (const)); + MOCK_METHOD(const absl::optional&, bootstrapVersion, (), (const)); + MOCK_METHOD(bool, allowUnknownStaticFields, (), (const)); + MOCK_METHOD(bool, rejectUnknownDynamicFields, (), (const)); + MOCK_METHOD(bool, ignoreUnknownDynamicFields, (), (const)); + MOCK_METHOD(const std::string&, adminAddressPath, (), (const)); + MOCK_METHOD(Network::Address::IpVersion, localAddressIpVersion, (), (const)); + MOCK_METHOD(std::chrono::seconds, drainTime, (), (const)); + MOCK_METHOD(std::chrono::seconds, parentShutdownTime, (), (const)); + MOCK_METHOD(Server::DrainStrategy, drainStrategy, (), (const)); + MOCK_METHOD(spdlog::level::level_enum, logLevel, (), (const)); + MOCK_METHOD((const std::vector>&), + componentLogLevels, (), (const)); + MOCK_METHOD(const std::string&, logFormat, (), (const)); + MOCK_METHOD(bool, logFormatEscaped, (), (const)); + MOCK_METHOD(const std::string&, logPath, (), (const)); + MOCK_METHOD(uint64_t, restartEpoch, (), (const)); + MOCK_METHOD(std::chrono::milliseconds, fileFlushIntervalMsec, (), (const)); + MOCK_METHOD(Mode, mode, (), (const)); + MOCK_METHOD(const std::string&, serviceClusterName, (), (const)); + MOCK_METHOD(const std::string&, serviceNodeName, (), (const)); + MOCK_METHOD(const std::string&, serviceZone, (), (const)); + MOCK_METHOD(bool, hotRestartDisabled, (), (const)); + MOCK_METHOD(bool, signalHandlingEnabled, (), (const)); + MOCK_METHOD(bool, mutexTracingEnabled, (), (const)); + MOCK_METHOD(bool, fakeSymbolTableEnabled, (), (const)); + MOCK_METHOD(bool, cpusetThreadsEnabled, (), (const)); + MOCK_METHOD(const std::vector&, disabledExtensions, (), (const)); + MOCK_METHOD(Server::CommandLineOptionsPtr, toCommandLineOptions, (), (const)); + + std::string config_path_; + envoy::config::bootstrap::v3::Bootstrap config_proto_; + std::string config_yaml_; + absl::optional bootstrap_version_; + bool allow_unknown_static_fields_{}; + bool reject_unknown_dynamic_fields_{}; + bool ignore_unknown_dynamic_fields_{}; + std::string admin_address_path_; + std::string service_cluster_name_; + std::string service_node_name_; + std::string service_zone_name_; + spdlog::level::level_enum log_level_{spdlog::level::trace}; + std::string log_path_; + uint32_t concurrency_{1}; + uint64_t hot_restart_epoch_{}; + bool hot_restart_disabled_{}; + bool signal_handling_enabled_{true}; + bool mutex_tracing_enabled_{}; + bool cpuset_threads_enabled_{}; + std::vector disabled_extensions_; +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/overload_manager.cc b/test/mocks/server/overload_manager.cc new file mode 100644 index 000000000000..d105df80e690 --- /dev/null +++ b/test/mocks/server/overload_manager.cc @@ -0,0 +1,19 @@ +#include "overload_manager.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::ReturnRef; +MockOverloadManager::MockOverloadManager() { + ON_CALL(*this, getThreadLocalOverloadState()).WillByDefault(ReturnRef(overload_state_)); +} + +MockOverloadManager::~MockOverloadManager() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/overload_manager.h b/test/mocks/server/overload_manager.h new file mode 100644 index 000000000000..8ce63cef1b12 --- /dev/null +++ b/test/mocks/server/overload_manager.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "envoy/server/overload_manager.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockOverloadManager : public OverloadManager { +public: + MockOverloadManager(); + ~MockOverloadManager() override; + + // OverloadManager + MOCK_METHOD(void, start, ()); + MOCK_METHOD(bool, registerForAction, + (const std::string& action, Event::Dispatcher& dispatcher, + OverloadActionCb callback)); + MOCK_METHOD(ThreadLocalOverloadState&, getThreadLocalOverloadState, ()); + + ThreadLocalOverloadState overload_state_; +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/server_lifecycle_notifier.cc b/test/mocks/server/server_lifecycle_notifier.cc new file mode 100644 index 000000000000..1cd6cd9d3b5c --- /dev/null +++ b/test/mocks/server/server_lifecycle_notifier.cc @@ -0,0 +1,16 @@ +#include "server_lifecycle_notifier.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +MockServerLifecycleNotifier::MockServerLifecycleNotifier() = default; + +MockServerLifecycleNotifier::~MockServerLifecycleNotifier() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/server_lifecycle_notifier.h b/test/mocks/server/server_lifecycle_notifier.h new file mode 100644 index 000000000000..3442a194ac32 --- /dev/null +++ b/test/mocks/server/server_lifecycle_notifier.h @@ -0,0 +1,20 @@ +#pragma once + +#include "envoy/server/lifecycle_notifier.h" + +#include "gmock/gmock.h" +#include "spdlog/spdlog.h" + +namespace Envoy { +namespace Server { +class MockServerLifecycleNotifier : public ServerLifecycleNotifier { +public: + MockServerLifecycleNotifier(); + ~MockServerLifecycleNotifier() override; + + MOCK_METHOD(ServerLifecycleNotifier::HandlePtr, registerCallback, (Stage, StageCallback)); + MOCK_METHOD(ServerLifecycleNotifier::HandlePtr, registerCallback, + (Stage, StageCallbackWithCompletion)); +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/tracer_factory.cc b/test/mocks/server/tracer_factory.cc new file mode 100644 index 000000000000..7dcaa39f7ae1 --- /dev/null +++ b/test/mocks/server/tracer_factory.cc @@ -0,0 +1,24 @@ +#include "tracer_factory.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +namespace Configuration { + +using ::testing::Invoke; + +MockTracerFactory::MockTracerFactory(const std::string& name) : name_(name) { + ON_CALL(*this, createEmptyConfigProto()).WillByDefault(Invoke([] { + return std::make_unique(); + })); +} + +MockTracerFactory::~MockTracerFactory() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/tracer_factory.h b/test/mocks/server/tracer_factory.h new file mode 100644 index 000000000000..e342116ee2d5 --- /dev/null +++ b/test/mocks/server/tracer_factory.h @@ -0,0 +1,27 @@ +#pragma once + +#include "envoy/protobuf/message_validator.h" +#include "envoy/server/tracer_config.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockTracerFactory : public TracerFactory { +public: + explicit MockTracerFactory(const std::string& name); + ~MockTracerFactory() override; + + std::string name() const override { return name_; } + + MOCK_METHOD(ProtobufTypes::MessagePtr, createEmptyConfigProto, ()); + MOCK_METHOD(Tracing::HttpTracerSharedPtr, createHttpTracer, + (const Protobuf::Message& config, TracerFactoryContext& context)); + +private: + std::string name_; +}; +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/tracer_factory_context.cc b/test/mocks/server/tracer_factory_context.cc new file mode 100644 index 000000000000..992a3854179f --- /dev/null +++ b/test/mocks/server/tracer_factory_context.cc @@ -0,0 +1,24 @@ +#include "tracer_factory_context.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +namespace Configuration { + +using ::testing::ReturnRef; + +MockTracerFactoryContext::MockTracerFactoryContext() { + ON_CALL(*this, serverFactoryContext()).WillByDefault(ReturnRef(server_factory_context_)); + ON_CALL(*this, messageValidationVisitor()) + .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); +} + +MockTracerFactoryContext::~MockTracerFactoryContext() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/tracer_factory_context.h b/test/mocks/server/tracer_factory_context.h new file mode 100644 index 000000000000..66a0212d0bb7 --- /dev/null +++ b/test/mocks/server/tracer_factory_context.h @@ -0,0 +1,25 @@ +#pragma once + +#include "envoy/server/configuration.h" + +#include "gmock/gmock.h" +#include "instance.h" +#include "tracer_factory.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockTracerFactoryContext : public TracerFactoryContext { +public: + MockTracerFactoryContext(); + ~MockTracerFactoryContext() override; + + MOCK_METHOD(ServerFactoryContext&, serverFactoryContext, ()); + MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); + + testing::NiceMock server_factory_context_; +}; +} // namespace Configuration + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/transport_socket_factory_context.cc b/test/mocks/server/transport_socket_factory_context.cc new file mode 100644 index 000000000000..0e4e50231a7d --- /dev/null +++ b/test/mocks/server/transport_socket_factory_context.cc @@ -0,0 +1,26 @@ +#include "transport_socket_factory_context.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +namespace Configuration { + +using ::testing::ReturnRef; + +MockTransportSocketFactoryContext::MockTransportSocketFactoryContext() + : secret_manager_(std::make_unique(config_tracker_)) { + ON_CALL(*this, clusterManager()).WillByDefault(ReturnRef(cluster_manager_)); + ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); + ON_CALL(*this, messageValidationVisitor()) + .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); +} + +MockTransportSocketFactoryContext::~MockTransportSocketFactoryContext() = default; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/transport_socket_factory_context.h b/test/mocks/server/transport_socket_factory_context.h new file mode 100644 index 000000000000..a86f2b348485 --- /dev/null +++ b/test/mocks/server/transport_socket_factory_context.h @@ -0,0 +1,44 @@ +#pragma once + +#include "envoy/server/transport_socket_config.h" + +#include "common/secret/secret_manager_impl.h" + +#include "test/mocks/api/mocks.h" +#include "test/mocks/upstream/mocks.h" + +#include "config_tracker.h" +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +namespace Configuration { +class MockTransportSocketFactoryContext : public TransportSocketFactoryContext { +public: + MockTransportSocketFactoryContext(); + ~MockTransportSocketFactoryContext() override; + + Secret::SecretManager& secretManager() override { return *(secret_manager_.get()); } + + MOCK_METHOD(Server::Admin&, admin, ()); + MOCK_METHOD(Ssl::ContextManager&, sslContextManager, ()); + MOCK_METHOD(Stats::Scope&, scope, ()); + MOCK_METHOD(Upstream::ClusterManager&, clusterManager, ()); + MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const)); + MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); + MOCK_METHOD(Envoy::Runtime::RandomGenerator&, random, ()); + MOCK_METHOD(Stats::Store&, stats, ()); + MOCK_METHOD(Init::Manager&, initManager, ()); + MOCK_METHOD(Singleton::Manager&, singletonManager, ()); + MOCK_METHOD(ThreadLocal::SlotAllocator&, threadLocal, ()); + MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); + MOCK_METHOD(Api::Api&, api, ()); + + testing::NiceMock cluster_manager_; + testing::NiceMock api_; + testing::NiceMock config_tracker_; + std::unique_ptr secret_manager_; +}; +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/watch_dog.cc b/test/mocks/server/watch_dog.cc new file mode 100644 index 000000000000..f07755963fe6 --- /dev/null +++ b/test/mocks/server/watch_dog.cc @@ -0,0 +1,16 @@ +#include "watch_dog.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +MockWatchDog::MockWatchDog() = default; + +MockWatchDog::~MockWatchDog() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/watch_dog.h b/test/mocks/server/watch_dog.h new file mode 100644 index 000000000000..105761781c36 --- /dev/null +++ b/test/mocks/server/watch_dog.h @@ -0,0 +1,21 @@ +#pragma once + +#include "envoy/server/watchdog.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockWatchDog : public WatchDog { +public: + MockWatchDog(); + ~MockWatchDog() override; + + // Server::WatchDog + MOCK_METHOD(void, startWatchdog, (Event::Dispatcher & dispatcher)); + MOCK_METHOD(void, touch, ()); + MOCK_METHOD(Thread::ThreadId, threadId, (), (const)); + MOCK_METHOD(MonotonicTime, lastTouchTime, (), (const)); +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/worker.cc b/test/mocks/server/worker.cc new file mode 100644 index 000000000000..a7e981b299b1 --- /dev/null +++ b/test/mocks/server/worker.cc @@ -0,0 +1,50 @@ +#include "worker.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::_; +using ::testing::Invoke; + +MockWorker::MockWorker() { + ON_CALL(*this, addListener(_, _, _)) + .WillByDefault( + Invoke([this](absl::optional overridden_listener, + Network::ListenerConfig& config, AddListenerCompletion completion) -> void { + UNREFERENCED_PARAMETER(overridden_listener); + config.listenSocketFactory().getListenSocket(); + EXPECT_EQ(nullptr, add_listener_completion_); + add_listener_completion_ = completion; + })); + + ON_CALL(*this, removeListener(_, _)) + .WillByDefault( + Invoke([this](Network::ListenerConfig&, std::function completion) -> void { + EXPECT_EQ(nullptr, remove_listener_completion_); + remove_listener_completion_ = completion; + })); + + ON_CALL(*this, stopListener(_, _)) + .WillByDefault(Invoke([](Network::ListenerConfig&, std::function completion) -> void { + if (completion != nullptr) { + completion(); + } + })); + + ON_CALL(*this, removeFilterChains(_, _, _)) + .WillByDefault(Invoke([this](uint64_t, const std::list&, + std::function completion) -> void { + EXPECT_EQ(nullptr, remove_filter_chains_completion_); + remove_filter_chains_completion_ = completion; + })); +} + +MockWorker::~MockWorker() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/worker.h b/test/mocks/server/worker.h new file mode 100644 index 000000000000..978ab3bbcc21 --- /dev/null +++ b/test/mocks/server/worker.h @@ -0,0 +1,54 @@ +#pragma once + +#include "envoy/server/worker.h" + +#include "absl/strings/string_view.h" +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockWorker : public Worker { +public: + MockWorker(); + ~MockWorker() override; + + void callAddCompletion(bool success) { + EXPECT_NE(nullptr, add_listener_completion_); + add_listener_completion_(success); + add_listener_completion_ = nullptr; + } + + void callRemovalCompletion() { + EXPECT_NE(nullptr, remove_listener_completion_); + remove_listener_completion_(); + remove_listener_completion_ = nullptr; + } + + void callDrainFilterChainsComplete() { + EXPECT_NE(nullptr, remove_filter_chains_completion_); + remove_filter_chains_completion_(); + remove_filter_chains_completion_ = nullptr; + } + + // Server::Worker + MOCK_METHOD(void, addListener, + (absl::optional overridden_listener, Network::ListenerConfig& listener, + AddListenerCompletion completion)); + MOCK_METHOD(uint64_t, numConnections, (), (const)); + MOCK_METHOD(void, removeListener, + (Network::ListenerConfig & listener, std::function completion)); + MOCK_METHOD(void, start, (GuardDog & guard_dog)); + MOCK_METHOD(void, initializeStats, (Stats::Scope & scope)); + MOCK_METHOD(void, stop, ()); + MOCK_METHOD(void, stopListener, + (Network::ListenerConfig & listener, std::function completion)); + MOCK_METHOD(void, removeFilterChains, + (uint64_t listener_tag, const std::list& filter_chains, + std::function completion)); + + AddListenerCompletion add_listener_completion_; + std::function remove_listener_completion_; + std::function remove_filter_chains_completion_; +}; +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/worker_factory.cc b/test/mocks/server/worker_factory.cc new file mode 100644 index 000000000000..65844ae8cc21 --- /dev/null +++ b/test/mocks/server/worker_factory.cc @@ -0,0 +1,14 @@ +#include "worker_factory.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +MockWorkerFactory::MockWorkerFactory() = default; + +MockWorkerFactory::~MockWorkerFactory() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/worker_factory.h b/test/mocks/server/worker_factory.h new file mode 100644 index 000000000000..3c05ed76566c --- /dev/null +++ b/test/mocks/server/worker_factory.h @@ -0,0 +1,23 @@ +#pragma once + +#include "envoy/server/worker.h" + +#include "gmock/gmock.h" +#include "worker.h" + +namespace Envoy { +namespace Server { +class MockWorkerFactory : public WorkerFactory { +public: + MockWorkerFactory(); + ~MockWorkerFactory() override; + + // Server::WorkerFactory + WorkerPtr createWorker(OverloadManager&, const std::string&) override { + return WorkerPtr{createWorker_()}; + } + + MOCK_METHOD(Worker*, createWorker_, ()); +}; +} // namespace Server +} // namespace Envoy