Skip to content

Commit

Permalink
test: cleaning up test runtime (envoyproxy#8012)
Browse files Browse the repository at this point in the history
Using the new runtime utility to clean up a bunch of test gorp. Yay utils!

Risk Level: n/a (test only)
Testing: tests pass
Docs Changes: n/a
Release Notes: n/a
Signed-off-by: Alyssa Wilk <alyssar@chromium.org>
  • Loading branch information
alyssawilk authored Aug 28, 2019
1 parent e674640 commit b020b63
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 92 deletions.
1 change: 1 addition & 0 deletions test/common/http/http1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ envoy_cc_test(
"//test/mocks/thread_local:thread_local_mocks",
"//test/mocks/upstream:upstream_mocks",
"//test/test_common:logging_lib",
"//test/test_common:test_runtime_lib",
],
)

Expand Down
46 changes: 3 additions & 43 deletions test/common/http/http1/codec_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@

#include "test/mocks/buffer/mocks.h"
#include "test/mocks/http/mocks.h"
#include "test/mocks/init/mocks.h"
#include "test/mocks/local_info/mocks.h"
#include "test/mocks/network/mocks.h"
#include "test/mocks/protobuf/mocks.h"
#include "test/mocks/runtime/mocks.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/logging.h"
#include "test/test_common/printers.h"
#include "test/test_common/utility.h"
#include "test/test_common/test_runtime.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
Expand All @@ -38,17 +33,6 @@ namespace Http1 {

class Http1ServerConnectionImplTest : public testing::Test {
public:
Http1ServerConnectionImplTest() : api_(Api::createApiForTest()) {
envoy::config::bootstrap::v2::LayeredRuntime config;
config.add_layers()->mutable_admin_layer();

// Create a runtime loader, so that tests can manually manipulate runtime
// guarded features.
loader_ = std::make_unique<Runtime::ScopedLoaderSingleton>(Runtime::LoaderPtr{
new Runtime::LoaderImpl(dispatcher_, tls_, config, local_info_, init_manager_, store_,
generator_, validation_visitor_, *api_)});
}

void initialize() {
codec_ = std::make_unique<ServerConnectionImpl>(connection_, store_, callbacks_,
codec_settings_, max_request_headers_kb_);
Expand All @@ -65,15 +49,7 @@ class Http1ServerConnectionImplTest : public testing::Test {

protected:
uint32_t max_request_headers_kb_{Http::DEFAULT_MAX_REQUEST_HEADERS_KB};
Event::MockDispatcher dispatcher_;
NiceMock<ThreadLocal::MockInstance> tls_;
Stats::IsolatedStoreImpl store_;
Runtime::MockRandomGenerator generator_;
Api::ApiPtr api_;
NiceMock<LocalInfo::MockLocalInfo> local_info_;
Init::MockManager init_manager_;
NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor_;
std::unique_ptr<Runtime::ScopedLoaderSingleton> loader_;
};

void Http1ServerConnectionImplTest::expect400(Protocol p, bool allow_absolute_url,
Expand Down Expand Up @@ -366,6 +342,7 @@ TEST_F(Http1ServerConnectionImplTest, HostHeaderTranslation) {
// Ensures that requests with invalid HTTP header values are not rejected
// when the runtime guard is not enabled for the feature.
TEST_F(Http1ServerConnectionImplTest, HeaderInvalidCharsRuntimeGuard) {
TestScopedRuntime scoped_runtime;
// When the runtime-guarded feature is NOT enabled, invalid header values
// should be accepted by the codec.
Runtime::LoaderSingleton::getExisting()->mergeValues(
Expand All @@ -384,6 +361,7 @@ TEST_F(Http1ServerConnectionImplTest, HeaderInvalidCharsRuntimeGuard) {
// Ensures that requests with invalid HTTP header values are properly rejected
// when the runtime guard is enabled for the feature.
TEST_F(Http1ServerConnectionImplTest, HeaderInvalidCharsRejection) {
TestScopedRuntime scoped_runtime;
// When the runtime-guarded feature is enabled, invalid header values
// should result in a rejection.
Runtime::LoaderSingleton::getExisting()->mergeValues(
Expand Down Expand Up @@ -850,16 +828,6 @@ TEST_F(Http1ServerConnectionImplTest, WatermarkTest) {

class Http1ClientConnectionImplTest : public testing::Test {
public:
Http1ClientConnectionImplTest() : api_(Api::createApiForTest()) {
envoy::config::bootstrap::v2::LayeredRuntime config;

// Create a runtime loader, so that tests can manually manipulate runtime
// guarded features.
loader_ = std::make_unique<Runtime::ScopedLoaderSingleton>(Runtime::LoaderPtr{
new Runtime::LoaderImpl(dispatcher_, tls_, config, local_info_, init_manager_, store_,
generator_, validation_visitor_, *api_)});
}

void initialize() {
codec_ = std::make_unique<ClientConnectionImpl>(connection_, store_, callbacks_);
}
Expand All @@ -869,15 +837,7 @@ class Http1ClientConnectionImplTest : public testing::Test {
std::unique_ptr<ClientConnectionImpl> codec_;

protected:
Event::MockDispatcher dispatcher_;
NiceMock<ThreadLocal::MockInstance> tls_;
Stats::IsolatedStoreImpl store_;
Runtime::MockRandomGenerator generator_;
Api::ApiPtr api_;
NiceMock<LocalInfo::MockLocalInfo> local_info_;
Init::MockManager init_manager_;
NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor_;
std::unique_ptr<Runtime::ScopedLoaderSingleton> loader_;
};

TEST_F(Http1ClientConnectionImplTest, SimpleGet) {
Expand Down
1 change: 1 addition & 0 deletions test/common/http/http2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ envoy_cc_test(
"//test/mocks/protobuf:protobuf_mocks",
"//test/mocks/thread_local:thread_local_mocks",
"//test/mocks/upstream:upstream_mocks",
"//test/test_common:test_runtime_lib",
"//test/test_common:utility_lib",
],
)
Expand Down
30 changes: 5 additions & 25 deletions test/common/http/http2/codec_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@
#include "test/common/http/common.h"
#include "test/common/http/http2/http2_frame.h"
#include "test/mocks/http/mocks.h"
#include "test/mocks/init/mocks.h"
#include "test/mocks/local_info/mocks.h"
#include "test/mocks/network/mocks.h"
#include "test/mocks/protobuf/mocks.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/printers.h"
#include "test/test_common/test_runtime.h"
#include "test/test_common/utility.h"

#include "codec_impl_test_util.h"
Expand Down Expand Up @@ -169,17 +166,7 @@ class Http2CodecImplTest : public ::testing::TestWithParam<Http2SettingsTestPara
protected Http2CodecImplTestFixture {
public:
Http2CodecImplTest()
: Http2CodecImplTestFixture(::testing::get<0>(GetParam()), ::testing::get<1>(GetParam())),
api_(Api::createApiForTest()) {
envoy::config::bootstrap::v2::LayeredRuntime config;
config.add_layers()->mutable_admin_layer();

// Create a runtime loader, so that tests can manually manipulate runtime
// guarded features.
loader_ = std::make_unique<Runtime::ScopedLoaderSingleton>(
std::make_unique<Runtime::LoaderImpl>(dispatcher_, tls_, config, local_info_, init_manager_,
store_, generator_, validation_visitor_, *api_));
}
: Http2CodecImplTestFixture(::testing::get<0>(GetParam()), ::testing::get<1>(GetParam())) {}

protected:
void priorityFlood() {
Expand Down Expand Up @@ -242,16 +229,9 @@ class Http2CodecImplTest : public ::testing::TestWithParam<Http2SettingsTestPara
}
}

private:
Event::MockDispatcher dispatcher_;
NiceMock<ThreadLocal::MockInstance> tls_;
Stats::IsolatedStoreImpl store_;
Runtime::MockRandomGenerator generator_;
Api::ApiPtr api_;
NiceMock<LocalInfo::MockLocalInfo> local_info_;
Init::MockManager init_manager_;
NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor_;
std::unique_ptr<Runtime::ScopedLoaderSingleton> loader_;
// Make sure the test fixture has a fake runtime, for the tests which use
// Runtime::LoaderSingleton::getExisting()->mergeValues(...)
TestScopedRuntime scoped_runtime_;
};

TEST_P(Http2CodecImplTest, ShutdownNotice) {
Expand Down
1 change: 1 addition & 0 deletions test/extensions/filters/http/buffer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ envoy_extension_cc_test(
"//test/mocks/protobuf:protobuf_mocks",
"//test/mocks/thread_local:thread_local_mocks",
"//test/mocks/upstream:upstream_mocks",
"//test/test_common:test_runtime_lib",
],
)

Expand Down
27 changes: 4 additions & 23 deletions test/extensions/filters/http/buffer/buffer_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@

#include "test/mocks/buffer/mocks.h"
#include "test/mocks/http/mocks.h"
#include "test/mocks/init/mocks.h"
#include "test/mocks/local_info/mocks.h"
#include "test/mocks/protobuf/mocks.h"
#include "test/mocks/runtime/mocks.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/printers.h"
#include "test/test_common/test_runtime.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
Expand All @@ -42,16 +38,8 @@ class BufferFilterTest : public testing::Test {
return std::make_shared<BufferFilterConfig>(proto_config);
}

BufferFilterTest() : config_(setupConfig()), filter_(config_), api_(Api::createApiForTest()) {
BufferFilterTest() : config_(setupConfig()), filter_(config_) {
filter_.setDecoderFilterCallbacks(callbacks_);

// Create a runtime loader, so that tests can manually manipulate runtime
// guarded features.
envoy::config::bootstrap::v2::LayeredRuntime config;
config.add_layers()->mutable_admin_layer();
loader_ = std::make_unique<Runtime::ScopedLoaderSingleton>(Runtime::LoaderPtr{
new Runtime::LoaderImpl(dispatcher_, tls_, config, local_info_, init_manager_, store_,
generator_, validation_visitor_, *api_)});
}

void routeLocalConfig(const Router::RouteSpecificFilterConfig* route_settings,
Expand All @@ -66,15 +54,8 @@ class BufferFilterTest : public testing::Test {
NiceMock<Http::MockStreamDecoderFilterCallbacks> callbacks_;
BufferFilterConfigSharedPtr config_;
BufferFilter filter_;
Event::MockDispatcher dispatcher_;
NiceMock<ThreadLocal::MockInstance> tls_;
Stats::IsolatedStoreImpl store_;
Runtime::MockRandomGenerator generator_;
Api::ApiPtr api_;
NiceMock<LocalInfo::MockLocalInfo> local_info_;
Init::MockManager init_manager_;
NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor_;
std::unique_ptr<Runtime::ScopedLoaderSingleton> loader_;
// Create a runtime loader, so that tests can manually manipulate runtime guarded features.
TestScopedRuntime scoped_runtime;
};

TEST_F(BufferFilterTest, HeaderOnlyRequest) {
Expand Down
1 change: 0 additions & 1 deletion test/test_common/test_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

namespace Envoy {

// TODO(alyssawilk) move existing runtime tests over to using this.
class TestScopedRuntime {
public:
TestScopedRuntime() : api_(Api::createApiForTest()) {
Expand Down

0 comments on commit b020b63

Please sign in to comment.