Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
fix(config): fix a freed pointer ref in groups
Browse files Browse the repository at this point in the history
fix(conf): fix also the problem on contactgroup
  • Loading branch information
SylvestreG committed Nov 21, 2019
1 parent b9a1488 commit 676ebc6
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ scripts/centengine.sh
src/simumod/neb.cc
tags
gtest.cbp
cmake-build-debug/*
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ include_directories("${INC_DIR}/compatibility")
# Version.
set(CENTREON_ENGINE_MAJOR 19)
set(CENTREON_ENGINE_MINOR 10)
set(CENTREON_ENGINE_PATCH 6)
set(CENTREON_ENGINE_PATCH 7)
if (CENTREON_ENGINE_PRERELEASE)
set(CENTREON_ENGINE_VERSION "${CENTREON_ENGINE_MAJOR}.${CENTREON_ENGINE_MINOR}.${CENTREON_ENGINE_PATCH}-${CENTREON_ENGINE_PRERELEASE}")
else ()
Expand Down
16 changes: 16 additions & 0 deletions doc/release_notes/engine19.10.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
=======================
Centreon Engine 19.10.7
=======================

*********
Bug fixes
*********

Groups update with deleted object
=================================

On object deletion, the groups containing these objects were not updated.
So on group update (conf reload, not restart) the group keeps in its
members a deleted object and can use it.


=======================
Centreon Engine 19.10.6
=======================
Expand Down
3 changes: 3 additions & 0 deletions src/configuration/applier/contact.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ void applier::contact::remove_object(
if (it != engine::contact::contacts.end()) {
engine::contact* cntct(it->second.get());

for (auto& it_c: it->second->get_parent_groups())
it_c.second->get_members().erase(obj.contact_name());

// Notify event broker.
timeval tv(get_broker_timestamp(nullptr));
broker_adaptive_contact_data(
Expand Down
4 changes: 4 additions & 0 deletions src/configuration/applier/host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ void applier::host::remove_object(configuration::host const& obj) {
// Remove events related to this host.
applier::scheduler::instance().remove_host(obj);

//remove host from hostgroup->members
for (auto& it_h: it->second->get_parent_groups())
it_h->members.erase(it->second->get_name());

// Notify event broker.
timeval tv(get_broker_timestamp(nullptr));
broker_adaptive_host_data(
Expand Down
4 changes: 4 additions & 0 deletions src/configuration/applier/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ void applier::service::remove_object(
// Remove events related to this service.
applier::scheduler::instance().remove_service(obj);

//remove service from servicegroup->members
for (auto& it_s: it->second->get_parent_groups())
it_s->members.erase({host_name, service_description});

// Notify event broker.
timeval tv(get_broker_timestamp(NULL));
broker_adaptive_service_data(
Expand Down
24 changes: 24 additions & 0 deletions tests/configuration/applier/applier-contactgroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,27 @@ TEST_F(ApplierContactgroup, SetContactgroupMembers) {
// grp1 must be reload because the expand_objects reload them totally.
ASSERT_TRUE(config->contactgroups_find("big_group")->members().size() == 1);
}

TEST_F(ApplierContactgroup, ContactRemove) {
configuration::applier::contact aply;
configuration::applier::contactgroup aply_grp;
configuration::contactgroup grp("test_group");

configuration::contact ctct("test");
aply.add_object(ctct);

configuration::contact ctct2("test2");
aply.add_object(ctct2);


grp.parse("members", "test, test2");
aply_grp.add_object(grp);
aply_grp.expand_objects(*config);
aply_grp.resolve_object(grp);
ASSERT_EQ(engine::contactgroup::contactgroups["test_group"]->get_members().size(), 2u);

aply.remove_object(ctct2);
ASSERT_EQ(engine::contactgroup::contactgroups["test_group"]->get_members().size(), 1u);
grp.parse("members", "test");
aply_grp.modify_object(grp);
}
40 changes: 40 additions & 0 deletions tests/configuration/applier/applier-hostgroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,43 @@ TEST_F(ApplierHostGroup, HostRenamed) {
ASSERT_EQ(engine::hostgroup::hostgroups.begin()->second->get_group_name(),
"temp_hg");
}

TEST_F(ApplierHostGroup, HostRemoved) {
configuration::applier::hostgroup hg_aply;
configuration::applier::host hst_aply;
configuration::hostgroup hg;
configuration::host hst_a;
configuration::host hst_c;

ASSERT_TRUE(hst_a.parse("host_name", "a"));
ASSERT_TRUE(hst_a.parse("address", "127.0.0.1"));
ASSERT_TRUE(hst_a.parse("_HOST_ID", "1"));

ASSERT_TRUE(hst_c.parse("host_name", "c"));
ASSERT_TRUE(hst_c.parse("address", "127.0.0.1"));
ASSERT_TRUE(hst_c.parse("_HOST_ID", "2"));

hst_aply.add_object(hst_a);
hst_aply.add_object(hst_c);

ASSERT_TRUE(hg.parse("hostgroup_name", "temphg"));
ASSERT_TRUE(hg.parse("members", "a,c"));
ASSERT_NO_THROW(hg_aply.add_object(hg));

ASSERT_NO_THROW(hst_aply.expand_objects(*config));
ASSERT_NO_THROW(hst_aply.expand_objects(*config));
ASSERT_NO_THROW(hg_aply.expand_objects(*config));

ASSERT_NO_THROW(hst_aply.resolve_object(hst_a));
ASSERT_NO_THROW(hst_aply.resolve_object(hst_c));
ASSERT_NO_THROW(hg_aply.resolve_object(hg));

engine::hostgroup *hg_obj{engine::hostgroup::hostgroups["temphg"].get()};
ASSERT_EQ(hg_obj->members.size(), 2u);
ASSERT_NO_THROW(hst_aply.remove_object(hst_a));
ASSERT_EQ(hg_obj->members.size(), 1u);

ASSERT_TRUE(hg.parse("members", "c"));
ASSERT_NO_THROW(hg_aply.modify_object(hg));

}
59 changes: 59 additions & 0 deletions tests/configuration/applier/applier-servicegroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,62 @@ TEST_F(ApplierServicegroup, RemoveServicegroupFromConfig) {
aply_grp.remove_object(grp);
ASSERT_EQ(engine::servicegroup::servicegroups.size(), 1u);
}

// Given a servicegroup applier
// And a configuration servicegroup in configuration
// When we remove the configuration
// Then it is really removed
TEST_F(ApplierServicegroup, RemoveServiceFromGroup) {
configuration::applier::host aply_hst;
configuration::applier::service aply_svc;
configuration::applier::command aply_cmd;
configuration::applier::servicegroup aply_grp;
configuration::servicegroup grp("test_group");

configuration::command cmd("cmd");
cmd.parse("command_line", "echo 1");
aply_cmd.add_object(cmd);

configuration::host hst;
ASSERT_TRUE(hst.parse("host_name", "test_host"));
ASSERT_TRUE(hst.parse("address", "127.0.0.1"));
ASSERT_TRUE(hst.parse("_HOST_ID", "12"));
aply_hst.add_object(hst);

configuration::service svc;
ASSERT_TRUE(svc.parse("service_description", "test"));
ASSERT_TRUE(svc.parse("hosts", "test_host"));
ASSERT_TRUE(svc.parse("service_id", "18"));
svc.parse("check_command", "cmd");
// We fake here the expand_object on configuration::service
svc.set_host_id(12);
aply_svc.add_object(svc);
ASSERT_TRUE(svc.parse("servicegroups", "test_group"));

configuration::service svc2;
ASSERT_TRUE(svc.parse("service_description", "test2"));
ASSERT_TRUE(svc.parse("hosts", "test_host"));
ASSERT_TRUE(svc.parse("service_id", "19"));
svc.parse("check_command", "cmd");
// We fake here the expand_object on configuration::service
svc.set_host_id(12);
aply_svc.add_object(svc);
ASSERT_TRUE(svc.parse("servicegroups", "test_group"));


grp.parse("members", "test_host,test,test_host,test2");
aply_grp.add_object(grp);
aply_grp.expand_objects(*config);
aply_grp.resolve_object(grp);
ASSERT_TRUE(grp.members().size() == 2);

engine::servicegroup *sg{engine::servicegroup::servicegroups["test_group"].get()};
ASSERT_EQ(sg->members.size(), 2u);
aply_svc.remove_object(svc);
ASSERT_EQ(sg->members.size(), 1u);

grp.parse("members", "test_host,test,test_host,test2");
aply_grp.modify_object(grp);

ASSERT_EQ(engine::servicegroup::servicegroups.size(), 1u);
}

0 comments on commit 676ebc6

Please sign in to comment.