Skip to content

Commit 0817ed3

Browse files
committed
Fix & adjust dependencies unittests
1 parent ea6e3cc commit 0817ed3

File tree

1 file changed

+86
-43
lines changed

1 file changed

+86
-43
lines changed

test/icinga-dependencies.cpp

Lines changed: 86 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static Host::Ptr CreateHost(const std::string& name)
1616
return host;
1717
}
1818

19-
static Dependency::Ptr CreateDependency(Checkable::Ptr parent, Checkable::Ptr child, const std::string& name)
19+
static Dependency::Ptr CreateDependency(Checkable::Ptr parent, Checkable::Ptr child, const String& name)
2020
{
2121
Dependency::Ptr dep = new Dependency();
2222
dep->SetParent(parent);
@@ -25,32 +25,34 @@ static Dependency::Ptr CreateDependency(Checkable::Ptr parent, Checkable::Ptr ch
2525
return dep;
2626
}
2727

28-
static void RegisterDependency(Dependency::Ptr dep, const std::string& redundancyGroup)
28+
static void RegisterDependency(Dependency::Ptr dep, const String& redundancyGroup)
2929
{
3030
dep->SetRedundancyGroup(redundancyGroup);
31-
DependencyGroup::Register(dep);
31+
dep->GetChild()->AddDependency(dep, true);
3232
dep->GetParent()->AddReverseDependency(dep);
3333
}
3434

35-
static void AssertCheckableRedundancyGroup(Checkable::Ptr checkable, int dependencyCount, int redundancyGroupCount, int memberCount)
35+
static void AssertCheckableRedundancyGroup(Checkable::Ptr checkable, int dependencyCount, int groupCount, int memberCount)
3636
{
3737
BOOST_CHECK_MESSAGE(
3838
dependencyCount == checkable->GetDependencies().size(),
3939
"Dependency count mismatch for '" << checkable->GetName() << "' - expected=" << dependencyCount << "; got="
4040
<< checkable->GetDependencies().size()
4141
);
42-
auto redundancyGroups(checkable->GetDependencyGroups());
42+
auto dependencyGroups(checkable->GetDependencyGroups());
4343
BOOST_CHECK_MESSAGE(
44-
redundancyGroupCount == redundancyGroups.size(),
45-
"Redundancy group count mismatch for '" << checkable->GetName() << "'" << " - expected=" << redundancyGroupCount
46-
<< "; got=" << redundancyGroups.size()
44+
groupCount == dependencyGroups.size(),
45+
"Dependency group count mismatch for '" << checkable->GetName() << "' - expected=" << groupCount
46+
<< "; got=" << dependencyGroups.size()
4747
);
48-
if (redundancyGroupCount > 0) {
49-
BOOST_REQUIRE_MESSAGE(1 <= redundancyGroups.size(), "Checkable '" << checkable->GetName() << "' should have at least one redundancy group.");
48+
if (groupCount > 0) {
49+
BOOST_REQUIRE_MESSAGE(!dependencyGroups.empty(), "Checkable '" << checkable->GetName() << "' should have at least one redundancy group.");
50+
auto dependencyGroup(dependencyGroups.front());
5051
BOOST_CHECK_MESSAGE(
51-
memberCount == redundancyGroups.begin()->get()->GetMemberCount(),
52-
"Member count mismatch for '" << checkable->GetName() << "'" << " - expected=" << memberCount
53-
<< "; got=" << redundancyGroups.begin()->get()->GetMemberCount()
52+
memberCount == dependencyGroup->GetMemberCount(),
53+
"Dependency group '" << dependencyGroup->GetName() << "' and Checkable '" << checkable->GetName()
54+
<< "' member count mismatch - expected=" << memberCount << "; got="
55+
<< dependencyGroup->GetMemberCount()
5456
);
5557
}
5658
}
@@ -106,19 +108,19 @@ BOOST_AUTO_TEST_CASE(multi_parent)
106108
// It should still be unreachable, due to the duplicated dependency object above with ignore_soft_states set to false.
107109
BOOST_CHECK(childHost->IsReachable() == false);
108110
parentHost1->SetStateType(StateTypeHard);
109-
DependencyGroup::Unregister(duplicateDep);
111+
childHost->RemoveDependency(duplicateDep);
110112

111113
/* The only DNS server is DOWN.
112114
* Expected result: childHost is unreachable.
113115
*/
114-
DependencyGroup::Unregister(dep1); // Remove the dep and re-add it with a configured redundancy group.
116+
childHost->RemoveDependency(dep1); // Remove the dep and re-add it with a configured redundancy group.
115117
RegisterDependency(dep1, "DNS");
116118
BOOST_CHECK(childHost->IsReachable() == false);
117119

118120
/* 1/2 DNS servers is DOWN.
119121
* Expected result: childHost is reachable.
120122
*/
121-
DependencyGroup::Unregister(dep2);
123+
childHost->RemoveDependency(dep2);
122124
RegisterDependency(dep2, "DNS");
123125
BOOST_CHECK(childHost->IsReachable() == true);
124126

@@ -132,7 +134,7 @@ BOOST_AUTO_TEST_CASE(multi_parent)
132134
RegisterDependency(dep3, "");
133135
// The grandparent is DOWN but the DNS redundancy group has to be still reachable.
134136
BOOST_CHECK_EQUAL(true, childHost->IsReachable());
135-
DependencyGroup::Unregister(dep3);
137+
childHost->RemoveDependency(dep3);
136138

137139
/* Both DNS servers are DOWN.
138140
* Expected result: childHost is unreachable.
@@ -153,29 +155,59 @@ BOOST_AUTO_TEST_CASE(default_redundancy_group_registration_unregistration)
153155

154156
Dependency::Ptr depCB(CreateDependency(CreateHost("B"), childHostC, "depCB"));
155157
RegisterDependency(depCB, "");
156-
AssertCheckableRedundancyGroup(childHostC, 2, 1, 2);
157-
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
158+
AssertCheckableRedundancyGroup(childHostC, 2, 2, 1);
159+
BOOST_CHECK_EQUAL(2, DependencyGroup::GetRegistrySize());
158160

159161
Checkable::Ptr childHostD(CreateHost("D"));
160162
Dependency::Ptr depDA(CreateDependency(depCA->GetParent(), childHostD, "depDA"));
161163
RegisterDependency(depDA, "");
162-
AssertCheckableRedundancyGroup(childHostD, 1, 1, 1);
164+
AssertCheckableRedundancyGroup(childHostD, 1, 1, 2);
163165
BOOST_CHECK_EQUAL(2, DependencyGroup::GetRegistrySize());
164166

165167
Dependency::Ptr depDB(CreateDependency(depCB->GetParent(), childHostD, "depDB"));
166168
RegisterDependency(depDB, "");
167-
AssertCheckableRedundancyGroup(childHostD, 2, 1, 4);
168-
AssertCheckableRedundancyGroup(childHostC, 2, 1, 4);
169-
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
169+
AssertCheckableRedundancyGroup(childHostD, 2, 2, 2);
170+
AssertCheckableRedundancyGroup(childHostC, 2, 2, 2);
171+
BOOST_TEST(childHostC->GetDependencyGroups() == childHostD->GetDependencyGroups(), boost::test_tools::per_element());
172+
BOOST_CHECK_EQUAL(2, DependencyGroup::GetRegistrySize());
173+
174+
// This is an exact duplicate of depCA, but with a different dependency name.
175+
Dependency::Ptr depCA2(CreateDependency(depCA->GetParent(), childHostC, "depCA2"));
176+
// This is a duplicate of depCA, but with a different state filter.
177+
Dependency::Ptr depCA3(CreateDependency(depCA->GetParent(), childHostC, "depCA3"));
178+
depCA3->SetStateFilter(StateFilterUp, true);
179+
// This is a duplicate of depCA, but with a different ignore_soft_states flag.
180+
Dependency::Ptr depCA4(CreateDependency(depCA->GetParent(), childHostC, "depCA4"));
181+
depCA4->SetIgnoreSoftStates(false, true);
182+
183+
for (auto& dependency : {depCA2, depCA3, depCA4}) {
184+
bool isAnExactDuplicate = dependency == depCA2;
185+
RegisterDependency(dependency, "");
186+
AssertCheckableRedundancyGroup(childHostD, 2, 2, 2);
187+
for (auto& dependencyGroup : childHostC->GetDependencyGroups()) {
188+
if (dependencyGroup->PeekParentCheckableName() == depCA->GetParent()->GetName()) {
189+
// If depCA2 is currently being processed, then the group should have 3 members, that's because
190+
// depCA2 is an exact duplicate of depCA, and depCA shares the same group with depDA.
191+
BOOST_CHECK_EQUAL(isAnExactDuplicate ? 3 : 2, dependencyGroup->GetMemberCount());
192+
} else {
193+
BOOST_CHECK_EQUAL(2, dependencyGroup->GetMemberCount());
194+
}
195+
// The 3 dependencies are depCA, depCB, and the current one from the loop.
196+
BOOST_CHECK_EQUAL(3, childHostC->GetDependencies().size());
197+
}
198+
BOOST_CHECK_EQUAL(isAnExactDuplicate ? 2 : 3, DependencyGroup::GetRegistrySize());
199+
childHostC->RemoveDependency(dependency);
200+
}
170201

171-
DependencyGroup::Unregister(depCA);
172-
DependencyGroup::Unregister(depDA);
202+
childHostC->RemoveDependency(depCA);
203+
childHostD->RemoveDependency(depDA);
173204
AssertCheckableRedundancyGroup(childHostC, 1, 1, 2);
174205
AssertCheckableRedundancyGroup(childHostD, 1, 1, 2);
206+
BOOST_TEST(childHostC->GetDependencyGroups() == childHostD->GetDependencyGroups(), boost::test_tools::per_element());
175207
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
176208

177-
DependencyGroup::Unregister(depCB);
178-
DependencyGroup::Unregister(depDB);
209+
childHostC->RemoveDependency(depCB);
210+
childHostD->RemoveDependency(depDB);
179211
AssertCheckableRedundancyGroup(childHostC, 0, 0, 0);
180212
AssertCheckableRedundancyGroup(childHostD, 0, 0, 0);
181213
BOOST_CHECK_EQUAL(0, DependencyGroup::GetRegistrySize());
@@ -206,31 +238,33 @@ BOOST_AUTO_TEST_CASE(simple_redundancy_group_registration_unregistration)
206238
// Still 1 redundancy group, but there should be 4 members now, i.e. 2 for each child Checkable.
207239
AssertCheckableRedundancyGroup(childHostC, 2, 1, 4);
208240
AssertCheckableRedundancyGroup(childHostD, 2, 1, 4);
241+
BOOST_TEST(childHostC->GetDependencyGroups() == childHostD->GetDependencyGroups(), boost::test_tools::per_element());
209242
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
210243

211-
DependencyGroup::Unregister(depCA);
244+
childHostC->RemoveDependency(depCA);
212245
// After unregistering depCA, childHostC should have a new redundancy group with only depCB as member, and...
213246
AssertCheckableRedundancyGroup(childHostC, 1, 1, 1);
214247
// ...childHostD should still have the same redundancy group as before but also with only two members.
215248
AssertCheckableRedundancyGroup(childHostD, 2, 1, 2);
216249
BOOST_CHECK_EQUAL(2, DependencyGroup::GetRegistrySize());
217250

218-
DependencyGroup::Unregister(depDA);
251+
childHostD->RemoveDependency(depDA);
219252
// Nothing should have changed for childHostC, but childHostD should now have a fewer group member, i.e.
220253
// both child hosts should have the same redundancy group with only depCB as member.
221254
AssertCheckableRedundancyGroup(childHostC, 1, 1, 2);
222255
AssertCheckableRedundancyGroup(childHostD, 1, 1, 2);
256+
BOOST_TEST(childHostC->GetDependencyGroups() == childHostD->GetDependencyGroups(), boost::test_tools::per_element());
223257
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
224258

225-
DependencyGroup::Register(depDA);
226-
DependencyGroup::Unregister(depDB);
259+
RegisterDependency(depDA, depDA->GetRedundancyGroup());
260+
childHostD->RemoveDependency(depDB);
227261
// Nothing should have changed for childHostC, but childHostD should now have a fewer group member.
228262
AssertCheckableRedundancyGroup(childHostC, 1, 1, 1);
229263
AssertCheckableRedundancyGroup(childHostD, 1, 1, 1);
230264
BOOST_CHECK_EQUAL(2, DependencyGroup::GetRegistrySize());
231265

232-
DependencyGroup::Unregister(depCB);
233-
DependencyGroup::Unregister(depDA);
266+
childHostC->RemoveDependency(depCB);
267+
childHostD->RemoveDependency(depDA);
234268
AssertCheckableRedundancyGroup(childHostC, 0, 0, 0);
235269
AssertCheckableRedundancyGroup(childHostD, 0, 0, 0);
236270
BOOST_CHECK_EQUAL(0, DependencyGroup::GetRegistrySize());
@@ -248,6 +282,7 @@ BOOST_AUTO_TEST_CASE(mixed_redundancy_group_registration_unregsitration)
248282
Dependency::Ptr depDA(CreateDependency(depCA->GetParent(), childHostD, "depDA"));
249283
RegisterDependency(depDA, "redundant");
250284
AssertCheckableRedundancyGroup(childHostD, 1, 1, 2);
285+
BOOST_TEST(childHostC->GetDependencyGroups() == childHostD->GetDependencyGroups(), boost::test_tools::per_element());
251286
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
252287

253288
Dependency::Ptr depCB(CreateDependency(CreateHost("B"), childHostC, "depCB"));
@@ -260,6 +295,7 @@ BOOST_AUTO_TEST_CASE(mixed_redundancy_group_registration_unregsitration)
260295
RegisterDependency(depDB, "redundant");
261296
AssertCheckableRedundancyGroup(childHostC, 2, 1, 4);
262297
AssertCheckableRedundancyGroup(childHostD, 2, 1, 4);
298+
BOOST_TEST(childHostC->GetDependencyGroups() == childHostD->GetDependencyGroups(), boost::test_tools::per_element());
263299
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
264300

265301
Checkable::Ptr childHostE(CreateHost("childE"));
@@ -274,42 +310,49 @@ BOOST_AUTO_TEST_CASE(mixed_redundancy_group_registration_unregsitration)
274310
AssertCheckableRedundancyGroup(childHostC, 2, 1, 6);
275311
AssertCheckableRedundancyGroup(childHostD, 2, 1, 6);
276312
AssertCheckableRedundancyGroup(childHostE, 2, 1, 6);
313+
auto childHostCGroups(childHostC->GetDependencyGroups());
314+
BOOST_TEST((childHostCGroups == childHostD->GetDependencyGroups() && childHostCGroups == childHostE->GetDependencyGroups()));
277315
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
278316

279317
Dependency::Ptr depEZ(CreateDependency(CreateHost("Z"), childHostE, "depEZ"));
280318
RegisterDependency(depEZ, "redundant");
281319
// Child host E should have a new redundancy group with 3 members and the other two should still share the same group.
282320
AssertCheckableRedundancyGroup(childHostC, 2, 1, 4);
283321
AssertCheckableRedundancyGroup(childHostD, 2, 1, 4);
322+
BOOST_TEST(childHostC->GetDependencyGroups() == childHostD->GetDependencyGroups(), boost::test_tools::per_element());
284323
AssertCheckableRedundancyGroup(childHostE, 3, 1, 3);
285324
BOOST_CHECK_EQUAL(2, DependencyGroup::GetRegistrySize());
286325

287-
DependencyGroup::Unregister(depEA);
326+
childHostE->RemoveDependency(depEA);
288327
AssertCheckableRedundancyGroup(childHostC, 2, 1, 4);
289328
AssertCheckableRedundancyGroup(childHostD, 2, 1, 4);
329+
BOOST_TEST(childHostC->GetDependencyGroups() == childHostD->GetDependencyGroups(), boost::test_tools::per_element());
290330
AssertCheckableRedundancyGroup(childHostE, 2, 1, 2);
291331
BOOST_CHECK_EQUAL(2, DependencyGroup::GetRegistrySize());
292332

293-
DependencyGroup::Register(depEA); // Re-register depEA and instead...
294-
DependencyGroup::Unregister(depEZ); // ...unregister depEZ and check if all the hosts share the same group again.
333+
RegisterDependency(depEA, depEA->GetRedundancyGroup()); // Re-register depEA and instead...
334+
childHostE->RemoveDependency(depEZ); // ...unregister depEZ and check if all the hosts share the same group again.
295335
// All 3 hosts share the same group again, and each host has 2 members, thus 6 members in total.
296336
AssertCheckableRedundancyGroup(childHostC, 2, 1, 6);
297337
AssertCheckableRedundancyGroup(childHostD, 2, 1, 6);
298338
AssertCheckableRedundancyGroup(childHostE, 2, 1, 6);
339+
childHostCGroups = childHostC->GetDependencyGroups();
340+
BOOST_TEST((childHostCGroups == childHostD->GetDependencyGroups() && childHostCGroups == childHostE->GetDependencyGroups()));
299341
BOOST_CHECK_EQUAL(1, DependencyGroup::GetRegistrySize());
300342

301-
DependencyGroup::Unregister(depCA);
302-
DependencyGroup::Unregister(depDB);
303-
DependencyGroup::Unregister(depEB);
343+
childHostC->RemoveDependency(depCA);
344+
childHostD->RemoveDependency(depDB);
345+
childHostE->RemoveDependency(depEB);
346+
// Child host C has now a separate group with only depCB as member, and child hosts D and E share the same group.
304347
AssertCheckableRedundancyGroup(childHostC, 1, 1, 1);
305348
AssertCheckableRedundancyGroup(childHostD, 1, 1, 2);
306349
AssertCheckableRedundancyGroup(childHostE, 1, 1, 2);
307-
// Child host C has now a separate group with only depCB as member, and child hosts D and E share the same group.
350+
BOOST_TEST(childHostD->GetDependencyGroups() == childHostE->GetDependencyGroups(), boost::test_tools::per_element());
308351
BOOST_CHECK_EQUAL(2, DependencyGroup::GetRegistrySize());
309352

310-
DependencyGroup::Unregister(depCB);
311-
DependencyGroup::Unregister(depDA);
312-
DependencyGroup::Unregister(depEA);
353+
childHostC->RemoveDependency(depCB);
354+
childHostD->RemoveDependency(depDA);
355+
childHostE->RemoveDependency(depEA);
313356
AssertCheckableRedundancyGroup(childHostC, 0, 0, 0);
314357
AssertCheckableRedundancyGroup(childHostD, 0, 0, 0);
315358
AssertCheckableRedundancyGroup(childHostE, 0, 0, 0);

0 commit comments

Comments
 (0)