1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
9 from TestKey import TestKey
10 from TestUser import TestUser
11 from TestNode import TestNode
12 from TestSsh import TestSsh
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)
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']
29 return self.slice_spec['slice_fields']['name']
31 def mode (self): return self.slice_spec['mode']
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"
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):
54 return (found,privatekey)
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'])
63 utils.header ("(Over)wrote %s"%file_name)
65 file_name=dir_name + os.sep + 'person.xml'
66 fileconf=open(file_name,'w')
67 fileconf.write(self.slice_spec['slice_person_xml'])
70 utils.header ("(Over)wrote %s"%file_name)
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 slice.xml")==0
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
79 def sfa_create_slice(self,options):
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()),
85 for command in commands:
86 if self.test_plc.run_in_guest(command)!=0: return False
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})
97 if node['node_id'] in slice['node_ids']:
98 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
100 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
104 # actually the same for now
105 def sfa_update_slice(self,options):
106 return self.sfa_create_slice(options)
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
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)
117 (found,remote_privatekey)=self.locate_key()
119 utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
122 # convert nodenames to real hostnames
123 slice_spec = self.slice_spec
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'])
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))
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)
140 utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
141 tocheck.remove(hostname)
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')
153 utils.header('nm restarted on %s'%hostname)
155 utils.header('Failed to restart nm on %s'%(hostname))
156 restarted.append(hostname)
160 if datetime.datetime.now() > timeout:
161 for hostname in tocheck:
162 utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
164 # wait for the period