renamed sfa_* into sfi_* for steps list/show/slices
[tests.git] / system / TestSliceSfa.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4 import utils
5 import os, os.path
6 import datetime
7 import time
8
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
14
15 class TestSliceSfa:
16
17     def __init__ (self,test_plc,sfa_slice_spec):
18         self.test_plc=test_plc
19         self.sfa_slice_spec=sfa_slice_spec
20         self.test_ssh=TestSsh(self.test_plc.test_ssh)
21         # shortcuts
22         self.sfa_spec=test_plc.plc_spec['sfa']
23         self.piuser=self.sfa_slice_spec['piuser']
24         self.regularuser=self.sfa_slice_spec['regularuser']
25         self.slicename=self.sfa_slice_spec['slicename']
26         self.login_base=self.sfa_slice_spec['login_base']
27         
28     def name(self):
29         return self.sfa_slice_spec['slice_fields']['name']
30     
31     def rspec_style (self): return self.sfa_slice_spec['rspec_style']
32
33     # the hrn for the site
34     def auth_hrn (self):
35         return self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
36
37     # the hrn for the site
38     def site_hrn (self):
39         return "%s.%s"%(self.auth_hrn(),self.login_base)
40
41     # something in the site (users typically)
42     def qualified_hrn (self, name):
43         return "%s.%s"%(self.site_hrn(),name)
44
45     # the slice hrn
46     def hrn(self): return self.qualified_hrn (self.slicename)
47
48     # result name
49     def resname (self,name,ext): return "%s.%s"%(name,ext)
50
51     def addslicefile (self): return self.resname("slice_record","xml")
52     def addpersonfile (self): return self.resname("person_record","xml")
53     def adfile (self): return self.resname("ad","rspec")
54     def reqfile (self): return self.resname("req","rspec")
55     def nodefile (self): return self.resname("nodes","txt")
56     # xxx this needs tweaks with more recent versions of sfa that have pgv2 as the default ?
57     def discover_option(self):
58         if self.rspec_style()=='pg': return "-r protogeni"
59         else:                        return "-r sfa"
60
61     def sfi_path (self):
62         return "/root/sfi/%s%s"%(self.slicename,self.rspec_style())
63
64     def locate_key(self):
65         for username,keyname in self.sfa_slice_spec['usernames']:
66                 key_spec=self.test_plc.locate_key(keyname)
67                 test_key=TestKey(self.test_plc,key_spec)
68                 publickey=test_key.publicpath()
69                 privatekey=test_key.privatepath()
70                 if os.path.isfile(publickey) and os.path.isfile(privatekey):
71                     found=True
72         return (found,privatekey)
73
74     # dir_name is local and will be pushed later on by TestPlc
75     # by default set SFI_USER to the pi, we'll overload this
76     # on the command line when needed
77     def sfi_config (self,dir_name):
78         plc_spec=self.test_plc.plc_spec
79         sfa_spec=self.sfa_spec
80         sfa_slice_spec=self.sfa_slice_spec
81         keys=plc_spec['keys']
82         for (contents,name) in [ (keys[0]['private'],             self.piuser+'.pkey'),
83                                  (keys[0]['key_fields']['key'],   self.piuser+'.pub'),
84                                  (keys[1]['private'],             self.regularuser+'.pkey'),
85                                  (keys[0]['key_fields']['key'],   self.regularuser+'.pub'),
86                                 ]:
87             file_name=os.path.join(dir_name,self.qualified_hrn(name))
88             fileconf=open(file_name,'w')
89             fileconf.write (contents)
90             fileconf.close()
91             utils.header ("(Over)wrote %s"%file_name)
92         #
93         file_name=dir_name + os.sep + self.addpersonfile()
94         fileconf=open(file_name,'w')
95         fileconf.write(sfa_slice_spec['person_record'])
96         fileconf.write('\n')
97         fileconf.close()
98         utils.header ("(Over)wrote %s"%file_name)
99         #
100         file_name=dir_name + os.sep + 'sfi_config'
101         fileconf=open(file_name,'w')
102         SFI_AUTH="%s"%(self.site_hrn())
103         fileconf.write ("SFI_AUTH='%s'"%SFI_AUTH)
104         fileconf.write('\n')
105         SFI_USER=SFI_AUTH + '.' + self.piuser
106         fileconf.write ("SFI_USER='%s'"%SFI_USER)
107         fileconf.write('\n')
108         SFI_REGISTRY='http://' + sfa_spec['SFA_REGISTRY_HOST'] + ':12345/'
109         fileconf.write ("SFI_REGISTRY='%s'"%SFI_REGISTRY)
110         fileconf.write('\n')
111         SFI_SM='http://' + sfa_spec['SFA_SM_HOST'] + ':12347/'
112         fileconf.write ("SFI_SM='%s'"%SFI_SM)
113         fileconf.write('\n')
114         fileconf.close()
115         utils.header ("(Over)wrote %s"%file_name)
116         #
117         file_name=dir_name + os.sep + self.addslicefile()
118         fileconf=open(file_name,'w')
119         fileconf.write(sfa_slice_spec['slice_record'])
120         fileconf.write('\n')
121         utils.header ("(Over)wrote %s"%file_name)
122         fileconf.close()
123
124     # using sfaadmin to bootstrap
125     def sfa_add_site (self, options):
126         "bootstrap a site using sfaadmin"
127         command="sfaadmin reg register -t authority -x %s"%self.site_hrn()
128         return self.test_plc.run_in_guest(command)==0
129
130     def sfa_add_pi (self, options):
131         "bootstrap a PI user for that site"
132         pi_hrn=self.qualified_hrn(self.piuser)
133         pi_mail=self.sfa_slice_spec['pimail']
134         # as installed by sfi_config
135         pi_key=os.path.join(self.sfi_path(),self.qualified_hrn(self.piuser+'.pub'))
136         command="sfaadmin reg register -t user -x %s --email %s --key %s"%(pi_hrn,pi_mail,pi_key)
137         if self.test_plc.run_in_guest(command)!=0: return False
138         command="sfaadmin reg update -t authority -x %s --pi %s"%(self.site_hrn(),pi_hrn)
139         return self.test_plc.run_in_guest(command)==0
140
141     # user management
142     def sfa_add_user (self, options):
143         "add a regular user using sfi.py add"
144         return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).add_user()
145     def sfa_update_user (self, options):
146         "update a user record using sfi.py update"
147         return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).update_user()
148     def sfa_delete_user (self, options):
149         "run sfi.py delete"
150         return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).delete_user()
151
152     # run as pi
153     def sfi_pi (self, command):
154         return "sfi.py -d %s -u %s %s"%(self.sfi_path(),self.qualified_hrn(self.piuser), command,)
155     # the sfi.py command line option to run as a regular user
156     def sfi_user (self, command):
157         return "sfi.py -d %s -u %s %s"%(self.sfi_path(),self.qualified_hrn(self.regularuser), command,)
158
159     # those are step names exposed as methods of TestPlc, hence the _sfa
160
161     def sfi_list (self, options):
162         "run (as regular user) sfi list (on Registry)"
163         return \
164             self.test_plc.run_in_guest(self.sfi_user("list -r %s"%self.auth_hrn()))==0 and \
165             self.test_plc.run_in_guest(self.sfi_user("list %s"%(self.site_hrn())))==0
166
167     def sfi_show (self, options):
168         "run (as regular user) sfi show (on Registry)"
169         return \
170             self.test_plc.run_in_guest(self.sfi_user("show %s"%(self.site_hrn())))==0
171
172     def sfi_slices (self, options):
173         "run (as regular user) sfi slices (on SM)"
174         return \
175             self.test_plc.run_in_guest(self.sfi_user("slices"))==0 
176
177     # needs to be run as pi
178     def sfa_add_slice(self,options):
179         "run sfi.py add (on Registry) from slice.xml"
180         return self.test_plc.run_in_guest(self.sfi_pi("add %s"%(self.addslicefile())))==0
181
182     # run as user
183     def sfa_discover(self,options):
184         "discover resources into resouces_in.rspec"
185         return self.test_plc.run_in_guest(self.sfi_user(\
186                 "resources %s -o %s/%s"% (self.discover_option(),self.sfi_path(),self.adfile())))==0
187
188     # run sfi create as a regular user
189     def sfa_create_slice(self,options):
190         "run sfi.py create (on SM) - 1st time"
191         commands=[
192             "sfiListNodes.py -i %s/%s -o %s/%s"%(self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile()),
193             "sfiAddSliver.py -i %s/%s -n %s/%s -o %s/%s"%\
194                 (self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile(),self.sfi_path(),self.reqfile()),
195             self.sfi_user("create %s %s"%(self.hrn(),self.reqfile())),
196             ]
197         for command in commands:
198             if self.test_plc.run_in_guest(command)!=0: return False
199         return True
200
201     # all local nodes in slice ?
202     def sfa_check_slice_plc (self,options):
203         "check sfa_create_slice at the plcs - all local nodes should be in slice"
204         slice_fields = self.sfa_slice_spec['slice_fields']
205         slice_name = slice_fields['name']
206         slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), slice_name)[0]
207         nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
208         result=True
209         for node in nodes: 
210             if node['node_id'] in slice['node_ids']:
211                 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
212             else:
213                 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
214                 result=False
215         return result
216
217     # actually the same for now
218     def sfa_update_slice(self,options):
219         "run sfi.py create (on SM) on existing object"
220         return self.sfa_create_slice(options)
221
222     # run as pi
223     def sfa_delete_slice(self,options):
224         "run sfi.py delete"
225         self.test_plc.run_in_guest(self.sfi_pi("delete %s"%(self.hrn(),)))
226         return self.test_plc.run_in_guest(self.sfi_pi("remove -t slice %s"%(self.hrn(),)))==0
227
228     # check the resulting sliver
229     def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
230         "tries to ssh-enter the SFA slice"
231         timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
232         graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
233         # locate a key
234         (found,remote_privatekey)=self.locate_key()
235         if not found :
236             utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
237             return False
238
239         # convert nodenames to real hostnames
240         sfa_slice_spec = self.sfa_slice_spec
241         restarted=[]
242         tocheck=[]
243         for nodename in sfa_slice_spec['nodenames']:
244             (site_spec,node_spec) = self.test_plc.locate_node(nodename)
245             tocheck.append(node_spec['node_fields']['hostname'])
246
247         utils.header("checking ssh access into slice %s on nodes %r"%(self.name(),tocheck))
248         utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
249                          (timeout_minutes,silent_minutes,period))
250         while tocheck:
251             for hostname in tocheck:
252                 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
253                 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.name())
254                 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
255                 date = utils.system (command, silent=datetime.datetime.now() < graceout)
256                 if date==0:
257                     utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
258                     tocheck.remove(hostname)
259                 else:
260                     # real nodes will have been checked once in case they're up - skip if not
261                     if TestNode.is_real_model(node_spec['node_fields']['model']):
262                         utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.name(),hostname))
263                         tocheck.remove(hostname)
264                     # nm restart after first failure, if requested 
265                     if options.forcenm and hostname not in restarted:
266                         utils.header ("forcenm option : restarting nm on %s"%hostname)
267                         restart_test_ssh=TestSsh(hostname,key="keys/key1.rsa")
268                         access=restart_test_ssh.actual_command('service nm restart')
269                         if (access==0):
270                             utils.header('nm restarted on %s'%hostname)
271                         else:
272                             utils.header('Failed to restart nm on %s'%(hostname))
273                         restarted.append(hostname)
274             if not tocheck:
275                 # we're done
276                 return True
277             if datetime.datetime.now() > timeout:
278                 for hostname in tocheck:
279                     utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
280                 return False
281             # wait for the period
282             time.sleep (period)
283         # for an empty slice
284         return True
285