Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Co-authored-by: DongRyeol Cha <[email protected]>
Signed-off-by: Kornel David <[email protected]>
  • Loading branch information
davidkornel and DongRyeol Cha committed Apr 14, 2021
1 parent 78f4b2c commit 0e9abcc
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/extensions/filters/udp/udp_proxy/hash_policy_impl_test.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <string>

#include "envoy/extensions/filters/udp/udp_proxy/v3/udp_proxy.pb.h"

#include "common/common/hash.h"
Expand Down Expand Up @@ -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.*");
Expand All @@ -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
Expand Down
55 changes: 55 additions & 0 deletions test/extensions/filters/udp/udp_proxy/udp_proxy_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e9abcc

Please sign in to comment.