|
| 1 | +#include "envoy/config/bootstrap/v3/bootstrap.pb.h" |
| 2 | +#include "envoy/event/dispatcher.h" |
| 3 | +#include "envoy/extensions/filters/listener/original_dst/v3/original_dst.pb.h" |
| 4 | +#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h" |
| 5 | +#include "envoy/http/header_map.h" |
| 6 | + |
| 7 | +#include "source/common/http/headers.h" |
| 8 | +#include "source/common/network/utility.h" |
| 9 | + |
| 10 | +#include "test/common/upstream/utility.h" |
| 11 | +#include "test/integration/http_integration.h" |
| 12 | +#include "test/integration/http_protocol_integration.h" |
| 13 | +#include "test/integration/utility.h" |
| 14 | + |
| 15 | +#include "gtest/gtest.h" |
| 16 | + |
| 17 | +namespace Envoy { |
| 18 | + |
| 19 | +using OriginalDstIntegrationTest = HttpProtocolIntegrationTest; |
| 20 | + |
| 21 | +ConfigHelper::ConfigModifierFunction setOriginalDstCluster(int port) { |
| 22 | + return [port](envoy::config::bootstrap::v3::Bootstrap& bootstrap) { |
| 23 | + RELEASE_ASSERT(bootstrap.mutable_static_resources()->clusters_size() == 1, ""); |
| 24 | + auto& cluster = *bootstrap.mutable_static_resources()->mutable_clusters(0); |
| 25 | + cluster.set_type(envoy::config::cluster::v3::Cluster::ORIGINAL_DST); |
| 26 | + cluster.set_lb_policy(envoy::config::cluster::v3::Cluster::CLUSTER_PROVIDED); |
| 27 | + cluster.mutable_original_dst_lb_config()->mutable_upstream_port_override()->set_value(port); |
| 28 | + cluster.clear_load_assignment(); |
| 29 | + |
| 30 | + auto* listener = bootstrap.mutable_static_resources()->mutable_listeners(0); |
| 31 | + listener->mutable_address()->mutable_socket_address()->set_address("0.0.0.0"); |
| 32 | + listener->set_traffic_direction(envoy::config::core::v3::INBOUND); |
| 33 | + |
| 34 | + auto* listener_filter = listener->add_listener_filters(); |
| 35 | + listener_filter->set_name("envoy.filters.listener.original_dst"); |
| 36 | + envoy::extensions::filters::listener::original_dst::v3::OriginalDst original_dst; |
| 37 | + listener_filter->mutable_typed_config()->PackFrom(original_dst); |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +INSTANTIATE_TEST_SUITE_P(Protocols, OriginalDstIntegrationTest, |
| 42 | + testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams( |
| 43 | + {Http::CodecType::HTTP1}, {Http::CodecType::HTTP1})), |
| 44 | + HttpProtocolIntegrationTest::protocolTestParamsToString); |
| 45 | + |
| 46 | +TEST_P(OriginalDstIntegrationTest, OriginalDstHttpManyConnections) { |
| 47 | + // Windows apparently have this loopback property. |
| 48 | + DISABLE_UNDER_WINDOWS; |
| 49 | + // Only do this for IPv4 as we can have many 127.0.0.x addresses listening on 0.0.0.0 |
| 50 | + if (version_ != Network::Address::IpVersion::v4) { |
| 51 | + return; |
| 52 | + } |
| 53 | + ASSERT(!upstream_tls_); |
| 54 | + auto address = Network::Test::getAnyAddress(Network::Address::IpVersion::v4); |
| 55 | + fake_upstreams_.emplace_back( |
| 56 | + std::make_unique<FakeUpstream>(Network::Test::createRawBufferDownstreamSocketFactory(), |
| 57 | + address, configWithType(upstreamProtocol()))); |
| 58 | + |
| 59 | + config_helper_.addConfigModifier( |
| 60 | + setOriginalDstCluster(fake_upstreams_[0]->localAddress()->ip()->port())); |
| 61 | + initialize(); |
| 62 | + |
| 63 | + const int32_t kMaxConnections = 10; |
| 64 | + for (int i = 0; i < kMaxConnections; ++i) { |
| 65 | + std::string address_str = fmt::format("127.0.0.{}", i + 1); |
| 66 | + Network::ClientConnectionPtr connection(dispatcher_->createClientConnection( |
| 67 | + Network::Utility::resolveUrl(fmt::format("tcp://{}:{}", address_str, lookupPort("http"))), |
| 68 | + Network::Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr, |
| 69 | + nullptr)); |
| 70 | + |
| 71 | + connection->enableHalfClose(enableHalfClose()); |
| 72 | + |
| 73 | + codec_client_ = makeHttpConnection(std::move(connection)); |
| 74 | + auto response = |
| 75 | + sendRequestAndWaitForResponse(default_request_headers_, 0, default_response_headers_, 0); |
| 76 | + EXPECT_EQ(address_str, fake_upstream_connection_->connection() |
| 77 | + .connectionInfoProvider() |
| 78 | + .localAddress() |
| 79 | + ->ip() |
| 80 | + ->addressAsString()); |
| 81 | + ASSERT_TRUE(fake_upstream_connection_->close()); |
| 82 | + fake_upstream_connection_.reset(); |
| 83 | + codec_client_->close(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class OriginalDstTcpProxyIntegrationTest |
| 88 | + : public testing::TestWithParam<Network::Address::IpVersion>, |
| 89 | + public BaseIntegrationTest { |
| 90 | +public: |
| 91 | + OriginalDstTcpProxyIntegrationTest() |
| 92 | + : BaseIntegrationTest(GetParam(), ConfigHelper::tcpProxyConfig()) { |
| 93 | + enableHalfClose(true); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +TEST_P(OriginalDstTcpProxyIntegrationTest, TestManyConnections) { |
| 98 | + // Windows apparently have this loopback property. |
| 99 | + DISABLE_UNDER_WINDOWS; |
| 100 | + if (version_ != Network::Address::IpVersion::v4) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Create an upstream on 0.0.0.0 |
| 105 | + auto address = Network::Test::getAnyAddress(Network::Address::IpVersion::v4); |
| 106 | + ASSERT(!upstream_tls_); |
| 107 | + fake_upstreams_.emplace_back( |
| 108 | + std::make_unique<FakeUpstream>(Network::Test::createRawBufferDownstreamSocketFactory(), |
| 109 | + address, configWithType(upstreamProtocol()))); |
| 110 | + |
| 111 | + config_helper_.addConfigModifier( |
| 112 | + setOriginalDstCluster(fake_upstreams_[0]->localAddress()->ip()->port())); |
| 113 | + initialize(); |
| 114 | + |
| 115 | + const int32_t kMaxConnections = 10; |
| 116 | + for (int i = 0; i < kMaxConnections; ++i) { |
| 117 | + // Set up the connection |
| 118 | + std::string address_str = fmt::format("127.0.0.{}", i + 1); |
| 119 | + IntegrationTcpClientPtr tcp_client = |
| 120 | + makeTcpConnection(lookupPort("listener_0"), nullptr, nullptr, address_str); |
| 121 | + FakeRawConnectionPtr fake_upstream_connection; |
| 122 | + ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(fake_upstream_connection)); |
| 123 | + EXPECT_EQ(address_str, fake_upstream_connection->connection() |
| 124 | + .connectionInfoProvider() |
| 125 | + .localAddress() |
| 126 | + ->ip() |
| 127 | + ->addressAsString()); |
| 128 | + |
| 129 | + // Write bidirectional data. |
| 130 | + ASSERT_TRUE(fake_upstream_connection->write("hello")); |
| 131 | + tcp_client->waitForData("hello"); |
| 132 | + ASSERT_TRUE(tcp_client->write("hello")); |
| 133 | + ASSERT_TRUE(fake_upstream_connection->waitForData(5)); |
| 134 | + |
| 135 | + // Close down the connection. |
| 136 | + ASSERT_TRUE(fake_upstream_connection->write("", true)); |
| 137 | + tcp_client->waitForHalfClose(); |
| 138 | + ASSERT_TRUE(tcp_client->write("", true)); |
| 139 | + ASSERT_TRUE(fake_upstream_connection->waitForHalfClose()); |
| 140 | + ASSERT_TRUE(fake_upstream_connection->waitForDisconnect()); |
| 141 | + } |
| 142 | + test_server_->waitForCounterGe("cluster_manager.cluster_updated", kMaxConnections); |
| 143 | +} |
| 144 | + |
| 145 | +INSTANTIATE_TEST_SUITE_P(IpVersions, OriginalDstTcpProxyIntegrationTest, |
| 146 | + testing::ValuesIn(TestEnvironment::getIpVersionsForTest()), |
| 147 | + TestUtility::ipTestParamsToString); |
| 148 | + |
| 149 | +} // namespace Envoy |
0 commit comments