modifications realted to special account
[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.sfaslicea1 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.sfaslicea1 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.sfaslicea1"%auth)
50         return \
51         self.test_plc.run_in_guest("sfi.py -d /root/.sfi/ remove -t slice %s.main.sfaslicea1"%auth)==0
52
53     def check_slice_sfa(self,options,timeout_minutes=40,silent_minutes=4,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                 if datetime.datetime.now() >= graceout:
78                     utils.header('Trying to enter into slice %s@%s'%(self.name(),hostname))
79                 # this can be ran locally as we have the key
80                 date = date_test_ssh.run("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")==0
81                 if date:
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