From 35116f807113afc265fc899fefdbacff06b5b347 Mon Sep 17 00:00:00 2001 From: Henna Huang Date: Mon, 9 Apr 2018 15:45:09 -0400 Subject: [PATCH 1/3] Use proto resolver for listener Signed-off-by: Henna Huang --- source/server/BUILD | 1 + source/server/listener_manager_impl.cc | 3 ++- test/server/listener_manager_impl_test.cc | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/source/server/BUILD b/source/server/BUILD index 293f396f4166..72d2b9d93721 100644 --- a/source/server/BUILD +++ b/source/server/BUILD @@ -205,6 +205,7 @@ envoy_cc_library( "//source/common/config:utility_lib", "//source/common/config:well_known_names", "//source/common/network:listen_socket_lib", + "//source/common/network:resolver_lib", "//source/common/network:socket_option_lib", "//source/common/network:utility_lib", "//source/common/protobuf:utility_lib", diff --git a/source/server/listener_manager_impl.cc b/source/server/listener_manager_impl.cc index 3a7e76d94181..1e6d969c47c1 100644 --- a/source/server/listener_manager_impl.cc +++ b/source/server/listener_manager_impl.cc @@ -9,6 +9,7 @@ #include "common/config/utility.h" #include "common/config/well_known_names.h" #include "common/network/listen_socket_impl.h" +#include "common/network/resolver_impl.h" #include "common/network/socket_option_impl.h" #include "common/network/utility.h" #include "common/protobuf/utility.h" @@ -120,7 +121,7 @@ class ListenerSocketOption : public Network::SocketOptionImpl { ListenerImpl::ListenerImpl(const envoy::api::v2::Listener& config, ListenerManagerImpl& parent, const std::string& name, bool modifiable, bool workers_started, uint64_t hash) - : parent_(parent), address_(Network::Utility::protobufAddressToAddress(config.address())), + : parent_(parent), address_(Network::Address::resolveProtoAddress(config.address())), global_scope_(parent_.server_.stats().createScope("")), listener_scope_( parent_.server_.stats().createScope(fmt::format("listener.{}.", address_->asString()))), diff --git a/test/server/listener_manager_impl_test.cc b/test/server/listener_manager_impl_test.cc index a819ec47e43b..e48301050b04 100644 --- a/test/server/listener_manager_impl_test.cc +++ b/test/server/listener_manager_impl_test.cc @@ -1512,6 +1512,23 @@ TEST_F(ListenerManagerImplWithRealFiltersTest, FreebindListenerEnabled) { } } +// Set the resolver to the default IP resolver. The address resolver logic is unit tested in +// resolver_impl_test.cc. +TEST_F(ListenerManagerImplWithRealFiltersTest, AddressResolver) { + const std::string yaml = TestEnvironment::substitute(R"EOF( + name: AddressResolverdListener + address: + socket_address: { address: 127.0.0.1, port_value: 1111, resolver_name: envoy.ip } + filter_chains: + - filters: + )EOF", + Network::Address::IpVersion::v4); + + EXPECT_CALL(listener_factory_, createListenSocket(_, _, true)); + manager_->addOrUpdateListener(parseListenerFromV2Yaml(yaml), true); + EXPECT_EQ(1U, manager_->listeners().size()); +} + TEST_F(ListenerManagerImplWithRealFiltersTest, CRLFilename) { const std::string yaml = TestEnvironment::substitute(R"EOF( address: From b6cce9213dfcdc7de71c9aba3dd11bd181e400d0 Mon Sep 17 00:00:00 2001 From: Henna Huang Date: Wed, 11 Apr 2018 02:43:02 -0400 Subject: [PATCH 2/3] Add mock address resolver to test Signed-off-by: Henna Huang --- test/server/BUILD | 4 ++++ test/server/listener_manager_impl_test.cc | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/test/server/BUILD b/test/server/BUILD index 60ebba427302..98a38609719c 100644 --- a/test/server/BUILD +++ b/test/server/BUILD @@ -128,10 +128,12 @@ envoy_cc_test( data = ["//test/common/ssl/test_data:certs"], deps = [ ":utility_lib", + "//include/envoy/network:resolver_interface", "//source/common/api:os_sys_calls_lib", "//source/common/config:metadata_lib", "//source/common/network:listen_socket_lib", "//source/common/network:socket_option_lib", + "//source/common/network:utility_lib", "//source/extensions/filters/listener/original_dst:config", "//source/extensions/transport_sockets/raw_buffer:config", "//source/extensions/transport_sockets/ssl:config", @@ -139,7 +141,9 @@ envoy_cc_test( "//source/server/config/network:http_connection_manager_lib", "//test/mocks/server:server_mocks", "//test/test_common:environment_lib", + "//test/test_common:registry_lib", "//test/test_common:threadsafe_singleton_injector_lib", + "@envoy_api//envoy/api/v2/core:base_cc", ], ) diff --git a/test/server/listener_manager_impl_test.cc b/test/server/listener_manager_impl_test.cc index e48301050b04..ada710645242 100644 --- a/test/server/listener_manager_impl_test.cc +++ b/test/server/listener_manager_impl_test.cc @@ -1,3 +1,5 @@ +#include "envoy/api/v2/core/address.pb.h" +#include "envoy/network/resolver.h" #include "envoy/registry/registry.h" #include "envoy/server/filter_config.h" @@ -6,6 +8,7 @@ #include "common/network/address_impl.h" #include "common/network/listen_socket_impl.h" #include "common/network/socket_option_impl.h" +#include "common/network/utility.h" #include "server/configuration_impl.h" #include "server/listener_manager_impl.h" @@ -15,6 +18,7 @@ #include "test/mocks/server/mocks.h" #include "test/server/utility.h" #include "test/test_common/environment.h" +#include "test/test_common/registry.h" #include "test/test_common/threadsafe_singleton_injector.h" #include "test/test_common/utility.h" @@ -123,6 +127,13 @@ class ListenerManagerImplWithRealFiltersTest : public ListenerManagerImplTest { } }; +class MockAddressResolver : public Network::Address::Resolver { +public: + MOCK_METHOD1(resolve, Network::Address::InstanceConstSharedPtr( + const envoy::api::v2::core::SocketAddress&)); + MOCK_CONST_METHOD0(name, std::string()); +}; + TEST_F(ListenerManagerImplWithRealFiltersTest, EmptyFilter) { const std::string json = R"EOF( { @@ -1518,12 +1529,20 @@ TEST_F(ListenerManagerImplWithRealFiltersTest, AddressResolver) { const std::string yaml = TestEnvironment::substitute(R"EOF( name: AddressResolverdListener address: - socket_address: { address: 127.0.0.1, port_value: 1111, resolver_name: envoy.ip } + socket_address: { address: 127.0.0.1, port_value: 1111, resolver_name: envoy.mock.resolver } filter_chains: - filters: )EOF", Network::Address::IpVersion::v4); + MockAddressResolver mock_resolver; + EXPECT_CALL(mock_resolver, name()).WillRepeatedly(Return("envoy.mock.resolver")); + + EXPECT_CALL(mock_resolver, resolve(_)) + .WillOnce(Return(Network::Utility::parseInternetAddress("127.0.0.1", 1111, false))); + + Registry::InjectFactory register_resolver(mock_resolver); + EXPECT_CALL(listener_factory_, createListenSocket(_, _, true)); manager_->addOrUpdateListener(parseListenerFromV2Yaml(yaml), true); EXPECT_EQ(1U, manager_->listeners().size()); From b51a2ec093adf9134b7ae5331a858d91ad879078 Mon Sep 17 00:00:00 2001 From: Henna Huang Date: Thu, 12 Apr 2018 01:24:14 -0400 Subject: [PATCH 3/3] Move mock resolver to test/mocks/network/... Signed-off-by: Henna Huang --- test/mocks/network/BUILD | 2 ++ test/mocks/network/mocks.cc | 6 ++++++ test/mocks/network/mocks.h | 12 ++++++++++++ test/server/BUILD | 3 +-- test/server/listener_manager_impl_test.cc | 14 ++------------ 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/test/mocks/network/BUILD b/test/mocks/network/BUILD index 5cc4100695b2..1ad8b522255c 100644 --- a/test/mocks/network/BUILD +++ b/test/mocks/network/BUILD @@ -17,10 +17,12 @@ envoy_cc_mock( "//include/envoy/network:connection_interface", "//include/envoy/network:drain_decision_interface", "//include/envoy/network:filter_interface", + "//include/envoy/network:resolver_interface", "//include/envoy/network:transport_socket_interface", "//include/envoy/server:listener_manager_interface", "//source/common/network:address_lib", "//source/common/network:utility_lib", "//test/mocks/event:event_mocks", + "@envoy_api//envoy/api/v2/core:base_cc", ], ) diff --git a/test/mocks/network/mocks.cc b/test/mocks/network/mocks.cc index 92ddb0bd3937..5930dae472c8 100644 --- a/test/mocks/network/mocks.cc +++ b/test/mocks/network/mocks.cc @@ -117,6 +117,12 @@ MockDnsResolver::MockDnsResolver() { MockDnsResolver::~MockDnsResolver() {} +MockAddressResolver::MockAddressResolver() { + ON_CALL(*this, name()).WillByDefault(Return("envoy.mock.resolver")); +} + +MockAddressResolver::~MockAddressResolver() {} + MockReadFilterCallbacks::MockReadFilterCallbacks() { ON_CALL(*this, connection()).WillByDefault(ReturnRef(connection_)); ON_CALL(*this, upstreamHost()).WillByDefault(ReturnPointee(&host_)); diff --git a/test/mocks/network/mocks.h b/test/mocks/network/mocks.h index 9b0d70ed699e..4037bd7ffa14 100644 --- a/test/mocks/network/mocks.h +++ b/test/mocks/network/mocks.h @@ -4,9 +4,11 @@ #include #include +#include "envoy/api/v2/core/address.pb.h" #include "envoy/network/connection.h" #include "envoy/network/drain_decision.h" #include "envoy/network/filter.h" +#include "envoy/network/resolver.h" #include "envoy/network/transport_socket.h" #include "common/stats/stats_impl.h" @@ -147,6 +149,16 @@ class MockDnsResolver : public DnsResolver { testing::NiceMock active_query_; }; +class MockAddressResolver : public Address::Resolver { +public: + MockAddressResolver(); + ~MockAddressResolver(); + + MOCK_METHOD1(resolve, + Address::InstanceConstSharedPtr(const envoy::api::v2::core::SocketAddress&)); + MOCK_CONST_METHOD0(name, std::string()); +}; + class MockReadFilterCallbacks : public ReadFilterCallbacks { public: MockReadFilterCallbacks(); diff --git a/test/server/BUILD b/test/server/BUILD index 98a38609719c..77c324dbe0e7 100644 --- a/test/server/BUILD +++ b/test/server/BUILD @@ -128,7 +128,6 @@ envoy_cc_test( data = ["//test/common/ssl/test_data:certs"], deps = [ ":utility_lib", - "//include/envoy/network:resolver_interface", "//source/common/api:os_sys_calls_lib", "//source/common/config:metadata_lib", "//source/common/network:listen_socket_lib", @@ -139,11 +138,11 @@ envoy_cc_test( "//source/extensions/transport_sockets/ssl:config", "//source/server:listener_manager_lib", "//source/server/config/network:http_connection_manager_lib", + "//test/mocks/network:network_mocks", "//test/mocks/server:server_mocks", "//test/test_common:environment_lib", "//test/test_common:registry_lib", "//test/test_common:threadsafe_singleton_injector_lib", - "@envoy_api//envoy/api/v2/core:base_cc", ], ) diff --git a/test/server/listener_manager_impl_test.cc b/test/server/listener_manager_impl_test.cc index ada710645242..bf71d137ff88 100644 --- a/test/server/listener_manager_impl_test.cc +++ b/test/server/listener_manager_impl_test.cc @@ -1,5 +1,3 @@ -#include "envoy/api/v2/core/address.pb.h" -#include "envoy/network/resolver.h" #include "envoy/registry/registry.h" #include "envoy/server/filter_config.h" @@ -15,6 +13,7 @@ #include "extensions/filters/listener/original_dst/original_dst.h" +#include "test/mocks/network/mocks.h" #include "test/mocks/server/mocks.h" #include "test/server/utility.h" #include "test/test_common/environment.h" @@ -127,13 +126,6 @@ class ListenerManagerImplWithRealFiltersTest : public ListenerManagerImplTest { } }; -class MockAddressResolver : public Network::Address::Resolver { -public: - MOCK_METHOD1(resolve, Network::Address::InstanceConstSharedPtr( - const envoy::api::v2::core::SocketAddress&)); - MOCK_CONST_METHOD0(name, std::string()); -}; - TEST_F(ListenerManagerImplWithRealFiltersTest, EmptyFilter) { const std::string json = R"EOF( { @@ -1535,9 +1527,7 @@ TEST_F(ListenerManagerImplWithRealFiltersTest, AddressResolver) { )EOF", Network::Address::IpVersion::v4); - MockAddressResolver mock_resolver; - EXPECT_CALL(mock_resolver, name()).WillRepeatedly(Return("envoy.mock.resolver")); - + NiceMock mock_resolver; EXPECT_CALL(mock_resolver, resolve(_)) .WillOnce(Return(Network::Utility::parseInternetAddress("127.0.0.1", 1111, false)));