a few more tweaks
[tests.git] / system / TestSliceSfa.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4
5 import datetime
6
7 import utils
8 from TestUser import TestUser
9 from TestBoxQemu import TestBoxQemu
10 from TestSsh import TestSsh
11
12
13 class TestSliceSfa:
14
15     def __init__ (self, test_auth_sfa, slice_spec):
16         self.test_auth_sfa=test_auth_sfa
17         self.slice_spec=slice_spec
18         # shortcuts
19         self.test_plc=self.test_auth_sfa.test_plc
20
21     def qualified(self,name): return self.test_auth_sfa.qualified(name)
22     def hrn (self): return self.qualified(self.slice_spec['name'])
23     def sfi_path (self): return self.test_auth_sfa.sfi_path()
24
25     # send back up to the TestAuthSfa
26     def rspec_style (self): return self.test_auth_sfa.rspec_style()
27     def sfi_pi(self,*args,**kwds): return self.test_auth_sfa.sfi_pi(*args, **kwds)
28     def sfi_user(self,*args,**kwds): return self.test_auth_sfa.sfi_user(*args, **kwds)
29
30     def discover_option(self):
31         if self.rspec_style()=='pg': return "-r protogeni"
32         else:                        return "-r sfa"
33
34     # those are step names exposed as methods of TestPlc, hence the _sfa
35
36     # needs to be run as pi
37     def sfa_add_slice(self,options):
38         "run sfi add (on Registry) from slice.xml"
39         sfi_command="add"
40         sfi_command += " --type slice"
41         sfi_command += " --xrn %s"%self.qualified(self.slice_spec['name'])
42         for opt in self.slice_spec['sfi_options']:
43             sfi_command += " %s"%(opt)
44         return self.test_plc.run_in_guest(self.sfi_pi(sfi_command))==0
45
46     # helper - filename to store a given result
47     def _resname (self,name,ext): return "%s.%s"%(name,ext)
48     def adfile (self): return self._resname("ad","rspec")
49     def reqfile (self): return self._resname("req","rspec")
50     def nodefile (self): return self._resname("nodes","txt")
51     
52     # run as user
53     def sfa_discover(self,options):
54         "discover resources into resouces_in.rspec"
55         return self.test_plc.run_in_guest(self.sfi_user(\
56                 "resources %s -o %s/%s"% (self.discover_option(),self.sfi_path(),self.adfile())))==0
57
58     # run sfi create as a regular user
59     def sfa_create_slice(self,options):
60         "run sfi create (on SM) - 1st time"
61         commands=[
62             "sfiListNodes.py -i %s/%s -o %s/%s"%(self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile()),
63             "sfiAddSliver.py -i %s/%s -n %s/%s -o %s/%s"%\
64                 (self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile(),self.sfi_path(),self.reqfile()),
65             self.sfi_user("create %s %s"%(self.hrn(),self.reqfile())),
66             ]
67         for command in commands:
68             if self.test_plc.run_in_guest(command)!=0: return False
69         return True
70
71     def plc_name (self):
72         return "%s_%s"%(self.test_auth_sfa.login_base,self.slice_spec['name'])
73
74     # all local nodes in slice ?
75     def sfa_check_slice_plc (self,options):
76         "check sfa_create_slice at the plcs - all local nodes should be in slice"
77         slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), self.plc_name())[0]
78         nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
79         result=True
80         for node in nodes: 
81             if node['node_id'] in slice['node_ids']:
82                 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
83             else:
84                 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
85                 result=False
86         return result
87
88     # actually the same for now
89     def sfa_update_slice(self,options):
90         "run sfi create (on SM) on existing object"
91         return self.sfa_create_slice(options)
92
93     # run as pi
94     def sfa_delete_slice(self,options):
95         "run sfi delete"
96         self.test_plc.run_in_guest(self.sfi_pi("delete %s"%(self.hrn(),)))
97         return self.test_plc.run_in_guest(self.sfi_pi("remove -t slice %s"%(self.hrn(),)))==0
98
99     def locate_private_key(self):
100         return self.test_plc.locate_private_key_from_key_names ( [ self.slice_spec['key_name'] ] )
101
102     # check the resulting sliver
103     def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
104         "tries to ssh-enter the SFA slice"
105         timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
106         graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
107         # locate a key
108         private_key=self.locate_private_key()
109         if not private_key :
110             utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
111             return False
112
113         # convert nodenames to real hostnames
114         restarted=[]
115         tocheck=[]
116         for nodename in self.slice_spec['nodenames']:
117             (site_spec,node_spec) = self.test_plc.locate_node(nodename)
118             tocheck.append(node_spec['node_fields']['hostname'])
119
120         utils.header("checking ssh access into slice %s on nodes %r"%(self.plc_name(),tocheck))
121         utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
122                          (timeout_minutes,silent_minutes,period))
123         while tocheck:
124             for hostname in tocheck:
125                 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
126                 date_test_ssh = TestSsh (hostname,key=private_key,username=self.plc_name())
127                 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
128                 date = utils.system (command, silent=datetime.datetime.now() < graceout)
129                 if date==0:
130                     utils.header("Successfuly entered slice %s on %s"%(self.plc_name(),hostname))
131                     tocheck.remove(hostname)
132                 else:
133                     # real nodes will have been checked once in case they're up - skip if not
134                     if TestNode.is_real_model(node_spec['node_fields']['model']):
135                         utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.plc_name(),hostname))
136                         tocheck.remove(hostname)
137                     # nm restart after first failure, if requested 
138                     if options.forcenm and hostname not in restarted:
139                         utils.header ("forcenm option : restarting nm on %s"%hostname)
140                         restart_test_ssh=TestSsh(hostname,key="keys/key_admin.rsa")
141                         access=restart_test_ssh.actual_command('service nm restart')
142                         if (access==0):
143                             utils.header('nm restarted on %s'%hostname)
144                         else:
145                             utils.header('Failed to restart nm on %s'%(hostname))
146                         restarted.append(hostname)
147             if not tocheck:
148                 # we're done
149                 return True
150             if datetime.datetime.now() > timeout:
151                 for hostname in tocheck:
152                     utils.header("FAILURE to ssh into %s@%s"%(self.plc_name(),hostname))
153                 return False
154             # wait for the period
155             time.sleep (period)
156         # for an empty slice
157         return True
158
159