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