renaming steps big time - more consistent - easier to remember
[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         auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
47         return self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ resources -o /root/.sfi/resources_in.rspec")==0
48
49     def sfa_create_slice(self,options):
50         auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
51         commands=[
52             "sfiListNodes.py -i /root/.sfi/resources_in.rspec -o /root/.sfi/all_nodes.txt",
53             "sfiAddSliver.py -i /root/.sfi/resources_in.rspec -n /root/.sfi/all_nodes.txt -o /root/.sfi/resources_out.rspec",
54             "sfi.py -d /root/.sfi/ create %s.%s.%s resources_out.rspec"%(auth,self.login_base,self.slicename),
55             ]
56         for command in commands:
57             if self.test_plc.run_in_guest(command)!=0: return False
58         return True
59
60     # actually the same for now
61     def sfa_update_slice(self,options):
62         return self.sfa_create_slice(options)
63
64     def sfa_delete_slice(self,options):
65         auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
66         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ delete %s.%s.%s"%(auth,self.login_base,self.slicename))
67         return self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ remove -t slice %s.%s.%s"%(auth,self.login_base,self.slicename))==0
68
69     # check the resulting sliver
70     def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
71         timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
72         graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
73         # locate a key
74         (found,remote_privatekey)=self.locate_key()
75         if not found :
76             utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
77             return False
78
79         # convert nodenames to real hostnames
80         slice_spec = self.slice_spec
81         restarted=[]
82         tocheck=[]
83         for nodename in slice_spec['nodenames']:
84             (site_spec,node_spec) = self.test_plc.locate_node(nodename)
85             tocheck.append(node_spec['node_fields']['hostname'])
86
87         utils.header("checking ssh access into slice %s on nodes %r"%(self.name(),tocheck))
88         utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
89                          (timeout_minutes,silent_minutes,period))
90         while tocheck:
91             for hostname in tocheck:
92                 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
93                 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.name())
94                 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
95                 date = utils.system (command, silent=datetime.datetime.now() < graceout)
96                 if date==0:
97                     utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
98                     tocheck.remove(hostname)
99                 else:
100                     # real nodes will have been checked once in case they're up - skip if not
101                     if TestNode.is_real_model(node_spec['node_fields']['model']):
102                         utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.name(),hostname))
103                         tocheck.remove(hostname)
104                     # nm restart after first failure, if requested 
105                     if options.forcenm and hostname not in restarted:
106                         utils.header ("forcenm option : restarting nm on %s"%hostname)
107                         restart_test_ssh=TestSsh(hostname,key="keys/key1.rsa")
108                         access=restart_test_ssh.actual_command('service nm restart')
109                         if (access==0):
110                             utils.header('nm restarted on %s'%hostname)
111                         else:
112                             utils.header('Failed to restart nm on %s'%(hostname))
113                         restarted.append(hostname)
114             if not tocheck:
115                 # we're done
116                 return True
117             if datetime.datetime.now() > timeout:
118                 for hostname in tocheck:
119                     utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
120                 return False
121             # wait for the period
122             time.sleep (period)
123         # for an empty slice
124         return True