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