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