Test ApplyRule::GetTarget*s()

This commit is contained in:
Alexander A. Klimov 2023-12-13 16:25:33 +01:00
parent ecfc9033b0
commit 191bf93f2e
2 changed files with 275 additions and 0 deletions

View File

@ -24,6 +24,7 @@ set(base_test_SOURCES
base-type.cpp base-type.cpp
base-utility.cpp base-utility.cpp
base-value.cpp base-value.cpp
config-apply.cpp
config-ops.cpp config-ops.cpp
icinga-checkresult.cpp icinga-checkresult.cpp
icinga-dependencies.cpp icinga-dependencies.cpp
@ -123,6 +124,38 @@ add_boost_test(base
base_value/scalar base_value/scalar
base_value/convert base_value/convert
base_value/format base_value/format
config_apply/gettargethosts_literal
config_apply/gettargethosts_const
config_apply/gettargethosts_swapped
config_apply/gettargethosts_two
config_apply/gettargethosts_three
config_apply/gettargethosts_mixed
config_apply/gettargethosts_redundant
config_apply/gettargethosts_badconst
config_apply/gettargethosts_notliteral
config_apply/gettargethosts_wrongop
config_apply/gettargethosts_wrongattr
config_apply/gettargethosts_wrongvar
config_apply/gettargethosts_noindexer
config_apply/gettargetservices_literal
config_apply/gettargetservices_const
config_apply/gettargetservices_swapped_outer
config_apply/gettargetservices_swapped_inner
config_apply/gettargetservices_two
config_apply/gettargetservices_three
config_apply/gettargetservices_mixed
config_apply/gettargetservices_redundant
config_apply/gettargetservices_badconst
config_apply/gettargetservices_notliteral
config_apply/gettargetservices_wrongop_outer
config_apply/gettargetservices_wrongop_host
config_apply/gettargetservices_wrongop_service
config_apply/gettargetservices_wrongattr_host
config_apply/gettargetservices_wrongattr_service
config_apply/gettargetservices_wrongvar_host
config_apply/gettargetservices_wrongvar_service
config_apply/gettargetservices_noindexer_host
config_apply/gettargetservices_noindexer_service
config_ops/simple config_ops/simple
config_ops/advanced config_ops/advanced
icinga_checkresult/host_1attempt icinga_checkresult/host_1attempt

242
test/config-apply.cpp Normal file
View File

@ -0,0 +1,242 @@
/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */
#include "config/applyrule.hpp"
#include "config/configcompiler.hpp"
#include <BoostTestTargetConfig.h>
using namespace icinga;
static Expression* RequireActualExpression(const std::unique_ptr<Expression>& compiledExpression)
{
BOOST_REQUIRE_NE(compiledExpression.get(), nullptr);
auto dict (dynamic_cast<DictExpression*>(compiledExpression.get()));
BOOST_REQUIRE_NE(dict, nullptr);
auto& subex (dict->GetExpressions());
BOOST_REQUIRE_EQUAL(subex.size(), 1u);
auto sub0 (subex.at(0).get());
BOOST_REQUIRE_NE(sub0, nullptr);
return sub0;
}
static void GetTargetHostsHelper(
const String& filter, const Dictionary::Ptr& constants, bool targeted, const std::vector<String>& hosts = {}
)
{
auto compiled (ConfigCompiler::CompileText("<test>", filter));
auto expr (RequireActualExpression(compiled));
std::vector<const String*> actualHosts;
BOOST_CHECK_EQUAL(ApplyRule::GetTargetHosts(expr, actualHosts, constants), targeted);
if (targeted) {
std::vector<String> actualHostNames;
actualHostNames.reserve(actualHosts.size());
for (auto h : actualHosts) {
actualHostNames.emplace_back(*h);
}
BOOST_CHECK_EQUAL_COLLECTIONS(actualHostNames.begin(), actualHostNames.end(), hosts.begin(), hosts.end());
}
}
static void GetTargetServicesHelper(
const String& filter, const Dictionary::Ptr& constants, bool targeted, const std::vector<std::pair<String, String>>& services = {}
)
{
auto compiled (ConfigCompiler::CompileText("<test>", filter));
auto expr (RequireActualExpression(compiled));
std::vector<std::pair<const String*, const String*>> actualServices;
BOOST_CHECK_EQUAL(ApplyRule::GetTargetServices(expr, actualServices, constants), targeted);
if (targeted) {
std::vector<std::pair<String, String>> actualServiceNames;
actualServiceNames.reserve(actualServices.size());
for (auto s : actualServices) {
actualServiceNames.emplace_back(*s.first, *s.second);
}
BOOST_CHECK(actualServiceNames == services);
}
}
BOOST_AUTO_TEST_SUITE(config_apply)
BOOST_AUTO_TEST_CASE(gettargethosts_literal)
{
GetTargetHostsHelper("host.name == \"foo\"", nullptr, true, {"foo"});
}
BOOST_AUTO_TEST_CASE(gettargethosts_const)
{
GetTargetHostsHelper("host.name == x", new Dictionary({{"x", "foo"}}), true, {"foo"});
}
BOOST_AUTO_TEST_CASE(gettargethosts_swapped)
{
GetTargetHostsHelper("\"foo\" == host.name", nullptr, true, {"foo"});
}
BOOST_AUTO_TEST_CASE(gettargethosts_two)
{
GetTargetHostsHelper("host.name == \"foo\" || host.name == \"bar\"", nullptr, true, {"foo", "bar"});
}
BOOST_AUTO_TEST_CASE(gettargethosts_three)
{
GetTargetHostsHelper(
"host.name == \"foo\" || host.name == \"bar\" || host.name == \"foobar\"",
nullptr, true, {"foo", "bar", "foobar"}
);
}
BOOST_AUTO_TEST_CASE(gettargethosts_mixed)
{
GetTargetHostsHelper("host.name == x || \"bar\" == host.name", new Dictionary({{"x", "foo"}}), true, {"foo", "bar"});
}
BOOST_AUTO_TEST_CASE(gettargethosts_redundant)
{
GetTargetHostsHelper("host.name == \"foo\" && 1", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargethosts_badconst)
{
GetTargetHostsHelper("host.name == NodeName", new Dictionary({{"x", "foo"}}), false);
}
BOOST_AUTO_TEST_CASE(gettargethosts_notliteral)
{
GetTargetHostsHelper("host.name == \"foo\" + \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargethosts_wrongop)
{
GetTargetHostsHelper("host.name != \"foo\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargethosts_wrongattr)
{
GetTargetHostsHelper("host.__name == \"foo\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargethosts_wrongvar)
{
GetTargetHostsHelper("service.name == \"foo\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargethosts_noindexer)
{
GetTargetHostsHelper("name == \"foo\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_literal)
{
GetTargetServicesHelper("host.name == \"foo\" && service.name == \"bar\"", nullptr, true, {{"foo", "bar"}});
}
BOOST_AUTO_TEST_CASE(gettargetservices_const)
{
GetTargetServicesHelper("host.name == x && service.name == y", new Dictionary({{"x", "foo"}, {"y", "bar"}}), true, {{"foo", "bar"}});
}
BOOST_AUTO_TEST_CASE(gettargetservices_swapped_outer)
{
GetTargetServicesHelper("service.name == \"bar\" && host.name == \"foo\"", nullptr, true, {{"foo", "bar"}});
}
BOOST_AUTO_TEST_CASE(gettargetservices_swapped_inner)
{
GetTargetServicesHelper("\"foo\" == host.name && \"bar\" == service.name", nullptr, true, {{"foo", "bar"}});
}
BOOST_AUTO_TEST_CASE(gettargetservices_two)
{
GetTargetServicesHelper(
"host.name == \"foo\" && service.name == \"bar\" || host.name == \"oof\" && service.name == \"rab\"",
nullptr, true, {{"foo", "bar"}, {"oof", "rab"}}
);
}
BOOST_AUTO_TEST_CASE(gettargetservices_three)
{
GetTargetServicesHelper(
"host.name == \"foo\" && service.name == \"bar\" || host.name == \"oof\" && service.name == \"rab\" || host.name == \"ofo\" && service.name == \"rba\"",
nullptr, true, {{"foo", "bar"}, {"oof", "rab"}, {"ofo", "rba"}}
);
}
BOOST_AUTO_TEST_CASE(gettargetservices_mixed)
{
GetTargetServicesHelper("\"bar\" == service.name && x == host.name", new Dictionary({{"x", "foo"}}), true, {{"foo", "bar"}});
}
BOOST_AUTO_TEST_CASE(gettargetservices_redundant)
{
GetTargetServicesHelper("host.name == \"foo\" && service.name == \"bar\" && 1", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_badconst)
{
GetTargetServicesHelper("host.name == NodeName && service.name == \"bar\"", new Dictionary({{"x", "foo"}}), false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_notliteral)
{
GetTargetServicesHelper("host.name == \"foo\" && service.name == \"b\" + \"ar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_wrongop_outer)
{
GetTargetServicesHelper("host.name == \"foo\" & service.name == \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_wrongop_host)
{
GetTargetServicesHelper("host.name != \"foo\" && service.name == \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_wrongop_service)
{
GetTargetServicesHelper("host.name == \"foo\" && service.name != \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_wrongattr_host)
{
GetTargetServicesHelper("host.__name == \"foo\" && service.name == \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_wrongattr_service)
{
GetTargetServicesHelper("host.name == \"foo\" && service.__name == \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_wrongvar_host)
{
GetTargetServicesHelper("horst.name == \"foo\" && service.name == \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_wrongvar_service)
{
GetTargetServicesHelper("host.name == \"foo\" && sehrvice.name == \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_noindexer_host)
{
GetTargetServicesHelper("name == \"foo\" && service.name == \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_CASE(gettargetservices_noindexer_service)
{
GetTargetServicesHelper("host.name == \"foo\" && name == \"bar\"", nullptr, false);
}
BOOST_AUTO_TEST_SUITE_END()