forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_extensions_against_registry_test.cc
79 lines (69 loc) · 3 KB
/
check_extensions_against_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
#include "envoy/registry/registry.h"
#include "test/test_common/environment.h"
#include "test/test_common/utility.h"
#include "gtest/gtest.h"
namespace Envoy {
namespace {
std::vector<std::string> stringsFromListValue(const ProtobufWkt::Value& value) {
std::vector<std::string> strings;
for (const auto& elt : value.list_value().values()) {
strings.push_back(elt.string_value());
}
return strings;
}
// Ensure that the type URLs in the static extension schema match the type URLs
// in the internal extension registry.
TEST(CheckExtensionsAgainstRegistry, CorrectMetadata) {
// Manifest schema example:
// <extension_name>:
// categories:
// - <category_name>
// type_urls:
// - <sorted type_url>
const std::string manifest_path =
TestEnvironment::runfilesPath("source/extensions/extensions_metadata.yaml");
const std::string manifest = TestEnvironment::readFileToStringForTest(manifest_path);
ProtobufWkt::Value value = ValueUtil::loadFromYaml(manifest);
ASSERT_EQ(ProtobufWkt::Value::kStructValue, value.kind_case());
const auto& json = value.struct_value();
for (const auto& ext : Registry::FactoryCategoryRegistry::registeredFactories()) {
auto registered_types = ext.second->registeredTypes();
for (const auto& name : ext.second->allRegisteredNames()) {
if (ext.second->canonicalFactoryName(name) != name) {
continue;
}
const auto& it = json.fields().find(std::string(name));
if (it == json.fields().end()) {
ENVOY_LOG_MISC(warn, "Missing extension '{}' from category '{}'.", name, ext.first);
continue;
}
ASSERT_EQ(ProtobufWkt::Value::kStructValue, it->second.kind_case())
<< "Malformed extension metadata for: " << name;
const auto& extension_fields = it->second.struct_value().fields();
// Validate that the extension category from the registry is listed.
const auto& categories_it = extension_fields.find("categories");
ASSERT_TRUE(categories_it != extension_fields.end())
<< "Missing field 'categories' for: " << name;
EXPECT_THAT(stringsFromListValue(categories_it->second), ::testing::Contains(ext.first))
<< "Missing category: " << name;
// Validate that the type URLs from the registry are listed.
std::vector<std::string> type_urls;
const auto& type_urls_it = extension_fields.find("type_urls");
if (type_urls_it != extension_fields.end()) {
type_urls = stringsFromListValue(type_urls_it->second);
}
const auto& expected_types = registered_types.find(name);
if (expected_types != registered_types.end()) {
std::sort(expected_types->second.begin(), expected_types->second.end());
EXPECT_THAT(type_urls, ::testing::IsSupersetOf(expected_types->second))
<< "Mismatched type URLs: " << name;
} else {
EXPECT_EQ(type_urls.size(), 0)
<< std::accumulate(type_urls.begin(), type_urls.end(), std::string(""));
;
}
}
}
}
} // namespace
} // namespace Envoy