rename sfi_show as sfi_show_site and add a new sfa_show_slice
[tests.git] / system / TestAuthSfa.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4 import os, os.path
5
6 import utils
7 from TestSsh import TestSsh
8 from TestUserSfa import TestUserSfa
9 from TestSliceSfa import TestSliceSfa
10
11
12 def slice_sfa_mapper (method):
13     def actual(self,*args, **kwds):
14         overall=True
15         slice_method = TestSliceSfa.__dict__[method.__name__]
16         with self.auth_sfa_spec['slice_spec'] as slice_spec:
17             test_slice_sfa = TestSliceSfa(self,slice_spec)
18             if not slice_method(test_slice_sfa, *args, **kwds): overall=False
19         return overall
20     # restore the doc text
21     actual.__doc__=TestSliceSfa.__dict__[method.__name__].__doc__
22     return actual
23
24
25 def user_sfa_mapper (method):
26     def actual(self,*args, **kwds):
27         overall=True
28         user_method = TestUserSfa.__dict__[method.__name__]
29         for user_spec in [ self.auth_sfa_spec['user_spec'] ]:
30             test_user_sfa = TestUserSfa(self,user_spec)
31             if not user_method(test_user_sfa, *args, **kwds): overall=False
32         return overall
33     # restore the doc text
34     actual.__doc__=TestUserSfa.__dict__[method.__name__].__doc__
35     return actual
36
37
38 class TestAuthSfa:
39
40     def __init__ (self,test_plc,auth_sfa_spec):
41         self.test_plc=test_plc
42         self.auth_sfa_spec=auth_sfa_spec
43         self.test_ssh=TestSsh(self.test_plc.test_ssh)
44 #        # shortcuts
45         self.login_base=self.auth_sfa_spec['login_base']
46 #        self.piuser=self.auth_sfa_spec['piuser']
47 #        self.regularuser=self.auth_sfa_spec['regularuser']
48 #        self.slicename=self.auth_sfa_spec['slicename']
49 #        
50 #    def plc_name(self):
51 #        return self.auth_sfa_spec['plc_slicename']
52     
53     def rspec_style (self): return self.auth_sfa_spec['rspec_style']
54
55     def sfi_path (self):
56         return "/root/sfi/%s"%(self.rspec_style())
57
58     # the hrn for the root authority
59     def root_hrn (self):
60         return self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
61
62     # the hrn for the auth/site
63     def auth_hrn (self):
64         return "%s.%s"%(self.root_hrn(),self.login_base)
65
66     # something in this site (users typically); for use by classes for subobjects
67     def obj_hrn (self, name):
68         return "%s.%s"%(self.auth_hrn(),name)
69
70     # xxx this needs tweaks with more recent versions of sfa that have pgv2 as the default ?
71     # dir_name is local and will be pushed later on by TestPlc
72     # by default set SFI_USER to the pi, we'll overload this
73     # on the command line when needed
74     def sfi_configure (self,dir_name):
75         plc_spec=self.test_plc.plc_spec
76         # cheat a bit: retrieve the global SFA spec from the plc obj
77         sfa_spec=self.test_plc.plc_spec['sfa']
78         # fetch keys in config spec and expose to sfi
79         for spec_name in ['pi_spec','user_spec']:
80             user_spec=self.auth_sfa_spec[spec_name]
81             user_leaf=user_spec['name']
82             key_name=user_spec['key_name']
83             key_spec = self.test_plc.locate_key (key_name)
84             for (kind,ext) in [ ('private', 'pkey'), ('public', 'pub') ] :
85                 contents=key_spec[kind]
86                 file_name=os.path.join(dir_name,self.obj_hrn(user_leaf))+"."+ext
87                 fileconf=open(file_name,'w')
88                 fileconf.write (contents)
89                 fileconf.close()
90                 utils.header ("(Over)wrote %s"%file_name)
91         #
92         file_name=dir_name + os.sep + 'sfi_config'
93         fileconf=open(file_name,'w')
94         SFI_AUTH=self.auth_hrn()
95         fileconf.write ("SFI_AUTH='%s'"%SFI_AUTH)
96         fileconf.write('\n')
97         # default is to run as a PI
98         SFI_USER=self.obj_hrn(self.auth_sfa_spec['pi_spec']['name'])
99         fileconf.write ("SFI_USER='%s'"%SFI_USER)
100         fileconf.write('\n')
101         SFI_REGISTRY='http://' + sfa_spec['SFA_REGISTRY_HOST'] + ':12345/'
102         fileconf.write ("SFI_REGISTRY='%s'"%SFI_REGISTRY)
103         fileconf.write('\n')
104         SFI_SM='http://' + sfa_spec['SFA_SM_HOST'] + ':12347/'
105         fileconf.write ("SFI_SM='%s'"%SFI_SM)
106         fileconf.write('\n')
107         fileconf.close()
108         utils.header ("(Over)wrote %s"%file_name)
109
110     # using sfaadmin to bootstrap
111     def sfa_add_site (self, options):
112         "bootstrap a site using sfaadmin"
113         command="sfaadmin reg register -t authority -x %s"%self.auth_hrn()
114         return self.test_plc.run_in_guest(command)==0
115
116     def sfa_add_pi (self, options):
117         "bootstrap a PI user for that site"
118         pi_spec = self.auth_sfa_spec['pi_spec']
119         pi_hrn=self.obj_hrn(pi_spec['name'])
120         pi_mail=pi_spec['email']
121         # as installed by sfi_config
122         pi_key=os.path.join(self.sfi_path(),self.obj_hrn(pi_spec['name']+'.pub'))
123         command="sfaadmin reg register -t user -x %s --email %s --key %s"%(pi_hrn,pi_mail,pi_key)
124         if self.test_plc.run_in_guest(command)!=0: return False
125         command="sfaadmin reg update -t authority -x %s --pi %s"%(self.auth_hrn(),pi_hrn)
126         return self.test_plc.run_in_guest(command)==0
127
128     # run as pi
129     def sfi_pi (self, command):
130         pi_name=self.auth_sfa_spec['pi_spec']['name']
131         return "sfi -d %s -u %s %s"%(self.sfi_path(),self.obj_hrn(pi_name), command,)
132     # the sfi command line option to run as a regular user
133     def sfi_user (self, command):
134         user_name=self.auth_sfa_spec['user_spec']['name']
135         return "sfi -d %s -u %s %s"%(self.sfi_path(),self.obj_hrn(user_name), command,)
136
137     # user management
138     @user_sfa_mapper
139     def sfa_add_user (self, *args, **kwds): pass
140     @user_sfa_mapper
141     def sfa_update_user (self, *args, **kwds): pass
142     @user_sfa_mapper
143     def sfa_delete_user (self, *args, **kwds): pass
144
145     def sfi_list (self, options):
146         "run (as regular user) sfi list (on Registry)"
147         return \
148             self.test_plc.run_in_guest(self.sfi_user("list -r %s"%self.root_hrn()))==0 and \
149             self.test_plc.run_in_guest(self.sfi_user("list %s"%(self.auth_hrn())))==0
150
151     def sfi_show_slice (self, options):
152         "run (as PI) sfi show -n <slice> (on Registry)"
153         slice_spec=self.auth_sfa_spec['slice_spec']
154         slice_hrn=self.obj_hrn(slice_spec['name'])
155         return \
156             self.test_plc.run_in_guest(self.sfi_pi("show -n %s"%slice_hrn))==0
157
158     def sfi_show_site (self, options):
159         "run (as regular user) sfi show (on Registry)"
160         return \
161             self.test_plc.run_in_guest(self.sfi_user("show %s"%(self.auth_hrn())))==0
162
163     # those are step names exposed as methods of TestPlc, hence the _sfa
164     @slice_sfa_mapper
165     def sfa_add_slice (self, *args, **kwds): pass
166     @slice_sfa_mapper
167     def sfa_renew_slice (self, *args, **kwds): pass
168     @slice_sfa_mapper
169     def sfa_discover (self, *args, **kwds): pass
170     @slice_sfa_mapper
171     def sfa_create_slice (self, *args, **kwds): pass
172     @slice_sfa_mapper
173     def sfa_check_slice_plc (self, *args, **kwds): pass
174     @slice_sfa_mapper
175     def sfa_update_slice (self, *args, **kwds): pass
176     @slice_sfa_mapper
177     def sfa_delete_slice (self, *args, **kwds): pass
178     @slice_sfa_mapper
179     def ssh_slice_sfa     (self, *args, **kwds): pass
180