forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_test.cc
127 lines (98 loc) · 4.39 KB
/
registry_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <algorithm>
#include "envoy/config/typed_config.h"
#include "envoy/registry/registry.h"
#include "source/common/common/fmt.h"
#include "test/test_common/logging.h"
#include "test/test_common/utility.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace Envoy {
namespace Config {
namespace {
using ::testing::Optional;
class InternalFactory : public Config::UntypedFactory {
public:
~InternalFactory() override = default;
std::string category() const override { return ""; }
};
class TestInternalFactory : public InternalFactory {
public:
std::string name() const override { return "testing.internal.test"; }
};
static Registry::RegisterInternalFactory<TestInternalFactory, InternalFactory>
test_internal_register;
// Ensure that the internal test factory name isn't visible from a
// published category. Note that the internal factory can't be in
// a registered category by definition since it doesn't have a static
// category method.
TEST(RegistryTest, InternalFactoryNotPublished) {
TestInternalFactory test;
// Expect that the published categories don't lead to the internal factory.
for (const auto& ext : Envoy::Registry::FactoryCategoryRegistry::registeredFactories()) {
for (const auto& name : ext.second->registeredNames()) {
EXPECT_NE(name, test.name());
}
}
// Expect that the factory is present.
EXPECT_NE(Registry::FactoryRegistry<InternalFactory>::getFactory("testing.internal.test"),
nullptr);
}
class PublishedFactory : public Config::UntypedFactory {
public:
~PublishedFactory() override = default;
std::string category() const override { return "testing.published"; }
};
class TestPublishedFactory : public PublishedFactory {
public:
std::string name() const override { return "testing.published.test"; }
};
REGISTER_FACTORY(TestPublishedFactory, PublishedFactory);
TEST(RegistryTest, DefaultFactoryPublished) {
const auto& factories = Envoy::Registry::FactoryCategoryRegistry::registeredFactories();
// Expect that the category is present.
ASSERT_NE(factories.find("testing.published"), factories.end());
// Expect that the factory is listed in the right category.
const auto& names = factories.find("testing.published")->second->registeredNames();
EXPECT_NE(std::find(names.begin(), names.end(), "testing.published.test"), std::end(names));
// Expect that the factory is present.
EXPECT_NE(Registry::FactoryRegistry<PublishedFactory>::getFactory("testing.published.test"),
nullptr);
// Expect no version
auto const version =
factories.find("testing.published")->second->getFactoryVersion("testing.published.test");
EXPECT_FALSE(version.has_value());
}
class TestVersionedFactory : public PublishedFactory {
public:
std::string name() const override { return "testing.published.versioned"; }
};
REGISTER_FACTORY(TestVersionedFactory,
PublishedFactory){FACTORY_VERSION(2, 5, 39, {{"build.label", "alpha"}})};
// Test registration of versioned factory
TEST(RegistryTest, VersionedFactory) {
const auto& factories = Envoy::Registry::FactoryCategoryRegistry::registeredFactories();
// Expect that the category is present.
ASSERT_NE(factories.find("testing.published"), factories.end());
// Expect that the factory is listed in the right category.
const auto& names = factories.find("testing.published")->second->registeredNames();
EXPECT_NE(std::find(names.begin(), names.end(), "testing.published.versioned"), std::end(names));
// Expect that the factory is present.
EXPECT_NE(Registry::FactoryRegistry<PublishedFactory>::getFactory("testing.published.versioned"),
nullptr);
auto version =
factories.find("testing.published")->second->getFactoryVersion("testing.published.versioned");
EXPECT_TRUE(version.has_value());
EXPECT_EQ(2, version.value().version().major_number());
EXPECT_EQ(5, version.value().version().minor_number());
EXPECT_EQ(39, version.value().version().patch());
EXPECT_EQ(1, version.value().metadata().fields().size());
EXPECT_EQ("alpha", version.value().metadata().fields().at("build.label").string_value());
}
TEST(RegistryTest, TestDoubleRegistrationByName) {
EXPECT_THROW_WITH_MESSAGE((Registry::RegisterFactory<TestPublishedFactory, PublishedFactory>()),
EnvoyException,
"Double registration for name: 'testing.published.test'");
}
} // namespace
} // namespace Config
} // namespace Envoy