test modules for sfa
[tests.git] / system / TestResources.py
1 #
2 # $Id$
3 #
4
5 import sys
6
7 import utils
8 from TestMapper import TestMapper
9 from TestPool import TestPoolQemu, TestPoolIP
10 from Trackers import TrackerPlc, TrackerQemu
11
12 class TestResources:
13
14     # need more specialization, see an example in OnelabTestResources
15
16     ########## 
17     def localize (self,plcs,options):
18         try:
19             plcs = self.localize_qemus(plcs,options)
20         except Exception, e:
21             print 'Could not localize qemus','--',e,'--','exiting'
22             sys.exit(1)
23         try:
24             plcs = self.localize_nodes(plcs,options)
25         except Exception,e:
26             print 'Could not localize nodes','--',e,'--','exiting'
27             sys.exit(1)
28         try:
29             plcs = self.localize_plcs(plcs,options)
30         except Exception,e:
31             print 'Could not localize plcs','--',e,'--','exiting'
32             sys.exit(1)
33         try:
34             plcs = self.localize_rspec(plcs,options)
35         except Exception,e:
36             print 'Could not localize RSpec','--',e,'--','exiting'
37             sys.exit(1)
38         return plcs
39
40     def localize_qemus (self,plcs,options):
41
42         # all plcs on the same vserver box
43         plc_box = self.plc_boxes()[0]
44
45         # informative
46         label=options.personality.replace("linux","")
47
48         node_map = []
49         qemu_pool = TestPoolQemu (self.qemus_ip_pool(), options)
50
51         for index in range(options.size):
52             index += 1
53             if options.ips_qemu:
54                 ip_or_hostname=options.ips_qemu.pop()
55                 (hostname,ip,unused)=qemu_pool.locate_entry(ip_or_hostname)
56             else:
57                 (hostname,ip,unused) = qemu_pool.next_free()
58
59             node_map += [ ('node%d'%index, {'host_box':hostname},) ]
60
61         mapper = {'plc': [ ('*' , {'hostname':plc_box,
62                                    'PLC_DB_HOST':plc_box,
63                                    'PLC_API_HOST':plc_box,
64                                    'PLC_BOOT_HOST':plc_box,
65                                    'PLC_WWW_HOST':plc_box,
66                                    'name':'%s-'+label } ) 
67                            ],
68                   'node': node_map,
69                   }
70     
71         return TestMapper(plcs,options).map(mapper)
72         
73
74     def localize_nodes (self, plcs, options):
75         
76         ip_pool = TestPoolIP (self.nodes_ip_pool(),options)
77         network_dict = self.network_dict()
78
79         test_mapper = TestMapper (plcs, options)
80     
81         all_nodenames = test_mapper.node_names()
82         maps = []
83         for nodename in all_nodenames:
84             if options.ips_node:
85                 ip_or_hostname=options.ips_node.pop()
86                 print 'debug','in',ip_or_hostname,'out',ip_pool.locate_entry(ip_or_hostname)
87                 (hostname,ip,mac)=ip_pool.locate_entry(ip_or_hostname)
88             else:
89                 (hostname,ip,mac) = ip_pool.next_free()
90             utils.header('Attaching node %s to %s (%s)'%(nodename,hostname,ip))
91             node_dict= {'node_fields:hostname':hostname,
92                         'interface_fields:ip':ip, 
93                         'interface_fields:mac':mac,
94                         }
95         
96             node_dict.update(network_dict)
97             maps.append ( ( nodename, node_dict) )
98     
99         plc_map = [ ( '*' , { 'PLC_NET_DNS1' : network_dict [ 'interface_fields:dns1' ],
100                               'PLC_NET_DNS2' : network_dict [ 'interface_fields:dns2' ], } ) ]
101     
102         return test_mapper.map ({'node': maps, 'plc' : plc_map } )
103         
104
105     def localize_plcs (self,plcs,options):
106         
107         utils.header ("Turning configuration into a vserver-based one for onelab")
108         ip_pool = TestPoolIP (self.plcs_ip_pool(),options)
109     
110         plc_counter=0
111         for plc in plcs:
112             if options.ips_plc :
113                 ip_or_hostname=options.ips_plc.pop()
114                 (hostname,ip,mac)=ip_pool.locate_entry(ip_or_hostname)
115                 if options.verbose:
116                     utils.header("Using user-provided %s %s for plc %s"%(
117                             hostname,ip_or_hostname,plc['name']))
118             else:
119                 (hostname,ip,mac)=ip_pool.next_free()
120                 if options.verbose:
121                     utils.header("Using auto-allocated %s %s for plc %s"%(
122                             hostname,ip,plc['name']))
123     
124             ### rewrite fields in plc
125             # compute a helpful vserver name - remove domain in hostname
126             simplehostname=hostname.split('.')[0]
127             vservername = options.buildname
128             if len(plcs) == 1 :
129                 vservername = "%s-%s" % (vservername,simplehostname)
130             else:
131                 plc_counter += 1
132                 vservername = "%s-%d-%s" % (vservername,plc_counter,simplehostname)
133             # apply
134             plc['vservername']=vservername
135             plc['vserverip']=ip
136             plc['name'] = "%s_%s"%(plc['name'],simplehostname)
137             utils.header("Attaching plc %s to vserver %s (%s)"%(
138                     plc['name'],plc['vservername'],plc['vserverip']))
139             for key in [ 'PLC_DB_HOST', 'PLC_API_HOST', 'PLC_WWW_HOST', 'PLC_BOOT_HOST',]:
140                 plc[key] = hostname
141     
142         return plcs
143
144     # as a plc step this should return a boolean
145     def step_pre (self,plc):
146         return self.trqemu_record (plc) and self.trqemu_free(plc)
147
148     def step_post (self,plc):
149         return self.trplc_record (plc) and self.trplc_free(plc)
150
151     def step_cleanup (self,plc):
152         return self.trqemu_cleanup(plc) and self.trplc_cleanup(plc)
153
154     def step_list (self,plc):
155         return self.trqemu_list(plc) and self.trplc_list(plc)
156
157     ####################
158     def trqemu_record (self,plc):
159         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
160         for site_spec in plc.plc_spec['sites']:
161             for node_spec in site_spec['nodes']:
162                 tracker.record(node_spec['host_box'],plc.options.buildname,node_spec['node_fields']['hostname'])
163         tracker.store()
164         return True
165
166     def trqemu_free (self,plc):
167         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
168         for site_spec in plc.plc_spec['sites']:
169             for node_spec in site_spec['nodes']:
170                 tracker.free()
171         tracker.store()
172         return True
173
174     ###
175     def trplc_record (self,plc):
176         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
177         tracker.record(plc.test_ssh.hostname,plc.vservername)
178         tracker.store()
179         return True
180
181     def trplc_free (self,plc):
182         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
183         tracker.free()
184         tracker.store()
185         return True
186
187     ###
188     def trqemu_cleanup (self,plc):
189         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
190         for site_spec in plc.plc_spec['sites']:
191             for node_spec in site_spec['nodes']:
192                 tracker.cleanup()
193         tracker.store()
194         return True
195
196     def trplc_cleanup (self,plc):
197         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
198         tracker.cleanup()
199         tracker.store()
200         return True
201
202     def trqemu_list (self,plc):
203         TrackerQemu(plc.options,instances=self.max_qemus()-1).list()
204         return True
205
206     def trplc_list (self,plc):
207         TrackerPlc(plc.options,instances=self.max_plcs()).list()
208         return True
209
210     def localize_rspec (self,plcs,options):
211         
212         utils.header ("Localize SFA Slice RSpec")
213
214         for plc in plcs:
215             for site in plc['sites']:
216                 for node in site['nodes']:
217                     plc['sfa']['sfa_slice_rspec']['part4'] = node['node_fields']['hostname']
218         
219         return plcs