Fix : fix hot module under windows test. (at leat I hope...)
[shinken.git] / test / test_module_service_perfdata.py
blob0d6cb86aaf7ad755ce0ccc1b59a154ec31246ad3
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/>.
23 # This file is used to test reading and processing of config files
26 import os
27 import time
29 from shinken_test import unittest, ShinkenTest
31 from shinken.modules.service_perfdata_broker import get_instance
34 class TestConfig(ShinkenTest):
35 #setUp is in shinken_test
37 #Change ME :)
38 def test_service_perfdata(self):
39 print self.conf.modules
40 #get our modules
41 mod = None
42 for m in self.conf.modules:
43 if m.module_type == 'service_perfdata':
44 mod = m
45 self.assert_(mod is not None)
46 self.assert_(mod.path == 'tmp/service-perfdata')
47 self.assert_(mod.module_name == 'Service-Perfdata')
48 self.assert_(mod.mode == 'a')
49 #Warning, the r (raw) is important here
50 self.assert_(mod.template == r'$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEOUTPUT$\t$SERVICESTATE$\t$SERVICEPERFDATA$\n')
52 try :
53 os.unlink(mod.path)
54 except :
55 pass
57 sl = get_instance(mod)
58 print sl
59 #Hack here :(
60 sl.properties = {}
61 sl.properties['to_queue'] = None
62 sl.init()
63 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
64 t = int(time.time())
65 print "T", t
66 self.scheduler_loop(1, [[svc, 2, 'BAD | value1=0 value2=0']])
67 #manage all service check result broks
68 for b in self.sched.broks.values():
69 if b.type == 'service_check_result':
70 sl.manage_brok(b)
71 self.sched.broks = {}
72 sl.file.close() # the sl has also an open (writing) file handle
74 fd = open(mod.path)
75 buf = fd.readline()
76 print "BUF:", buf
77 comparison = '%d\t%s\t%s\t%s\t%s\t%s\n' % (t, "test_host_0", "test_ok_0", 'BAD ', 'CRITICAL', ' value1=0 value2=0')
78 print "Comparison:", comparison
79 self.assert_(buf == comparison)
80 fd.close()
81 os.unlink(mod.path)
83 #Now change with a new template
84 #and direct in the instance (do not do this in prod :) )
85 mod.template = '$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\t$SERVICESTATE$\n'
86 sl2 = get_instance(mod)
87 sl2.init()
88 print sl2.__dict__
89 t = int(time.time())
90 print "T", t
91 self.scheduler_loop(1, [[svc, 2, 'BAD | value1=0 value2=0']])
92 #manage all service check result broks
93 for b in self.sched.broks.values():
94 if b.type == 'service_check_result':
95 sl2.manage_brok(b)
96 sl2.file.close()
98 fd = open(mod.path)
99 buf = fd.readline()
100 print "BUF:", buf
101 comparison = '%d\t%s\t%s\t%s\t%s\t%s\n' % (t, "test_host_0", "test_ok_0", 'BAD ', ' value1=0 value2=0', 'CRITICAL')
102 print "Comparison:", comparison
103 self.assert_(buf == comparison)
104 fd.close()
105 os.unlink(mod.path)
108 if __name__ == '__main__':
109 unittest.main()