1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
9 from TestKey import TestKey
10 from TestUser import TestUser
11 from TestNode import TestNode
12 from TestSsh import TestSsh
13 from TestUserSfa import TestUserSfa
17 def __init__ (self,test_plc,test_site,sfa_slice_spec):
18 self.test_plc=test_plc
19 self.test_site=test_site
20 self.sfa_slice_spec=sfa_slice_spec
21 self.test_ssh=TestSsh(self.test_plc.test_ssh)
23 self.sfa_spec=test_plc.plc_spec['sfa']
24 self.piuser=self.sfa_slice_spec['piuser']
25 self.regularuser=self.sfa_slice_spec['regularuser']
26 self.slicename=self.sfa_slice_spec['slicename']
27 self.login_base=self.sfa_slice_spec['login_base']
30 return self.sfa_slice_spec['slice_fields']['name']
32 def rspec_style (self): return self.sfa_slice_spec['rspec_style']
35 root_auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
36 return "%s.%s.%s"%(root_auth,self.login_base,self.slicename)
37 # def resname (self,name,ext): return "%s_%s.%s"%(self.slicename,name,ext)
38 def resname (self,name,ext): return "%s.%s"%(name,ext)
40 def addslicefile (self): return self.resname("addslice","xml")
41 def addpersonfile (self): return self.resname("addperson","xml")
42 def adfile (self): return self.resname("ad","rspec")
43 def reqfile (self): return self.resname("req","rspec")
44 def nodefile (self): return self.resname("nodes","txt")
45 def discover_option(self):
46 if self.rspec_style()=='pg': return "-r protogeni"
50 return "/root/sfi/%s"%self.slicename
53 for username,keyname in self.sfa_slice_spec['usernames']:
54 key_spec=self.test_plc.locate_key(keyname)
55 test_key=TestKey(self.test_plc,key_spec)
56 publickey=test_key.publicpath()
57 privatekey=test_key.privatepath()
58 if os.path.isfile(publickey) and os.path.isfile(privatekey):
60 return (found,privatekey)
62 # dir_name is local and will be pushed later on by TestPlc
63 def sfi_config (self,dir_name):
64 plc_spec=self.test_plc.plc_spec
65 sfa_spec=self.sfa_spec
66 sfa_slice_spec=self.sfa_slice_spec
68 file_name=dir_name + os.sep + self.piuser + '.pkey'
69 fileconf=open(file_name,'w')
70 fileconf.write (plc_spec['keys'][0]['private'])
72 utils.header ("(Over)wrote %s"%file_name)
74 file_name=dir_name + os.sep + self.addpersonfile()
75 fileconf=open(file_name,'w')
76 fileconf.write(sfa_slice_spec['slice_person_xml'])
79 utils.header ("(Over)wrote %s"%file_name)
81 file_name=dir_name + os.sep + 'sfi_config'
82 fileconf=open(file_name,'w')
83 SFI_AUTH="%s.%s"%(sfa_spec['SFA_REGISTRY_ROOT_AUTH'],self.login_base)
84 fileconf.write ("SFI_AUTH='%s'"%SFI_AUTH)
86 SFI_USER=SFI_AUTH + '.' + self.piuser
87 fileconf.write ("SFI_USER='%s'"%SFI_USER)
89 SFI_REGISTRY='http://' + sfa_spec['SFA_REGISTRY_HOST'] + ':12345/'
90 fileconf.write ("SFI_REGISTRY='%s'"%SFI_REGISTRY)
92 SFI_SM='http://' + sfa_spec['SFA_SM_HOST'] + ':12347/'
93 fileconf.write ("SFI_SM='%s'"%SFI_SM)
96 utils.header ("(Over)wrote %s"%file_name)
98 file_name=dir_name + os.sep + self.addslicefile()
99 fileconf=open(file_name,'w')
100 fileconf.write(sfa_slice_spec['slice_add_xml'])
102 utils.header ("(Over)wrote %s"%file_name)
106 def sfa_add_user (self, options):
107 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).add_user()
108 def sfa_update_user (self, options):
109 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).update_user()
110 def sfa_delete_user (self, options):
111 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).delete_user()
114 # those are step names exposed as methods of TestPlc, hence the _sfa
115 def sfa_view (self, options):
116 "run sfi.py list and sfi.py show (both on Registry) and sfi.py slices (on SM)"
117 root_auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
119 self.test_plc.run_in_guest("sfi.py -d %s list %s.%s"%(self.sfi_path(),root_auth,self.login_base))==0 and \
120 self.test_plc.run_in_guest("sfi.py -d %s show %s.%s"%(self.sfi_path(),root_auth,self.login_base))==0 and \
121 self.test_plc.run_in_guest("sfi.py -d %s slices"%self.sfi_path())==0
123 def sfa_add_slice(self,options):
124 return self.test_plc.run_in_guest("sfi.py -d %s add %s"%(self.sfi_path(),self.addslicefile()))==0
126 def sfa_discover(self,options):
127 return self.test_plc.run_in_guest("sfi.py -d %s resources %s -o %s/%s"%\
128 (self.sfi_path(),self.discover_option(),self.sfi_path(),self.adfile()))==0
130 def sfa_create_slice(self,options):
132 "sfiListNodes.py -i %s/%s -o %s/%s"%(self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile()),
133 "sfiAddSliver.py -i %s/%s -n %s/%s -o %s/%s"%\
134 (self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile(),self.sfi_path(),self.reqfile()),
135 "sfi.py -d %s create %s %s"%(self.sfi_path(),self.hrn(),self.reqfile()),
137 for command in commands:
138 if self.test_plc.run_in_guest(command)!=0: return False
141 # all local nodes in slice ?
142 def sfa_check_slice_plc (self,options):
143 slice_fields = self.sfa_slice_spec['slice_fields']
144 slice_name = slice_fields['name']
145 slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), slice_name)[0]
146 nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
149 if node['node_id'] in slice['node_ids']:
150 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
152 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
156 # actually the same for now
157 def sfa_update_slice(self,options):
158 return self.sfa_create_slice(options)
160 def sfa_delete_slice(self,options):
161 self.test_plc.run_in_guest("sfi.py -d %s delete %s"%(self.sfi_path(),self.hrn()))
162 return self.test_plc.run_in_guest("sfi.py -d %s remove -t slice %s"%(self.sfi_path(),self.hrn()))==0
164 # check the resulting sliver
165 def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
166 timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
167 graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
169 (found,remote_privatekey)=self.locate_key()
171 utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
174 # convert nodenames to real hostnames
175 sfa_slice_spec = self.sfa_slice_spec
178 for nodename in sfa_slice_spec['nodenames']:
179 (site_spec,node_spec) = self.test_plc.locate_node(nodename)
180 tocheck.append(node_spec['node_fields']['hostname'])
182 utils.header("checking ssh access into slice %s on nodes %r"%(self.name(),tocheck))
183 utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
184 (timeout_minutes,silent_minutes,period))
186 for hostname in tocheck:
187 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
188 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.name())
189 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
190 date = utils.system (command, silent=datetime.datetime.now() < graceout)
192 utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
193 tocheck.remove(hostname)
195 # real nodes will have been checked once in case they're up - skip if not
196 if TestNode.is_real_model(node_spec['node_fields']['model']):
197 utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.name(),hostname))
198 tocheck.remove(hostname)
199 # nm restart after first failure, if requested
200 if options.forcenm and hostname not in restarted:
201 utils.header ("forcenm option : restarting nm on %s"%hostname)
202 restart_test_ssh=TestSsh(hostname,key="keys/key1.rsa")
203 access=restart_test_ssh.actual_command('service nm restart')
205 utils.header('nm restarted on %s'%hostname)
207 utils.header('Failed to restart nm on %s'%(hostname))
208 restarted.append(hostname)
212 if datetime.datetime.now() > timeout:
213 for hostname in tocheck:
214 utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
216 # wait for the period