check sfa slice right away with plcapi
[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.sfa_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 locate_key(self):
32         for username,keyname in self.slice_spec['usernames']:
33                 key_spec=self.test_plc.locate_key(keyname)
34                 test_key=TestKey(self.test_plc,key_spec)
35                 publickey=test_key.publicpath()
36                 privatekey=test_key.privatepath()
37                 if os.path.isfile(publickey) and os.path.isfile(privatekey):
38                     found=True
39         return (found,privatekey)
40
41     # those are step names exposed as methods of TestPlc, hence the _sfa
42     def sfa_add_slice(self,options):
43         return self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ add slice.xml")==0
44
45     def sfa_discover(self,options):
46         return self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ resources -o /root/.sfi/resources_in.rspec")==0
47
48     def sfa_create_slice(self,options):
49         root_auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
50         commands=[
51             "sfiListNodes.py -i /root/.sfi/resources_in.rspec -o /root/.sfi/all_nodes.txt",
52             "sfiAddSliver.py -i /root/.sfi/resources_in.rspec -n /root/.sfi/all_nodes.txt -o /root/.sfi/resources_out.rspec",
53             "sfi.py -d /root/.sfi/ create %s.%s.%s resources_out.rspec"%(root_auth,self.login_base,self.slicename),
54             ]
55         for command in commands:
56             if self.test_plc.run_in_guest(command)!=0: return False
57         return True
58
59     # all local nodes in slice ?
60     def sfa_check_slice_plc (self,options):
61         slice_fields = self.slice_spec['slice_fields']
62         slice_name = slice_fields['name']
63         slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), slice_name)[0]
64         nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
65         result=True
66         for node in nodes: 
67             if node['node_id'] in slice['node_ids']:
68                 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
69             else:
70                 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
71                 result=False
72         return result
73
74     # actually the same for now
75     def sfa_update_slice(self,options):
76         return self.sfa_create_slice(options)
77
78     def sfa_delete_slice(self,options):
79         root_auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
80         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ delete %s.%s.%s"%(root_auth,self.login_base,self.slicename))
81         return self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ remove -t slice %s.%s.%s"%(root_auth,self.login_base,self.slicename))==0
82
83     # check the resulting sliver
84     def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
85         timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
86         graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
87         # locate a key
88         (found,remote_privatekey)=self.locate_key()
89         if not found :
90             utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
91             return False
92
93         # convert nodenames to real hostnames
94         slice_spec = self.slice_spec
95         restarted=[]
96         tocheck=[]
97         for nodename in slice_spec['nodenames']:
98             (site_spec,node_spec) = self.test_plc.locate_node(nodename)
99             tocheck.append(node_spec['node_fields']['hostname'])
100
101         utils.header("checking ssh access into slice %s on nodes %r"%(self.name(),tocheck))
102         utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
103                          (timeout_minutes,silent_minutes,period))
104         while tocheck:
105             for hostname in tocheck:
106                 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
107                 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.name())
108                 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
109                 date = utils.system (command, silent=datetime.datetime.now() < graceout)
110                 if date==0:
111                     utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
112                     tocheck.remove(hostname)
113                 else:
114                     # real nodes will have been checked once in case they're up - skip if not
115                     if TestNode.is_real_model(node_spec['node_fields']['model']):
116                         utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.name(),hostname))
117                         tocheck.remove(hostname)
118                     # nm restart after first failure, if requested 
119                     if options.forcenm and hostname not in restarted:
120                         utils.header ("forcenm option : restarting nm on %s"%hostname)
121                         restart_test_ssh=TestSsh(hostname,key="keys/key1.rsa")
122                         access=restart_test_ssh.actual_command('service nm restart')
123                         if (access==0):
124                             utils.header('nm restarted on %s'%hostname)
125                         else:
126                             utils.header('Failed to restart nm on %s'%(hostname))
127                         restarted.append(hostname)
128             if not tocheck:
129                 # we're done
130                 return True
131             if datetime.datetime.now() > timeout:
132                 for hostname in tocheck:
133                     utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
134                 return False
135             # wait for the period
136             time.sleep (period)
137         # for an empty slice
138         return True