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