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
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
39 modconf
.module_name
= "PickleRetention"
40 modconf
.module_type
= hot_dependencies_arbiter
.properties
['type']
41 modconf
.properties
= hot_dependencies_arbiter
.properties
.copy()
47 # For old Python version, load
48 # simple json (it can be hard json?! It's 2 functions guy!)
50 import simplejson
as json
52 print "Error : you need the json or simplejson module for this script"
55 class TestModuleHotDep(ShinkenTest
):
56 #setUp is in shinken_test
58 self
.setup_with_file('etc/nagios_module_hot_dependencies_arbiter.cfg')
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)
82 mod
= sl
= Hot_dependencies_arbiter(modconf
, 'tmp/vmware_mapping_file.json', "", 30, 300)
85 os
.unlink(mod
.mapping_file
)
93 sl
.properties
['to_queue'] = None
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
))
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
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
))
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
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)
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'})
167 os
.unlink(mod
.mapping_file
)
171 sl
= get_instance(mod
)
176 sl
.properties
['to_queue'] = None
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
193 # Now we look if it's finished, and we get data and manage them
194 # with case 1 (0 and 1 linked, not with 1 and 2)
197 # Now we should see link between 1 and 0, but not between 2 and 1
198 self
.assert_(host1
.is_linked_with_host(host0
) == True)
199 self
.assert_(host1
.is_linked_with_host(host2
) == False)
202 print "Go in case2 " * 10
204 sl
.mapping_command
= 'libexec/hot_dep_export.py case2 tmp/vmware_mapping_file.json'
206 sl
.mapping_command
= 'python.exe libexec\\hot_dep_export.py case2 tmp\\vmware_mapping_file.json'
207 # We lie in the interval :p (not 0, because 0 mean : disabled)
208 sl
.mapping_command_interval
= 0.1
211 #But we need another tick to get all of it
214 # Now we should see link between 1 and 0, but not between 2 and 1
215 self
.assert_(host1
.is_linked_with_host(host0
) == False)
216 self
.assert_(host1
.is_linked_with_host(host2
) == True)
219 #Ok, we can delete the retention file
220 os
.unlink(mod
.mapping_file
)
225 if __name__
== '__main__':