Skip to content

Commit 2e69c34

Browse files
authored
Increase policies unit tests and code coverage (#685)
Co-authored-by: Leo Parente <lnparente@outlook.com>
1 parent e164c05 commit 2e69c34

File tree

1 file changed

+173
-3
lines changed

1 file changed

+173
-3
lines changed

src/tests/test_policies.cpp

Lines changed: 173 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,38 @@ version: "1.0"
6363
- "slack.com"
6464
)";
6565

66+
auto policies_config_merge = R"(
67+
visor:
68+
taps:
69+
anycast_merge:
70+
input_type: mock
71+
config:
72+
iface: eth0
73+
policies:
74+
default_merge:
75+
config:
76+
merge_like_handlers: true
77+
kind: collection
78+
input:
79+
tap: anycast_merge
80+
input_type: mock
81+
config:
82+
sample: value
83+
handlers:
84+
window_config:
85+
num_periods: 5
86+
deep_sample_rate: 100
87+
modules:
88+
default_dns_1:
89+
type: dns
90+
default_dns_2:
91+
type: dns
92+
default_net_1:
93+
type: net
94+
default_net_2:
95+
type: net
96+
)";
97+
6698
auto policies_config_hseq = R"(
6799
visor:
68100
taps:
@@ -201,6 +233,13 @@ version: "1.0"
201233
type: net
202234
)";
203235

236+
auto policies_config_bad0 = R"(
237+
version: "1.0"
238+
239+
visor:
240+
no_policies:
241+
)";
242+
204243
auto policies_config_bad1 = R"(
205244
version: "2.0"
206245
@@ -251,8 +290,6 @@ version: "1.0"
251290
)";
252291

253292
auto policies_config_bad4 = R"(
254-
version: "1.0"
255-
256293
visor:
257294
taps:
258295
anycast:
@@ -492,6 +529,28 @@ version: "1.0"
492529
type: net
493530
)";
494531

532+
auto policies_config_bad14 = R"(
533+
version: "1.0"
534+
535+
visor:
536+
taps:
537+
anycast:
538+
input_type: mock
539+
config:
540+
iface: eth0
541+
policies:
542+
default_view:
543+
config: not_a_map
544+
kind: collection
545+
input:
546+
tap: anycast
547+
input_type: mock
548+
handlers:
549+
modules:
550+
default_net:
551+
type: net
552+
)";
553+
495554
auto policies_config_hseq_bad2 = R"(
496555
version: "1.0"
497556
@@ -767,6 +826,35 @@ TEST_CASE("Policies", "[policies]")
767826
CHECK(policy->modules()[0]->running());
768827
CHECK(policy->modules()[1]->running());
769828
CHECK(policy->modules()[2]->running());
829+
830+
json j;
831+
REQUIRE_NOTHROW(policy->info_json(j));
832+
CHECK(j["input"] != nullptr);
833+
CHECK(j["modules"]["default_view-anycast-default_net"] != nullptr);
834+
CHECK(j["taps"]["anycast"]["config"]["iface"] == "eth0");
835+
}
836+
837+
SECTION("Good Config merge like handlers")
838+
{
839+
CoreRegistry registry;
840+
registry.start(nullptr);
841+
YAML::Node config_file = YAML::Load(policies_config_merge);
842+
CHECK(config_file["visor"]["policies"]);
843+
CHECK(config_file["visor"]["policies"].IsMap());
844+
REQUIRE_NOTHROW(registry.tap_manager()->load(config_file["visor"]["taps"]));
845+
REQUIRE_NOTHROW(registry.policy_manager()->load(config_file["visor"]["policies"]));
846+
847+
REQUIRE(registry.policy_manager()->module_exists("default_merge"));
848+
auto [policy, lock] = registry.policy_manager()->module_get_locked("default_merge");
849+
CHECK(policy->name() == "default_merge");
850+
CHECK(policy->input_stream().back()->name().find("anycast_merge-") != std::string::npos);
851+
CHECK(policy->modules()[0]->name() == "default_merge-anycast_merge-default_dns_1");
852+
CHECK(policy->modules()[1]->name() == "default_merge-anycast_merge-default_dns_2");
853+
CHECK(policy->modules()[2]->name() == "default_merge-anycast_merge-default_net_1");
854+
CHECK(policy->modules()[3]->name() == "default_merge-anycast_merge-default_net_2");
855+
CHECK(policy->input_stream().back()->running());
856+
CHECK(policy->modules()[0]->running());
857+
CHECK(policy->modules()[1]->running());
770858
}
771859

772860
SECTION("Good Config sequence modules")
@@ -834,7 +922,13 @@ TEST_CASE("Policies", "[policies]")
834922
REQUIRE(registry.policy_manager()->module_exists("default_view"));
835923
}
836924

837-
SECTION("Bad Config")
925+
SECTION("Bad Config: no policies schema")
926+
{
927+
CoreRegistry registry;
928+
REQUIRE_THROWS_WITH(registry.policy_manager()->load_from_str(policies_config_bad0), "no policies found in schema");
929+
}
930+
931+
SECTION("Bad Config: no policies map config")
838932
{
839933
CoreRegistry registry;
840934
YAML::Node config_file = YAML::Load(policies_config_bad1);
@@ -977,6 +1071,16 @@ TEST_CASE("Policies", "[policies]")
9771071
REQUIRE_THROWS_WITH(registry.handler_manager()->set_default_handler_config(config_file["visor"]["global_handler_config"]), "expecting global_handler_config configuration map");
9781072
}
9791073

1074+
SECTION("Bad Config: policy config not a map")
1075+
{
1076+
CoreRegistry registry;
1077+
registry.start(nullptr);
1078+
YAML::Node config_file = YAML::Load(policies_config_bad14);
1079+
1080+
REQUIRE_NOTHROW(registry.tap_manager()->load(config_file["visor"]["taps"], true));
1081+
REQUIRE_THROWS_WITH(registry.policy_manager()->load(config_file["visor"]["policies"]), "policy configuration is not a map");
1082+
}
1083+
9801084
SECTION("Bad Config: invalid handler modules YAML type")
9811085
{
9821086
CoreRegistry registry;
@@ -1200,3 +1304,69 @@ TEST_CASE("Policies", "[policies]")
12001304
lock2.unlock();
12011305
}
12021306
}
1307+
1308+
TEST_CASE("Policies and Metrics", "[policies][metrics]")
1309+
{
1310+
CoreRegistry registry;
1311+
registry.start(nullptr);
1312+
YAML::Node config_file = YAML::Load(policies_config);
1313+
CHECK(config_file["visor"]["policies"]);
1314+
CHECK(config_file["visor"]["policies"].IsMap());
1315+
REQUIRE_NOTHROW(registry.tap_manager()->load(config_file["visor"]["taps"]));
1316+
REQUIRE_NOTHROW(registry.policy_manager()->load(config_file["visor"]["policies"]));
1317+
REQUIRE(registry.policy_manager()->module_exists("default_view"));
1318+
1319+
SECTION("JSON")
1320+
{
1321+
auto [policy, lock] = registry.policy_manager()->module_get_locked("default_view");
1322+
json j;
1323+
policy->json_metrics(j, 0, false);
1324+
CHECK(j["default_view"]["default_view-anycast-default_dns"] != nullptr);
1325+
CHECK(j["default_view"]["default_view-anycast-default_net"] != nullptr);
1326+
}
1327+
1328+
SECTION("Prometheus")
1329+
{
1330+
auto [policy, lock] = registry.policy_manager()->module_get_locked("default_view");
1331+
std::stringstream out;
1332+
REQUIRE_NOTHROW(policy->prometheus_metrics(out));
1333+
}
1334+
1335+
SECTION("Opentelemetry")
1336+
{
1337+
auto [policy, lock] = registry.policy_manager()->module_get_locked("default_view");
1338+
metrics::v1::ScopeMetrics scope;
1339+
REQUIRE_NOTHROW(policy->opentelemetry_metrics(scope));
1340+
}
1341+
1342+
config_file = YAML::Load(policies_config_merge);
1343+
CHECK(config_file["visor"]["policies"]);
1344+
CHECK(config_file["visor"]["policies"].IsMap());
1345+
REQUIRE_NOTHROW(registry.tap_manager()->load(config_file["visor"]["taps"]));
1346+
REQUIRE_NOTHROW(registry.policy_manager()->load(config_file["visor"]["policies"]));
1347+
REQUIRE(registry.policy_manager()->module_exists("default_merge"));
1348+
1349+
SECTION("JSON merge_like_handlers")
1350+
{
1351+
auto [policy, lock] = registry.policy_manager()->module_get_locked("default_merge");
1352+
json j;
1353+
policy->json_metrics(j, 0, false);
1354+
std::cout << j.dump();
1355+
CHECK(j["default_merge"]["dns_merged"] != nullptr);
1356+
CHECK(j["default_merge"]["packets_merged"] != nullptr);
1357+
}
1358+
1359+
SECTION("Prometheus merge_like_handlers")
1360+
{
1361+
auto [policy, lock] = registry.policy_manager()->module_get_locked("default_merge");
1362+
std::stringstream out;
1363+
REQUIRE_NOTHROW(policy->prometheus_metrics(out));
1364+
}
1365+
1366+
SECTION("Opentelemetry merge_like_handlers")
1367+
{
1368+
auto [policy, lock] = registry.policy_manager()->module_get_locked("default_merge");
1369+
metrics::v1::ScopeMetrics scope;
1370+
REQUIRE_NOTHROW(policy->opentelemetry_metrics(scope));
1371+
}
1372+
}

0 commit comments

Comments
 (0)