Fix: (Grégory Starck) french name in variables.
[shinken.git] / test / test_hosts.py
bloba877d4b86392157f26c3beeab5821cb515519784
1 #!/usr/bin/env python2.6
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 # This file is used to test reading and processing of config files
25 import copy
26 #It's ugly I know....
27 from shinken_test import *
30 class TestConfig(ShinkenTest):
31 #setUp is in shinken_test
33 def get_hst(self):
34 return self.sched.hosts.find_by_name("test_host_0")
37 #Look if get_*_name return the good result
38 def test_get_name(self):
39 hst = self.get_hst()
40 print hst.get_dbg_name()
41 self.assert_(hst.get_name() == 'test_host_0')
42 self.assert_(hst.get_dbg_name() == 'test_host_0')
44 #getstate should be with all properties in dict class + id
45 #check also the setstate
46 def test___getstate__(self):
47 hst = self.get_hst()
48 cls = hst.__class__
49 #We get teh state
50 state = hst.__getstate__()
51 #Check it's the good lenght
52 self.assert_(len(state) == len(cls.properties) + len(cls.running_properties) + 1)
53 #we copy the service
54 hst_copy = copy.copy(hst)
55 #reset the state in the original service
56 hst.__setstate__(state)
57 #And it should be the same :then before :)
58 for p in cls.properties:
59 # print getattr(hst_copy, p)
60 # print getattr(hst, p)
61 self.assert_(getattr(hst_copy, p) == getattr(hst, p))
64 #Look if it can detect all incorrect cases
65 def test_is_correct(self):
66 hst = self.get_hst()
68 #first it's ok
69 self.assert_(hst.is_correct() == True)
71 #Now try to delete a required property
72 max_check_attempts = hst.max_check_attempts
73 del hst.max_check_attempts
74 self.assert_(hst.is_correct() == False)
75 hst.max_check_attempts = max_check_attempts
77 ###
78 ### Now special cases
79 ###
81 #no contacts with notification enabled is a problem
82 hst.notifications_enabled = True
83 contacts = hst.contacts
84 contact_groups = hst.contact_groups
85 del hst.contacts
86 del hst.contact_groups
87 self.assert_(hst.is_correct() == False)
88 #and with disabled it's ok
89 hst.notifications_enabled = False
90 self.assert_(hst.is_correct() == True)
91 hst.contacts = contacts
92 hst.contact_groups = contact_groups
94 hst.notifications_enabled = True
95 self.assert_(hst.is_correct() == True)
97 #no check command
98 check_command = hst.check_command
99 del hst.check_command
100 self.assert_(hst.is_correct() == False)
101 hst.check_command = check_command
102 self.assert_(hst.is_correct() == True)
104 #no notification_interval
105 notification_interval = hst.notification_interval
106 del hst.notification_interval
107 self.assert_(hst.is_correct() == False)
108 hst.notification_interval = notification_interval
109 self.assert_(hst.is_correct() == True)
111 #no realm
112 realm = hst.realm
113 del hst.realm
114 self.assert_(hst.is_correct() == False)
115 hst.realm = realm
116 self.assert_(hst.is_correct() == True)
119 #Look for set/unset impacted states (unknown)
120 def test_impact_state(self):
121 hst = self.get_hst()
122 ori_state = hst.state
123 ori_state_id = hst.state_id
124 hst.set_impact_state()
125 self.assert_(hst.state == 'UNREACHABLE')
126 self.assert_(hst.state_id == 2)
127 hst.unset_impact_state()
128 self.assert_(hst.state == ori_state)
129 self.assert_(hst.state_id == ori_state_id)
131 def test_set_state_from_exit_status(self):
132 hst = self.get_hst()
133 #First OK
134 hst.set_state_from_exit_status(0)
135 self.assert_(hst.state == 'UP')
136 self.assert_(hst.state_id == 0)
137 self.assert_(hst.is_state('UP') == True)
138 self.assert_(hst.is_state('o') == True)
139 #Then warning
140 hst.set_state_from_exit_status(1)
141 self.assert_(hst.state == 'DOWN')
142 self.assert_(hst.state_id == 1)
143 self.assert_(hst.is_state('DOWN') == True)
144 self.assert_(hst.is_state('d') == True)
145 #Then Critical
146 hst.set_state_from_exit_status(2)
147 self.assert_(hst.state == 'DOWN')
148 self.assert_(hst.state_id == 1)
149 self.assert_(hst.is_state('DOWN') == True)
150 self.assert_(hst.is_state('d') == True)
151 #And unknown
152 hst.set_state_from_exit_status(3)
153 self.assert_(hst.state == 'DOWN')
154 self.assert_(hst.state_id == 1)
155 self.assert_(hst.is_state('DOWN') == True)
156 self.assert_(hst.is_state('d') == True)
158 #And something else :)
159 hst.set_state_from_exit_status(99)
160 self.assert_(hst.state == 'DOWN')
161 self.assert_(hst.state_id == 1)
162 self.assert_(hst.is_state('DOWN') == True)
163 self.assert_(hst.is_state('d') == True)
166 def test_hostgroup(self):
167 hg = self.sched.hostgroups.find_by_name("hostgroup_01")
168 self.assert_(hg != None)
169 h = self.sched.hosts.find_by_name('test_host_0')
170 self.assert_(h in hg.members)
171 self.assert_(hg in h.hostgroups)
174 def test_childs(self):
175 h = self.sched.hosts.find_by_name('test_host_0')
176 r = self.sched.hosts.find_by_name('test_router_0')
178 #Search if h is in r.childs
179 self.assert_(h in r.childs)
180 #and the reverse
181 self.assert_(r in h.parents)
182 print "r.childs", r.childs
183 print "h.childs", h.childs
185 if __name__ == '__main__':
186 unittest.main()