Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/mocks/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ licenses(["notice"]) # Apache 2

envoy_package()

envoy_cc_mock(
name = "config_tracker_mocks",
srcs = ["config_tracker.cc"],
hdrs = ["config_tracker.h"],
deps = [
"//include/envoy/server:configuration_interface",
],
)

envoy_cc_mock(
name = "admin_mocks",
srcs = ["admin.cc"],
hdrs = ["admin.h"],
deps = [
"//include/envoy/server:admin_interface",
"//test/mocks/server:config_tracker_mocks",
],
)

envoy_cc_mock(
name = "server_mocks",
srcs = ["mocks.cc"],
Expand Down Expand Up @@ -44,6 +63,8 @@ envoy_cc_mock(
"//test/mocks/router:router_mocks",
"//test/mocks/runtime:runtime_mocks",
"//test/mocks/secret:secret_mocks",
"//test/mocks/server:admin_mocks",
"//test/mocks/server:config_tracker_mocks",
"//test/mocks/thread_local:thread_local_mocks",
"//test/mocks/tracing:tracing_mocks",
"//test/mocks/upstream:upstream_mocks",
Expand Down
18 changes: 18 additions & 0 deletions test/mocks/server/admin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "admin.h"

#include <string>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace Envoy {
namespace Server {
MockAdmin::MockAdmin() {
ON_CALL(*this, getConfigTracker()).WillByDefault(testing::ReturnRef(config_tracker_));
}

MockAdmin::~MockAdmin() = default;

} // namespace Server

} // namespace Envoy
41 changes: 41 additions & 0 deletions test/mocks/server/admin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <string>

#include "envoy/server/admin.h"

#include "absl/strings/string_view.h"
#include "config_tracker.h"
#include "gmock/gmock.h"

using testing::NiceMock;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using-declarations should only be used in .cc files. See https://google.github.io/styleguide/cppguide.html#Namespaces

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


namespace Envoy {
namespace Server {
class MockAdmin : public Admin {
public:
MockAdmin();
~MockAdmin() override;

// Server::Admin
MOCK_METHOD(bool, addHandler,
(const std::string& prefix, const std::string& help_text, HandlerCb callback,
bool removable, bool mutates_server_state));
MOCK_METHOD(bool, removeHandler, (const std::string& prefix));
MOCK_METHOD(Network::Socket&, socket, ());
MOCK_METHOD(ConfigTracker&, getConfigTracker, ());
MOCK_METHOD(void, startHttpListener,
(const std::string& access_log_path, const std::string& address_out_path,
Network::Address::InstanceConstSharedPtr address,
const Network::Socket::OptionsSharedPtr& socket_options,
Stats::ScopePtr&& listener_scope));
MOCK_METHOD(Http::Code, request,
(absl::string_view path_and_query, absl::string_view method,
Http::ResponseHeaderMap& response_headers, std::string& body));
MOCK_METHOD(void, addListenerToHandler, (Network::ConnectionHandler * handler));

NiceMock<MockConfigTracker> config_tracker_;
};
} // namespace Server

} // namespace Envoy
26 changes: 26 additions & 0 deletions test/mocks/server/config_tracker.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "config_tracker.h"

#include <string>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

using testing::_;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be moved to inside the innermost namespace, and they should be fully-qualified. See https://abseil.io/tips/119

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

using testing::Invoke;

namespace Envoy {
namespace Server {
MockConfigTracker::MockConfigTracker() {
ON_CALL(*this, add_(_, _))
.WillByDefault(Invoke([this](const std::string& key, Cb callback) -> EntryOwner* {
EXPECT_TRUE(config_tracker_callbacks_.find(key) == config_tracker_callbacks_.end());
config_tracker_callbacks_[key] = callback;
return new MockEntryOwner();
}));
}

MockConfigTracker::~MockConfigTracker() = default;

} // namespace Server

} // namespace Envoy
30 changes: 30 additions & 0 deletions test/mocks/server/config_tracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <string>

#include "envoy/server/config_tracker.h"

#include "gmock/gmock.h"

namespace Envoy {
namespace Server {
class MockConfigTracker : public ConfigTracker {
public:
MockConfigTracker();
~MockConfigTracker() override;

struct MockEntryOwner : public EntryOwner {};

MOCK_METHOD(EntryOwner*, add_, (std::string, Cb));

// Server::ConfigTracker
MOCK_METHOD(const CbsMap&, getCallbacksMap, (), (const));
EntryOwnerPtr add(const std::string& key, Cb callback) override {
return EntryOwnerPtr{add_(key, std::move(callback))};
}

std::unordered_map<std::string, Cb> config_tracker_callbacks_;
};
} // namespace Server

} // namespace Envoy
15 changes: 0 additions & 15 deletions test/mocks/server/mocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,6 @@ MockOptions::MockOptions(const std::string& config_path) : config_path_(config_p
}
MockOptions::~MockOptions() = default;

MockConfigTracker::MockConfigTracker() {
ON_CALL(*this, add_(_, _))
.WillByDefault(Invoke([this](const std::string& key, Cb callback) -> EntryOwner* {
EXPECT_TRUE(config_tracker_callbacks_.find(key) == config_tracker_callbacks_.end());
config_tracker_callbacks_[key] = callback;
return new MockEntryOwner();
}));
}
MockConfigTracker::~MockConfigTracker() = default;

MockAdmin::MockAdmin() {
ON_CALL(*this, getConfigTracker()).WillByDefault(testing::ReturnRef(config_tracker_));
}
MockAdmin::~MockAdmin() = default;

MockAdminStream::MockAdminStream() = default;
MockAdminStream::~MockAdminStream() = default;

Expand Down
45 changes: 2 additions & 43 deletions test/mocks/server/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#include "test/test_common/test_time_system.h"

#include "absl/strings/string_view.h"
#include "admin.h"
#include "config_tracker.h"
#include "gmock/gmock.h"
#include "spdlog/spdlog.h"

Expand Down Expand Up @@ -128,49 +130,6 @@ class MockOptions : public Options {
std::vector<std::string> disabled_extensions_;
};

class MockConfigTracker : public ConfigTracker {
public:
MockConfigTracker();
~MockConfigTracker() override;

struct MockEntryOwner : public EntryOwner {};

MOCK_METHOD(EntryOwner*, add_, (std::string, Cb));

// Server::ConfigTracker
MOCK_METHOD(const CbsMap&, getCallbacksMap, (), (const));
EntryOwnerPtr add(const std::string& key, Cb callback) override {
return EntryOwnerPtr{add_(key, std::move(callback))};
}

std::unordered_map<std::string, Cb> config_tracker_callbacks_;
};

class MockAdmin : public Admin {
public:
MockAdmin();
~MockAdmin() override;

// Server::Admin
MOCK_METHOD(bool, addHandler,
(const std::string& prefix, const std::string& help_text, HandlerCb callback,
bool removable, bool mutates_server_state));
MOCK_METHOD(bool, removeHandler, (const std::string& prefix));
MOCK_METHOD(Network::Socket&, socket, ());
MOCK_METHOD(ConfigTracker&, getConfigTracker, ());
MOCK_METHOD(void, startHttpListener,
(const std::string& access_log_path, const std::string& address_out_path,
Network::Address::InstanceConstSharedPtr address,
const Network::Socket::OptionsSharedPtr& socket_options,
Stats::ScopePtr&& listener_scope));
MOCK_METHOD(Http::Code, request,
(absl::string_view path_and_query, absl::string_view method,
Http::ResponseHeaderMap& response_headers, std::string& body));
MOCK_METHOD(void, addListenerToHandler, (Network::ConnectionHandler * handler));

NiceMock<MockConfigTracker> config_tracker_;
};

class MockAdminStream : public AdminStream {
public:
MockAdminStream();
Expand Down
3 changes: 2 additions & 1 deletion test/server/config_validation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ envoy_cc_test(
"//include/envoy/upstream:resource_manager_interface",
"//include/envoy/upstream:upstream_interface",
"//source/common/api:api_lib",
"//source/common/singleton:manager_impl_lib",
"//source/common/stats:stats_lib",
"//source/extensions/transport_sockets/tls:context_lib",
"//source/server/config_validation:cluster_manager_lib",
Expand All @@ -39,7 +40,7 @@ envoy_cc_test(
"//test/mocks/protobuf:protobuf_mocks",
"//test/mocks/runtime:runtime_mocks",
"//test/mocks/secret:secret_mocks",
"//test/mocks/server:server_mocks",
"//test/mocks/server:admin_mocks",
"//test/mocks/thread_local:thread_local_mocks",
"//test/mocks/upstream:upstream_mocks",
"//test/test_common:simulated_time_system_lib",
Expand Down
3 changes: 2 additions & 1 deletion test/server/config_validation/cluster_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "envoy/upstream/upstream.h"

#include "common/api/api_impl.h"
#include "common/grpc/context_impl.h"
#include "common/http/context_impl.h"
#include "common/singleton/manager_impl.h"

Expand All @@ -18,7 +19,7 @@
#include "test/mocks/protobuf/mocks.h"
#include "test/mocks/runtime/mocks.h"
#include "test/mocks/secret/mocks.h"
#include "test/mocks/server/mocks.h"
#include "test/mocks/server/admin.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/mocks/upstream/mocks.h"
#include "test/test_common/simulated_time_system.h"
Expand Down