Fix : fix hot module under windows test. (at leat I hope...)
[shinken.git] / test / test_macroresolver.py
blob3e672638adb84bee35ff2ce82f88d542631e4ad0
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 from shinken.macroresolver import MacroResolver
28 from shinken.objects.command import Command,CommandCall
30 class TestConfig(ShinkenTest):
31 #setUp is in shinken_test
33 def get_mr(self):
34 mr = MacroResolver()
35 mr.init(self.conf)
36 return mr
38 def get_hst_svc(self):
39 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
40 hst = self.sched.hosts.find_by_name("test_host_0")
41 return (svc, hst)
43 #Change ME :)
44 def test_resolv_simple(self):
45 mr = self.get_mr()
46 (svc, hst) = self.get_hst_svc()
47 data = svc.get_data_for_checks()
48 com = mr.resolve_command(svc.check_command, data)
49 print com
50 self.assert_(com == "plugins/test_servicecheck.pl --type=ok --failchance=5% --previous-state=PENDING --state-duration=0 --total-critical-on-host=0 --total-warning-on-host=0 --hostname test_host_0 --servicedesc test_ok_0")
53 #Here call with a special macro TOTALHOSTSUP
54 #but call it as arg. So will need 2 pass in macro resolver
55 #at last to resolv it.
56 def test_special_macros(self):
57 mr = self.get_mr()
58 (svc, hst) = self.get_hst_svc()
59 data = svc.get_data_for_checks()
60 hst.state = 'UP'
61 dummy_call = "special_macro!$TOTALHOSTSUP$"
62 cc = CommandCall(self.conf.commands, dummy_call)
63 com = mr.resolve_command(cc, data)
64 print com
65 self.assert_(com == 'plugins/nothing 1')
67 #For output macro we want to delete all illegal macro caracter
68 def test_illegal_macro_output_chars(self):
69 "$HOSTOUTPUT$, $HOSTPERFDATA$, $HOSTACKAUTHOR$, $HOSTACKCOMMENT$, $SERVICEOUTPUT$, $SERVICEPERFDATA$, $SERVICEACKAUTHOR$, and $SERVICEACKCOMMENT$ "
70 mr = self.get_mr()
71 (svc, hst) = self.get_hst_svc()
72 data = svc.get_data_for_checks()
73 illegal_macro_output_chars = self.sched.conf.illegal_macro_output_chars
74 print "Illegal macros caracters :", illegal_macro_output_chars
75 hst.output = 'monculcestdupoulet'
76 dummy_call = "special_macro!$HOSTOUTPUT$"
78 for c in illegal_macro_output_chars:
79 hst.output = 'monculcestdupoulet'+c
80 cc = CommandCall(self.conf.commands, dummy_call)
81 com = mr.resolve_command(cc, data)
82 print com
83 self.assert_(com == 'plugins/nothing monculcestdupoulet')
86 def test_env_macros(self):
87 mr = self.get_mr()
88 (svc, hst) = self.get_hst_svc()
89 data = svc.get_data_for_checks()
91 env = mr.get_env_macros(data)
92 print "Env:", env
93 self.assert_(env != {})
94 self.assert_(env['NAGIOS_HOSTNAME'] == 'test_host_0')
95 self.assert_(env['NAGIOS_SERVICEPERCENTCHANGE'] == '0.0')
98 def test_resource_file(self):
99 mr = self.get_mr()
100 (svc, hst) = self.get_hst_svc()
101 data = svc.get_data_for_checks()
102 dummy_call = "special_macro!$USER1$"
103 cc = CommandCall(self.conf.commands, dummy_call)
104 com = mr.resolve_command(cc, data)
105 self.assert_(com == 'plugins/nothing plugins')
107 dummy_call = "special_macro!$INTERESTINGVARIABLE$"
108 cc = CommandCall(self.conf.commands, dummy_call)
109 com = mr.resolve_command(cc, data)
110 print "CUCU", com
111 self.assert_(com == 'plugins/nothing interestingvalue')
114 if __name__ == '__main__':
115 unittest.main()