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