configurable rspec styles using -y
[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     def hrn(self): 
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)
39
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"
47         else: return ""
48
49     def sfi_path (self):
50         return "/root/sfi/%s"%self.slicename
51
52     def locate_key(self):
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):
59                     found=True
60         return (found,privatekey)
61
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
67         #
68         file_name=dir_name + os.sep + self.piuser + '.pkey'
69         fileconf=open(file_name,'w')
70         fileconf.write (plc_spec['keys'][0]['private'])
71         fileconf.close()
72         utils.header ("(Over)wrote %s"%file_name)
73         #
74         file_name=dir_name + os.sep + self.addpersonfile()
75         fileconf=open(file_name,'w')
76         fileconf.write(sfa_slice_spec['slice_person_xml'])
77         fileconf.write('\n')
78         fileconf.close()
79         utils.header ("(Over)wrote %s"%file_name)
80         #
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)
85         fileconf.write('\n')
86         SFI_USER=SFI_AUTH + '.' + self.piuser
87         fileconf.write ("SFI_USER='%s'"%SFI_USER)
88         fileconf.write('\n')
89         SFI_REGISTRY='http://' + sfa_spec['SFA_PLC_DB_HOST'] + ':12345/'
90         fileconf.write ("SFI_REGISTRY='%s'"%SFI_REGISTRY)
91         fileconf.write('\n')
92         SFI_SM='http://' + sfa_spec['SFA_PLC_DB_HOST'] + ':12347/'
93         fileconf.write ("SFI_SM='%s'"%SFI_SM)
94         fileconf.write('\n')
95         fileconf.close()
96         utils.header ("(Over)wrote %s"%file_name)
97         #
98         file_name=dir_name + os.sep + self.addslicefile()
99         fileconf=open(file_name,'w')
100         fileconf.write(sfa_slice_spec['slice_add_xml'])
101         fileconf.write('\n')
102         utils.header ("(Over)wrote %s"%file_name)
103         fileconf.close()
104
105     # user management
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()
112
113
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']
118         return \
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 
122
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
125
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
129
130     def sfa_create_slice(self,options):
131         commands=[
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()),
136             ]
137         for command in commands:
138             if self.test_plc.run_in_guest(command)!=0: return False
139         return True
140
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})
147         result=True
148         for node in nodes: 
149             if node['node_id'] in slice['node_ids']:
150                 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
151             else:
152                 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
153                 result=False
154         return result
155
156     # actually the same for now
157     def sfa_update_slice(self,options):
158         return self.sfa_create_slice(options)
159
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
163
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)
168         # locate a key
169         (found,remote_privatekey)=self.locate_key()
170         if not found :
171             utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
172             return False
173
174         # convert nodenames to real hostnames
175         sfa_slice_spec = self.sfa_slice_spec
176         restarted=[]
177         tocheck=[]
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'])
181
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))
185         while tocheck:
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)
191                 if date==0:
192                     utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
193                     tocheck.remove(hostname)
194                 else:
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')
204                         if (access==0):
205                             utils.header('nm restarted on %s'%hostname)
206                         else:
207                             utils.header('Failed to restart nm on %s'%(hostname))
208                         restarted.append(hostname)
209             if not tocheck:
210                 # we're done
211                 return True
212             if datetime.datetime.now() > timeout:
213                 for hostname in tocheck:
214                     utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
215                 return False
216             # wait for the period
217             time.sleep (period)
218         # for an empty slice
219         return True
220