1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
8 from TestUser import TestUser
9 from TestBoxQemu import TestBoxQemu
10 from TestSsh import TestSsh
15 def __init__ (self, test_auth_sfa, slice_spec):
16 self.test_auth_sfa=test_auth_sfa
17 self.slice_spec=slice_spec
19 self.test_plc=self.test_auth_sfa.test_plc
21 def qualified(self,name): return self.test_auth_sfa.qualified(name)
22 def hrn (self): return self.qualified(self.slice_spec['name'])
23 def sfi_path (self): return self.test_auth_sfa.sfi_path()
25 # send back up to the TestAuthSfa
26 def rspec_style (self): return self.test_auth_sfa.rspec_style()
27 def sfi_pi(self,*args,**kwds): return self.test_auth_sfa.sfi_pi(*args, **kwds)
28 def sfi_user(self,*args,**kwds): return self.test_auth_sfa.sfi_user(*args, **kwds)
30 def discover_option(self):
31 if self.rspec_style()=='pg': return "-r protogeni"
34 # those are step names exposed as methods of TestPlc, hence the _sfa
36 # needs to be run as pi
37 def sfa_add_slice(self,options):
38 "run sfi add (on Registry) from slice.xml"
40 sfi_command += " --type slice"
41 sfi_command += " --xrn %s"%self.qualified(self.slice_spec['name'])
42 for opt in self.slice_spec['sfi_options']:
43 sfi_command += " %s"%(opt)
44 return self.test_plc.run_in_guest(self.sfi_pi(sfi_command))==0
46 # helper - filename to store a given result
47 def _resname (self,name,ext): return "%s.%s"%(name,ext)
48 def adfile (self): return self._resname("ad","rspec")
49 def reqfile (self): return self._resname("req","rspec")
50 def nodefile (self): return self._resname("nodes","txt")
53 def sfa_discover(self,options):
54 "discover resources into resouces_in.rspec"
55 return self.test_plc.run_in_guest(self.sfi_user(\
56 "resources %s -o %s/%s"% (self.discover_option(),self.sfi_path(),self.adfile())))==0
58 # run sfi create as a regular user
59 def sfa_create_slice(self,options):
60 "run sfi create (on SM) - 1st time"
62 "sfiListNodes.py -i %s/%s -o %s/%s"%(self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile()),
63 "sfiAddSliver.py -i %s/%s -n %s/%s -o %s/%s"%\
64 (self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile(),self.sfi_path(),self.reqfile()),
65 self.sfi_user("create %s %s"%(self.hrn(),self.reqfile())),
67 for command in commands:
68 if self.test_plc.run_in_guest(command)!=0: return False
72 return "%s_%s"%(self.test_auth_sfa.login_base,self.slice_spec['name'])
74 # all local nodes in slice ?
75 def sfa_check_slice_plc (self,options):
76 "check sfa_create_slice at the plcs - all local nodes should be in slice"
77 slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), self.plc_name())[0]
78 nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
81 if node['node_id'] in slice['node_ids']:
82 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
84 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
88 # actually the same for now
89 def sfa_update_slice(self,options):
90 "run sfi create (on SM) on existing object"
91 return self.sfa_create_slice(options)
94 def sfa_delete_slice(self,options):
96 self.test_plc.run_in_guest(self.sfi_pi("delete %s"%(self.hrn(),)))
97 return self.test_plc.run_in_guest(self.sfi_pi("remove -t slice %s"%(self.hrn(),)))==0
99 def locate_private_key(self):
100 return self.test_plc.locate_private_key_from_key_names ( [ self.slice_spec['key_name'] ] )
102 # check the resulting sliver
103 def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
104 "tries to ssh-enter the SFA slice"
105 timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
106 graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
108 private_key=self.locate_private_key()
110 utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
113 # convert nodenames to real hostnames
116 for nodename in self.slice_spec['nodenames']:
117 (site_spec,node_spec) = self.test_plc.locate_node(nodename)
118 tocheck.append(node_spec['node_fields']['hostname'])
120 utils.header("checking ssh access into slice %s on nodes %r"%(self.plc_name(),tocheck))
121 utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
122 (timeout_minutes,silent_minutes,period))
124 for hostname in tocheck:
125 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
126 date_test_ssh = TestSsh (hostname,key=private_key,username=self.plc_name())
127 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
128 date = utils.system (command, silent=datetime.datetime.now() < graceout)
130 utils.header("Successfuly entered slice %s on %s"%(self.plc_name(),hostname))
131 tocheck.remove(hostname)
133 # real nodes will have been checked once in case they're up - skip if not
134 if TestNode.is_real_model(node_spec['node_fields']['model']):
135 utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.plc_name(),hostname))
136 tocheck.remove(hostname)
137 # nm restart after first failure, if requested
138 if options.forcenm and hostname not in restarted:
139 utils.header ("forcenm option : restarting nm on %s"%hostname)
140 restart_test_ssh=TestSsh(hostname,key="keys/key_admin.rsa")
141 access=restart_test_ssh.actual_command('service nm restart')
143 utils.header('nm restarted on %s'%hostname)
145 utils.header('Failed to restart nm on %s'%(hostname))
146 restarted.append(hostname)
150 if datetime.datetime.now() > timeout:
151 for hostname in tocheck:
152 utils.header("FAILURE to ssh into %s@%s"%(self.plc_name(),hostname))
154 # wait for the period