Add : service without host will be just droped, like Nagios.
[shinken.git] / test / test_system_time_change.py
blob37883cc566bb94961211e889f92242be4f663764
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 #It's ugly I know....
26 from shinken_test import *
27 import commands
29 class TestConfig(ShinkenTest):
30 #setUp is in shinken_test
32 def set_time(self, d):
33 cmd = 'sudo date -s "%s"' % d
34 print "CMD,", cmd
35 # NB: disabled for now because we test in a totally direct way
36 #a = commands.getstatusoutput(cmd)
37 #Check the time is set correctly!
38 #self.assert_(a[0] == 0)
42 #Change ME :)
43 def test_dummy(self):
45 # Config is not correct because of a wrong relative path
46 # in the main config file
48 print "Get the hosts and services"
49 host = self.sched.hosts.find_by_name("test_host_0")
50 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
51 now = time.time()
52 now_str = time.asctime(time.localtime(now))
53 print "Now:", now
54 print "Now:", time.asctime(time.localtime(now))
55 tomorow = time.asctime(time.localtime(now+86400))
56 yesterday = time.asctime(time.localtime(now-86400))
58 host.schedule()
59 host_check = host.actions[0]
61 svc.schedule()
62 srv_check = svc.actions[0]
63 print "Service check", srv_check, time.asctime(time.localtime(srv_check.t_to_go))
65 print "Current Host last_state_change", time.asctime(time.localtime(host.last_state_change))
67 #Ok, start to check for bad time
68 self.set_time(tomorow)
69 last_state_change = host.last_state_change
70 host.compensate_system_time_change(86400)
71 self.assert_(host.last_state_change - last_state_change == 86400)
72 svc.compensate_system_time_change(86400)
73 print "Tomorow Host last_state_change", time.asctime(time.localtime(host.last_state_change))
75 #And now a huge change : yesterday (so a 2 day move)
76 self.set_time(yesterday)
77 last_state_change = host.last_state_change
78 host.compensate_system_time_change(-86400 * 2)
79 self.assert_(host.last_state_change - last_state_change == -86400*2)
80 svc.compensate_system_time_change(-86400*2)
81 print "Yesterday Host last_state_change", time.asctime(time.localtime(host.last_state_change))
83 self.set_time(now_str)
85 #Ok, now the scheduler and check things
86 #Put checks in the scheduler
87 self.sched.get_new_actions()
89 host_to_go = host_check.t_to_go
90 srv_to_go = srv_check.t_to_go
91 print "current Host check", time.asctime(time.localtime(host_check.t_to_go))
92 print "current Service check", time.asctime(time.localtime(srv_check.t_to_go))
93 self.set_time(tomorow)
94 self.sched.sched_daemon.compensate_system_time_change(86400)
95 print "Tomorow Host check", time.asctime(time.localtime(host_check.t_to_go))
96 print "Tomorow Service check", time.asctime(time.localtime(srv_check.t_to_go))
97 self.assert_(host_check.t_to_go - host_to_go == 86400)
98 self.assert_(srv_check.t_to_go - srv_to_go == 86400)
100 #and yesterday
101 host_to_go = host_check.t_to_go
102 srv_to_go = srv_check.t_to_go
103 self.set_time(yesterday)
104 self.sched.sched_daemon.compensate_system_time_change(-86400*2)
105 print "Yesterday Host check", time.asctime(time.localtime(host_check.t_to_go))
106 print "Yesterday Service check", time.asctime(time.localtime(srv_check.t_to_go))
107 print "New host check", time.asctime(time.localtime(host.next_chk))
108 self.assert_(host.next_chk == host_check.t_to_go)
109 self.assert_(svc.next_chk == srv_check.t_to_go)
110 self.assert_(host_check.t_to_go - host_to_go == -86400*2)
111 self.assert_(srv_check.t_to_go - srv_to_go == -86400*2)
113 self.set_time(now_str)
117 if __name__ == '__main__':
118 unittest.main()