use more compact slice names pslc<x> and fslc<x> for plc- and sfa- based slices resp.
[tests.git] / system / TestSliceSfa.py
1 import utils
2 import os, os.path
3 import datetime
4 import time
5
6 from TestKey import TestKey
7 from TestUser import TestUser
8 from TestNode import TestNode
9 from TestSsh import TestSsh
10
11 class TestSliceSfa:
12
13     def __init__ (self,test_plc,test_site,slice_spec):
14         self.test_plc=test_plc
15         self.test_site=test_site
16         #self.slice_spec=plc_spec_sfa['slices_sfa'][0]
17         self.slice_spec=slice_spec
18         self.test_ssh=TestSsh(self.test_plc.test_ssh)
19         
20     def name(self):
21         return self.slice_spec['slice_fields']['name']
22     
23     def locate_key(self):
24         for username,keyname in self.slice_spec['usernames']:
25                 key_spec=self.test_plc.locate_key(keyname)
26                 test_key=TestKey(self.test_plc,key_spec)
27                 publickey=test_key.publicpath()
28                 privatekey=test_key.privatepath()
29                 if os.path.isfile(publickey) and os.path.isfile(privatekey):
30                     found=True
31         return (found,privatekey)
32
33     def add_slice(self):
34         return \
35         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ add slice.xml")==0
36
37     def create_slice(self):
38         auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
39         return \
40         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ create %s.main.fslc1 slice.rspec"%auth)==0
41
42     def update_slice(self):
43         auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
44         return \
45         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ create %s.main.fslc1 slice.rspec"%auth)==0
46
47     def delete_slice(self):
48         auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
49         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ delete %s.main.fslc1"%auth)
50         return \
51         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ remove -t slice %s.main.fslc1"%auth)==0
52
53     def check_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
54         timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
55         graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
56         # locate a key
57         (found,remote_privatekey)=self.locate_key()
58         if not found :
59             utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
60             return False
61
62         # convert nodenames to real hostnames
63         slice_spec = self.slice_spec
64         restarted=[]
65         tocheck=[]
66         for nodename in slice_spec['nodenames']:
67             (site_spec,node_spec) = self.test_plc.locate_node(nodename)
68             tocheck.append(node_spec['node_fields']['hostname'])
69
70         utils.header("checking ssh access into slice %s on nodes %r"%(self.name(),tocheck))
71         utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
72                          (timeout_minutes,silent_minutes,period))
73         while tocheck:
74             for hostname in tocheck:
75                 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
76                 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.name())
77                 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
78                 date = utils.system (command, silent=datetime.datetime.now() < graceout)
79                 if date==0:
80                     utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
81                     tocheck.remove(hostname)
82                 else:
83                     # real nodes will have been checked once in case they're up - skip if not
84                     if TestNode.is_real_model(node_spec['node_fields']['model']):
85                         utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.name(),hostname))
86                         tocheck.remove(hostname)
87                     # nm restart after first failure, if requested 
88                     if options.forcenm and hostname not in restarted:
89                         utils.header ("forcenm option : restarting nm on %s"%hostname)
90                         restart_test_ssh=TestSsh(hostname,key="keys/key1.rsa")
91                         access=restart_test_ssh.actual_command('service nm restart')
92                         if (access==0):
93                             utils.header('nm restarted on %s'%hostname)
94                         else:
95                             utils.header('Failed to restart nm on %s'%(hostname))
96                         restarted.append(hostname)
97             if not tocheck:
98                 # we're done
99                 return True
100             if datetime.datetime.now() > timeout:
101                 for hostname in tocheck:
102                     utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
103                 return False
104             # wait for the period
105             time.sleep (period)
106         # for an empty slice
107         return True