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