1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
9 from datetime import datetime, timedelta
10 from types import StringTypes
13 from Completer import Completer, CompleterTask
14 from TestSite import TestSite
15 from TestNode import TestNode, CompleterTaskNodeSsh
16 from TestUser import TestUser
17 from TestKey import TestKey
18 from TestSlice import TestSlice
19 from TestSliver import TestSliver
20 from TestBoxQemu import TestBoxQemu
21 from TestSsh import TestSsh
22 from TestApiserver import TestApiserver
23 from TestAuthSfa import TestAuthSfa
24 from PlcapiUrlScanner import PlcapiUrlScanner
26 from TestBonding import TestBonding
28 has_sfa_cache_filename="sfa-cache"
30 # step methods must take (self) and return a boolean (options is a member of the class)
32 def standby(minutes, dry_run):
33 utils.header('Entering StandBy for %d mn'%minutes)
37 time.sleep(60*minutes)
40 def standby_generic(func):
42 minutes = int(func.__name__.split("_")[1])
43 return standby(minutes, self.options.dry_run)
46 def node_mapper(method):
47 def map_on_nodes(self, *args, **kwds):
49 node_method = TestNode.__dict__[method.__name__]
50 for test_node in self.all_nodes():
51 if not node_method(test_node, *args, **kwds):
54 # maintain __name__ for ignore_result
55 map_on_nodes.__name__ = method.__name__
56 # restore the doc text
57 map_on_nodes.__doc__ = TestNode.__dict__[method.__name__].__doc__
60 def slice_mapper(method):
61 def map_on_slices(self):
63 slice_method = TestSlice.__dict__[method.__name__]
64 for slice_spec in self.plc_spec['slices']:
65 site_spec = self.locate_site (slice_spec['sitename'])
66 test_site = TestSite(self,site_spec)
67 test_slice = TestSlice(self,test_site,slice_spec)
68 if not slice_method(test_slice, self.options):
71 # maintain __name__ for ignore_result
72 map_on_slices.__name__ = method.__name__
73 # restore the doc text
74 map_on_slices.__doc__ = TestSlice.__dict__[method.__name__].__doc__
77 def bonding_redirector(method):
78 bonding_name = method.__name__.replace('bonding_', '')
80 bonding_method = TestBonding.__dict__[bonding_name]
81 return bonding_method(self.test_bonding)
82 # maintain __name__ for ignore_result
83 redirect.__name__ = method.__name__
84 # restore the doc text
85 redirect.__doc__ = TestBonding.__dict__[bonding_name].__doc__
88 # run a step but return True so that we can go on
89 def ignore_result(method):
91 # ssh_slice_ignore->ssh_slice
92 ref_name = method.__name__.replace('_ignore', '').replace('force_', '')
93 ref_method = TestPlc.__dict__[ref_name]
94 result = ref_method(self)
95 print "Actual (but ignored) result for %(ref_name)s is %(result)s" % locals()
96 return Ignored(result)
97 name = method.__name__.replace('_ignore', '').replace('force_', '')
98 ignoring.__name__ = name
99 ignoring.__doc__ = "ignored version of " + name
102 # a variant that expects the TestSlice method to return a list of CompleterTasks that
103 # are then merged into a single Completer run to avoid wating for all the slices
104 # esp. useful when a test fails of course
105 # because we need to pass arguments we use a class instead..
106 class slice_mapper__tasks(object):
107 # could not get this to work with named arguments
108 def __init__(self, timeout_minutes, silent_minutes, period_seconds):
109 self.timeout = timedelta(minutes = timeout_minutes)
110 self.silent = timedelta(minutes = silent_minutes)
111 self.period = timedelta(seconds = period_seconds)
112 def __call__(self, method):
114 # compute augmented method name
115 method_name = method.__name__ + "__tasks"
116 # locate in TestSlice
117 slice_method = TestSlice.__dict__[ method_name ]
120 for slice_spec in self.plc_spec['slices']:
121 site_spec = self.locate_site (slice_spec['sitename'])
122 test_site = TestSite(self, site_spec)
123 test_slice = TestSlice(self, test_site, slice_spec)
124 tasks += slice_method (test_slice, self.options)
125 return Completer (tasks, message=method.__name__).\
126 run(decorator_self.timeout, decorator_self.silent, decorator_self.period)
127 # restore the doc text from the TestSlice method even if a bit odd
128 wrappee.__name__ = method.__name__
129 wrappee.__doc__ = slice_method.__doc__
132 def auth_sfa_mapper(method):
135 auth_method = TestAuthSfa.__dict__[method.__name__]
136 for auth_spec in self.plc_spec['sfa']['auth_sfa_specs']:
137 test_auth = TestAuthSfa(self, auth_spec)
138 if not auth_method(test_auth, self.options):
141 # restore the doc text
142 actual.__doc__ = TestAuthSfa.__dict__[method.__name__].__doc__
146 def __init__(self, result):
156 'plcvm_delete','plcvm_timestamp','plcvm_create', SEP,
157 'plc_install', 'plc_configure', 'plc_start', SEP,
158 'keys_fetch', 'keys_store', 'keys_clear_known_hosts', SEP,
159 'plcapi_urls','speed_up_slices', SEP,
160 'initscripts', 'sites', 'nodes', 'slices', 'nodegroups', 'leases', SEP,
161 # slices created under plcsh interactively seem to be fine but these ones don't have the tags
162 # keep this our of the way for now
163 'check_vsys_defaults_ignore', SEP,
164 # run this first off so it's easier to re-run on another qemu box
165 'qemu_kill_mine', SEP,
166 'nodestate_reinstall', 'qemu_local_init','bootcd', 'qemu_local_config', SEP,
167 'qemu_clean_mine', 'qemu_export', 'qemu_start', 'qemu_timestamp', SEP,
168 'sfa_install_all', 'sfa_configure', 'cross_sfa_configure', 'sfa_start', 'sfa_import', SEPSFA,
169 'sfi_configure@1', 'sfa_register_site@1','sfa_register_pi@1', SEPSFA,
170 'sfa_register_user@1', 'sfa_update_user@1', 'sfa_register_slice@1', 'sfa_renew_slice@1', SEPSFA,
171 'sfa_remove_user_from_slice@1','sfi_show_slice_researchers@1',
172 'sfa_insert_user_in_slice@1','sfi_show_slice_researchers@1', SEPSFA,
173 'sfa_discover@1', 'sfa_rspec@1', 'sfa_allocate@1', 'sfa_provision@1', SEPSFA,
174 'sfa_check_slice_plc@1', 'sfa_update_slice@1', SEPSFA,
175 'sfi_list@1', 'sfi_show_site@1', 'sfa_utest@1', SEPSFA,
176 # we used to run plcsh_stress_test, and then ssh_node_debug and ssh_node_boot
177 # but as the stress test might take a while, we sometimes missed the debug mode..
178 'probe_kvm_iptables',
179 'ping_node', 'ssh_node_debug', 'plcsh_stress_test@1', SEP,
180 'ssh_node_boot', 'node_bmlogs', 'ssh_slice', 'ssh_slice_basics', 'check_initscripts', SEP,
181 'ssh_slice_sfa@1', SEPSFA,
182 'sfa_rspec_empty@1', 'sfa_allocate_empty@1', 'sfa_provision_empty@1','sfa_check_slice_plc_empty@1', SEPSFA,
183 'sfa_delete_slice@1', 'sfa_delete_user@1', SEPSFA,
184 'cross_check_tcp@1', 'check_system_slice', SEP,
185 # for inspecting the slice while it runs the first time
187 # check slices are turned off properly
188 'empty_slices', 'ssh_slice_off', 'slice_fs_deleted_ignore', SEP,
189 # check they are properly re-created with the same name
190 'fill_slices', 'ssh_slice_again', SEP,
191 'gather_logs_force', SEP,
194 'export', 'show_boxes', 'super_speed_up_slices', SEP,
195 'check_hooks', 'plc_stop', 'plcvm_start', 'plcvm_stop', SEP,
196 'delete_initscripts', 'delete_nodegroups','delete_all_sites', SEP,
197 'delete_sites', 'delete_nodes', 'delete_slices', 'keys_clean', SEP,
198 'delete_leases', 'list_leases', SEP,
200 'nodestate_show','nodestate_safeboot','nodestate_boot', SEP,
201 'qemu_list_all', 'qemu_list_mine', 'qemu_kill_all', SEP,
202 'sfa_install_core', 'sfa_install_sfatables', 'sfa_install_plc', 'sfa_install_client', SEPSFA,
203 'sfa_plcclean', 'sfa_dbclean', 'sfa_stop','sfa_uninstall', 'sfi_clean', SEPSFA,
204 'sfa_get_expires', SEPSFA,
205 'plc_db_dump' , 'plc_db_restore', SEP,
206 'check_netflow','check_drl', SEP,
207 'debug_nodemanager', 'slice_fs_present', SEP,
208 'standby_1_through_20','yes','no',SEP,
211 'bonding_init_partial',
213 'bonding_install_rpms', SEP,
217 def printable_steps(list):
218 single_line = " ".join(list) + " "
219 return single_line.replace(" "+SEP+" ", " \\\n").replace(" "+SEPSFA+" ", " \\\n")
221 def valid_step(step):
222 return step != SEP and step != SEPSFA
224 # turn off the sfa-related steps when build has skipped SFA
225 # this was originally for centos5 but is still valid
226 # for up to f12 as recent SFAs with sqlalchemy won't build before f14
228 def _has_sfa_cached(rpms_url):
229 if os.path.isfile(has_sfa_cache_filename):
230 cached = file(has_sfa_cache_filename).read() == "yes"
231 utils.header("build provides SFA (cached):%s" % cached)
233 # warning, we're now building 'sface' so let's be a bit more picky
234 # full builds are expected to return with 0 here
235 utils.header("Checking if build provides SFA package...")
236 retcod = os.system("curl --silent %s/ | grep -q sfa-"%rpms_url) == 0
237 encoded = 'yes' if retcod else 'no'
238 with open(has_sfa_cache_filename,'w')as out:
243 def check_whether_build_has_sfa(rpms_url):
244 has_sfa = TestPlc._has_sfa_cached(rpms_url)
246 utils.header("build does provide SFA")
248 # move all steps containing 'sfa' from default_steps to other_steps
249 utils.header("SFA package not found - removing steps with sfa or sfi")
250 sfa_steps = [ step for step in TestPlc.default_steps
251 if step.find('sfa') >= 0 or step.find("sfi") >= 0 ]
252 TestPlc.other_steps += sfa_steps
253 for step in sfa_steps:
254 TestPlc.default_steps.remove(step)
256 def __init__(self, plc_spec, options):
257 self.plc_spec = plc_spec
258 self.options = options
259 self.test_ssh = TestSsh(self.plc_spec['host_box'], self.options.buildname)
260 self.vserverip = plc_spec['vserverip']
261 self.vservername = plc_spec['vservername']
262 self.url = "https://%s:443/PLCAPI/" % plc_spec['vserverip']
263 self.apiserver = TestApiserver(self.url, options.dry_run)
264 (self.ssh_node_boot_timeout, self.ssh_node_boot_silent) = plc_spec['ssh_node_boot_timers']
265 (self.ssh_node_debug_timeout, self.ssh_node_debug_silent) = plc_spec['ssh_node_debug_timers']
267 def has_addresses_api(self):
268 return self.apiserver.has_method('AddIpAddress')
271 name = self.plc_spec['name']
272 return "%s.%s" % (name,self.vservername)
275 return self.plc_spec['host_box']
278 return self.test_ssh.is_local()
280 # define the API methods on this object through xmlrpc
281 # would help, but not strictly necessary
285 def actual_command_in_guest(self,command, backslash=False):
286 raw1 = self.host_to_guest(command)
287 raw2 = self.test_ssh.actual_command(raw1, dry_run=self.options.dry_run, backslash=backslash)
290 def start_guest(self):
291 return utils.system(self.test_ssh.actual_command(self.start_guest_in_host(),
292 dry_run=self.options.dry_run))
294 def stop_guest(self):
295 return utils.system(self.test_ssh.actual_command(self.stop_guest_in_host(),
296 dry_run=self.options.dry_run))
298 def run_in_guest(self, command, backslash=False):
299 raw = self.actual_command_in_guest(command, backslash)
300 return utils.system(raw)
302 def run_in_host(self,command):
303 return self.test_ssh.run_in_buildname(command, dry_run=self.options.dry_run)
305 # backslashing turned out so awful at some point that I've turned off auto-backslashing
306 # see e.g. plc_start esp. the version for f14
307 #command gets run in the plc's vm
308 def host_to_guest(self, command):
309 vservername = self.vservername
310 personality = self.options.personality
311 raw = "%(personality)s virsh -c lxc:/// lxc-enter-namespace %(vservername)s" % locals()
312 # f14 still needs some extra help
313 if self.options.fcdistro == 'f14':
314 raw +=" -- /usr/bin/env PATH=/bin:/sbin:/usr/bin:/usr/sbin %(command)s" % locals()
316 raw +=" -- /usr/bin/env %(command)s" % locals()
319 # this /vservers thing is legacy...
320 def vm_root_in_host(self):
321 return "/vservers/%s/" % (self.vservername)
323 def vm_timestamp_path(self):
324 return "/vservers/%s/%s.timestamp" % (self.vservername,self.vservername)
326 #start/stop the vserver
327 def start_guest_in_host(self):
328 return "virsh -c lxc:/// start %s" % (self.vservername)
330 def stop_guest_in_host(self):
331 return "virsh -c lxc:/// destroy %s" % (self.vservername)
334 def run_in_guest_piped(self,local,remote):
335 return utils.system(local+" | "+self.test_ssh.actual_command(self.host_to_guest(remote),
338 def yum_check_installed(self, rpms):
339 if isinstance(rpms, list):
341 return self.run_in_guest("rpm -q %s"%rpms) == 0
343 # does a yum install in the vs, ignore yum retcod, check with rpm
344 def yum_install(self, rpms):
345 if isinstance(rpms, list):
347 self.run_in_guest("yum -y install %s" % rpms)
348 # yum-complete-transaction comes with yum-utils, that is in vtest.pkgs
349 self.run_in_guest("yum-complete-transaction -y")
350 return self.yum_check_installed(rpms)
353 return {'Username' : self.plc_spec['settings']['PLC_ROOT_USER'],
354 'AuthMethod' : 'password',
355 'AuthString' : self.plc_spec['settings']['PLC_ROOT_PASSWORD'],
356 'Role' : self.plc_spec['role'],
359 def locate_site(self,sitename):
360 for site in self.plc_spec['sites']:
361 if site['site_fields']['name'] == sitename:
363 if site['site_fields']['login_base'] == sitename:
365 raise Exception,"Cannot locate site %s" % sitename
367 def locate_node(self, nodename):
368 for site in self.plc_spec['sites']:
369 for node in site['nodes']:
370 if node['name'] == nodename:
372 raise Exception, "Cannot locate node %s" % nodename
374 def locate_hostname(self, hostname):
375 for site in self.plc_spec['sites']:
376 for node in site['nodes']:
377 if node['node_fields']['hostname'] == hostname:
379 raise Exception,"Cannot locate hostname %s" % hostname
381 def locate_key(self, key_name):
382 for key in self.plc_spec['keys']:
383 if key['key_name'] == key_name:
385 raise Exception,"Cannot locate key %s" % key_name
387 def locate_private_key_from_key_names(self, key_names):
388 # locate the first avail. key
390 for key_name in key_names:
391 key_spec = self.locate_key(key_name)
392 test_key = TestKey(self,key_spec)
393 publickey = test_key.publicpath()
394 privatekey = test_key.privatepath()
395 if os.path.isfile(publickey) and os.path.isfile(privatekey):
402 def locate_slice(self, slicename):
403 for slice in self.plc_spec['slices']:
404 if slice['slice_fields']['name'] == slicename:
406 raise Exception,"Cannot locate slice %s" % slicename
408 def all_sliver_objs(self):
410 for slice_spec in self.plc_spec['slices']:
411 slicename = slice_spec['slice_fields']['name']
412 for nodename in slice_spec['nodenames']:
413 result.append(self.locate_sliver_obj(nodename, slicename))
416 def locate_sliver_obj(self, nodename, slicename):
417 site,node = self.locate_node(nodename)
418 slice = self.locate_slice(slicename)
420 test_site = TestSite(self, site)
421 test_node = TestNode(self, test_site, node)
422 # xxx the slice site is assumed to be the node site - mhh - probably harmless
423 test_slice = TestSlice(self, test_site, slice)
424 return TestSliver(self, test_node, test_slice)
426 def locate_first_node(self):
427 nodename = self.plc_spec['slices'][0]['nodenames'][0]
428 site,node = self.locate_node(nodename)
429 test_site = TestSite(self, site)
430 test_node = TestNode(self, test_site, node)
433 def locate_first_sliver(self):
434 slice_spec = self.plc_spec['slices'][0]
435 slicename = slice_spec['slice_fields']['name']
436 nodename = slice_spec['nodenames'][0]
437 return self.locate_sliver_obj(nodename,slicename)
439 # all different hostboxes used in this plc
440 def get_BoxNodes(self):
441 # maps on sites and nodes, return [ (host_box,test_node) ]
443 for site_spec in self.plc_spec['sites']:
444 test_site = TestSite(self,site_spec)
445 for node_spec in site_spec['nodes']:
446 test_node = TestNode(self, test_site, node_spec)
447 if not test_node.is_real():
448 tuples.append( (test_node.host_box(),test_node) )
449 # transform into a dict { 'host_box' -> [ test_node .. ] }
451 for (box,node) in tuples:
452 if not result.has_key(box):
455 result[box].append(node)
458 # a step for checking this stuff
459 def show_boxes(self):
460 'print summary of nodes location'
461 for box,nodes in self.get_BoxNodes().iteritems():
462 print box,":"," + ".join( [ node.name() for node in nodes ] )
465 # make this a valid step
466 def qemu_kill_all(self):
467 'kill all qemu instances on the qemu boxes involved by this setup'
468 # this is the brute force version, kill all qemus on that host box
469 for (box,nodes) in self.get_BoxNodes().iteritems():
470 # pass the first nodename, as we don't push template-qemu on testboxes
471 nodedir = nodes[0].nodedir()
472 TestBoxQemu(box, self.options.buildname).qemu_kill_all(nodedir)
475 # make this a valid step
476 def qemu_list_all(self):
477 'list all qemu instances on the qemu boxes involved by this setup'
478 for box,nodes in self.get_BoxNodes().iteritems():
479 # this is the brute force version, kill all qemus on that host box
480 TestBoxQemu(box, self.options.buildname).qemu_list_all()
483 # kill only the qemus related to this test
484 def qemu_list_mine(self):
485 'list qemu instances for our nodes'
486 for (box,nodes) in self.get_BoxNodes().iteritems():
487 # the fine-grain version
492 # kill only the qemus related to this test
493 def qemu_clean_mine(self):
494 'cleanup (rm -rf) qemu instances for our nodes'
495 for box,nodes in self.get_BoxNodes().iteritems():
496 # the fine-grain version
501 # kill only the right qemus
502 def qemu_kill_mine(self):
503 'kill the qemu instances for our nodes'
504 for box,nodes in self.get_BoxNodes().iteritems():
505 # the fine-grain version
510 #################### display config
512 "show test configuration after localization"
517 # uggly hack to make sure 'run export' only reports about the 1st plc
518 # to avoid confusion - also we use 'inri_slice1' in various aliases..
521 "print cut'n paste-able stuff to export env variables to your shell"
522 # guess local domain from hostname
523 if TestPlc.exported_id > 1:
524 print "export GUESTHOSTNAME%d=%s" % (TestPlc.exported_id, self.plc_spec['vservername'])
526 TestPlc.exported_id += 1
527 domain = socket.gethostname().split('.',1)[1]
528 fqdn = "%s.%s" % (self.plc_spec['host_box'], domain)
529 print "export BUILD=%s" % self.options.buildname
530 print "export PLCHOSTLXC=%s" % fqdn
531 print "export GUESTNAME=%s" % self.plc_spec['vservername']
532 vplcname = self.plc_spec['vservername'].split('-')[-1]
533 print "export GUESTHOSTNAME=%s.%s"%(vplcname, domain)
534 # find hostname of first node
535 hostname, qemubox = self.all_node_infos()[0]
536 print "export KVMHOST=%s.%s" % (qemubox, domain)
537 print "export NODE=%s" % (hostname)
541 always_display_keys=['PLC_WWW_HOST', 'nodes', 'sites']
542 def show_pass(self, passno):
543 for (key,val) in self.plc_spec.iteritems():
544 if not self.options.verbose and key not in TestPlc.always_display_keys:
549 self.display_site_spec(site)
550 for node in site['nodes']:
551 self.display_node_spec(node)
552 elif key == 'initscripts':
553 for initscript in val:
554 self.display_initscript_spec(initscript)
555 elif key == 'slices':
557 self.display_slice_spec(slice)
560 self.display_key_spec(key)
562 if key not in ['sites', 'initscripts', 'slices', 'keys']:
563 print '+ ', key, ':', val
565 def display_site_spec(self, site):
566 print '+ ======== site', site['site_fields']['name']
567 for k,v in site.iteritems():
568 if not self.options.verbose and k not in TestPlc.always_display_keys:
572 print '+ ','nodes : ',
574 print node['node_fields']['hostname'],'',
580 print user['name'],'',
582 elif k == 'site_fields':
583 print '+ login_base', ':', v['login_base']
584 elif k == 'address_fields':
590 def display_initscript_spec(self, initscript):
591 print '+ ======== initscript', initscript['initscript_fields']['name']
593 def display_key_spec(self, key):
594 print '+ ======== key', key['key_name']
596 def display_slice_spec(self, slice):
597 print '+ ======== slice', slice['slice_fields']['name']
598 for k,v in slice.iteritems():
605 elif k == 'usernames':
611 elif k == 'slice_fields':
612 print '+ fields',':',
613 print 'max_nodes=',v['max_nodes'],
618 def display_node_spec(self, node):
619 print "+ node=%s host_box=%s" % (node['name'],node['host_box']),
620 print "hostname=", node['node_fields']['hostname'],
621 print "ip=", node['interface_fields']['ip']
622 if self.options.verbose:
623 utils.pprint("node details", node, depth=3)
625 # another entry point for just showing the boxes involved
626 def display_mapping(self):
627 TestPlc.display_mapping_plc(self.plc_spec)
631 def display_mapping_plc(plc_spec):
632 print '+ MyPLC',plc_spec['name']
633 # WARNING this would not be right for lxc-based PLC's - should be harmless though
634 print '+\tvserver address = root@%s:/vservers/%s' % (plc_spec['host_box'], plc_spec['vservername'])
635 print '+\tIP = %s/%s' % (plc_spec['settings']['PLC_API_HOST'], plc_spec['vserverip'])
636 for site_spec in plc_spec['sites']:
637 for node_spec in site_spec['nodes']:
638 TestPlc.display_mapping_node(node_spec)
641 def display_mapping_node(node_spec):
642 print '+ NODE %s' % (node_spec['name'])
643 print '+\tqemu box %s' % node_spec['host_box']
644 print '+\thostname=%s' % node_spec['node_fields']['hostname']
646 # write a timestamp in /vservers/<>.timestamp
647 # cannot be inside the vserver, that causes vserver .. build to cough
648 def plcvm_timestamp(self):
649 "Create a timestamp to remember creation date for this plc"
650 now = int(time.time())
651 # TODO-lxc check this one
652 # a first approx. is to store the timestamp close to the VM root like vs does
653 stamp_path = self.vm_timestamp_path()
654 stamp_dir = os.path.dirname(stamp_path)
655 utils.system(self.test_ssh.actual_command("mkdir -p %s" % stamp_dir))
656 return utils.system(self.test_ssh.actual_command("echo %d > %s" % (now, stamp_path))) == 0
658 # this is called inconditionnally at the beginning of the test sequence
659 # just in case this is a rerun, so if the vm is not running it's fine
660 def plcvm_delete(self):
661 "vserver delete the test myplc"
662 stamp_path = self.vm_timestamp_path()
663 self.run_in_host("rm -f %s" % stamp_path)
664 self.run_in_host("virsh -c lxc:// destroy %s" % self.vservername)
665 self.run_in_host("virsh -c lxc:// undefine %s" % self.vservername)
666 self.run_in_host("rm -fr /vservers/%s" % self.vservername)
670 # historically the build was being fetched by the tests
671 # now the build pushes itself as a subdir of the tests workdir
672 # so that the tests do not have to worry about extracting the build (svn, git, or whatever)
673 def plcvm_create(self):
674 "vserver creation (no install done)"
675 # push the local build/ dir to the testplc box
677 # a full path for the local calls
678 build_dir = os.path.dirname(sys.argv[0])
679 # sometimes this is empty - set to "." in such a case
682 build_dir += "/build"
684 # use a standard name - will be relative to remote buildname
686 # remove for safety; do *not* mkdir first, otherwise we end up with build/build/
687 self.test_ssh.rmdir(build_dir)
688 self.test_ssh.copy(build_dir, recursive=True)
689 # the repo url is taken from arch-rpms-url
690 # with the last step (i386) removed
691 repo_url = self.options.arch_rpms_url
692 for level in [ 'arch' ]:
693 repo_url = os.path.dirname(repo_url)
695 # invoke initvm (drop support for vs)
696 script = "lbuild-initvm.sh"
698 # pass the vbuild-nightly options to [lv]test-initvm
699 script_options += " -p %s" % self.options.personality
700 script_options += " -d %s" % self.options.pldistro
701 script_options += " -f %s" % self.options.fcdistro
702 script_options += " -r %s" % repo_url
703 vserver_name = self.vservername
705 vserver_hostname = socket.gethostbyaddr(self.vserverip)[0]
706 script_options += " -n %s" % vserver_hostname
708 print "Cannot reverse lookup %s" % self.vserverip
709 print "This is considered fatal, as this might pollute the test results"
711 create_vserver="%(build_dir)s/%(script)s %(script_options)s %(vserver_name)s" % locals()
712 return self.run_in_host(create_vserver) == 0
715 def plc_install(self):
716 "yum install myplc, noderepo, and the plain bootstrapfs"
718 # workaround for getting pgsql8.2 on centos5
719 if self.options.fcdistro == "centos5":
720 self.run_in_guest("rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm")
723 if self.options.personality == "linux32":
725 elif self.options.personality == "linux64":
728 raise Exception, "Unsupported personality %r"%self.options.personality
729 nodefamily = "%s-%s-%s" % (self.options.pldistro, self.options.fcdistro, arch)
732 pkgs_list.append("slicerepo-%s" % nodefamily)
733 pkgs_list.append("myplc")
734 pkgs_list.append("noderepo-%s" % nodefamily)
735 pkgs_list.append("nodeimage-%s-plain" % nodefamily)
736 pkgs_string=" ".join(pkgs_list)
737 return self.yum_install(pkgs_list)
740 def mod_python(self):
741 """yum install mod_python, useful on f18 and above so as to avoid broken wsgi"""
742 return self.yum_install( ['mod_python'] )
745 def plc_configure(self):
747 tmpname = '%s.plc-config-tty' % self.name()
748 with open(tmpname,'w') as fileconf:
749 for (var,value) in self.plc_spec['settings'].iteritems():
750 fileconf.write('e %s\n%s\n'%(var,value))
751 fileconf.write('w\n')
752 fileconf.write('q\n')
753 utils.system('cat %s' % tmpname)
754 self.run_in_guest_piped('cat %s' % tmpname, 'plc-config-tty')
755 utils.system('rm %s' % tmpname)
758 # f14 is a bit odd in this respect, although this worked fine in guests up to f18
759 # however using a vplc guest under f20 requires this trick
760 # the symptom is this: service plc start
761 # Starting plc (via systemctl): Failed to get D-Bus connection: \
762 # Failed to connect to socket /org/freedesktop/systemd1/private: Connection refused
763 # weird thing is the doc says f14 uses upstart by default and not systemd
764 # so this sounds kind of harmless
765 def start_service(self, service):
766 return self.start_stop_service(service, 'start')
767 def stop_service(self, service):
768 return self.start_stop_service(service, 'stop')
770 def start_stop_service(self, service, start_or_stop):
771 "utility to start/stop a service with the special trick for f14"
772 if self.options.fcdistro != 'f14':
773 return self.run_in_guest("service %s %s" % (service, start_or_stop)) == 0
775 # patch /sbin/service so it does not reset environment
776 self.run_in_guest('sed -i -e \\"s,env -i,env,\\" /sbin/service')
777 # this is because our own scripts in turn call service
778 return self.run_in_guest("SYSTEMCTL_SKIP_REDIRECT=true service %s %s" % \
779 (service, start_or_stop)) == 0
783 return self.start_service('plc')
787 return self.stop_service('plc')
789 def plcvm_start(self):
790 "start the PLC vserver"
794 def plcvm_stop(self):
795 "stop the PLC vserver"
799 # stores the keys from the config for further use
800 def keys_store(self):
801 "stores test users ssh keys in keys/"
802 for key_spec in self.plc_spec['keys']:
803 TestKey(self,key_spec).store_key()
806 def keys_clean(self):
807 "removes keys cached in keys/"
808 utils.system("rm -rf ./keys")
811 # fetches the ssh keys in the plc's /etc/planetlab and stores them in keys/
812 # for later direct access to the nodes
813 def keys_fetch(self):
814 "gets ssh keys in /etc/planetlab/ and stores them locally in keys/"
816 if not os.path.isdir(dir):
818 vservername = self.vservername
819 vm_root = self.vm_root_in_host()
821 prefix = 'debug_ssh_key'
822 for ext in ['pub', 'rsa'] :
823 src = "%(vm_root)s/etc/planetlab/%(prefix)s.%(ext)s" % locals()
824 dst = "keys/%(vservername)s-debug.%(ext)s" % locals()
825 if self.test_ssh.fetch(src, dst) != 0:
830 "create sites with PLCAPI"
831 return self.do_sites()
833 def delete_sites(self):
834 "delete sites with PLCAPI"
835 return self.do_sites(action="delete")
837 def do_sites(self, action="add"):
838 for site_spec in self.plc_spec['sites']:
839 test_site = TestSite(self,site_spec)
840 if (action != "add"):
841 utils.header("Deleting site %s in %s" % (test_site.name(), self.name()))
842 test_site.delete_site()
843 # deleted with the site
844 #test_site.delete_users()
847 utils.header("Creating site %s & users in %s" % (test_site.name(), self.name()))
848 test_site.create_site()
849 test_site.create_users()
852 def delete_all_sites(self):
853 "Delete all sites in PLC, and related objects"
854 print 'auth_root', self.auth_root()
855 sites = self.apiserver.GetSites(self.auth_root(), {}, ['site_id','login_base'])
857 # keep automatic site - otherwise we shoot in our own foot, root_auth is not valid anymore
858 if site['login_base'] == self.plc_spec['settings']['PLC_SLICE_PREFIX']:
860 site_id = site['site_id']
861 print 'Deleting site_id', site_id
862 self.apiserver.DeleteSite(self.auth_root(), site_id)
866 "create nodes with PLCAPI"
867 return self.do_nodes()
868 def delete_nodes(self):
869 "delete nodes with PLCAPI"
870 return self.do_nodes(action="delete")
872 def do_nodes(self, action="add"):
873 for site_spec in self.plc_spec['sites']:
874 test_site = TestSite(self, site_spec)
876 utils.header("Deleting nodes in site %s" % test_site.name())
877 for node_spec in site_spec['nodes']:
878 test_node = TestNode(self, test_site, node_spec)
879 utils.header("Deleting %s" % test_node.name())
880 test_node.delete_node()
882 utils.header("Creating nodes for site %s in %s" % (test_site.name(), self.name()))
883 for node_spec in site_spec['nodes']:
884 utils.pprint('Creating node %s' % node_spec, node_spec)
885 test_node = TestNode(self, test_site, node_spec)
886 test_node.create_node()
889 def nodegroups(self):
890 "create nodegroups with PLCAPI"
891 return self.do_nodegroups("add")
892 def delete_nodegroups(self):
893 "delete nodegroups with PLCAPI"
894 return self.do_nodegroups("delete")
898 def translate_timestamp(start, grain, timestamp):
899 if timestamp < TestPlc.YEAR:
900 return start+timestamp*grain
905 def timestamp_printable(timestamp):
906 return time.strftime('%m-%d %H:%M:%S UTC', time.gmtime(timestamp))
909 "create leases (on reservable nodes only, use e.g. run -c default -c resa)"
910 now = int(time.time())
911 grain = self.apiserver.GetLeaseGranularity(self.auth_root())
912 print 'API answered grain=', grain
913 start = (now/grain)*grain
915 # find out all nodes that are reservable
916 nodes = self.all_reservable_nodenames()
918 utils.header("No reservable node found - proceeding without leases")
921 # attach them to the leases as specified in plc_specs
922 # this is where the 'leases' field gets interpreted as relative of absolute
923 for lease_spec in self.plc_spec['leases']:
924 # skip the ones that come with a null slice id
925 if not lease_spec['slice']:
927 lease_spec['t_from'] = TestPlc.translate_timestamp(start, grain, lease_spec['t_from'])
928 lease_spec['t_until'] = TestPlc.translate_timestamp(start, grain, lease_spec['t_until'])
929 lease_addition = self.apiserver.AddLeases(self.auth_root(), nodes, lease_spec['slice'],
930 lease_spec['t_from'],lease_spec['t_until'])
931 if lease_addition['errors']:
932 utils.header("Cannot create leases, %s"%lease_addition['errors'])
935 utils.header('Leases on nodes %r for %s from %d (%s) until %d (%s)' % \
936 (nodes, lease_spec['slice'],
937 lease_spec['t_from'], TestPlc.timestamp_printable(lease_spec['t_from']),
938 lease_spec['t_until'], TestPlc.timestamp_printable(lease_spec['t_until'])))
942 def delete_leases(self):
943 "remove all leases in the myplc side"
944 lease_ids = [ l['lease_id'] for l in self.apiserver.GetLeases(self.auth_root())]
945 utils.header("Cleaning leases %r" % lease_ids)
946 self.apiserver.DeleteLeases(self.auth_root(), lease_ids)
949 def list_leases(self):
950 "list all leases known to the myplc"
951 leases = self.apiserver.GetLeases(self.auth_root())
952 now = int(time.time())
954 current = l['t_until'] >= now
955 if self.options.verbose or current:
956 utils.header("%s %s from %s until %s" % \
957 (l['hostname'], l['name'],
958 TestPlc.timestamp_printable(l['t_from']),
959 TestPlc.timestamp_printable(l['t_until'])))
962 # create nodegroups if needed, and populate
963 def do_nodegroups(self, action="add"):
964 # 1st pass to scan contents
966 for site_spec in self.plc_spec['sites']:
967 test_site = TestSite(self,site_spec)
968 for node_spec in site_spec['nodes']:
969 test_node = TestNode(self, test_site, node_spec)
970 if node_spec.has_key('nodegroups'):
971 nodegroupnames = node_spec['nodegroups']
972 if isinstance(nodegroupnames, StringTypes):
973 nodegroupnames = [ nodegroupnames ]
974 for nodegroupname in nodegroupnames:
975 if not groups_dict.has_key(nodegroupname):
976 groups_dict[nodegroupname] = []
977 groups_dict[nodegroupname].append(test_node.name())
978 auth = self.auth_root()
980 for (nodegroupname,group_nodes) in groups_dict.iteritems():
982 print 'nodegroups:', 'dealing with nodegroup',\
983 nodegroupname, 'on nodes', group_nodes
984 # first, check if the nodetagtype is here
985 tag_types = self.apiserver.GetTagTypes(auth, {'tagname':nodegroupname})
987 tag_type_id = tag_types[0]['tag_type_id']
989 tag_type_id = self.apiserver.AddTagType(auth,
990 {'tagname' : nodegroupname,
991 'description' : 'for nodegroup %s' % nodegroupname,
992 'category' : 'test'})
993 print 'located tag (type)', nodegroupname, 'as', tag_type_id
995 nodegroups = self.apiserver.GetNodeGroups(auth, {'groupname' : nodegroupname})
997 self.apiserver.AddNodeGroup(auth, nodegroupname, tag_type_id, 'yes')
998 print 'created nodegroup', nodegroupname, \
999 'from tagname', nodegroupname, 'and value', 'yes'
1000 # set node tag on all nodes, value='yes'
1001 for nodename in group_nodes:
1003 self.apiserver.AddNodeTag(auth, nodename, nodegroupname, "yes")
1005 traceback.print_exc()
1006 print 'node', nodename, 'seems to already have tag', nodegroupname
1009 expect_yes = self.apiserver.GetNodeTags(auth,
1010 {'hostname' : nodename,
1011 'tagname' : nodegroupname},
1012 ['value'])[0]['value']
1013 if expect_yes != "yes":
1014 print 'Mismatch node tag on node',nodename,'got',expect_yes
1017 if not self.options.dry_run:
1018 print 'Cannot find tag', nodegroupname, 'on node', nodename
1022 print 'cleaning nodegroup', nodegroupname
1023 self.apiserver.DeleteNodeGroup(auth, nodegroupname)
1025 traceback.print_exc()
1029 # a list of TestNode objs
1030 def all_nodes(self):
1032 for site_spec in self.plc_spec['sites']:
1033 test_site = TestSite(self,site_spec)
1034 for node_spec in site_spec['nodes']:
1035 nodes.append(TestNode(self, test_site, node_spec))
1038 # return a list of tuples (nodename,qemuname)
1039 def all_node_infos(self) :
1041 for site_spec in self.plc_spec['sites']:
1042 node_infos += [ (node_spec['node_fields']['hostname'], node_spec['host_box']) \
1043 for node_spec in site_spec['nodes'] ]
1046 def all_nodenames(self):
1047 return [ x[0] for x in self.all_node_infos() ]
1048 def all_reservable_nodenames(self):
1050 for site_spec in self.plc_spec['sites']:
1051 for node_spec in site_spec['nodes']:
1052 node_fields = node_spec['node_fields']
1053 if 'node_type' in node_fields and node_fields['node_type'] == 'reservable':
1054 res.append(node_fields['hostname'])
1057 # silent_minutes : during the first <silent_minutes> minutes nothing gets printed
1058 def nodes_check_boot_state(self, target_boot_state, timeout_minutes,
1059 silent_minutes, period_seconds = 15):
1060 if self.options.dry_run:
1064 class CompleterTaskBootState(CompleterTask):
1065 def __init__(self, test_plc, hostname):
1066 self.test_plc = test_plc
1067 self.hostname = hostname
1068 self.last_boot_state = 'undef'
1069 def actual_run(self):
1071 node = self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(),
1074 self.last_boot_state = node['boot_state']
1075 return self.last_boot_state == target_boot_state
1079 return "CompleterTaskBootState with node %s" % self.hostname
1080 def failure_epilogue(self):
1081 print "node %s in state %s - expected %s" %\
1082 (self.hostname, self.last_boot_state, target_boot_state)
1084 timeout = timedelta(minutes=timeout_minutes)
1085 graceout = timedelta(minutes=silent_minutes)
1086 period = timedelta(seconds=period_seconds)
1087 # the nodes that haven't checked yet - start with a full list and shrink over time
1088 utils.header("checking nodes boot state (expected %s)" % target_boot_state)
1089 tasks = [ CompleterTaskBootState(self,hostname) \
1090 for (hostname,_) in self.all_node_infos() ]
1091 message = 'check_boot_state={}'.format(target_boot_state)
1092 return Completer(tasks, message=message).run(timeout, graceout, period)
1094 def nodes_booted(self):
1095 return self.nodes_check_boot_state('boot', timeout_minutes=30, silent_minutes=28)
1097 def probe_kvm_iptables(self):
1098 (_,kvmbox) = self.all_node_infos()[0]
1099 TestSsh(kvmbox).run("iptables-save")
1103 def check_nodes_ping(self, timeout_seconds=30, period_seconds=10):
1104 class CompleterTaskPingNode(CompleterTask):
1105 def __init__(self, hostname):
1106 self.hostname = hostname
1107 def run(self, silent):
1108 command="ping -c 1 -w 1 %s >& /dev/null" % self.hostname
1109 return utils.system(command, silent=silent) == 0
1110 def failure_epilogue(self):
1111 print "Cannot ping node with name %s" % self.hostname
1112 timeout = timedelta(seconds = timeout_seconds)
1114 period = timedelta(seconds = period_seconds)
1115 node_infos = self.all_node_infos()
1116 tasks = [ CompleterTaskPingNode(h) for (h,_) in node_infos ]
1117 return Completer(tasks, message='ping_node').run(timeout, graceout, period)
1119 # ping node before we try to reach ssh, helpful for troubleshooting failing bootCDs
1120 def ping_node(self):
1122 return self.check_nodes_ping()
1124 def check_nodes_ssh(self, debug, timeout_minutes, silent_minutes, period_seconds=15):
1126 timeout = timedelta(minutes=timeout_minutes)
1127 graceout = timedelta(minutes=silent_minutes)
1128 period = timedelta(seconds=period_seconds)
1129 vservername = self.vservername
1132 completer_message = 'ssh_node_debug'
1133 local_key = "keys/%(vservername)s-debug.rsa" % locals()
1136 completer_message = 'ssh_node_boot'
1137 local_key = "keys/key_admin.rsa"
1138 utils.header("checking ssh access to nodes (expected in %s mode)" % message)
1139 node_infos = self.all_node_infos()
1140 tasks = [ CompleterTaskNodeSsh(nodename, qemuname, local_key,
1141 boot_state=message, dry_run=self.options.dry_run) \
1142 for (nodename, qemuname) in node_infos ]
1143 return Completer(tasks, message=completer_message).run(timeout, graceout, period)
1145 def ssh_node_debug(self):
1146 "Tries to ssh into nodes in debug mode with the debug ssh key"
1147 return self.check_nodes_ssh(debug = True,
1148 timeout_minutes = self.ssh_node_debug_timeout,
1149 silent_minutes = self.ssh_node_debug_silent)
1151 def ssh_node_boot(self):
1152 "Tries to ssh into nodes in production mode with the root ssh key"
1153 return self.check_nodes_ssh(debug = False,
1154 timeout_minutes = self.ssh_node_boot_timeout,
1155 silent_minutes = self.ssh_node_boot_silent)
1157 def node_bmlogs(self):
1158 "Checks that there's a non-empty dir. /var/log/bm/raw"
1159 return utils.system(self.actual_command_in_guest("ls /var/log/bm/raw")) == 0
1162 def qemu_local_init(self): pass
1164 def bootcd(self): pass
1166 def qemu_local_config(self): pass
1168 def nodestate_reinstall(self): pass
1170 def nodestate_safeboot(self): pass
1172 def nodestate_boot(self): pass
1174 def nodestate_show(self): pass
1176 def qemu_export(self): pass
1178 ### check hooks : invoke scripts from hooks/{node,slice}
1179 def check_hooks_node(self):
1180 return self.locate_first_node().check_hooks()
1181 def check_hooks_sliver(self) :
1182 return self.locate_first_sliver().check_hooks()
1184 def check_hooks(self):
1185 "runs unit tests in the node and slice contexts - see hooks/{node,slice}"
1186 return self.check_hooks_node() and self.check_hooks_sliver()
1189 def do_check_initscripts(self):
1190 class CompleterTaskInitscript(CompleterTask):
1191 def __init__(self, test_sliver, stamp):
1192 self.test_sliver = test_sliver
1194 def actual_run(self):
1195 return self.test_sliver.check_initscript_stamp(self.stamp)
1197 return "initscript checker for %s" % self.test_sliver.name()
1198 def failure_epilogue(self):
1199 print "initscript stamp %s not found in sliver %s"%\
1200 (self.stamp, self.test_sliver.name())
1203 for slice_spec in self.plc_spec['slices']:
1204 if not slice_spec.has_key('initscriptstamp'):
1206 stamp = slice_spec['initscriptstamp']
1207 slicename = slice_spec['slice_fields']['name']
1208 for nodename in slice_spec['nodenames']:
1209 print 'nodename', nodename, 'slicename', slicename, 'stamp', stamp
1210 site,node = self.locate_node(nodename)
1211 # xxx - passing the wrong site - probably harmless
1212 test_site = TestSite(self, site)
1213 test_slice = TestSlice(self, test_site, slice_spec)
1214 test_node = TestNode(self, test_site, node)
1215 test_sliver = TestSliver(self, test_node, test_slice)
1216 tasks.append(CompleterTaskInitscript(test_sliver, stamp))
1217 return Completer(tasks, message='check_initscripts').\
1218 run (timedelta(minutes=5), timedelta(minutes=4), timedelta(seconds=10))
1220 def check_initscripts(self):
1221 "check that the initscripts have triggered"
1222 return self.do_check_initscripts()
1224 def initscripts(self):
1225 "create initscripts with PLCAPI"
1226 for initscript in self.plc_spec['initscripts']:
1227 utils.pprint('Adding Initscript in plc %s' % self.plc_spec['name'], initscript)
1228 self.apiserver.AddInitScript(self.auth_root(), initscript['initscript_fields'])
1231 def delete_initscripts(self):
1232 "delete initscripts with PLCAPI"
1233 for initscript in self.plc_spec['initscripts']:
1234 initscript_name = initscript['initscript_fields']['name']
1235 print('Attempting to delete %s in plc %s' % (initscript_name, self.plc_spec['name']))
1237 self.apiserver.DeleteInitScript(self.auth_root(), initscript_name)
1238 print initscript_name, 'deleted'
1240 print 'deletion went wrong - probably did not exist'
1245 "create slices with PLCAPI"
1246 return self.do_slices(action="add")
1248 def delete_slices(self):
1249 "delete slices with PLCAPI"
1250 return self.do_slices(action="delete")
1252 def fill_slices(self):
1253 "add nodes in slices with PLCAPI"
1254 return self.do_slices(action="fill")
1256 def empty_slices(self):
1257 "remove nodes from slices with PLCAPI"
1258 return self.do_slices(action="empty")
1260 def do_slices(self, action="add"):
1261 for slice in self.plc_spec['slices']:
1262 site_spec = self.locate_site(slice['sitename'])
1263 test_site = TestSite(self,site_spec)
1264 test_slice=TestSlice(self,test_site,slice)
1265 if action == "delete":
1266 test_slice.delete_slice()
1267 elif action == "fill":
1268 test_slice.add_nodes()
1269 elif action == "empty":
1270 test_slice.delete_nodes()
1272 test_slice.create_slice()
1275 @slice_mapper__tasks(20, 10, 15)
1276 def ssh_slice(self): pass
1277 @slice_mapper__tasks(20, 19, 15)
1278 def ssh_slice_off(self): pass
1279 @slice_mapper__tasks(1, 1, 15)
1280 def slice_fs_present(self): pass
1281 @slice_mapper__tasks(1, 1, 15)
1282 def slice_fs_deleted(self): pass
1284 # use another name so we can exclude/ignore it from the tests on the nightly command line
1285 def ssh_slice_again(self): return self.ssh_slice()
1286 # note that simply doing ssh_slice_again=ssh_slice would kind of work too
1287 # but for some reason the ignore-wrapping thing would not
1290 def ssh_slice_basics(self): pass
1292 def check_vsys_defaults(self): pass
1295 def keys_clear_known_hosts(self): pass
1297 def plcapi_urls(self):
1298 return PlcapiUrlScanner(self.auth_root(), ip=self.vserverip).scan()
1300 def speed_up_slices(self):
1301 "tweak nodemanager cycle (wait time) to 30+/-10 s"
1302 return self._speed_up_slices (30, 10)
1303 def super_speed_up_slices(self):
1304 "dev mode: tweak nodemanager cycle (wait time) to 5+/-1 s"
1305 return self._speed_up_slices(5, 1)
1307 def _speed_up_slices(self, p, r):
1308 # create the template on the server-side
1309 template = "%s.nodemanager" % self.name()
1310 with open(template,"w") as template_file:
1311 template_file.write('OPTIONS="-p %s -r %s -d"\n'%(p,r))
1312 in_vm = "/var/www/html/PlanetLabConf/nodemanager"
1313 remote = "%s/%s" % (self.vm_root_in_host(), in_vm)
1314 self.test_ssh.copy_abs(template, remote)
1316 if not self.apiserver.GetConfFiles(self.auth_root(),
1317 {'dest' : '/etc/sysconfig/nodemanager'}):
1318 self.apiserver.AddConfFile(self.auth_root(),
1319 {'dest' : '/etc/sysconfig/nodemanager',
1320 'source' : 'PlanetLabConf/nodemanager',
1321 'postinstall_cmd' : 'service nm restart',})
1324 def debug_nodemanager(self):
1325 "sets verbose mode for nodemanager, and speeds up cycle even more (needs speed_up_slices first)"
1326 template = "%s.nodemanager" % self.name()
1327 with open(template,"w") as template_file:
1328 template_file.write('OPTIONS="-p 10 -r 6 -v -d"\n')
1329 in_vm = "/var/www/html/PlanetLabConf/nodemanager"
1330 remote = "%s/%s" % (self.vm_root_in_host(), in_vm)
1331 self.test_ssh.copy_abs(template, remote)
1335 def qemu_start(self) : pass
1338 def qemu_timestamp(self) : pass
1340 # when a spec refers to a node possibly on another plc
1341 def locate_sliver_obj_cross(self, nodename, slicename, other_plcs):
1342 for plc in [ self ] + other_plcs:
1344 return plc.locate_sliver_obj(nodename, slicename)
1347 raise Exception, "Cannot locate sliver %s@%s among all PLCs" % (nodename, slicename)
1349 # implement this one as a cross step so that we can take advantage of different nodes
1350 # in multi-plcs mode
1351 def cross_check_tcp(self, other_plcs):
1352 "check TCP connectivity between 2 slices (or in loopback if only one is defined)"
1353 if 'tcp_specs' not in self.plc_spec or not self.plc_spec['tcp_specs']:
1354 utils.header("check_tcp: no/empty config found")
1356 specs = self.plc_spec['tcp_specs']
1359 # first wait for the network to be up and ready from the slices
1360 class CompleterTaskNetworkReadyInSliver(CompleterTask):
1361 def __init__(self, test_sliver):
1362 self.test_sliver = test_sliver
1363 def actual_run(self):
1364 return self.test_sliver.check_tcp_ready(port = 9999)
1366 return "network ready checker for %s" % self.test_sliver.name()
1367 def failure_epilogue(self):
1368 print "could not bind port from sliver %s" % self.test_sliver.name()
1372 managed_sliver_names = set()
1374 # locate the TestSliver instances involved, and cache them in the spec instance
1375 spec['s_sliver'] = self.locate_sliver_obj_cross(spec['server_node'], spec['server_slice'], other_plcs)
1376 spec['c_sliver'] = self.locate_sliver_obj_cross(spec['client_node'], spec['client_slice'], other_plcs)
1377 message = "Will check TCP between s=%s and c=%s" % \
1378 (spec['s_sliver'].name(), spec['c_sliver'].name())
1379 if 'client_connect' in spec:
1380 message += " (using %s)" % spec['client_connect']
1381 utils.header(message)
1382 # we need to check network presence in both slivers, but also
1383 # avoid to insert a sliver several times
1384 for sliver in [ spec['s_sliver'], spec['c_sliver'] ]:
1385 if sliver.name() not in managed_sliver_names:
1386 tasks.append(CompleterTaskNetworkReadyInSliver(sliver))
1387 # add this sliver's name in the set
1388 managed_sliver_names .update( {sliver.name()} )
1390 # wait for the netork to be OK in all server sides
1391 if not Completer(tasks, message='check for network readiness in slivers').\
1392 run(timedelta(seconds=30), timedelta(seconds=24), period=timedelta(seconds=5)):
1395 # run server and client
1399 # the issue here is that we have the server run in background
1400 # and so we have no clue if it took off properly or not
1401 # looks like in some cases it does not
1402 if not spec['s_sliver'].run_tcp_server(port, timeout=20):
1406 # idem for the client side
1407 # use nodename from located sliver, unless 'client_connect' is set
1408 if 'client_connect' in spec:
1409 destination = spec['client_connect']
1411 destination = spec['s_sliver'].test_node.name()
1412 if not spec['c_sliver'].run_tcp_client(destination, port):
1416 # painfully enough, we need to allow for some time as netflow might show up last
1417 def check_system_slice(self):
1418 "all nodes: check that a system slice is alive"
1419 # netflow currently not working in the lxc distro
1420 # drl not built at all in the wtx distro
1421 # if we find either of them we're happy
1422 return self.check_netflow() or self.check_drl()
1425 def check_netflow(self): return self._check_system_slice('netflow')
1426 def check_drl(self): return self._check_system_slice('drl')
1428 # we have the slices up already here, so it should not take too long
1429 def _check_system_slice(self, slicename, timeout_minutes=5, period_seconds=15):
1430 class CompleterTaskSystemSlice(CompleterTask):
1431 def __init__(self, test_node, dry_run):
1432 self.test_node = test_node
1433 self.dry_run = dry_run
1434 def actual_run(self):
1435 return self.test_node._check_system_slice(slicename, dry_run=self.dry_run)
1437 return "System slice %s @ %s" % (slicename, self.test_node.name())
1438 def failure_epilogue(self):
1439 print "COULD not find system slice %s @ %s"%(slicename, self.test_node.name())
1440 timeout = timedelta(minutes=timeout_minutes)
1441 silent = timedelta(0)
1442 period = timedelta(seconds=period_seconds)
1443 tasks = [ CompleterTaskSystemSlice(test_node, self.options.dry_run) \
1444 for test_node in self.all_nodes() ]
1445 return Completer(tasks, message='_check_system_slice').run(timeout, silent, period)
1447 def plcsh_stress_test(self):
1448 "runs PLCAPI stress test, that checks Add/Update/Delete on all types - preserves contents"
1449 # install the stress-test in the plc image
1450 location = "/usr/share/plc_api/plcsh_stress_test.py"
1451 remote = "%s/%s" % (self.vm_root_in_host(), location)
1452 self.test_ssh.copy_abs("plcsh_stress_test.py", remote)
1454 command += " -- --check"
1455 if self.options.size == 1:
1456 command += " --tiny"
1457 return self.run_in_guest(command) == 0
1459 # populate runs the same utility without slightly different options
1460 # in particular runs with --preserve (dont cleanup) and without --check
1461 # also it gets run twice, once with the --foreign option for creating fake foreign entries
1463 def sfa_install_all(self):
1464 "yum install sfa sfa-plc sfa-sfatables sfa-client"
1465 return self.yum_install("sfa sfa-plc sfa-sfatables sfa-client")
1467 def sfa_install_core(self):
1469 return self.yum_install("sfa")
1471 def sfa_install_plc(self):
1472 "yum install sfa-plc"
1473 return self.yum_install("sfa-plc")
1475 def sfa_install_sfatables(self):
1476 "yum install sfa-sfatables"
1477 return self.yum_install("sfa-sfatables")
1479 # for some very odd reason, this sometimes fails with the following symptom
1480 # # yum install sfa-client
1481 # Setting up Install Process
1483 # Downloading Packages:
1484 # Running rpm_check_debug
1485 # Running Transaction Test
1486 # Transaction Test Succeeded
1487 # Running Transaction
1488 # Transaction couldn't start:
1489 # installing package sfa-client-2.1-7.onelab.2012.05.23.i686 needs 68KB on the / filesystem
1490 # [('installing package sfa-client-2.1-7.onelab.2012.05.23.i686 needs 68KB on the / filesystem', (9, '/', 69632L))]
1491 # even though in the same context I have
1492 # [2012.05.23--f14-32-sfastd1-1-vplc07] / # df -h
1493 # Filesystem Size Used Avail Use% Mounted on
1494 # /dev/hdv1 806G 264G 501G 35% /
1495 # none 16M 36K 16M 1% /tmp
1497 # so as a workaround, we first try yum install, and then invoke rpm on the cached rpm...
1498 def sfa_install_client(self):
1499 "yum install sfa-client"
1500 first_try = self.yum_install("sfa-client")
1503 utils.header("********** Regular yum failed - special workaround in place, 2nd chance")
1504 code, cached_rpm_path = \
1505 utils.output_of(self.actual_command_in_guest('find /var/cache/yum -name sfa-client\*.rpm'))
1506 utils.header("rpm_path=<<%s>>" % rpm_path)
1508 self.run_in_guest("rpm -i %s" % cached_rpm_path)
1509 return self.yum_check_installed("sfa-client")
1511 def sfa_dbclean(self):
1512 "thoroughly wipes off the SFA database"
1513 return self.run_in_guest("sfaadmin reg nuke") == 0 or \
1514 self.run_in_guest("sfa-nuke.py") == 0 or \
1515 self.run_in_guest("sfa-nuke-plc.py") == 0
1517 def sfa_fsclean(self):
1518 "cleanup /etc/sfa/trusted_roots and /var/lib/sfa"
1519 self.run_in_guest("rm -rf /etc/sfa/trusted_roots /var/lib/sfa/authorities")
1522 def sfa_plcclean(self):
1523 "cleans the PLC entries that were created as a side effect of running the script"
1525 sfa_spec = self.plc_spec['sfa']
1527 for auth_sfa_spec in sfa_spec['auth_sfa_specs']:
1528 login_base = auth_sfa_spec['login_base']
1530 self.apiserver.DeleteSite(self.auth_root(),login_base)
1532 print "Site %s already absent from PLC db"%login_base
1534 for spec_name in ['pi_spec','user_spec']:
1535 user_spec = auth_sfa_spec[spec_name]
1536 username = user_spec['email']
1538 self.apiserver.DeletePerson(self.auth_root(),username)
1540 # this in fact is expected as sites delete their members
1541 #print "User %s already absent from PLC db"%username
1544 print "REMEMBER TO RUN sfa_import AGAIN"
1547 def sfa_uninstall(self):
1548 "uses rpm to uninstall sfa - ignore result"
1549 self.run_in_guest("rpm -e sfa sfa-sfatables sfa-client sfa-plc")
1550 self.run_in_guest("rm -rf /var/lib/sfa")
1551 self.run_in_guest("rm -rf /etc/sfa")
1552 self.run_in_guest("rm -rf /var/log/sfa_access.log /var/log/sfa_import_plc.log /var/log/sfa.daemon")
1554 self.run_in_guest("rpm -e --noscripts sfa-plc")
1557 ### run unit tests for SFA
1558 # NOTE: for some reason on f14/i386, yum install sfa-tests fails for no reason
1559 # Running Transaction
1560 # Transaction couldn't start:
1561 # installing package sfa-tests-1.0-21.onelab.i686 needs 204KB on the / filesystem
1562 # [('installing package sfa-tests-1.0-21.onelab.i686 needs 204KB on the / filesystem', (9, '/', 208896L))]
1563 # no matter how many Gbs are available on the testplc
1564 # could not figure out what's wrong, so...
1565 # if the yum install phase fails, consider the test is successful
1566 # other combinations will eventually run it hopefully
1567 def sfa_utest(self):
1568 "yum install sfa-tests and run SFA unittests"
1569 self.run_in_guest("yum -y install sfa-tests")
1570 # failed to install - forget it
1571 if self.run_in_guest("rpm -q sfa-tests") != 0:
1572 utils.header("WARNING: SFA unit tests failed to install, ignoring")
1574 return self.run_in_guest("/usr/share/sfa/tests/testAll.py") == 0
1578 dirname = "conf.%s" % self.plc_spec['name']
1579 if not os.path.isdir(dirname):
1580 utils.system("mkdir -p %s" % dirname)
1581 if not os.path.isdir(dirname):
1582 raise Exception,"Cannot create config dir for plc %s" % self.name()
1585 def conffile(self, filename):
1586 return "%s/%s" % (self.confdir(),filename)
1587 def confsubdir(self, dirname, clean, dry_run=False):
1588 subdirname = "%s/%s" % (self.confdir(),dirname)
1590 utils.system("rm -rf %s" % subdirname)
1591 if not os.path.isdir(subdirname):
1592 utils.system("mkdir -p %s" % subdirname)
1593 if not dry_run and not os.path.isdir(subdirname):
1594 raise "Cannot create config subdir %s for plc %s" % (dirname,self.name())
1597 def conffile_clean(self, filename):
1598 filename=self.conffile(filename)
1599 return utils.system("rm -rf %s" % filename)==0
1602 def sfa_configure(self):
1603 "run sfa-config-tty"
1604 tmpname = self.conffile("sfa-config-tty")
1605 with open(tmpname,'w') as fileconf:
1606 for (var,value) in self.plc_spec['sfa']['settings'].iteritems():
1607 fileconf.write('e %s\n%s\n'%(var,value))
1608 fileconf.write('w\n')
1609 fileconf.write('R\n')
1610 fileconf.write('q\n')
1611 utils.system('cat %s' % tmpname)
1612 self.run_in_guest_piped('cat %s' % tmpname, 'sfa-config-tty')
1615 def aggregate_xml_line(self):
1616 port = self.plc_spec['sfa']['neighbours-port']
1617 return '<aggregate addr="%s" hrn="%s" port="%r"/>' % \
1618 (self.vserverip, self.plc_spec['sfa']['settings']['SFA_REGISTRY_ROOT_AUTH'], port)
1620 def registry_xml_line(self):
1621 return '<registry addr="%s" hrn="%s" port="12345"/>' % \
1622 (self.vserverip, self.plc_spec['sfa']['settings']['SFA_REGISTRY_ROOT_AUTH'])
1625 # a cross step that takes all other plcs in argument
1626 def cross_sfa_configure(self, other_plcs):
1627 "writes aggregates.xml and registries.xml that point to all other PLCs in the test"
1628 # of course with a single plc, other_plcs is an empty list
1631 agg_fname = self.conffile("agg.xml")
1632 with open(agg_fname,"w") as out:
1633 out.write("<aggregates>%s</aggregates>\n" % \
1634 " ".join([ plc.aggregate_xml_line() for plc in other_plcs ]))
1635 utils.header("(Over)wrote %s" % agg_fname)
1636 reg_fname=self.conffile("reg.xml")
1637 with open(reg_fname,"w") as out:
1638 out.write("<registries>%s</registries>\n" % \
1639 " ".join([ plc.registry_xml_line() for plc in other_plcs ]))
1640 utils.header("(Over)wrote %s" % reg_fname)
1641 return self.test_ssh.copy_abs(agg_fname,
1642 '/%s/etc/sfa/aggregates.xml' % self.vm_root_in_host()) == 0 \
1643 and self.test_ssh.copy_abs(reg_fname,
1644 '/%s/etc/sfa/registries.xml' % self.vm_root_in_host()) == 0
1646 def sfa_import(self):
1647 "use sfaadmin to import from plc"
1648 auth = self.plc_spec['sfa']['settings']['SFA_REGISTRY_ROOT_AUTH']
1649 return self.run_in_guest('sfaadmin reg import_registry') == 0
1651 def sfa_start(self):
1653 return self.start_service('sfa')
1656 def sfi_configure(self):
1657 "Create /root/sfi on the plc side for sfi client configuration"
1658 if self.options.dry_run:
1659 utils.header("DRY RUN - skipping step")
1661 sfa_spec = self.plc_spec['sfa']
1662 # cannot use auth_sfa_mapper to pass dir_name
1663 for slice_spec in self.plc_spec['sfa']['auth_sfa_specs']:
1664 test_slice = TestAuthSfa(self, slice_spec)
1665 dir_basename = os.path.basename(test_slice.sfi_path())
1666 dir_name = self.confsubdir("dot-sfi/%s" % dir_basename,
1667 clean=True, dry_run=self.options.dry_run)
1668 test_slice.sfi_configure(dir_name)
1669 # push into the remote /root/sfi area
1670 location = test_slice.sfi_path()
1671 remote = "%s/%s" % (self.vm_root_in_host(), location)
1672 self.test_ssh.mkdir(remote, abs=True)
1673 # need to strip last level or remote otherwise we get an extra dir level
1674 self.test_ssh.copy_abs(dir_name, os.path.dirname(remote), recursive=True)
1678 def sfi_clean(self):
1679 "clean up /root/sfi on the plc side"
1680 self.run_in_guest("rm -rf /root/sfi")
1683 def sfa_rspec_empty(self):
1684 "expose a static empty rspec (ships with the tests module) in the sfi directory"
1685 filename = "empty-rspec.xml"
1687 for slice_spec in self.plc_spec['sfa']['auth_sfa_specs']:
1688 test_slice = TestAuthSfa(self, slice_spec)
1689 in_vm = test_slice.sfi_path()
1690 remote = "%s/%s" % (self.vm_root_in_host(), in_vm)
1691 if self.test_ssh.copy_abs(filename, remote) !=0:
1696 def sfa_register_site(self): pass
1698 def sfa_register_pi(self): pass
1700 def sfa_register_user(self): pass
1702 def sfa_update_user(self): pass
1704 def sfa_register_slice(self): pass
1706 def sfa_renew_slice(self): pass
1708 def sfa_get_expires(self): pass
1710 def sfa_discover(self): pass
1712 def sfa_rspec(self): pass
1714 def sfa_allocate(self): pass
1716 def sfa_allocate_empty(self): pass
1718 def sfa_provision(self): pass
1720 def sfa_provision_empty(self): pass
1722 def sfa_check_slice_plc(self): pass
1724 def sfa_check_slice_plc_empty(self): pass
1726 def sfa_update_slice(self): pass
1728 def sfa_remove_user_from_slice(self): pass
1730 def sfa_insert_user_in_slice(self): pass
1732 def sfi_list(self): pass
1734 def sfi_show_site(self): pass
1736 def sfi_show_slice(self): pass
1738 def sfi_show_slice_researchers(self): pass
1740 def ssh_slice_sfa(self): pass
1742 def sfa_delete_user(self): pass
1744 def sfa_delete_slice(self): pass
1748 return self.stop_service('sfa')
1751 "creates random entries in the PLCAPI"
1752 # install the stress-test in the plc image
1753 location = "/usr/share/plc_api/plcsh_stress_test.py"
1754 remote = "%s/%s" % (self.vm_root_in_host(), location)
1755 self.test_ssh.copy_abs("plcsh_stress_test.py", remote)
1757 command += " -- --preserve --short-names"
1758 local = (self.run_in_guest(command) == 0);
1759 # second run with --foreign
1760 command += ' --foreign'
1761 remote = (self.run_in_guest(command) == 0);
1762 return local and remote
1765 ####################
1767 def bonding_init_partial(self): pass
1770 def bonding_add_yum(self): pass
1773 def bonding_install_rpms(self): pass
1775 ####################
1777 def gather_logs(self):
1778 "gets all possible logs from plc's/qemu node's/slice's for future reference"
1779 # (1.a) get the plc's /var/log/ and store it locally in logs/myplc.var-log.<plcname>/*
1780 # (1.b) get the plc's /var/lib/pgsql/data/pg_log/ -> logs/myplc.pgsql-log.<plcname>/*
1781 # (1.c) get the plc's /root/sfi -> logs/sfi.<plcname>/
1782 # (2) get all the nodes qemu log and store it as logs/node.qemu.<node>.log
1783 # (3) get the nodes /var/log and store is as logs/node.var-log.<node>/*
1784 # (4) as far as possible get the slice's /var/log as logs/sliver.var-log.<sliver>/*
1786 print "-------------------- TestPlc.gather_logs : PLC's /var/log"
1787 self.gather_var_logs()
1789 print "-------------------- TestPlc.gather_logs : PLC's /var/lib/psql/data/pg_log/"
1790 self.gather_pgsql_logs()
1792 print "-------------------- TestPlc.gather_logs : PLC's /root/sfi/"
1793 self.gather_root_sfi()
1795 print "-------------------- TestPlc.gather_logs : nodes's QEMU logs"
1796 for site_spec in self.plc_spec['sites']:
1797 test_site = TestSite(self,site_spec)
1798 for node_spec in site_spec['nodes']:
1799 test_node = TestNode(self, test_site, node_spec)
1800 test_node.gather_qemu_logs()
1802 print "-------------------- TestPlc.gather_logs : nodes's /var/log"
1803 self.gather_nodes_var_logs()
1805 print "-------------------- TestPlc.gather_logs : sample sliver's /var/log"
1806 self.gather_slivers_var_logs()
1809 def gather_slivers_var_logs(self):
1810 for test_sliver in self.all_sliver_objs():
1811 remote = test_sliver.tar_var_logs()
1812 utils.system("mkdir -p logs/sliver.var-log.%s" % test_sliver.name())
1813 command = remote + " | tar -C logs/sliver.var-log.%s -xf -" % test_sliver.name()
1814 utils.system(command)
1817 def gather_var_logs(self):
1818 utils.system("mkdir -p logs/myplc.var-log.%s" % self.name())
1819 to_plc = self.actual_command_in_guest("tar -C /var/log/ -cf - .")
1820 command = to_plc + "| tar -C logs/myplc.var-log.%s -xf -" % self.name()
1821 utils.system(command)
1822 command = "chmod a+r,a+x logs/myplc.var-log.%s/httpd" % self.name()
1823 utils.system(command)
1825 def gather_pgsql_logs(self):
1826 utils.system("mkdir -p logs/myplc.pgsql-log.%s" % self.name())
1827 to_plc = self.actual_command_in_guest("tar -C /var/lib/pgsql/data/pg_log/ -cf - .")
1828 command = to_plc + "| tar -C logs/myplc.pgsql-log.%s -xf -" % self.name()
1829 utils.system(command)
1831 def gather_root_sfi(self):
1832 utils.system("mkdir -p logs/sfi.%s"%self.name())
1833 to_plc = self.actual_command_in_guest("tar -C /root/sfi/ -cf - .")
1834 command = to_plc + "| tar -C logs/sfi.%s -xf -"%self.name()
1835 utils.system(command)
1837 def gather_nodes_var_logs(self):
1838 for site_spec in self.plc_spec['sites']:
1839 test_site = TestSite(self, site_spec)
1840 for node_spec in site_spec['nodes']:
1841 test_node = TestNode(self, test_site, node_spec)
1842 test_ssh = TestSsh(test_node.name(), key="keys/key_admin.rsa")
1843 command = test_ssh.actual_command("tar -C /var/log -cf - .")
1844 command = command + "| tar -C logs/node.var-log.%s -xf -" % test_node.name()
1845 utils.system("mkdir -p logs/node.var-log.%s" % test_node.name())
1846 utils.system(command)
1849 # returns the filename to use for sql dump/restore, using options.dbname if set
1850 def dbfile(self, database):
1851 # uses options.dbname if it is found
1853 name = self.options.dbname
1854 if not isinstance(name, StringTypes):
1860 return "/root/%s-%s.sql" % (database, name)
1862 def plc_db_dump(self):
1863 'dump the planetlab5 DB in /root in the PLC - filename has time'
1864 dump=self.dbfile("planetab5")
1865 self.run_in_guest('pg_dump -U pgsqluser planetlab5 -f '+ dump)
1866 utils.header('Dumped planetlab5 database in %s' % dump)
1869 def plc_db_restore(self):
1870 'restore the planetlab5 DB - looks broken, but run -n might help'
1871 dump = self.dbfile("planetab5")
1872 ##stop httpd service
1873 self.run_in_guest('service httpd stop')
1874 # xxx - need another wrapper
1875 self.run_in_guest_piped('echo drop database planetlab5', 'psql --user=pgsqluser template1')
1876 self.run_in_guest('createdb -U postgres --encoding=UNICODE --owner=pgsqluser planetlab5')
1877 self.run_in_guest('psql -U pgsqluser planetlab5 -f ' + dump)
1878 ##starting httpd service
1879 self.run_in_guest('service httpd start')
1881 utils.header('Database restored from ' + dump)
1884 def create_ignore_steps():
1885 for step in TestPlc.default_steps + TestPlc.other_steps:
1886 # default step can have a plc qualifier
1888 step, qualifier = step.split('@')
1889 # or be defined as forced or ignored by default
1890 for keyword in ['_ignore','_force']:
1891 if step.endswith(keyword):
1892 step=step.replace(keyword,'')
1893 if step == SEP or step == SEPSFA :
1895 method = getattr(TestPlc,step)
1896 name = step + '_ignore'
1897 wrapped = ignore_result(method)
1898 # wrapped.__doc__ = method.__doc__ + " (run in ignore-result mode)"
1899 setattr(TestPlc, name, wrapped)
1902 # def ssh_slice_again_ignore (self): pass
1904 # def check_initscripts_ignore (self): pass
1906 def standby_1_through_20(self):
1907 """convenience function to wait for a specified number of minutes"""
1910 def standby_1(): pass
1912 def standby_2(): pass
1914 def standby_3(): pass
1916 def standby_4(): pass
1918 def standby_5(): pass
1920 def standby_6(): pass
1922 def standby_7(): pass
1924 def standby_8(): pass
1926 def standby_9(): pass
1928 def standby_10(): pass
1930 def standby_11(): pass
1932 def standby_12(): pass
1934 def standby_13(): pass
1936 def standby_14(): pass
1938 def standby_15(): pass
1940 def standby_16(): pass
1942 def standby_17(): pass
1944 def standby_18(): pass
1946 def standby_19(): pass
1948 def standby_20(): pass
1950 # convenience for debugging the test logic
1951 def yes(self): return True
1952 def no(self): return False
1953 def fail(self): return False