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