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 *
29 class TestConfig(ShinkenTest
):
30 #setUp is in shinken_test
32 def set_time(self
, d
):
33 cmd
= 'sudo date -s "%s"' % d
35 #a = commands.getstatusoutput(cmd)
36 #Check the time is set correctly!
37 #self.assert_(a[0] == 0)
44 # Config is not correct because of a wrong relative path
45 # in the main config file
47 print "Get the hosts and services"
48 host
= self
.sched
.hosts
.find_by_name("test_host_0")
49 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
51 now_str
= time
.asctime(time
.localtime(now
))
53 print "Now:", time
.asctime(time
.localtime(now
))
54 tomorow
= time
.asctime(time
.localtime(now
+86400))
55 yesterday
= time
.asctime(time
.localtime(now
-86400))
58 host_check
= host
.actions
[0]
61 srv_check
= svc
.actions
[0]
62 print "Service check", srv_check
, time
.asctime(time
.localtime(srv_check
.t_to_go
))
64 print "Current Host last_state_change", time
.asctime(time
.localtime(host
.last_state_change
))
66 #Ok, start to check for bad time
67 self
.set_time(tomorow
)
68 last_state_change
= host
.last_state_change
69 host
.compensate_system_time_change(86400)
70 self
.assert_(host
.last_state_change
- last_state_change
== 86400)
71 svc
.compensate_system_time_change(86400)
72 print "Tomorow Host last_state_change", time
.asctime(time
.localtime(host
.last_state_change
))
74 #And now a huge change : yesterday (so a 2 day move)
75 self
.set_time(yesterday
)
76 last_state_change
= host
.last_state_change
77 host
.compensate_system_time_change(-86400 * 2)
78 self
.assert_(host
.last_state_change
- last_state_change
== -86400*2)
79 svc
.compensate_system_time_change(-86400*2)
80 print "Yesterday Host last_state_change", time
.asctime(time
.localtime(host
.last_state_change
))
82 self
.set_time(now_str
)
84 #Ok, now the scheduler and check things
85 #Put checks in the scheduler
86 self
.sched
.get_new_actions()
88 host_to_go
= host_check
.t_to_go
89 srv_to_go
= srv_check
.t_to_go
90 print "current Host check", time
.asctime(time
.localtime(host_check
.t_to_go
))
91 print "current Service check", time
.asctime(time
.localtime(srv_check
.t_to_go
))
92 self
.set_time(tomorow
)
93 self
.sched
.compensate_system_time_change(86400)
94 print "Tomorow Host check", time
.asctime(time
.localtime(host_check
.t_to_go
))
95 print "Tomorow Service check", time
.asctime(time
.localtime(srv_check
.t_to_go
))
96 self
.assert_(host_check
.t_to_go
- host_to_go
== 86400)
97 self
.assert_(srv_check
.t_to_go
- srv_to_go
== 86400)
100 host_to_go
= host_check
.t_to_go
101 srv_to_go
= srv_check
.t_to_go
102 self
.set_time(yesterday
)
103 self
.sched
.compensate_system_time_change(-86400*2)
104 print "Yesterday Host check", time
.asctime(time
.localtime(host_check
.t_to_go
))
105 print "Yesterday Service check", time
.asctime(time
.localtime(srv_check
.t_to_go
))
106 print "New host check", time
.asctime(time
.localtime(host
.next_chk
))
107 self
.assert_(host
.next_chk
== host_check
.t_to_go
)
108 self
.assert_(svc
.next_chk
== srv_check
.t_to_go
)
109 self
.assert_(host_check
.t_to_go
- host_to_go
== -86400*2)
110 self
.assert_(srv_check
.t_to_go
- srv_to_go
== -86400*2)
112 self
.set_time(now_str
)
116 if __name__
== '__main__':