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