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
82 check_command
= hst
.check_command
84 self
.assert_(hst
.is_correct() == False)
85 hst
.check_command
= check_command
86 self
.assert_(hst
.is_correct() == True)
88 #no notification_interval
89 notification_interval
= hst
.notification_interval
90 del hst
.notification_interval
91 self
.assert_(hst
.is_correct() == False)
92 hst
.notification_interval
= notification_interval
93 self
.assert_(hst
.is_correct() == True)
98 self
.assert_(hst
.is_correct() == False)
100 self
.assert_(hst
.is_correct() == True)
103 #Look for set/unset impacted states (unknown)
104 def test_impact_state(self
):
106 ori_state
= hst
.state
107 ori_state_id
= hst
.state_id
108 hst
.set_impact_state()
109 self
.assert_(hst
.state
== 'UNREACHABLE')
110 self
.assert_(hst
.state_id
== 2)
111 hst
.unset_impact_state()
112 self
.assert_(hst
.state
== ori_state
)
113 self
.assert_(hst
.state_id
== ori_state_id
)
115 def test_set_state_from_exit_status(self
):
118 hst
.set_state_from_exit_status(0)
119 self
.assert_(hst
.state
== 'UP')
120 self
.assert_(hst
.state_id
== 0)
121 self
.assert_(hst
.is_state('UP') == True)
122 self
.assert_(hst
.is_state('o') == True)
124 hst
.set_state_from_exit_status(1)
125 self
.assert_(hst
.state
== 'DOWN')
126 self
.assert_(hst
.state_id
== 1)
127 self
.assert_(hst
.is_state('DOWN') == True)
128 self
.assert_(hst
.is_state('d') == True)
130 hst
.set_state_from_exit_status(2)
131 self
.assert_(hst
.state
== 'DOWN')
132 self
.assert_(hst
.state_id
== 1)
133 self
.assert_(hst
.is_state('DOWN') == True)
134 self
.assert_(hst
.is_state('d') == True)
136 hst
.set_state_from_exit_status(3)
137 self
.assert_(hst
.state
== 'DOWN')
138 self
.assert_(hst
.state_id
== 1)
139 self
.assert_(hst
.is_state('DOWN') == True)
140 self
.assert_(hst
.is_state('d') == True)
142 #And something else :)
143 hst
.set_state_from_exit_status(99)
144 self
.assert_(hst
.state
== 'DOWN')
145 self
.assert_(hst
.state_id
== 1)
146 self
.assert_(hst
.is_state('DOWN') == True)
147 self
.assert_(hst
.is_state('d') == True)
150 def test_hostgroup(self
):
151 hg
= self
.sched
.hostgroups
.find_by_name("hostgroup_01")
152 self
.assert_(hg
is not None)
153 h
= self
.sched
.hosts
.find_by_name('test_host_0')
154 self
.assert_(h
in hg
.members
)
155 self
.assert_(hg
in h
.hostgroups
)
158 def test_childs(self
):
159 h
= self
.sched
.hosts
.find_by_name('test_host_0')
160 r
= self
.sched
.hosts
.find_by_name('test_router_0')
162 #Search if h is in r.childs
163 self
.assert_(h
in r
.childs
)
165 self
.assert_(r
in h
.parents
)
166 print "r.childs", r
.childs
167 print "h.childs", h
.childs
169 # And also in the parent/childs dep list
170 self
.assert_(h
in r
.child_dependencies
)
172 self
.assert_(r
in h
.parent_dependencies
)
175 if __name__
== '__main__':