Merge branch 'master' of ssh://lausser,shinken@shinken.git.sourceforge.net/gitroot...
[shinken.git] / test / test_module_hot_dependencies_arbiter.py
blobbeb717b76271350b8734b208b951ca7799278e7b
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, sys, time
28 from shinken_test import unittest, ShinkenTest
30 from shinken.log import logger
32 from shinken.objects.module import Module
34 from shinken.modules import hot_dependencies_arbiter
35 from shinken.modules.hot_dependencies_arbiter import Hot_dependencies_arbiter, get_instance
38 modconf = Module()
39 modconf.module_name = "PickleRetention"
40 modconf.module_type = hot_dependencies_arbiter.properties['type']
41 modconf.properties = hot_dependencies_arbiter.properties.copy()
44 try:
45 import json
46 except ImportError:
47 # For old Python version, load
48 # simple json (it can be hard json?! It's 2 functions guy!)
49 try:
50 import simplejson as json
51 except ImportError:
52 print "Error : you need the json or simplejson module for this script"
53 sys.exit(0)
55 class TestModuleHotDep(ShinkenTest):
56 #setUp is in shinken_test
57 def setUp(self):
58 self.setup_with_file('etc/nagios_module_hot_dependencies_arbiter.cfg')
60 #Change ME :)
61 def test_simple_json_read(self):
62 print self.conf.modules
64 host0 = self.sched.conf.hosts.find_by_name('test_host_0')
65 self.assert_(host0 is not None)
66 host1 = self.sched.conf.hosts.find_by_name('test_host_1')
67 self.assert_(host1 is not None)
68 host2 = self.sched.conf.hosts.find_by_name('test_host_2')
69 self.assert_(host2 is not None)
71 # From now there is no link between hosts (just parent with the router)
72 # but it's not imporant here
73 self.assert_(host0.is_linked_with_host(host1) == False)
74 self.assert_(host1.is_linked_with_host(host0) == False)
75 self.assert_(host0.is_linked_with_host(host2) == False)
76 self.assert_(host2.is_linked_with_host(host0) == False)
77 self.assert_(host2.is_linked_with_host(host1) == False)
78 self.assert_(host1.is_linked_with_host(host2) == False)
81 #get our modules
82 mod = sl = Hot_dependencies_arbiter(modconf, 'tmp/vmware_mapping_file.json', "", 30, 300)
84 try :
85 os.unlink(mod.mapping_file)
86 except :
87 pass
89 print "Instance", sl
91 # Hack here :(
92 sl.properties = {}
93 sl.properties['to_queue'] = None
94 sl.init()
95 l = logger
97 # We simulate a uniq link, here the vm host1 is on the host host0
98 links = [[["host", "test_host_0"], ["host", "test_host_1"]]]
99 f = open(mod.mapping_file, 'wb')
100 f.write(json.dumps(links))
101 f.close()
103 # Try the hook for the late config, so it will create
104 # the link between host1 and host0
105 sl.hook_late_configuration(self)
107 # We can look is now the hosts are linked or not :)
108 self.assert_(host1.is_linked_with_host(host0) == True)
110 print "Mapping after first pass?", sl.mapping
112 # We sleep because the dile we generated should have
113 # a different modification time, so more than 1s
114 time.sleep(1.5)
116 ### Ok now we update the mappign file with this time
117 # link between 1 and 2, bye bye 0 :)
118 # host2 is the host, host1 the VM
119 links = [[["host", "test_host_2"], ["host", "test_host_1"]]]
120 f = open(mod.mapping_file, 'wb')
121 f.write(json.dumps(links))
122 f.close()
124 sl.hook_tick(self)
126 # Now we should see link between 1 and 2, but not between 0 and 1
127 self.assert_(host1.is_linked_with_host(host0) == False)
128 self.assert_(host1.is_linked_with_host(host2) == True)
130 #Ok, we can delete the retention file
131 os.unlink(mod.mapping_file)
136 # We are trying to see if we can have good data with 2 commands call
137 # CASE1 : link between host0 and 1
138 # then after some second, :
139 # CASE2 : link between host1 and host2, so like the previous test, but with
140 # command calls
141 def test_json_read_with_command(self):
142 print self.conf.modules
144 host0 = self.sched.conf.hosts.find_by_name('test_host_0')
145 self.assert_(host0 is not None)
146 host1 = self.sched.conf.hosts.find_by_name('test_host_1')
147 self.assert_(host1 is not None)
148 host2 = self.sched.conf.hosts.find_by_name('test_host_2')
149 self.assert_(host2 is not None)
151 # From now there is no link between hosts (just parent with the router)
152 # but it's not imporant here
153 self.assert_(host0.is_linked_with_host(host1) == False)
154 self.assert_(host1.is_linked_with_host(host0) == False)
155 self.assert_(host0.is_linked_with_host(host2) == False)
156 self.assert_(host2.is_linked_with_host(host0) == False)
157 self.assert_(host2.is_linked_with_host(host1) == False)
158 self.assert_(host1.is_linked_with_host(host2) == False)
161 #get our modules
162 mod = None
163 mod = Module({'type' : 'hot_dependencies', 'module_name' : 'VMWare_auto_linking', 'mapping_file' : 'tmp/vmware_mapping_file.json',
164 'mapping_command' : "libexec/hot_dep_export.py case1 tmp/vmware_mapping_file.json", 'mapping_command_interval' : '30'})
166 try :
167 os.unlink(mod.mapping_file)
168 except :
169 pass
171 sl = get_instance(mod)
172 print "Instance", sl
174 # Hack here :(
175 sl.properties = {}
176 sl.properties['to_queue'] = None
177 sl.init()
178 l = logger
180 # Try the hook for the late config, so it will create
181 # the link between host1 and host0
182 sl.hook_late_configuration(self)
184 # We can look is now the hosts are linked or not :)
185 self.assert_(host1.is_linked_with_host(host0) == False)
187 print "Mapping after first pass?", sl.mapping
189 # The hook_late should have seen a problem of no file
190 # and so launch the command. We can wait it finished
191 time.sleep(1.5)
193 # Under windows, call pythoin.exe
194 if os.name == 'nt':
195 sl.mapping_command = 'python.exe libexec/hot_dep_export.py case2 tmp/vmware_mapping_file.json'
198 # Now we look if it's finished, and we get data and manage them
199 # with case 1 (0 and 1 linked, not with 1 and 2)
200 sl.hook_tick(self)
202 # Now we should see link between 1 and 0, but not between 2 and 1
203 self.assert_(host1.is_linked_with_host(host0) == True)
204 self.assert_(host1.is_linked_with_host(host2) == False)
206 # Now we go in case2
207 print "Go in case2 " * 10
208 if os.name != 'nt':
209 sl.mapping_command = 'libexec/hot_dep_export.py case2 tmp/vmware_mapping_file.json'
210 else:
211 sl.mapping_command = 'python.exe libexec\\hot_dep_export.py case2 tmp\\vmware_mapping_file.json'
212 # We lie in the interval :p (not 0, because 0 mean : disabled)
213 sl.mapping_command_interval = 0.1
214 sl.hook_tick(self)
215 time.sleep(1.5)
216 #But we need another tick to get all of it
217 sl.hook_tick(self)
219 # Now we should see link between 1 and 0, but not between 2 and 1
220 self.assert_(host1.is_linked_with_host(host0) == False)
221 self.assert_(host1.is_linked_with_host(host2) == True)
224 #Ok, we can delete the retention file
225 os.unlink(mod.mapping_file)
230 if __name__ == '__main__':
231 unittest.main()