From 3078392d288a1af949782cfa5a0e88fe8051eba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20MOHIER?= Date: Mon, 12 Sep 2016 14:05:39 +0200 Subject: [PATCH] Hostgroup members (#110) and tests refactoring (2) Remove old test test_groups_pickle --- alignak/objects/contactgroup.py | 1 + alignak/objects/servicegroup.py | 12 +-- test/_old/etc/alignak_groups_pickle.cfg | 56 ------------ test/_old/test_groups_pickle.py | 90 ------------------- .../hostgroup/alignak_hostgroup_members.cfg | 2 +- test/test_hostgroup.py | 57 +++++++++++- 6 files changed, 64 insertions(+), 154 deletions(-) delete mode 100644 test/_old/etc/alignak_groups_pickle.cfg delete mode 100644 test/_old/test_groups_pickle.py diff --git a/alignak/objects/contactgroup.py b/alignak/objects/contactgroup.py index 02639dd11..ad21b7148 100644 --- a/alignak/objects/contactgroup.py +++ b/alignak/objects/contactgroup.py @@ -71,6 +71,7 @@ class Contactgroup(Itemgroup): 'uuid': StringProp(default='', fill_brok=['full_status']), 'contactgroup_name': StringProp(fill_brok=['full_status']), 'alias': StringProp(fill_brok=['full_status']), + 'contactgroup_members': StringProp(fill_brok=['full_status']), }) macros = { diff --git a/alignak/objects/servicegroup.py b/alignak/objects/servicegroup.py index ae7fe42cb..1f4710177 100644 --- a/alignak/objects/servicegroup.py +++ b/alignak/objects/servicegroup.py @@ -66,13 +66,13 @@ class Servicegroup(Itemgroup): properties = Itemgroup.properties.copy() properties.update({ - 'uuid': StringProp(default='', fill_brok=['full_status']), - 'servicegroup_name': StringProp(fill_brok=['full_status']), - 'alias': StringProp(fill_brok=['full_status']), - 'notes': StringProp(default='', fill_brok=['full_status']), - 'notes_url': StringProp(default='', fill_brok=['full_status']), - 'action_url': StringProp(default='', fill_brok=['full_status']), + 'uuid': StringProp(default='', fill_brok=['full_status']), + 'servicegroup_name': StringProp(fill_brok=['full_status']), + 'alias': StringProp(fill_brok=['full_status']), 'servicegroup_members': StringProp(default='', fill_brok=['full_status']), + 'notes': StringProp(default='', fill_brok=['full_status']), + 'notes_url': StringProp(default='', fill_brok=['full_status']), + 'action_url': StringProp(default='', fill_brok=['full_status']), }) macros = { diff --git a/test/_old/etc/alignak_groups_pickle.cfg b/test/_old/etc/alignak_groups_pickle.cfg deleted file mode 100644 index f2942618d..000000000 --- a/test/_old/etc/alignak_groups_pickle.cfg +++ /dev/null @@ -1,56 +0,0 @@ - -define realm{ - realm_name World - default 1 - realm_members R1, R2 -} - - -define realm{ - realm_name R1 -} - -define realm{ - realm_name R2 -} - -define scheduler { - scheduler_name R1 - address localhost - realm R1 -} - - -define scheduler { - scheduler_name world - address localhsot - realm World -} - -define scheduler { - scheduler_name R2 - address localhsot - realm R2 -} - - -define hostgroup{ - hostgroup_name everyone - members * -} - - -define host{ - use generic-host - host_name HR1 - realm R1 - hostgroups everyone -} - - -define host{ - use generic-host - host_name HR2 - realm R2 - hostgroups everyone -} diff --git a/test/_old/test_groups_pickle.py b/test/_old/test_groups_pickle.py deleted file mode 100644 index fc5165828..000000000 --- a/test/_old/test_groups_pickle.py +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (C) 2015-2015: Alignak team, see AUTHORS.txt file for contributors -# -# This file is part of Alignak. -# -# Alignak is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Alignak is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Alignak. If not, see . -# -# -# This file incorporates work covered by the following copyright and -# permission notice: -# -# Copyright (C) 2009-2010: -# Jean Gabes, naparuba@gmail.com - -# This file is part of Shinken. -# -# Shinken is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Shinken is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Shinken. If not, see . - -# -# This file is used to test reading and processing of config files -# - -from alignak_test import * - - -class TestConfig(AlignakTest): - - def setUp(self): - self.setup_with_file(['etc/alignak_groups_pickle.cfg']) - - def test_dispatch(self): - - - sub_confs = self.conf.confs - print "NB SUB CONFS", len(sub_confs) - - vcfg = None - # Find where hr1 is - for cfg in sub_confs.values(): - if 'HR1' in [h.get_name() for h in cfg.hosts]: - print 'FOUNCED', len(cfg.hosts) - vcfg = cfg - - - # Look ifthe hg in the conf is valid - vhg = vcfg.hostgroups.find_by_name('everyone') - self.assert_(len(vhg.members) == 1) - - hr1 = [h for h in vcfg.hosts if h.get_name() == "HR1"][0] - print hr1.hostgroups - hg1 = None - for hg in hr1.hostgroups: - if vcfg.hostgroups[hg].get_name() == 'everyone': - hg1 = vcfg.hostgroups[hg] - - - - print "Founded hostgroup", hg1 - print 'There should be only one host there' - self.assert_(len(hg1.members) == 1) - print 'and should be the same than the vcfg one!' - self.assert_(hg1 == vhg) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/cfg/hostgroup/alignak_hostgroup_members.cfg b/test/cfg/hostgroup/alignak_hostgroup_members.cfg index ab4615fd1..ec5a906fc 100755 --- a/test/cfg/hostgroup/alignak_hostgroup_members.cfg +++ b/test/cfg/hostgroup/alignak_hostgroup_members.cfg @@ -4,5 +4,5 @@ define hostgroup { hostgroup_name allhosts_and_groups alias All Hosts and groups members test_router_0,test_host_0 - hostgroup_members router, hostgroup_01, hostgroup_02, hostgroup_03, hostgroup_04 + hostgroup_members hostgroup_01, hostgroup_02, hostgroup_03, hostgroup_04 } diff --git a/test/test_hostgroup.py b/test/test_hostgroup.py index 9d6f3536d..2cf4babf9 100755 --- a/test/test_hostgroup.py +++ b/test/test_hostgroup.py @@ -26,6 +26,7 @@ import time +from alignak.objects import Host from alignak.objects import Hostgroup from alignak_test import AlignakTest @@ -51,6 +52,8 @@ class TestHostGroupMembers(AlignakTest): def test_hostgroup_members(self): """ + Test if members are linked from group + :return: None """ self.print_header() @@ -67,9 +70,61 @@ def test_hostgroup_members(self): 2 ) - self.assertEqual(len(hg.get_hostgroup_members()), 5) + self.assertEqual(len(hg.get_hostgroup_members()), 4) + + self.assertEqual(len(hg.get_hosts()), 2) + + def test_members_hostgroup(self): + """ + Test if group is linked from the member + :return: None + """ + self.print_header() + self.setup_with_file('cfg/hostgroup/alignak_hostgroup_members.cfg') + self.assertTrue(self.schedulers[0].conf.conf_is_correct) + + #  Found a hostgroup named allhosts_and_groups + hg = self.schedulers[0].sched.hostgroups.find_by_name("allhosts_and_groups") + self.assertIsInstance(hg, Hostgroup) + self.assertEqual(hg.get_name(), "allhosts_and_groups") + + self.assertEqual( + len(self.schedulers[0].sched.hostgroups.get_members_by_name("allhosts_and_groups")), + 2 + ) self.assertEqual(len(hg.get_hosts()), 2) + print("List hostgroup hosts:") + for host_id in hg.members: + host = self.schedulers[0].sched.hosts[host_id] + print("Host: %s" % host) + self.assertIsInstance(host, Host) + + if host.get_name() == 'test_router_0': + self.assertEqual(len(host.get_hostgroups()), 3) + for group_id in host.hostgroups: + group = self.schedulers[0].sched.hostgroups[group_id] + print("Group: %s" % group) + self.assertIn(group.get_name(), [ + 'router', 'allhosts', 'allhosts_and_groups' + ]) + + if host.get_name() == 'test_host_0': + self.assertEqual(len(host.get_hostgroups()), 4) + for group_id in host.hostgroups: + group = self.schedulers[0].sched.hostgroups[group_id] + print("Group: %s" % group) + self.assertIn(group.get_name(), [ + 'allhosts', 'allhosts_and_groups', 'up', 'hostgroup_01' + ]) + + print("List hostgroup groups:") + self.assertEqual(len(hg.get_hostgroup_members()), 4) + for group in hg.get_hostgroup_members(): + print("Group: %s" % group) + self.assertIn(group, [ + 'hostgroup_01', 'hostgroup_02', 'hostgroup_03', 'hostgroup_04' + ]) class TestHostGroupNoHost(AlignakTest):