From 0e9abcc25ec78da0844d69650ff6234b00c4cd0c Mon Sep 17 00:00:00 2001 From: davidkornel Date: Wed, 14 Apr 2021 13:22:40 +0200 Subject: [PATCH] Add tests Co-authored-by: DongRyeol Cha Signed-off-by: Kornel David --- .../udp/udp_proxy/hash_policy_impl_test.cc | 32 +++++++++++ .../udp/udp_proxy/udp_proxy_filter_test.cc | 55 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/test/extensions/filters/udp/udp_proxy/hash_policy_impl_test.cc b/test/extensions/filters/udp/udp_proxy/hash_policy_impl_test.cc index 98fbbae74078..a3a0c31646d3 100644 --- a/test/extensions/filters/udp/udp_proxy/hash_policy_impl_test.cc +++ b/test/extensions/filters/udp/udp_proxy/hash_policy_impl_test.cc @@ -1,3 +1,5 @@ +#include + #include "envoy/extensions/filters/udp/udp_proxy/v3/udp_proxy.pb.h" #include "common/common/hash.h" @@ -50,6 +52,16 @@ class HashPolicyImplSourceIpTest : public HashPolicyImplBaseTest { const Network::Address::InstanceConstSharedPtr pipe_address_; }; +class HashPolicyImplKeyTest : public HashPolicyImplBaseTest { +public: + HashPolicyImplKeyTest() : Key("key"), EmptyKey("") {} + + void additionalSetup() override { hash_policy_config_->set_key(Key); } + + const std::string Key; + const std::string EmptyKey; +}; + // Check invalid policy type TEST_F(HashPolicyImplBaseTest, NotSupportedPolicy) { EXPECT_DEATH(setup(), ".*panic: not reached.*"); @@ -74,6 +86,26 @@ TEST_F(HashPolicyImplSourceIpTest, SourceIpWithUnixDomainSocketType) { EXPECT_FALSE(hash.has_value()); } +// Check if generate correct hash +TEST_F(HashPolicyImplKeyTest, KeyHash) { + setup(); + + auto generated_hash = HashUtil::xxHash64(Key); + auto hash = hash_policy_->generateHash(*peer_address_); + + EXPECT_EQ(generated_hash, hash.value()); +} + +// Check that returns null hash in case of hash key has been changed to an empty string +TEST_F(HashPolicyImplKeyTest, KeyWithEmptyString) { + setup(); + + hash_policy_config_->set_key(EmptyKey); + auto hash = hash_policy_->generateHash(*peer_address_); + + EXPECT_FALSE(hash.has_value()); +} + } // namespace } // namespace UdpProxy } // namespace UdpFilters diff --git a/test/extensions/filters/udp/udp_proxy/udp_proxy_filter_test.cc b/test/extensions/filters/udp/udp_proxy/udp_proxy_filter_test.cc index 618b07a82967..ddcb11b89157 100644 --- a/test/extensions/filters/udp/udp_proxy/udp_proxy_filter_test.cc +++ b/test/extensions/filters/udp/udp_proxy/udp_proxy_filter_test.cc @@ -796,6 +796,61 @@ cluster: fake_cluster test_sessions_[0].recvDataFromUpstream("world"); } +// Make sure hash policy with key is created. +TEST_F(UdpProxyFilterTest, HashPolicyWithKey) { + InSequence s; + + setup(R"EOF( +stat_prefix: foo +cluster: fake_cluster +hash_policies: +- key: "key" + )EOF"); + + EXPECT_NE(nullptr, config_->hashPolicy()); +} + +// Make sure validation fails if key is an empty string. +TEST_F(UdpProxyFilterTest, ValidateHashPolicyWithKey) { + InSequence s; + auto config = R"EOF( +stat_prefix: foo +cluster: fake_cluster +hash_policies: +- key: "" + )EOF"; + + EXPECT_THROW_WITH_REGEX(setup(config), EnvoyException, + "caused by HashPolicyValidationError\\.Key"); +} + +// Expect correct hash is created if hash_policy with key is mentioned. +TEST_F(UdpProxyFilterTest, HashWithKey) { + InSequence s; + + setup(R"EOF( +stat_prefix: foo +cluster: fake_cluster +hash_policies: +- key: "key" + )EOF"); + + auto host = createHost(upstream_address_); + auto generated_hash = HashUtil::xxHash64("key"); + EXPECT_CALL(cluster_manager_.thread_local_cluster_.lb_, chooseHost(_)) + .WillOnce(Invoke([host, generated_hash]( + Upstream::LoadBalancerContext* context) -> Upstream::HostConstSharedPtr { + auto hash = context->computeHashKey(); + EXPECT_TRUE(hash.has_value()); + EXPECT_EQ(generated_hash, hash.value()); + return host; + })); + expectSessionCreate(upstream_address_); + test_sessions_[0].expectWriteToUpstream("hello"); + recvDataFromDownstream("10.0.0.1:1000", "10.0.0.2:80", "hello"); + test_sessions_[0].recvDataFromUpstream("world"); +} + } // namespace } // namespace UdpProxy } // namespace UdpFilters