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