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
26 from shinken_test
import *
27 from shinken
.objects
.timeperiod
import Timeperiod
30 class TestConfig(ShinkenTest
):
32 self
.setup_with_file('etc/nagios_maintenance_period.cfg')
35 def test_check_defined_maintenance_period(self
):
36 a_24_7
= self
.sched
.timeperiods
.find_by_name("24x7")
37 print "Get the hosts and services"
38 test_router_0
= self
.sched
.hosts
.find_by_name("test_router_0")
39 test_host_0
= self
.sched
.hosts
.find_by_name("test_host_0")
40 test_nobody
= self
.sched
.hosts
.find_by_name("test_nobody")
42 svc1
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
43 svc2
= self
.sched
.services
.find_srv_by_name_and_hostname("test_router_0", "test_ok_0")
44 svc3
= self
.sched
.services
.find_srv_by_name_and_hostname("test_nobody", "test_ok_0")
47 self
.assert_(test_router_0
.maintenance_period
== a_24_7
)
48 self
.assert_(test_host_0
.maintenance_period
is None)
49 self
.assert_(test_nobody
.maintenance_period
is None)
51 #Now inplicit inheritance
52 #This one is defined in the service conf
53 self
.assert_(svc1
.maintenance_period
== a_24_7
)
54 #This one is empty, because maintenance_period are not inherited from the
55 #host like check/notification_periods
56 self
.assert_(svc2
.maintenance_period
is None)
57 #This one got nothing :)
58 self
.assert_(svc3
.maintenance_period
is None)
61 def test_check_enter_downtime(self
):
62 test_router_0
= self
.sched
.hosts
.find_by_name("test_router_0")
63 test_host_0
= self
.sched
.hosts
.find_by_name("test_host_0")
64 test_nobody
= self
.sched
.hosts
.find_by_name("test_nobody")
66 svc1
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
67 svc2
= self
.sched
.services
.find_srv_by_name_and_hostname("test_router_0", "test_ok_0")
68 svc3
= self
.sched
.services
.find_srv_by_name_and_hostname("test_nobody", "test_ok_0")
69 # we want to focus on only one maintenance
70 test_router_0
.maintenance_period
= None
71 test_host_0
.maintenance_period
= None
72 test_nobody
.maintenance_period
= None
73 svc1
.maintenance_period
= None
74 svc2
.maintenance_period
= None
76 # be sure we have some time before a new minute begins.
77 # otherwise we get a race condition and a failed test here.
84 print "now it is", time
.asctime(time
.localtime(now
))
85 nowday
= time
.strftime("%A", time
.localtime(now
+ 60)).lower()
86 soonstart
= time
.strftime("%H:%M", time
.localtime(now
+ 60))
87 soonend
= time
.strftime("%H:%M", time
.localtime(now
+ 180))
89 range = "%s %s-%s" % (nowday
, soonstart
, soonend
)
90 print "range is ", range
92 t
.timeperiod_name
= ''
93 t
.resolve_daterange(t
.dateranges
, range)
94 t_next
= t
.get_next_valid_time_from_t(now
)
95 print "planned start", time
.asctime(time
.localtime(t_next
))
96 t_next
= t
.get_next_invalid_time_from_t(t_next
+ 1)
97 print "planned stop ", time
.asctime(time
.localtime(t_next
))
98 svc3
.maintenance_period
= t
100 self
.assert_(not hasattr(svc3
, 'in_maintenance'))
102 # now let the scheduler run and wait until the maintenance period begins
104 self
.scheduler_loop(3, [[svc3
, 0, 'OK']], do_sleep
=True)
106 self
.assert_(hasattr(svc3
, 'in_maintenance'))
107 self
.assert_(len(self
.sched
.downtimes
) == 1)
108 self
.assert_(len(svc3
.downtimes
) == 1)
109 self
.assert_(svc3
.downtimes
[0] in self
.sched
.downtimes
.values())
110 self
.assert_(svc3
.in_scheduled_downtime
)
111 self
.assert_(svc3
.downtimes
[0].fixed
)
112 self
.assert_(svc3
.downtimes
[0].is_in_effect
)
113 self
.assert_(not svc3
.downtimes
[0].can_be_deleted
)
114 self
.assert_(svc3
.in_maintenance
== svc3
.downtimes
[0].id)
117 # now the downtime should expire...
119 self
.scheduler_loop(3, [[svc3
, 0, 'OK']], do_sleep
=True)
121 self
.assert_(len(self
.sched
.downtimes
) == 0)
122 self
.assert_(len(svc3
.downtimes
) == 0)
123 self
.assert_(not svc3
.in_scheduled_downtime
)
124 self
.assert_(svc3
.in_maintenance
== False)
128 if __name__
== '__main__':