1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
6 from datetime import datetime, timedelta
9 from TestNode import TestNode
10 from TestUser import TestUser
11 from TestBoxQemu import TestBoxQemu
12 from TestSsh import TestSsh
14 from Completer import Completer, CompleterTask
15 from TestSlice import CompleterTaskSshSlice
19 def __init__ (self, test_auth_sfa, slice_spec):
20 self.test_auth_sfa=test_auth_sfa
21 self.slice_spec=slice_spec
23 self.test_plc=self.test_auth_sfa.test_plc
25 def qualified(self,name): return self.test_auth_sfa.qualified(name)
26 def hrn (self): return self.qualified(self.slice_spec['name'])
27 def sfi_path (self): return self.test_auth_sfa.sfi_path()
29 # send back up to the TestAuthSfa
30 def rspec_style (self): return self.test_auth_sfa.rspec_style()
31 def sfi_pi(self,*args,**kwds): return self.test_auth_sfa.sfi_pi(*args, **kwds)
32 def sfi_user(self,*args,**kwds): return self.test_auth_sfa.sfi_user(*args, **kwds)
34 def discover_option(self):
35 if self.rspec_style()=='pg': return "-r GENI"
38 # those are step names exposed as methods of TestPlc, hence the _sfa
40 # needs to be run as pi
41 def sfa_add_slice(self,options):
42 "run sfi add (on Registry)"
44 sfi_command += " --type slice"
45 sfi_command += " --xrn %s"%self.hrn()
46 for opt in self.slice_spec['add_options']:
47 sfi_command += " %s"%(opt)
48 return self.test_plc.run_in_guest(self.sfi_pi(sfi_command))==0
50 def sfa_renew_slice(self, options):
51 "run sfi renew (on Aggregates)"
52 too_late = datetime.now() + timedelta(weeks=52)
53 one_month = datetime.now() + timedelta(weeks=4)
54 # we expect this to fail on too long term attemps, but to succeed otherwise
56 for ( renew_until, expected) in [ (too_late, False), (one_month, True) ] :
58 sfi_command += " %s"%self.hrn()
59 sfi_command += " %s"%renew_until.strftime("%Y-%m-%d")
60 succeeded = self.test_plc.run_in_guest(self.sfi_user(sfi_command))==0
61 if succeeded!=expected:
62 utils.header ("Expecting success=%s, got %s"%(expected,succeeded))
63 # however it turns out sfi renew always returns fine....
65 # so for helping manual checks:
66 sfi_command="show -k hrn -k expires %s"%self.hrn()
67 self.test_plc.run_in_guest(self.sfi_user(sfi_command))
70 # helper - filename to store a given result
71 def _resname (self,name,ext): return "%s.%s"%(name,ext)
72 def adfile (self): return self._resname("ad","rspec")
73 def reqfile (self): return self._resname("req","rspec")
74 def nodefile (self): return self._resname("nodes","txt")
77 def sfa_discover(self,options):
78 "discover resources into resouces_in.rspec"
79 return self.test_plc.run_in_guest(self.sfi_user(\
80 "resources %s -o %s/%s"% (self.discover_option(),self.sfi_path(),self.adfile())))==0
82 # run sfi create as a regular user
83 def sfa_create_slice(self,options):
84 "run sfi create (on SM) - 1st time"
86 "sfiListNodes.py -i %s/%s -o %s/%s"%(self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile()),
87 "sfiAddSliver.py -i %s/%s -n %s/%s -o %s/%s"%\
88 (self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile(),self.sfi_path(),self.reqfile()),
89 self.sfi_user("allocate %s %s"%(self.hrn(),self.reqfile())),
90 self.sfi_user("provision %s"%(self.hrn())),
92 for command in commands:
93 if self.test_plc.run_in_guest(command)!=0: return False
97 return "%s_%s"%(self.test_auth_sfa.login_base,self.slice_spec['name'])
99 # all local nodes in slice ?
100 def sfa_check_slice_plc (self,options):
101 "check sfa_create_slice at the plcs - all local nodes should be in slice"
102 slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), self.plc_name())[0]
103 nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
106 if node['node_id'] in slice['node_ids']:
107 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
109 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
113 # actually the same for now
114 def sfa_update_slice(self,options):
115 "run sfi create (on SM) on existing object"
116 return self.sfa_create_slice(options)
119 def sfa_delete_slice(self,options):
121 self.test_plc.run_in_guest(self.sfi_pi("delete %s"%(self.hrn(),)))
122 return self.test_plc.run_in_guest(self.sfi_pi("remove -t slice %s"%(self.hrn(),)))==0
124 def locate_private_key(self):
125 return self.test_plc.locate_private_key_from_key_names ( [ self.slice_spec['key_name'] ] )
127 # check the resulting sliver
128 def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=0,period_seconds=15):
129 "tries to ssh-enter the SFA slice"
130 timeout = timedelta(minutes=timeout_minutes)
131 graceout = timedelta(minutes=silent_minutes)
132 period = timedelta(seconds=period_seconds)
134 private_key=self.locate_private_key()
136 utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
138 command="echo hostname ; hostname; echo id; id; echo uname -a ; uname -a"
141 slicename=self.plc_name()
142 dry_run = getattr(options,'dry_run',False)
143 for nodename in self.slice_spec['nodenames']:
144 (site_spec,node_spec) = self.test_plc.locate_node(nodename)
145 tasks.append( CompleterTaskSshSlice(self.test_plc,node_spec['node_fields']['hostname'],
146 slicename,private_key,command,expected=True,dry_run=dry_run))
147 return Completer (tasks).run (timeout, graceout, period)