Add: reactionner dead test in end_to_end test
[shinken.git] / test / test_module_service_perfdata.py
blob97a7a0c25453bb8e0a4a72c89b0788deec060d6d
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 #It's ugly I know....
27 import os
28 from shinken_test import *
29 sys.path.append("../shinken/modules")
30 from service_perfdata_broker import *
31 from shinken.brok import Brok
33 class TestConfig(ShinkenTest):
34 #setUp is in shinken_test
36 #Change ME :)
37 def test_service_perfdata(self):
38 print self.conf.modules
39 #get our modules
40 mod = None
41 for m in self.conf.modules:
42 if m.module_type == 'service_perfdata':
43 mod = m
44 self.assert_(mod != None)
45 self.assert_(mod.path == '/dev/shm/service-perfdata')
46 self.assert_(mod.module_name == 'Service-Perfdata')
47 self.assert_(mod.mode == 'a')
48 #Warning, the r (raw) is important here
49 self.assert_(mod.template == r'$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEOUTPUT$\t$SERVICESTATE$\t$SERVICEPERFDATA$\n')
51 try :
52 os.unlink(mod.path)
53 except :
54 pass
56 sl = get_instance(mod)
57 print sl
58 #Hack here :(
59 sl.properties = {}
60 sl.properties['to_queue'] = None
61 sl.init()
62 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
63 t = int(time.time())
64 print "T", t
65 self.scheduler_loop(1, [[svc, 2, 'BAD | value1=0 value2=0']])
66 #manage all service check result broks
67 for b in self.sched.broks.values():
68 if b.type == 'service_check_result':
69 sl.manage_brok(b)
70 self.sched.broks = {}
72 fd = open(mod.path)
73 buf = fd.readline()
74 print "BUF:", buf
75 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')
76 print "Comparison:", comparison
77 self.assert_(buf == comparison)
78 fd.close()
79 os.unlink(mod.path)
81 #Now change with a new template
82 #and direct in the instance (do not do this in prod :) )
83 mod.template = '$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\t$SERVICESTATE$\n'
84 sl2 = get_instance(mod)
85 sl2.init()
86 print sl2.__dict__
87 t = int(time.time())
88 print "T", t
89 self.scheduler_loop(1, [[svc, 2, 'BAD | value1=0 value2=0']])
90 #manage all service check result broks
91 for b in self.sched.broks.values():
92 if b.type == 'service_check_result':
93 sl2.manage_brok(b)
95 fd = open(mod.path)
96 buf = fd.readline()
97 print "BUF:", buf
98 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')
99 print "Comparison:", comparison
100 self.assert_(buf == comparison)
101 fd.close()
102 os.unlink(mod.path)
105 if __name__ == '__main__':
106 unittest.main()