Merge pull request #24521 from yang-g/ip46

Check localhost is resolved to ipv4 or ipv6 and update test accordingly.
pull/24555/head
Yang Gao 4 years ago committed by GitHub
commit 01596014af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      CMakeLists.txt
  2. 6
      build_autogenerated.yaml
  3. 2
      gRPC-Core.podspec
  4. 2
      grpc.gyp
  5. 3
      test/core/util/BUILD
  6. 58
      test/core/util/resolve_localhost_ip46.cc
  7. 29
      test/core/util/resolve_localhost_ip46.h
  8. 46
      test/cpp/end2end/client_lb_end2end_test.cc
  9. 4
      test/cpp/end2end/end2end_test.cc
  10. 188
      test/cpp/end2end/grpclb_end2end_test.cc
  11. 2
      test/cpp/end2end/server_early_return_test.cc
  12. 10
      test/cpp/end2end/service_config_end2end_test.cc
  13. 488
      test/cpp/end2end/xds_end2end_test.cc
  14. 2
      test/cpp/ext/filters/census/stats_plugin_end2end_test.cc

@ -2087,6 +2087,7 @@ add_library(grpc_test_util
test/core/util/port_isolated_runtime_environment.cc
test/core/util/port_server_client.cc
test/core/util/reconnect_server.cc
test/core/util/resolve_localhost_ip46.cc
test/core/util/slice_splitter.cc
test/core/util/subprocess_posix.cc
test/core/util/subprocess_windows.cc
@ -2158,6 +2159,7 @@ add_library(grpc_test_util_unsecure
test/core/util/port_isolated_runtime_environment.cc
test/core/util/port_server_client.cc
test/core/util/reconnect_server.cc
test/core/util/resolve_localhost_ip46.cc
test/core/util/slice_splitter.cc
test/core/util/subprocess_posix.cc
test/core/util/subprocess_windows.cc
@ -15072,6 +15074,7 @@ if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
test/core/util/port_isolated_runtime_environment.cc
test/core/util/port_server_client.cc
test/core/util/reconnect_server.cc
test/core/util/resolve_localhost_ip46.cc
test/core/util/slice_splitter.cc
test/core/util/subprocess_posix.cc
test/core/util/subprocess_windows.cc

@ -1416,6 +1416,7 @@ libs:
- test/core/util/port.h
- test/core/util/port_server_client.h
- test/core/util/reconnect_server.h
- test/core/util/resolve_localhost_ip46.h
- test/core/util/slice_splitter.h
- test/core/util/subprocess.h
- test/core/util/test_config.h
@ -1438,6 +1439,7 @@ libs:
- test/core/util/port_isolated_runtime_environment.cc
- test/core/util/port_server_client.cc
- test/core/util/reconnect_server.cc
- test/core/util/resolve_localhost_ip46.cc
- test/core/util/slice_splitter.cc
- test/core/util/subprocess_posix.cc
- test/core/util/subprocess_windows.cc
@ -1472,6 +1474,7 @@ libs:
- test/core/util/port.h
- test/core/util/port_server_client.h
- test/core/util/reconnect_server.h
- test/core/util/resolve_localhost_ip46.h
- test/core/util/slice_splitter.h
- test/core/util/subprocess.h
- test/core/util/test_config.h
@ -1494,6 +1497,7 @@ libs:
- test/core/util/port_isolated_runtime_environment.cc
- test/core/util/port_server_client.cc
- test/core/util/reconnect_server.cc
- test/core/util/resolve_localhost_ip46.cc
- test/core/util/slice_splitter.cc
- test/core/util/subprocess_posix.cc
- test/core/util/subprocess_windows.cc
@ -7667,6 +7671,7 @@ targets:
- test/core/util/port.h
- test/core/util/port_server_client.h
- test/core/util/reconnect_server.h
- test/core/util/resolve_localhost_ip46.h
- test/core/util/slice_splitter.h
- test/core/util/subprocess.h
- test/core/util/test_config.h
@ -7692,6 +7697,7 @@ targets:
- test/core/util/port_isolated_runtime_environment.cc
- test/core/util/port_server_client.cc
- test/core/util/reconnect_server.cc
- test/core/util/resolve_localhost_ip46.cc
- test/core/util/slice_splitter.cc
- test/core/util/subprocess_posix.cc
- test/core/util/subprocess_windows.cc

@ -2021,6 +2021,8 @@ Pod::Spec.new do |s|
'test/core/util/port_server_client.h',
'test/core/util/reconnect_server.cc',
'test/core/util/reconnect_server.h',
'test/core/util/resolve_localhost_ip46.cc',
'test/core/util/resolve_localhost_ip46.h',
'test/core/util/slice_splitter.cc',
'test/core/util/slice_splitter.h',
'test/core/util/subprocess.h',

@ -1007,6 +1007,7 @@
'test/core/util/port_isolated_runtime_environment.cc',
'test/core/util/port_server_client.cc',
'test/core/util/reconnect_server.cc',
'test/core/util/resolve_localhost_ip46.cc',
'test/core/util/slice_splitter.cc',
'test/core/util/subprocess_posix.cc',
'test/core/util/subprocess_windows.cc',
@ -1044,6 +1045,7 @@
'test/core/util/port_isolated_runtime_environment.cc',
'test/core/util/port_server_client.cc',
'test/core/util/reconnect_server.cc',
'test/core/util/resolve_localhost_ip46.cc',
'test/core/util/slice_splitter.cc',
'test/core/util/subprocess_posix.cc',
'test/core/util/subprocess_windows.cc',

@ -51,6 +51,7 @@ grpc_cc_library(
"port_isolated_runtime_environment.cc",
"port_server_client.cc",
"reconnect_server.cc",
"resolve_localhost_ip46.cc",
"slice_splitter.cc",
"subprocess_posix.cc",
"subprocess_windows.cc",
@ -73,6 +74,7 @@ grpc_cc_library(
"port.h",
"port_server_client.h",
"reconnect_server.h",
"resolve_localhost_ip46.h",
"slice_splitter.h",
"subprocess.h",
"test_config.h",
@ -94,6 +96,7 @@ grpc_cc_library(
deps = [
":grpc_debugger_macros",
"//:gpr",
"//:grpc_base_c",
"//:grpc_common",
],
)

@ -0,0 +1,58 @@
//
//
// Copyright 2020 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#include "test/core/util/resolve_localhost_ip46.h"
#include <grpc/support/log.h>
#include "src/core/lib/iomgr/port.h"
#include "src/core/lib/iomgr/resolve_address.h"
#include "src/core/lib/iomgr/sockaddr.h"
namespace grpc_core {
namespace {
bool localhost_to_ipv4 = false;
bool localhost_to_ipv6 = false;
gpr_once g_resolve_localhost_ipv46 = GPR_ONCE_INIT;
void InitResolveLocalhost() {
grpc_resolved_addresses* addresses;
grpc_error* err =
grpc_blocking_resolve_address("localhost", "https", &addresses);
GPR_ASSERT(err == GRPC_ERROR_NONE);
for (size_t i = 0; i < addresses->naddrs; i++) {
grpc_sockaddr* addr =
reinterpret_cast<grpc_sockaddr*>(addresses->addrs[i].addr);
if (addr->sa_family == GRPC_AF_INET) {
localhost_to_ipv4 = true;
} else if (addr->sa_family == GRPC_AF_INET6) {
localhost_to_ipv6 = true;
}
}
grpc_resolved_addresses_destroy(addresses);
}
} // namespace
void LocalhostResolves(bool* ipv4, bool* ipv6) {
gpr_once_init(&g_resolve_localhost_ipv46, InitResolveLocalhost);
*ipv4 = localhost_to_ipv4;
*ipv6 = localhost_to_ipv6;
}
} // namespace grpc_core

@ -0,0 +1,29 @@
//
//
// Copyright 2020 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#ifndef GRPC_TEST_CORE_UTIL_RESOLVE_LOCALHOST_IP46_H_
#define GRPC_TEST_CORE_UTIL_RESOLVE_LOCALHOST_IP46_H_
namespace grpc_core {
// Test whether localhost resolves to ipv4 and/or ipv6
void LocalhostResolves(bool* ipv4, bool* ipv6);
} // namespace grpc_core
#endif // GRPC_TEST_CORE_UTIL_RESOLVE_LOCALHOST_IP46_H_

@ -59,6 +59,7 @@
#include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "src/proto/grpc/testing/xds/orca_load_report_for_test.pb.h"
#include "test/core/util/port.h"
#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/test_config.h"
#include "test/core/util/test_lb_policies.h"
#include "test/cpp/end2end/test_service_impl.h"
@ -152,12 +153,14 @@ class MyTestServiceImpl : public TestServiceImpl {
class FakeResolverResponseGeneratorWrapper {
public:
FakeResolverResponseGeneratorWrapper()
: response_generator_(grpc_core::MakeRefCounted<
explicit FakeResolverResponseGeneratorWrapper(bool ipv6_only)
: ipv6_only_(ipv6_only),
response_generator_(grpc_core::MakeRefCounted<
grpc_core::FakeResolverResponseGenerator>()) {}
FakeResolverResponseGeneratorWrapper(
FakeResolverResponseGeneratorWrapper&& other) noexcept {
ipv6_only_ = other.ipv6_only_;
response_generator_ = std::move(other.response_generator_);
}
@ -167,13 +170,22 @@ class FakeResolverResponseGeneratorWrapper {
std::unique_ptr<grpc_core::ServerAddress::AttributeInterface> attribute =
nullptr) {
grpc_core::ExecCtx exec_ctx;
response_generator_->SetResponse(BuildFakeResults(
ports, service_config_json, attribute_key, std::move(attribute)));
// This is a dummy log line as a workaround for
// https://github.com/grpc/grpc/issues/24550. It should be removed once the
// testing infrastructure updates compiler for MacOS.
if (service_config_json != nullptr) {
gpr_log(GPR_INFO, "SetNextResolution with service_config_json: %s",
service_config_json);
}
response_generator_->SetResponse(
BuildFakeResults(ipv6_only_, ports, service_config_json, attribute_key,
std::move(attribute)));
}
void SetNextResolutionUponError(const std::vector<int>& ports) {
grpc_core::ExecCtx exec_ctx;
response_generator_->SetReresolutionResponse(BuildFakeResults(ports));
response_generator_->SetReresolutionResponse(
BuildFakeResults(ipv6_only_, ports));
}
void SetFailureOnReresolution() {
@ -187,13 +199,15 @@ class FakeResolverResponseGeneratorWrapper {
private:
static grpc_core::Resolver::Result BuildFakeResults(
const std::vector<int>& ports, const char* service_config_json = nullptr,
bool ipv6_only, const std::vector<int>& ports,
const char* service_config_json = nullptr,
const char* attribute_key = nullptr,
std::unique_ptr<grpc_core::ServerAddress::AttributeInterface> attribute =
nullptr) {
grpc_core::Resolver::Result result;
for (const int& port : ports) {
std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", port);
std::string lb_uri_str =
absl::StrCat(ipv6_only ? "ipv6:[::1]:" : "ipv4:127.0.0.1:", port);
grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);
GPR_ASSERT(lb_uri != nullptr);
grpc_resolved_address address;
@ -216,6 +230,7 @@ class FakeResolverResponseGeneratorWrapper {
return result;
}
bool ipv6_only_ = false;
grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>
response_generator_;
};
@ -238,7 +253,14 @@ class ClientLbEnd2endTest : public ::testing::Test {
#endif
}
void SetUp() override { grpc_init(); }
void SetUp() override {
grpc_init();
bool localhost_resolves_to_ipv4 = false;
bool localhost_resolves_to_ipv6 = false;
grpc_core::LocalhostResolves(&localhost_resolves_to_ipv4,
&localhost_resolves_to_ipv6);
ipv6_only_ = !localhost_resolves_to_ipv4 && localhost_resolves_to_ipv6;
}
void TearDown() override {
for (size_t i = 0; i < servers_.size(); ++i) {
@ -278,7 +300,7 @@ class ClientLbEnd2endTest : public ::testing::Test {
}
FakeResolverResponseGeneratorWrapper BuildResolverResponseGenerator() {
return FakeResolverResponseGeneratorWrapper();
return FakeResolverResponseGeneratorWrapper(ipv6_only_);
}
std::unique_ptr<grpc::testing::EchoTestService::Stub> BuildStub(
@ -468,6 +490,7 @@ class ClientLbEnd2endTest : public ::testing::Test {
std::vector<std::unique_ptr<ServerData>> servers_;
const std::string kRequestMessage_;
std::shared_ptr<ChannelCredentials> creds_;
bool ipv6_only_ = false;
};
TEST_F(ClientLbEnd2endTest, ChannelStateConnectingWhenResolving) {
@ -1969,8 +1992,9 @@ TEST_F(ClientLbAddressTest, Basic) {
// Make sure that the attributes wind up on the subchannels.
std::vector<std::string> expected;
for (const int port : GetServersPorts()) {
expected.emplace_back(absl::StrCat(
"127.0.0.1:", port, " args={} attributes={", kAttributeKey, "=foo}"));
expected.emplace_back(
absl::StrCat(ipv6_only_ ? "[::1]:" : "127.0.0.1:", port,
" args={} attributes={", kAttributeKey, "=foo}"));
}
EXPECT_EQ(addresses_seen(), expected);
}

@ -350,7 +350,7 @@ class End2endTest : public ::testing::TestWithParam<TestScenario> {
void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
int port = grpc_pick_unused_port_or_die();
first_picked_port_ = port;
server_address_ << "127.0.0.1:" << port;
server_address_ << "localhost:" << port;
// Setup server
BuildAndStartServer(processor);
}
@ -1414,7 +1414,7 @@ TEST_P(End2endTest, ChannelStateTimeout) {
}
int port = grpc_pick_unused_port_or_die();
std::ostringstream server_address;
server_address << "127.0.0.1:" << port;
server_address << "localhost:" << port;
// Channel to non-existing server
auto channel =
grpc::CreateChannel(server_address.str(), InsecureChannelCredentials());

@ -53,6 +53,7 @@
#include "src/cpp/server/secure_server_credentials.h"
#include "test/core/util/port.h"
#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/test_config.h"
#include "test/cpp/end2end/test_service_impl.h"
@ -184,6 +185,12 @@ std::string Ip4ToPackedString(const char* ip_str) {
return std::string(reinterpret_cast<const char*>(&ip4), sizeof(ip4));
}
std::string Ip6ToPackedString(const char* ip_str) {
struct in6_addr ip6;
GPR_ASSERT(inet_pton(AF_INET6, ip_str, &ip6) == 1);
return std::string(reinterpret_cast<const char*>(&ip6), sizeof(ip6));
}
struct ClientStats {
size_t num_calls_started = 0;
size_t num_calls_finished = 0;
@ -321,28 +328,6 @@ class BalancerServiceImpl : public BalancerService {
gpr_log(GPR_INFO, "LB[%p]: shut down", this);
}
static LoadBalanceResponse BuildResponseForBackends(
const std::vector<int>& backend_ports,
const std::map<std::string, size_t>& drop_token_counts) {
LoadBalanceResponse response;
for (const auto& drop_token_count : drop_token_counts) {
for (size_t i = 0; i < drop_token_count.second; ++i) {
auto* server = response.mutable_server_list()->add_servers();
server->set_drop(true);
server->set_load_balance_token(drop_token_count.first);
}
}
for (const int& backend_port : backend_ports) {
auto* server = response.mutable_server_list()->add_servers();
server->set_ip_address(Ip4ToPackedString("127.0.0.1"));
server->set_port(backend_port);
static int token_count = 0;
server->set_load_balance_token(
absl::StrFormat("token%03d", ++token_count));
}
return response;
}
ClientStats WaitForLoadReport() {
grpc::internal::MutexLock lock(&mu_);
grpc::internal::CondVar cv;
@ -418,6 +403,11 @@ class GrpclbEnd2endTest : public ::testing::Test {
static void TearDownTestCase() { grpc_shutdown(); }
void SetUp() override {
bool localhost_resolves_to_ipv4 = false;
bool localhost_resolves_to_ipv6 = false;
grpc_core::LocalhostResolves(&localhost_resolves_to_ipv4,
&localhost_resolves_to_ipv6);
ipv6_only_ = !localhost_resolves_to_ipv4 && localhost_resolves_to_ipv6;
response_generator_ =
grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
// Start the backends.
@ -546,11 +536,12 @@ class GrpclbEnd2endTest : public ::testing::Test {
std::string balancer_name;
};
static grpc_core::ServerAddressList CreateLbAddressesFromAddressDataList(
grpc_core::ServerAddressList CreateLbAddressesFromAddressDataList(
const std::vector<AddressData>& address_data) {
grpc_core::ServerAddressList addresses;
for (const auto& addr : address_data) {
std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", addr.port);
std::string lb_uri_str = absl::StrCat(
ipv6_only_ ? "ipv6:[::1]:" : "ipv4:127.0.0.1:", addr.port);
grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);
GPR_ASSERT(lb_uri != nullptr);
grpc_resolved_address address;
@ -565,7 +556,7 @@ class GrpclbEnd2endTest : public ::testing::Test {
return addresses;
}
static grpc_core::Resolver::Result MakeResolverResult(
grpc_core::Resolver::Result MakeResolverResult(
const std::vector<AddressData>& balancer_address_data,
const std::vector<AddressData>& backend_address_data = {},
const char* service_config_json = kDefaultServiceConfig) {
@ -628,6 +619,29 @@ class GrpclbEnd2endTest : public ::testing::Test {
balancers_[i]->service_.add_response(response, delay_ms);
}
LoadBalanceResponse BuildResponseForBackends(
const std::vector<int>& backend_ports,
const std::map<std::string, size_t>& drop_token_counts) {
LoadBalanceResponse response;
for (const auto& drop_token_count : drop_token_counts) {
for (size_t i = 0; i < drop_token_count.second; ++i) {
auto* server = response.mutable_server_list()->add_servers();
server->set_drop(true);
server->set_load_balance_token(drop_token_count.first);
}
}
for (const int& backend_port : backend_ports) {
auto* server = response.mutable_server_list()->add_servers();
server->set_ip_address(ipv6_only_ ? Ip6ToPackedString("::1")
: Ip4ToPackedString("127.0.0.1"));
server->set_port(backend_port);
static int token_count = 0;
server->set_load_balance_token(
absl::StrFormat("token%03d", ++token_count));
}
return response;
}
Status SendRpc(EchoResponse* response = nullptr, int timeout_ms = 1000,
bool wait_for_ready = false,
const Status& expected_status = Status::OK) {
@ -726,6 +740,7 @@ class GrpclbEnd2endTest : public ::testing::Test {
const size_t num_backends_;
const size_t num_balancers_;
const int client_load_reporting_interval_seconds_;
bool ipv6_only_ = false;
std::shared_ptr<Channel> channel_;
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
std::vector<std::unique_ptr<ServerThread<BackendServiceImpl>>> backends_;
@ -745,8 +760,7 @@ TEST_F(SingleBalancerTest, Vanilla) {
SetNextResolutionAllBalancers();
const size_t kNumRpcsPerAddress = 100;
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
// Make sure that trying to connect works without a call.
channel_->GetState(true /* try_to_connect */);
// We need to wait for all backends to come online.
@ -771,8 +785,7 @@ TEST_F(SingleBalancerTest, Vanilla) {
TEST_F(SingleBalancerTest, ReturnServerStatus) {
SetNextResolutionAllBalancers();
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
// We need to wait for all backends to come online.
WaitForAllBackends();
// Send a request that the backend will fail, and make sure we get
@ -793,8 +806,7 @@ TEST_F(SingleBalancerTest, SelectGrpclbWithMigrationServiceConfig) {
" ]\n"
"}");
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
CheckRpcSendOk(1, 1000 /* timeout_ms */, true /* wait_for_ready */);
balancers_[0]->service_.NotifyDoneWithServerlists();
// The balancer got a single request.
@ -841,8 +853,7 @@ TEST_F(SingleBalancerTest, UsePickFirstChildPolicy) {
" ]\n"
"}");
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
const size_t kNumRpcs = num_backends_ * 2;
CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */);
balancers_[0]->service_.NotifyDoneWithServerlists();
@ -872,8 +883,7 @@ TEST_F(SingleBalancerTest, SwapChildPolicy) {
" ]\n"
"}");
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
const size_t kNumRpcs = num_backends_ * 2;
CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */);
// Check that all requests went to the first backend. This verifies
@ -908,8 +918,7 @@ TEST_F(SingleBalancerTest, SameBackendListedMultipleTimes) {
ports.push_back(backends_[0]->port_);
ports.push_back(backends_[0]->port_);
const size_t kNumRpcsPerAddress = 10;
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(ports, {}), 0);
ScheduleResponseForBalancer(0, BuildResponseForBackends(ports, {}), 0);
// We need to wait for the backend to come online.
WaitForBackend(0);
// Send kNumRpcsPerAddress RPCs per server.
@ -927,8 +936,7 @@ TEST_F(SingleBalancerTest, SecureNaming) {
SetNextResolution({AddressData{balancers_[0]->port_, "lb"}});
const size_t kNumRpcsPerAddress = 100;
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
// Make sure that trying to connect works without a call.
channel_->GetState(true /* try_to_connect */);
// We need to wait for all backends to come online.
@ -970,8 +978,7 @@ TEST_F(SingleBalancerTest, InitiallyEmptyServerlist) {
ScheduleResponseForBalancer(0, LoadBalanceResponse(), 0);
// Send non-empty serverlist only after kServerlistDelayMs
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
kServerlistDelayMs);
0, BuildResponseForBackends(GetBackendPorts(), {}), kServerlistDelayMs);
const auto t0 = system_clock::now();
// Client will block: LB will initially send empty serverlist.
CheckRpcSendOk(1, kCallDeadlineMs, true /* wait_for_ready */);
@ -997,8 +1004,7 @@ TEST_F(SingleBalancerTest, AllServersUnreachableFailFast) {
for (size_t i = 0; i < kNumUnreachableServers; ++i) {
ports.push_back(grpc_pick_unused_port_or_die());
}
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(ports, {}), 0);
ScheduleResponseForBalancer(0, BuildResponseForBackends(ports, {}), 0);
const Status status = SendRpc();
// The error shouldn't be DEADLINE_EXCEEDED.
EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code());
@ -1027,7 +1033,7 @@ TEST_F(SingleBalancerTest, Fallback) {
// Send non-empty serverlist only after kServerlistDelayMs.
ScheduleResponseForBalancer(
0,
BalancerServiceImpl::BuildResponseForBackends(
BuildResponseForBackends(
GetBackendPorts(kNumBackendsInResolution /* start_index */), {}),
kServerlistDelayMs);
@ -1096,7 +1102,7 @@ TEST_F(SingleBalancerTest, FallbackUpdate) {
// Send non-empty serverlist only after kServerlistDelayMs.
ScheduleResponseForBalancer(
0,
BalancerServiceImpl::BuildResponseForBackends(
BuildResponseForBackends(
GetBackendPorts(kNumBackendsInResolution +
kNumBackendsInResolutionUpdate /* start_index */),
{}),
@ -1201,10 +1207,9 @@ TEST_F(SingleBalancerTest,
balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""});
}
SetNextResolution(balancer_addresses, backend_addresses);
ScheduleResponseForBalancer(0,
BalancerServiceImpl::BuildResponseForBackends(
GetBackendPorts(kNumFallbackBackends), {}),
0);
ScheduleResponseForBalancer(
0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}),
0);
// Try to connect.
channel_->GetState(true /* try_to_connect */);
WaitForAllBackends(1 /* num_requests_multiple_of */,
@ -1234,10 +1239,9 @@ TEST_F(SingleBalancerTest,
// Now start the balancer again. This should cause us to exit
// fallback mode.
balancers_[0]->Start(server_host_);
ScheduleResponseForBalancer(0,
BalancerServiceImpl::BuildResponseForBackends(
GetBackendPorts(kNumFallbackBackends), {}),
0);
ScheduleResponseForBalancer(
0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}),
0);
WaitForAllBackends(1 /* num_requests_multiple_of */,
kNumFallbackBackends /* start_index */);
}
@ -1256,10 +1260,9 @@ TEST_F(SingleBalancerTest,
balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""});
}
SetNextResolution(balancer_addresses, backend_addresses);
ScheduleResponseForBalancer(0,
BalancerServiceImpl::BuildResponseForBackends(
GetBackendPorts(kNumFallbackBackends), {}),
0);
ScheduleResponseForBalancer(
0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}),
0);
// Try to connect.
channel_->GetState(true /* try_to_connect */);
WaitForAllBackends(1 /* num_requests_multiple_of */,
@ -1287,10 +1290,9 @@ TEST_F(SingleBalancerTest,
// Now start the balancer again. This should cause us to exit
// fallback mode.
balancers_[0]->Start(server_host_);
ScheduleResponseForBalancer(0,
BalancerServiceImpl::BuildResponseForBackends(
GetBackendPorts(kNumFallbackBackends), {}),
0);
ScheduleResponseForBalancer(
0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}),
0);
WaitForAllBackends(1 /* num_requests_multiple_of */,
kNumFallbackBackends /* start_index */);
}
@ -1358,7 +1360,7 @@ TEST_F(SingleBalancerTest, FallbackControlledByBalancer_AfterFirstServerlist) {
// then sends the serverlist again.
// The serverlist points to backend 1.
LoadBalanceResponse serverlist_resp =
BalancerServiceImpl::BuildResponseForBackends({backends_[1]->port_}, {});
BuildResponseForBackends({backends_[1]->port_}, {});
LoadBalanceResponse fallback_resp;
fallback_resp.mutable_fallback_response();
ScheduleResponseForBalancer(0, serverlist_resp, 0);
@ -1375,8 +1377,7 @@ TEST_F(SingleBalancerTest, BackendsRestart) {
SetNextResolutionAllBalancers();
const size_t kNumRpcsPerAddress = 100;
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
// Make sure that trying to connect works without a call.
channel_->GetState(true /* try_to_connect */);
// Send kNumRpcsPerAddress RPCs per server.
@ -1406,8 +1407,7 @@ TEST_F(SingleBalancerTest, ServiceNameFromLbPolicyConfig) {
SetNextResolutionAllBalancers(kServiceConfigWithTarget);
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
// Make sure that trying to connect works without a call.
channel_->GetState(true /* try_to_connect */);
// We need to wait for all backends to come online.
@ -1424,10 +1424,10 @@ TEST_F(UpdatesTest, UpdateBalancersButKeepUsingOriginalBalancer) {
SetNextResolutionAllBalancers();
const std::vector<int> first_backend{GetBackendPorts()[0]};
const std::vector<int> second_backend{GetBackendPorts()[1]};
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0);
ScheduleResponseForBalancer(
1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0);
ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}),
0);
ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}),
0);
// Wait until the first backend is ready.
WaitForBackend(0);
@ -1482,10 +1482,10 @@ TEST_F(UpdatesTest, UpdateBalancersRepeated) {
const std::vector<int> first_backend{GetBackendPorts()[0]};
const std::vector<int> second_backend{GetBackendPorts()[0]};
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0);
ScheduleResponseForBalancer(
1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0);
ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}),
0);
ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}),
0);
// Wait until the first backend is ready.
WaitForBackend(0);
@ -1555,10 +1555,10 @@ TEST_F(UpdatesTest, UpdateBalancersDeadUpdate) {
const std::vector<int> first_backend{GetBackendPorts()[0]};
const std::vector<int> second_backend{GetBackendPorts()[1]};
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0);
ScheduleResponseForBalancer(
1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0);
ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}),
0);
ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}),
0);
// Start servers and send 10 RPCs per server.
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");
@ -1688,10 +1688,10 @@ class UpdatesWithClientLoadReportingTest : public GrpclbEnd2endTest {
TEST_F(UpdatesWithClientLoadReportingTest, ReresolveDeadBalancer) {
const std::vector<int> first_backend{GetBackendPorts()[0]};
const std::vector<int> second_backend{GetBackendPorts()[1]};
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0);
ScheduleResponseForBalancer(
1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0);
ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}),
0);
ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}),
0);
// Ask channel to connect to trigger resolver creation.
channel_->GetState(true);
@ -1767,7 +1767,7 @@ TEST_F(SingleBalancerTest, Drop) {
const int num_total_addresses = num_backends_ + num_of_drop_addresses;
ScheduleResponseForBalancer(
0,
BalancerServiceImpl::BuildResponseForBackends(
BuildResponseForBackends(
GetBackendPorts(),
{{"rate_limiting", num_of_drop_by_rate_limiting_addresses},
{"load_balancing", num_of_drop_by_load_balancing_addresses}}),
@ -1806,7 +1806,7 @@ TEST_F(SingleBalancerTest, DropAllFirst) {
const int num_of_drop_by_load_balancing_addresses = 1;
ScheduleResponseForBalancer(
0,
BalancerServiceImpl::BuildResponseForBackends(
BuildResponseForBackends(
{}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},
{"load_balancing", num_of_drop_by_load_balancing_addresses}}),
0);
@ -1818,13 +1818,12 @@ TEST_F(SingleBalancerTest, DropAllFirst) {
TEST_F(SingleBalancerTest, DropAll) {
SetNextResolutionAllBalancers();
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
const int num_of_drop_by_rate_limiting_addresses = 1;
const int num_of_drop_by_load_balancing_addresses = 1;
ScheduleResponseForBalancer(
0,
BalancerServiceImpl::BuildResponseForBackends(
BuildResponseForBackends(
{}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},
{"load_balancing", num_of_drop_by_load_balancing_addresses}}),
1000);
@ -1850,8 +1849,7 @@ TEST_F(SingleBalancerWithClientLoadReportingTest, Vanilla) {
SetNextResolutionAllBalancers();
const size_t kNumRpcsPerAddress = 100;
ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),
0);
0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
// Wait until all backends are ready.
int num_ok = 0;
int num_failure = 0;
@ -1892,8 +1890,7 @@ TEST_F(SingleBalancerWithClientLoadReportingTest, BalancerRestart) {
// Balancer returns backends starting at index 1.
ScheduleResponseForBalancer(
0,
BalancerServiceImpl::BuildResponseForBackends(
GetBackendPorts(0, kNumBackendsFirstPass), {}),
BuildResponseForBackends(GetBackendPorts(0, kNumBackendsFirstPass), {}),
0);
// Wait until all backends returned by the balancer are ready.
int num_ok = 0;
@ -1922,10 +1919,9 @@ TEST_F(SingleBalancerWithClientLoadReportingTest, BalancerRestart) {
}
// Now restart the balancer, this time pointing to all backends.
balancers_[0]->Start(server_host_);
ScheduleResponseForBalancer(0,
BalancerServiceImpl::BuildResponseForBackends(
GetBackendPorts(kNumBackendsFirstPass), {}),
0);
ScheduleResponseForBalancer(
0, BuildResponseForBackends(GetBackendPorts(kNumBackendsFirstPass), {}),
0);
// Wait for queries to start going to one of the new backends.
// This tells us that we're now using the new serverlist.
do {
@ -1955,7 +1951,7 @@ TEST_F(SingleBalancerWithClientLoadReportingTest, Drop) {
const int num_total_addresses = num_backends_ + num_of_drop_addresses;
ScheduleResponseForBalancer(
0,
BalancerServiceImpl::BuildResponseForBackends(
BuildResponseForBackends(
GetBackendPorts(),
{{"rate_limiting", num_of_drop_by_rate_limiting_addresses},
{"load_balancing", num_of_drop_by_load_balancing_addresses}}),

@ -115,7 +115,7 @@ class ServerEarlyReturnTest : public ::testing::Test {
void SetUp() override {
int port = grpc_pick_unused_port_or_die();
picked_port_ = port;
server_address_ << "127.0.0.1:" << port;
server_address_ << "localhost:" << port;
ServerBuilder builder;
builder.AddListeningPort(server_address_.str(),
InsecureServerCredentials());

@ -57,6 +57,7 @@
#include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "test/core/util/port.h"
#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/test_config.h"
#include "test/cpp/end2end/test_service_impl.h"
@ -131,6 +132,11 @@ class ServiceConfigEnd2endTest : public ::testing::Test {
grpc_init();
response_generator_ =
grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
bool localhost_resolves_to_ipv4 = false;
bool localhost_resolves_to_ipv6 = false;
grpc_core::LocalhostResolves(&localhost_resolves_to_ipv4,
&localhost_resolves_to_ipv6);
ipv6_only_ = !localhost_resolves_to_ipv4 && localhost_resolves_to_ipv6;
}
void TearDown() override {
@ -169,7 +175,8 @@ class ServiceConfigEnd2endTest : public ::testing::Test {
grpc_core::Resolver::Result BuildFakeResults(const std::vector<int>& ports) {
grpc_core::Resolver::Result result;
for (const int& port : ports) {
std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", port);
std::string lb_uri_str =
absl::StrCat(ipv6_only_ ? "ipv6:[::1]:" : "ipv4:127.0.0.1:", port);
grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);
GPR_ASSERT(lb_uri != nullptr);
grpc_resolved_address address;
@ -422,6 +429,7 @@ class ServiceConfigEnd2endTest : public ::testing::Test {
return "{\"version\": \"invalid_default\"";
}
bool ipv6_only_ = false;
const std::string server_host_;
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
std::vector<std::unique_ptr<ServerData>> servers_;

File diff suppressed because it is too large Load Diff

@ -81,7 +81,7 @@ class StatsPluginEnd2EndTest : public ::testing::Test {
server_ = builder.BuildAndStart();
ASSERT_NE(nullptr, server_);
ASSERT_NE(0, port);
server_address_ = absl::StrCat("0.0.0.0:", port);
server_address_ = absl::StrCat("localhost:", port);
server_thread_ = std::thread(&StatsPluginEnd2EndTest::RunServerLoop, this);
stub_ = EchoTestService::NewStub(::grpc::CreateChannel(

Loading…
Cancel
Save