Fix : fix hot module under windows test. (at leat I hope...)
[shinken.git] / test / test_external_commands.py
blob854c73208993d2721fbfece8feb38ec11620ac95
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 os
29 class TestConfig(ShinkenTest):
30 #setUp is in shinken_test
32 def send_cmd(self, line):
33 s = '[%d] %s\n' % (int(time.time()), line)
34 print "Writing %s in %s" % (s, self.conf.command_file)
35 fd = open(self.conf.command_file, 'wb')
36 fd.write(s)
37 fd.close()
40 #Change ME :)
41 def test_external_comand(self):
42 now = time.time()
43 host = self.sched.hosts.find_by_name("test_host_0")
44 router = self.sched.hosts.find_by_name("test_router_0")
45 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
46 self.scheduler_loop(2, [[host, 0, 'UP | value1=1 value2=2'], [router, 0, 'UP | rtt=10'], [svc, 2, 'BAD | value1=0 value2=0']])
47 self.assert_(host.state == 'UP')
48 self.assert_(host.state_type == 'HARD')
50 excmd = '[%d] PROCESS_HOST_CHECK_RESULT;test_host_0;2;Bob is not happy' % int(time.time())
51 self.sched.run_external_command(excmd)
52 self.scheduler_loop(1, [])
53 self.scheduler_loop(1, []) #Need 2 run for get then consume)
54 self.assert_(host.state == 'DOWN')
55 self.assert_(host.output == 'Bob is not happy')
57 # Now with performance data
58 excmd = '[%d] PROCESS_HOST_CHECK_RESULT;test_host_0;2;Bob is not happy|rtt=9999' % int(time.time())
59 self.sched.run_external_command(excmd)
60 self.scheduler_loop(1, [])
61 self.scheduler_loop(1, []) #Need 2 run for get then consume)
62 self.assert_(host.state == 'DOWN')
63 self.assert_(host.output == 'Bob is not happy')
64 self.assert_(host.perf_data == 'rtt=9999')
66 # Now with full-blown performance data. Here we have to watch out:
67 # Is a ";" a separator for the external command or is it
68 # part of the performance data?
69 excmd = '[%d] PROCESS_HOST_CHECK_RESULT;test_host_0;2;Bob is not happy|rtt=9999;5;10;0;10000' % int(time.time())
70 self.sched.run_external_command(excmd)
71 self.scheduler_loop(1, [])
72 self.scheduler_loop(1, []) #Need 2 run for get then consume)
73 self.assert_(host.state == 'DOWN')
74 self.assert_(host.output == 'Bob is not happy')
75 print "perf (%s)" % host.perf_data
76 self.assert_(host.perf_data == 'rtt=9999;5;10;0;10000')
78 # The same with a service
79 excmd = '[%d] PROCESS_SERVICE_CHECK_RESULT;test_host_0;test_ok_0;1;Bobby is not happy|rtt=9999;5;10;0;10000' % int(time.time())
80 self.sched.run_external_command(excmd)
81 self.scheduler_loop(1, [])
82 self.scheduler_loop(1, []) #Need 2 run for get then consume)
83 self.assert_(svc.state == 'WARNING')
84 self.assert_(svc.output == 'Bobby is not happy')
85 print "perf (%s)" % svc.perf_data
86 self.assert_(svc.perf_data == 'rtt=9999;5;10;0;10000')
88 #Clean the command_file
89 #try:
90 # os.unlink(self.conf.command_file)
91 #except :
92 # pass
95 if __name__ == '__main__':
96 unittest.main()