From 1eae3303e26a3156e0c408dd20a100394354f4bf Mon Sep 17 00:00:00 2001 From: Qin Qin Date: Thu, 19 Nov 2020 10:55:06 -0500 Subject: [PATCH] fix test Signed-off-by: Qin Qin --- source/extensions/filters/http/kill_request/BUILD | 1 + .../http/kill_request/kill_request_config.cc | 3 +-- .../http/kill_request/kill_request_filter.cc | 14 ++++---------- .../http/kill_request/kill_request_config_test.cc | 10 ---------- .../http/kill_request/kill_request_filter_test.cc | 6 +++--- 5 files changed, 9 insertions(+), 25 deletions(-) diff --git a/source/extensions/filters/http/kill_request/BUILD b/source/extensions/filters/http/kill_request/BUILD index 1b570a21fbf0..9dbaa63e1c1f 100644 --- a/source/extensions/filters/http/kill_request/BUILD +++ b/source/extensions/filters/http/kill_request/BUILD @@ -20,6 +20,7 @@ envoy_cc_library( "//source/common/http:header_map_lib", "//source/common/http:header_utility_lib", "//source/common/http:headers_lib", + "//source/common/protobuf:utility_lib", "//source/extensions/filters/common/fault:fault_config_lib", "@envoy_api//envoy/extensions/filters/http/kill_request/v3:pkg_cc_proto", ], diff --git a/source/extensions/filters/http/kill_request/kill_request_config.cc b/source/extensions/filters/http/kill_request/kill_request_config.cc index 6f959a64647c..59a9ec7d4426 100644 --- a/source/extensions/filters/http/kill_request/kill_request_config.cc +++ b/source/extensions/filters/http/kill_request/kill_request_config.cc @@ -23,8 +23,7 @@ Http::FilterFactoryCb KillRequestFilterFactory::createFilterFactoryFromProtoType /** * Static registration for the KillRequest filter. @see RegisterFactory. */ -REGISTER_FACTORY(KillRequestFilterFactory, - Server::Configuration::NamedHttpFilterConfigFactory){"envoy.kill_request"}; +REGISTER_FACTORY(KillRequestFilterFactory, Server::Configuration::NamedHttpFilterConfigFactory); } // namespace KillRequest } // namespace HttpFilters diff --git a/source/extensions/filters/http/kill_request/kill_request_filter.cc b/source/extensions/filters/http/kill_request/kill_request_filter.cc index 69683e9c32ac..5036a7024493 100644 --- a/source/extensions/filters/http/kill_request/kill_request_filter.cc +++ b/source/extensions/filters/http/kill_request/kill_request_filter.cc @@ -2,6 +2,8 @@ #include +#include "common/protobuf/utility.h" + #include "extensions/filters/common/fault/fault_config.h" namespace Envoy { @@ -10,16 +12,8 @@ namespace HttpFilters { namespace KillRequest { bool KillRequestFilter::isKillRequestEnabled() { - uint32_t denominator = 100; - if (kill_request_.probability().denominator() == - envoy::type::v3::FractionalPercent::TEN_THOUSAND) { - denominator = 10000; - } else if (kill_request_.probability().denominator() == - envoy::type::v3::FractionalPercent::MILLION) { - denominator = 1000000; - } - - return (random_generator_.random() % denominator) < kill_request_.probability().numerator(); + return ProtobufPercentHelper::evaluateFractionalPercent(kill_request_.probability(), + random_generator_.random()); } Http::FilterHeadersStatus KillRequestFilter::decodeHeaders(Http::RequestHeaderMap& headers, bool) { diff --git a/test/extensions/filters/http/kill_request/kill_request_config_test.cc b/test/extensions/filters/http/kill_request/kill_request_config_test.cc index 7605c514aa87..a803ae45a347 100644 --- a/test/extensions/filters/http/kill_request/kill_request_config_test.cc +++ b/test/extensions/filters/http/kill_request/kill_request_config_test.cc @@ -39,16 +39,6 @@ TEST(KillRequestConfigTest, KillRequestFilterWithEmptyProto) { cb(filter_callback); } -// Test that the deprecated extension name still functions. -TEST(KillRequestConfigTest, DEPRECATED_FEATURE_TEST(DeprecatedExtensionFilterName)) { - const std::string deprecated_name = "envoy.kill_request"; - - ASSERT_NE( - nullptr, - Registry::FactoryRegistry::getFactory( - deprecated_name)); -} - } // namespace } // namespace KillRequest } // namespace HttpFilters diff --git a/test/extensions/filters/http/kill_request/kill_request_filter_test.cc b/test/extensions/filters/http/kill_request/kill_request_filter_test.cc index ef857da9c5bf..9301c27ee0b6 100644 --- a/test/extensions/filters/http/kill_request/kill_request_filter_test.cc +++ b/test/extensions/filters/http/kill_request/kill_request_filter_test.cc @@ -47,17 +47,17 @@ TEST_F(KillRequestFilterTest, KillRequestWithMillionDenominatorCrashEnvoy) { SetUpTest(kill_request); request_headers_.addCopy("x-envoy-kill-request", "yes"); - ON_CALL(random_generator_, random()).WillByDefault(Return(1000000)); + ON_CALL(random_generator_, random()).WillByDefault(Return(0)); EXPECT_DEATH(filter_->decodeHeaders(request_headers_, false), ""); } TEST_F(KillRequestFilterTest, KillRequestDisabledWhenIsKillRequestEnabledReturnsFalse) { envoy::extensions::filters::http::kill_request::v3::KillRequest kill_request; - kill_request.mutable_probability()->set_numerator(100); + kill_request.mutable_probability()->set_numerator(0); SetUpTest(kill_request); request_headers_.addCopy("x-envoy-kill-request", "true"); - ON_CALL(random_generator_, random()).WillByDefault(Return(99)); + ON_CALL(random_generator_, random()).WillByDefault(Return(1)); EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers_, false)); }