don't use protogeni yet as the tests are not yet smooth with that
[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
14 class TestSliceSfa:
15
16     def __init__ (self,test_plc,test_site,slice_spec):
17         self.test_plc=test_plc
18         self.test_site=test_site
19         self.slice_spec=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_spec['piuser']
24         self.regularuser=self.sfa_spec['regularuser']
25         self.slicename=self.slice_spec['slicename']
26         self.login_base=self.sfa_spec['login_base']
27         
28     def name(self):
29         return self.slice_spec['slice_fields']['name']
30     
31     def mode (self): return self.slice_spec['mode']
32
33     def hrn(self): 
34         root_auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
35         return "%s.%s.%s"%(root_auth,self.login_base,self.slicename)
36     def addslicefile (self):
37         return self.resname("addslice","xml")
38     def resname (self,name,ext): return "%s_%s.%s"%(self.slicename,name,ext)
39     def adfile (self): return self.resname("ad","xml")
40     def reqfile (self): return self.resname("req","xml")
41     def nodefile (self): return self.resname("nodes","txt")
42     def discover_option(self):
43         if self.mode()=='pg': return "-r protogeni"
44         else: return ""
45
46     def locate_key(self):
47         for username,keyname in self.slice_spec['usernames']:
48                 key_spec=self.test_plc.locate_key(keyname)
49                 test_key=TestKey(self.test_plc,key_spec)
50                 publickey=test_key.publicpath()
51                 privatekey=test_key.privatepath()
52                 if os.path.isfile(publickey) and os.path.isfile(privatekey):
53                     found=True
54         return (found,privatekey)
55
56     # dir_name is local and will be pushed later on
57     def sfi_config (self,dir_name):
58         # TODO this needs to be done remotely
59         file_name=dir_name + os.sep + self.addslicefile()
60         fileconf=open(file_name,'w')
61         fileconf.write(self.slice_spec['slice_add_xml'])
62         fileconf.write('\n')
63         utils.header ("(Over)wrote %s"%file_name)
64         fileconf.close()
65         file_name=dir_name + os.sep + 'person.xml'
66         fileconf=open(file_name,'w')
67         fileconf.write(self.slice_spec['slice_person_xml'])
68         fileconf.write('\n')
69         fileconf.close()
70         utils.header ("(Over)wrote %s"%file_name)
71         
72     # those are step names exposed as methods of TestPlc, hence the _sfa
73     def sfa_add_slice(self,options):
74         return self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ add %s"%(self.addslicefile()))==0
75
76     def sfa_discover(self,options):
77         return self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ %s resources -o /root/.sfi/%s"%(self.discover_option(),self.adfile()))==0
78
79     def sfa_create_slice(self,options):
80         commands=[
81             "sfiListNodes.py -i /root/.sfi/%s -o /root/.sfi/%s"%(self.adfile(),self.nodefile()),
82             "sfiAddSliver.py -i /root/.sfi/%s -n /root/.sfi/%s -o /root/.sfi/%s"%(self.adfile(),self.nodefile(),self.reqfile()),
83             "sfi.py -d /root/.sfi/ create %s %s"%(self.hrn(),self.reqfile()),
84             ]
85         for command in commands:
86             if self.test_plc.run_in_guest(command)!=0: return False
87         return True
88
89     # all local nodes in slice ?
90     def sfa_check_slice_plc (self,options):
91         slice_fields = self.slice_spec['slice_fields']
92         slice_name = slice_fields['name']
93         slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), slice_name)[0]
94         nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
95         result=True
96         for node in nodes: 
97             if node['node_id'] in slice['node_ids']:
98                 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
99             else:
100                 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
101                 result=False
102         return result
103
104     # actually the same for now
105     def sfa_update_slice(self,options):
106         return self.sfa_create_slice(options)
107
108     def sfa_delete_slice(self,options):
109         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ delete %s.%s.%s"%(root_auth,self.login_base,self.slicename))
110         return self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ remove -t slice %s"%(self.hrn()))==0
111
112     # check the resulting sliver
113     def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
114         timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
115         graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
116         # locate a key
117         (found,remote_privatekey)=self.locate_key()
118         if not found :
119             utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
120             return False
121
122         # convert nodenames to real hostnames
123         slice_spec = self.slice_spec
124         restarted=[]
125         tocheck=[]
126         for nodename in slice_spec['nodenames']:
127             (site_spec,node_spec) = self.test_plc.locate_node(nodename)
128             tocheck.append(node_spec['node_fields']['hostname'])
129
130         utils.header("checking ssh access into slice %s on nodes %r"%(self.name(),tocheck))
131         utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
132                          (timeout_minutes,silent_minutes,period))
133         while tocheck:
134             for hostname in tocheck:
135                 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
136                 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.name())
137                 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
138                 date = utils.system (command, silent=datetime.datetime.now() < graceout)
139                 if date==0:
140                     utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
141                     tocheck.remove(hostname)
142                 else:
143                     # real nodes will have been checked once in case they're up - skip if not
144                     if TestNode.is_real_model(node_spec['node_fields']['model']):
145                         utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.name(),hostname))
146                         tocheck.remove(hostname)
147                     # nm restart after first failure, if requested 
148                     if options.forcenm and hostname not in restarted:
149                         utils.header ("forcenm option : restarting nm on %s"%hostname)
150                         restart_test_ssh=TestSsh(hostname,key="keys/key1.rsa")
151                         access=restart_test_ssh.actual_command('service nm restart')
152                         if (access==0):
153                             utils.header('nm restarted on %s'%hostname)
154                         else:
155                             utils.header('Failed to restart nm on %s'%(hostname))
156                         restarted.append(hostname)
157             if not tocheck:
158                 # we're done
159                 return True
160             if datetime.datetime.now() > timeout:
161                 for hostname in tocheck:
162                     utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
163                 return False
164             # wait for the period
165             time.sleep (period)
166         # for an empty slice
167         return True