Skip to content

Commit eb97676

Browse files
committed
Add basic test cases for Type::GetConfigTypesSortedByLoadDependencies()
1 parent 467e8b1 commit eb97676

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

test/CMakeLists.txt

+53-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,59 @@
22

33
include(BoostTestTargets)
44

5+
set(types_test_SOURCES
6+
icingaapplication-fixture.cpp
7+
base-type.cpp
8+
${base_OBJS}
9+
$<TARGET_OBJECTS:config>
10+
$<TARGET_OBJECTS:remote>
11+
$<TARGET_OBJECTS:icinga>
12+
$<TARGET_OBJECTS:methods>
13+
)
14+
15+
if(ICINGA2_WITH_CHECKER)
16+
list(APPEND types_test_SOURCES $<TARGET_OBJECTS:checker>)
17+
endif()
18+
19+
if(ICINGA2_WITH_MYSQL)
20+
list(APPEND types_test_SOURCES $<TARGET_OBJECTS:db_ido> $<TARGET_OBJECTS:db_ido_mysql>)
21+
endif()
22+
23+
if(ICINGA2_WITH_PGSQL)
24+
list(APPEND types_test_SOURCES $<TARGET_OBJECTS:db_ido> $<TARGET_OBJECTS:db_ido_pgsql>)
25+
endif()
26+
27+
if(ICINGA2_WITH_ICINGADB)
28+
list(APPEND types_test_SOURCES $<TARGET_OBJECTS:icingadb>)
29+
endif()
30+
31+
if(ICINGA2_WITH_NOTIFICATION)
32+
list(APPEND types_test_SOURCES $<TARGET_OBJECTS:notification>)
33+
endif()
34+
35+
if(ICINGA2_WITH_PERFDATA)
36+
list(APPEND types_test_SOURCES $<TARGET_OBJECTS:perfdata>)
37+
endif()
38+
39+
if(ICINGA2_UNITY_BUILD)
40+
mkunity_target(types test types_test_SOURCES)
41+
endif()
42+
43+
# In order to test the order of all Icinga 2 config type load dependencies, we need to link against all the libraries,
44+
# but this results in boost signals e.g. in dbevents.cpp being triggered by icinga-checkresult.cpp test cases that
45+
# only pass partially initialised objects. Therefore, the types test cases are decoupled from base and moved to a
46+
# separate executable to not crash the base test cases.
47+
add_boost_test(types
48+
SOURCES test-runner.cpp ${types_test_SOURCES}
49+
LIBRARIES ${base_DEPS}
50+
TESTS
51+
types/gettype
52+
types/assign
53+
types/byname
54+
types/instantiate
55+
types/sort_by_load_after
56+
)
57+
558
set(base_test_SOURCES
659
icingaapplication-fixture.cpp
760
base-array.cpp
@@ -21,7 +74,6 @@ set(base_test_SOURCES
2174
base-string.cpp
2275
base-timer.cpp
2376
base-tlsutility.cpp
24-
base-type.cpp
2577
base-utility.cpp
2678
base-value.cpp
2779
config-apply.cpp
@@ -117,10 +169,6 @@ add_boost_test(base
117169
base_tlsutility/iscertuptodate_ok
118170
base_tlsutility/iscertuptodate_expiring
119171
base_tlsutility/iscertuptodate_old
120-
base_type/gettype
121-
base_type/assign
122-
base_type/byname
123-
base_type/instantiate
124172
base_utility/parse_version
125173
base_utility/compare_version
126174
base_utility/comparepasswords_works

test/base-type.cpp

+32-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
#include "base/objectlock.hpp"
66
#include "base/application.hpp"
77
#include "base/type.hpp"
8+
#include "icinga/host.hpp"
9+
#include "icinga/service.hpp"
810
#include <BoostTestTargetConfig.h>
911

1012
using namespace icinga;
1113

12-
BOOST_AUTO_TEST_SUITE(base_type)
14+
BOOST_AUTO_TEST_SUITE(types)
1315

1416
BOOST_AUTO_TEST_CASE(gettype)
1517
{
@@ -44,4 +46,33 @@ BOOST_AUTO_TEST_CASE(instantiate)
4446
BOOST_CHECK(p);
4547
}
4648

49+
BOOST_AUTO_TEST_CASE(sort_by_load_after)
50+
{
51+
int totalDependencies{0};
52+
std::unordered_set<Type*> previousTypes;
53+
for (auto type : Type::GetConfigTypesSortedByLoadDependencies()) {
54+
BOOST_CHECK_EQUAL(true, ConfigObject::TypeInstance->IsAssignableFrom(type));
55+
56+
totalDependencies += type->GetLoadDependencies().size();
57+
for (Type* dependency : type->GetLoadDependencies()) {
58+
// Note, Type::GetConfigTypesSortedByLoadDependencies() does not detect any cyclic load dependencies,
59+
// instead, it relies on this test case to fail.
60+
BOOST_CHECK_MESSAGE(previousTypes.find(dependency) != previousTypes.end(), "type '" << type->GetName()
61+
<< "' depends on '"<< dependency->GetName() << "' type, but it's not loaded before");
62+
}
63+
64+
previousTypes.emplace(type.get());
65+
}
66+
67+
// The magic number 12 is the sum of host,service,comment,downtime and endpoint load_after dependencies.
68+
BOOST_CHECK_MESSAGE(totalDependencies >= 12, "total size of load dependency must be at least 12");
69+
BOOST_CHECK_MESSAGE(previousTypes.find(Host::TypeInstance.get()) != previousTypes.end(), "Host type should be in the list");
70+
BOOST_CHECK_MESSAGE(previousTypes.find(Service::TypeInstance.get()) != previousTypes.end(), "Service type should be in the list");
71+
BOOST_CHECK_MESSAGE(previousTypes.find(Downtime::TypeInstance.get()) != previousTypes.end(), "Downtime type should be in the list");
72+
BOOST_CHECK_MESSAGE(previousTypes.find(Comment::TypeInstance.get()) != previousTypes.end(), "Comment type should be in the list");
73+
BOOST_CHECK_MESSAGE(previousTypes.find(Notification::TypeInstance.get()) != previousTypes.end(), "Notification type should be in the list");
74+
BOOST_CHECK_MESSAGE(previousTypes.find(Zone::TypeInstance.get()) != previousTypes.end(), "Zone type should be in the list");
75+
BOOST_CHECK_MESSAGE(previousTypes.find(Endpoint::TypeInstance.get()) != previousTypes.end(), "Endpoint type should be in the list");
76+
}
77+
4778
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)