updated method calls
[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 GENI"
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)"
41         sfi_command="add"
42         sfi_command += " --type slice"
43         sfi_command += " --xrn %s"%self.hrn()
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     def sfa_renew_slice(self, options):
49         "run sfi renew (on Aggregates)"
50         too_late = datetime.datetime.now()+datetime.timedelta(weeks=52)
51         one_month = datetime.datetime.now()+datetime.timedelta(weeks=4)
52         # we expect this to fail on too long term attemps, but to succeed otherwise
53         overall=True
54         for ( renew_until, expected) in [ (too_late, False), (one_month, True) ] :
55             sfi_command="renew"
56             sfi_command += " %s"%self.hrn()
57             sfi_command += " %s"%renew_until.strftime("%Y-%m-%d")
58             succeeded = self.test_plc.run_in_guest(self.sfi_user(sfi_command))==0
59             if succeeded!=expected:
60                 utils.header ("Expecting success=%s, got %s"%(expected,succeeded))
61                 # however it turns out sfi renew always returns fine....
62                 #overall=False
63             # so for helping manual checks:
64             sfi_command="show -k hrn -k expires %s"%self.hrn()
65             self.test_plc.run_in_guest(self.sfi_user(sfi_command))
66         return overall
67
68     # helper - filename to store a given result
69     def _resname (self,name,ext): return "%s.%s"%(name,ext)
70     def adfile (self): return self._resname("ad","rspec")
71     def reqfile (self): return self._resname("req","rspec")
72     def nodefile (self): return self._resname("nodes","txt")
73     
74     # run as user
75     def sfa_discover(self,options):
76         "discover resources into resouces_in.rspec"
77         return self.test_plc.run_in_guest(self.sfi_user(\
78                 "resources %s -o %s/%s"% (self.discover_option(),self.sfi_path(),self.adfile())))==0
79
80     # run sfi create as a regular user
81     def sfa_create_slice(self,options):
82         "run sfi create (on SM) - 1st time"
83         commands=[
84             "sfiListNodes.py -i %s/%s -o %s/%s"%(self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile()),
85             "sfiAddSliver.py -i %s/%s -n %s/%s -o %s/%s"%\
86                 (self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile(),self.sfi_path(),self.reqfile()),
87             self.sfi_user("allocate %s %s"%(self.hrn(),self.reqfile())),
88             self.sfi_user("provision %s"%(self.hrn())),
89             ]
90         for command in commands:
91             if self.test_plc.run_in_guest(command)!=0: return False
92         return True
93
94     def plc_name (self):
95         return "%s_%s"%(self.test_auth_sfa.login_base,self.slice_spec['name'])
96
97     # all local nodes in slice ?
98     def sfa_check_slice_plc (self,options):
99         "check sfa_create_slice at the plcs - all local nodes should be in slice"
100         slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), self.plc_name())[0]
101         nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
102         result=True
103         for node in nodes: 
104             if node['node_id'] in slice['node_ids']:
105                 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
106             else:
107                 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
108                 result=False
109         return result
110
111     # actually the same for now
112     def sfa_update_slice(self,options):
113         "run sfi create (on SM) on existing object"
114         return self.sfa_create_slice(options)
115
116     # run as pi
117     def sfa_delete_slice(self,options):
118         "run sfi delete"
119         self.test_plc.run_in_guest(self.sfi_pi("delete %s"%(self.hrn(),)))
120         return self.test_plc.run_in_guest(self.sfi_pi("remove -t slice %s"%(self.hrn(),)))==0
121
122     def locate_private_key(self):
123         return self.test_plc.locate_private_key_from_key_names ( [ self.slice_spec['key_name'] ] )
124
125     # check the resulting sliver
126     def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
127         "tries to ssh-enter the SFA slice"
128         timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
129         graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
130         # locate a key
131         private_key=self.locate_private_key()
132         if not private_key :
133             utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
134             return False
135
136         # convert nodenames to real hostnames
137         restarted=[]
138         tocheck=[]
139         for nodename in self.slice_spec['nodenames']:
140             (site_spec,node_spec) = self.test_plc.locate_node(nodename)
141             tocheck.append(node_spec['node_fields']['hostname'])
142
143         utils.header("checking ssh access into slice %s on nodes %r"%(self.plc_name(),tocheck))
144         utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
145                          (timeout_minutes,silent_minutes,period))
146         while tocheck:
147             for hostname in tocheck:
148                 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
149                 date_test_ssh = TestSsh (hostname,key=private_key,username=self.plc_name())
150                 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
151                 date = utils.system (command, silent=datetime.datetime.now() < graceout)
152                 if date==0:
153                     utils.header("Successfuly entered slice %s on %s"%(self.plc_name(),hostname))
154                     tocheck.remove(hostname)
155                 else:
156                     # real nodes will have been checked once in case they're up - skip if not
157                     if TestNode.is_real_model(node_spec['node_fields']['model']):
158                         utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.plc_name(),hostname))
159                         tocheck.remove(hostname)
160                     # nm restart after first failure, if requested 
161                     if options.forcenm and hostname not in restarted:
162                         utils.header ("forcenm option : restarting nm on %s"%hostname)
163                         restart_test_ssh=TestSsh(hostname,key="keys/key_admin.rsa")
164                         access=restart_test_ssh.actual_command('service nm restart')
165                         if (access==0):
166                             utils.header('nm restarted on %s'%hostname)
167                         else:
168                             utils.header('Failed to restart nm on %s'%(hostname))
169                         restarted.append(hostname)
170             if not tocheck:
171                 # we're done
172                 return True
173             if datetime.datetime.now() > timeout:
174                 for hostname in tocheck:
175                     utils.header("FAILURE to ssh into %s@%s"%(self.plc_name(),hostname))
176                 return False
177             # wait for the period
178             time.sleep (period)
179         # for an empty slice
180         return True
181
182