Repository: envoyproxy/envoy
File: test/extensions/clusters/common/logical_host_test.cc (C++)

Code before:
  }

  std::shared_ptr<NiceMock<Upstream::MockClusterInfo>> cluster_info_;
  Upstream::MockTransportSocketMatcher* transport_socket_matcher_;
};

// Test that per-connection resolution is triggered when all conditions are met.
TEST_F(LogicalHostTransportSocketResolutionTest, PerConnectionResolutionWhenAllConditionsMet) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(true));
  auto options =
      createTransportSocketOptionsWithFilterState("envoy.network.namespace", "/run/netns/ns1");

  EXPECT_TRUE(needsPerConnectionResolution(options));
}

// Test that per-connection resolution is not triggered when usesFilterState returns false.
TEST_F(LogicalHostTransportSocketResolutionTest,
       NoPerConnectionResolutionWhenUsesFilterStateFalse) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(false));
  auto options =
      createTransportSocketOptionsWithFilterState("envoy.network.namespace", "/run/netns/ns1");

  EXPECT_FALSE(needsPerConnectionResolution(options));
}

// Test that per-connection resolution is not triggered when transport socket options is null.
TEST_F(LogicalHostTransportSocketResolutionTest, NoPerConnectionResolutionWhenOptionsNull) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(true));
  Network::TransportSocketOptionsConstSharedPtr options = nullptr;

  EXPECT_FALSE(needsPerConnectionResolution(options));
}

// Test that per-connection resolution is not triggered when filter state objects are empty.
TEST_F(LogicalHostTransportSocketResolutionTest,
       NoPerConnectionResolutionWhenFilterStateObjectsEmpty) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(true));
  auto options = std::make_shared<Network::TransportSocketOptionsImpl>(
      "", std::vector<std::string>{}, std::vector<std::string>{}, std::vector<std::string>{});

  EXPECT_FALSE(needsPerConnectionResolution(options));
}

// Test that override transport socket options takes precedence over passed options.
TEST_F(LogicalHostTransportSocketResolutionTest, OverrideTransportSocketOptionsTakesPrecedence) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(true));

  // Create override options with filter state.
  auto override_options =
      createTransportSocketOptionsWithFilterState("envoy.network.namespace", "/run/netns/ns1");


Review comment (suggestion):
```suggestion
// Test that per-connection resolution is not triggered when transport socket options are null.
```

Code after:
  }

  std::shared_ptr<NiceMock<Upstream::MockClusterInfo>> cluster_info_;
  Upstream::MockTransportSocketMatcher* transport_socket_matcher_;
};

// Test that per-connection resolution is triggered when all conditions are met.
TEST_F(LogicalHostTransportSocketResolutionTest, PerConnectionResolutionWhenAllConditionsMet) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(true));
  auto options =
      createTransportSocketOptionsWithFilterState("envoy.network.namespace", "/run/netns/ns1");

  EXPECT_TRUE(needsPerConnectionResolution(options));
}

// Test that per-connection resolution is not triggered when usesFilterState returns false.
TEST_F(LogicalHostTransportSocketResolutionTest,
       NoPerConnectionResolutionWhenUsesFilterStateFalse) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(false));
  auto options =
      createTransportSocketOptionsWithFilterState("envoy.network.namespace", "/run/netns/ns1");

  EXPECT_FALSE(needsPerConnectionResolution(options));
}

// Test that per-connection resolution is not triggered when transport socket options are null.
TEST_F(LogicalHostTransportSocketResolutionTest, NoPerConnectionResolutionWhenOptionsNull) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(true));
  Network::TransportSocketOptionsConstSharedPtr options = nullptr;

  EXPECT_FALSE(needsPerConnectionResolution(options));
}

// Test that per-connection resolution is not triggered when filter state objects are empty.
TEST_F(LogicalHostTransportSocketResolutionTest,
       NoPerConnectionResolutionWhenFilterStateObjectsEmpty) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(true));
  auto options = std::make_shared<Network::TransportSocketOptionsImpl>(
      "", std::vector<std::string>{}, std::vector<std::string>{}, std::vector<std::string>{});

  EXPECT_FALSE(needsPerConnectionResolution(options));
}

// Test that override transport socket options takes precedence over passed options.
TEST_F(LogicalHostTransportSocketResolutionTest, OverrideTransportSocketOptionsTakesPrecedence) {
  ON_CALL(*transport_socket_matcher_, usesFilterState()).WillByDefault(Return(true));

  // Create override options with filter state.
  auto override_options =
      createTransportSocketOptionsWithFilterState("envoy.network.namespace", "/run/netns/ns1");