message: admin: call listen in tests to verify behavior of SO_REUSEPORT
diff: @@ -568,17 +568,37 @@ TEST_P(ServerInstanceImplTest, BootstrapNodeWithoutAccessLog) {
  "An admin access log path is required for a listening server.");
  }
  +namespace {
  +void bindAndListenTcpSocket(const Network::Address::InstanceConstSharedPtr& address,
  + const Network::Socket::OptionsSharedPtr& options) {
  + auto socket = std::make_unique<Network::TcpListenSocket>(address, options, true);
  + // Some kernels erroneously allow `bind` without SO_REUSEPORT for addresses
  + // with some other socket already listening on it, see #7636.
  + if (::listen(socket->ioHandle().fd(), 1) != 0) {
  + // Mimic bind exception for the test simplicity.
  + throw Network::SocketBindException(fmt::format("cannot listen: {}", strerror(errno)), errno);
  + }
  +}
  +} // namespace
  +
  // Test that `socket_options` field in an Admin proto is honored.
  TEST_P(ServerInstanceImplTest, BootstrapNodeWithSocketOptions) {
  + // Start Envoy instance with admin port with SO_REUSEPORT option.
  ASSERT_NO_THROW(initialize("test/server/node_bootstrap_with_admin_socket_options.yaml"));
  const auto address = server_->admin().socket().localAddress();
  - EXPECT_THAT_THROWS_MESSAGE(std::make_unique<Network::TcpListenSocket>(address, nullptr, true),
  - EnvoyException, HasSubstr("Address already in use"));
  +
  + // First attempt to bind and listen socket should fail due to the lack of SO_REUSEPORT socket
  + // options.
  + EXPECT_THAT_THROWS_MESSAGE(bindAndListenTcpSocket(address, nullptr), EnvoyException,
  + HasSubstr(strerror(EADDRINUSE)));
  +
  + // Second attempt should succeed as kernel allows multiple sockets to listen the same address iff
  + // both of them use SO_REUSEPORT socket option.
  auto options = std::make_shared<Network::Socket::Options>();
  options->emplace_back(std::make_shared<Network::SocketOptionImpl>(
  envoy::api::v2::core::SocketOption::STATE_PREBIND,
  ENVOY_MAKE_SOCKET_OPTION_NAME(SOL_SOCKET, SO_REUSEPORT), 1));
  - EXPECT_NO_THROW(std::make_unique<Network::TcpListenSocket>(address, options, true));
  + EXPECT_NO_THROW(bindAndListenTcpSocket(address, options));
  }
  // Empty bootstrap succeeds.
  