missing import
[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     # all local nodes in slice ?
72     def sfa_check_slice_plc (self,options):
73         "check sfa_create_slice at the plcs - all local nodes should be in slice"
74         login_base=self.test_auth_sfa.login_base
75         slice_name = "%s_%s"%(login_base,self.slice_spec['name'])
76         slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), slice_name)[0]
77         nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
78         result=True
79         for node in nodes: 
80             if node['node_id'] in slice['node_ids']:
81                 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
82             else:
83                 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
84                 result=False
85         return result
86
87     # actually the same for now
88     def sfa_update_slice(self,options):
89         "run sfi create (on SM) on existing object"
90         return self.sfa_create_slice(options)
91
92     # run as pi
93     def sfa_delete_slice(self,options):
94         "run sfi delete"
95         self.test_plc.run_in_guest(self.sfi_pi("delete %s"%(self.hrn(),)))
96         return self.test_plc.run_in_guest(self.sfi_pi("remove -t slice %s"%(self.hrn(),)))==0
97
98     # check the resulting sliver
99     def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
100         "tries to ssh-enter the SFA slice"
101         timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
102         graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
103         # locate a key
104         (found,remote_privatekey)=self.locate_key()
105         if not found :
106             utils.header("WARNING: Cannot find a valid key for slice %s"%self.plc_name())
107             return False
108
109         # convert nodenames to real hostnames
110         restarted=[]
111         tocheck=[]
112         for nodename in self.auth_sfa_spec['nodenames']:
113             (site_spec,node_spec) = self.test_plc.locate_node(nodename)
114             tocheck.append(node_spec['node_fields']['hostname'])
115
116         utils.header("checking ssh access into slice %s on nodes %r"%(self.plc_name(),tocheck))
117         utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
118                          (timeout_minutes,silent_minutes,period))
119         while tocheck:
120             for hostname in tocheck:
121                 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
122                 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.plc_name())
123                 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
124                 date = utils.system (command, silent=datetime.datetime.now() < graceout)
125                 if date==0:
126                     utils.header("Successfuly entered slice %s on %s"%(self.plc_name(),hostname))
127                     tocheck.remove(hostname)
128                 else:
129                     # real nodes will have been checked once in case they're up - skip if not
130                     if TestNode.is_real_model(node_spec['node_fields']['model']):
131                         utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.plc_name(),hostname))
132                         tocheck.remove(hostname)
133                     # nm restart after first failure, if requested 
134                     if options.forcenm and hostname not in restarted:
135                         utils.header ("forcenm option : restarting nm on %s"%hostname)
136                         restart_test_ssh=TestSsh(hostname,key="keys/key_admin.rsa")
137                         access=restart_test_ssh.actual_command('service nm restart')
138                         if (access==0):
139                             utils.header('nm restarted on %s'%hostname)
140                         else:
141                             utils.header('Failed to restart nm on %s'%(hostname))
142                         restarted.append(hostname)
143             if not tocheck:
144                 # we're done
145                 return True
146             if datetime.datetime.now() > timeout:
147                 for hostname in tocheck:
148                     utils.header("FAILURE to ssh into %s@%s"%(self.plc_name(),hostname))
149                 return False
150             # wait for the period
151             time.sleep (period)
152         # for an empty slice
153         return True
154
155