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
27 from shinken_test
import *
30 class TestConfig(ShinkenTest
):
31 #setUp is in shinken_test
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
):
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
):
50 state
= hst
.__getstate
__()
51 #Check it's the good lenght
52 self
.assert_(len(state
) == len(cls
.properties
) + len(cls
.running_properties
) + 1)
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
):
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
81 #no contacts with notification enabled is a problem
82 hst
.notifications_enabled
= True
83 contacts
= hst
.contacts
84 contact_groups
= hst
.contact_groups
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)
98 check_command
= 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)
114 self
.assert_(hst
.is_correct() == False)
116 self
.assert_(hst
.is_correct() == True)
119 #Look for set/unset impacted states (unknown)
120 def test_impact_state(self
):
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
):
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)
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)
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)
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
)
181 self
.assert_(r
in h
.parents
)
182 print "r.childs", r
.childs
183 print "h.childs", h
.childs
185 if __name__
== '__main__':