Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Qin Qin <[email protected]>
  • Loading branch information
qqustc committed Nov 24, 2020
1 parent 29ea211 commit 1eae330
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 25 deletions.
1 change: 1 addition & 0 deletions source/extensions/filters/http/kill_request/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions source/extensions/filters/http/kill_request/kill_request_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <csignal>

#include "common/protobuf/utility.h"

#include "extensions/filters/common/fault/fault_config.h"

namespace Envoy {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Server::Configuration::NamedHttpFilterConfigFactory>::getFactory(
deprecated_name));
}

} // namespace
} // namespace KillRequest
} // namespace HttpFilters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit 1eae330

Please sign in to comment.