Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 13 additions & 6 deletions source/common/config/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ template <typename factoryClass> class TypedMetadataImpl : public TypedMetadata
static_assert(std::is_base_of<Config::TypedMetadataFactory, factoryClass>::value,
"Factory type must be inherited from Envoy::Config::TypedMetadataFactory.");
TypedMetadataImpl(const envoy::api::v2::core::Metadata& metadata) : data_() {
populateFrom(metadata);
}

const TypedMetadata::Object* getData(const std::string& key) const override {
const auto& it = data_.find(key);
return it == data_.end() ? nullptr : it->second.get();
}

protected:
/* Attempt to run each of the registered factories for TypedMetadata, to
* populate the data_ map.
*/
void populateFrom(const envoy::api::v2::core::Metadata& metadata) {
auto& data_by_key = metadata.filter_metadata();
for (const auto& it : Registry::FactoryRegistry<factoryClass>::factories()) {
const auto& meta_iter = data_by_key.find(it.first);
Expand All @@ -64,12 +77,6 @@ template <typename factoryClass> class TypedMetadataImpl : public TypedMetadata
}
}

const TypedMetadata::Object* getData(const std::string& key) const override {
const auto& it = data_.find(key);
return it == data_.end() ? nullptr : it->second.get();
}

private:
std::unordered_map<std::string, std::unique_ptr<const TypedMetadata::Object>> data_;
};

Expand Down
1 change: 1 addition & 0 deletions test/mocks/upstream/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ envoy_cc_mock(
deps = [
"//include/envoy/upstream:cluster_manager_interface",
"//include/envoy/upstream:upstream_interface",
"//source/common/config:metadata_lib",
"//source/common/network:raw_buffer_socket_lib",
"//source/common/upstream:upstream_includes",
"//source/common/upstream:upstream_lib",
Expand Down
13 changes: 13 additions & 0 deletions test/mocks/upstream/cluster_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <limits>

#include "envoy/upstream/upstream.h"

#include "common/config/metadata.h"
#include "common/network/raw_buffer_socket.h"
#include "common/upstream/upstream_impl.h"

Expand Down Expand Up @@ -62,6 +65,16 @@ MockClusterInfo::MockClusterInfo()
ON_CALL(*this, lbOriginalDstConfig()).WillByDefault(ReturnRef(lb_original_dst_config_));
ON_CALL(*this, lbConfig()).WillByDefault(ReturnRef(lb_config_));
ON_CALL(*this, clusterSocketOptions()).WillByDefault(ReturnRef(cluster_socket_options_));
ON_CALL(*this, metadata()).WillByDefault(ReturnRef(metadata_));
// Delayed construction of typed_metadata_, to allow for injection of metadata
ON_CALL(*this, typedMetadata())
.WillByDefault(Invoke([this]() -> const Envoy::Config::TypedMetadata& {
if (typed_metadata_ == nullptr) {
typed_metadata_ =
absl::make_unique<Config::TypedMetadataImpl<ClusterTypedMetadataFactory>>(metadata_);
}
return *typed_metadata_;
}));
}

MockClusterInfo::~MockClusterInfo() {}
Expand Down
18 changes: 18 additions & 0 deletions test/mocks/upstream/cluster_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ class MockLoadBalancerSubsetInfo : public LoadBalancerSubsetInfo {
std::vector<std::set<std::string>> subset_keys_;
};

// While this mock class doesn't have any direct use in public Envoy tests, it's
// useful for constructing tests of downstream private filters that use
// ClusterTypedMetadata.
class MockClusterTypedMetadata : public Config::TypedMetadataImpl<ClusterTypedMetadataFactory> {
public:
using Config::TypedMetadataImpl<ClusterTypedMetadataFactory>::TypedMetadataImpl;

void inject(const std::string& key, std::unique_ptr<const TypedMetadata::Object> value) {
data_[key] = std::move(value);
}

std::unordered_map<std::string, std::unique_ptr<const TypedMetadata::Object>>& data() {
return data_;
}
};

class MockClusterInfo : public ClusterInfo {
public:
MockClusterInfo();
Expand Down Expand Up @@ -108,6 +124,8 @@ class MockClusterInfo : public ClusterInfo {
absl::optional<envoy::api::v2::Cluster::OriginalDstLbConfig> lb_original_dst_config_;
Network::ConnectionSocket::OptionsSharedPtr cluster_socket_options_;
envoy::api::v2::Cluster::CommonLbConfig lb_config_;
envoy::api::v2::core::Metadata metadata_;
std::unique_ptr<Envoy::Config::TypedMetadata> typed_metadata_;
};

class MockIdleTimeEnabledClusterInfo : public MockClusterInfo {
Expand Down