too numerous changes to detail
[plcapi.git] / TestPeers.py
1 #!/usr/bin/env python
2 ###
3 ##############################
4 ###
5 ### preparation / requirements
6 ###
7 ### two separate instances of myplc
8 ### for now they are located on the same box on lurch
9 ###
10 ### expectations :
11 ### your myplcs should more or less come out of the box, 
12 ### I prefer not to alter the default PLC_ROOT_USER value,
13 ### instead we create a PI account on the site_id=1
14 ###
15 ##############################
16
17 # support reloading without wiping everything off
18 # dunno how to do (defvar plc)
19
20 ## we use indexes 1 and 2 
21 try:
22     dir(plc)
23 except:
24     plc=[None,None,None]
25 ## the server objects
26 try:
27     dir(s)
28 except:
29     s=[None,None,None]
30 ## the authentication objects
31 ## our user
32 try:
33     dir(a)
34 except:
35     a=[None,None,None]
36 ## the builtin root user for bootstrapping
37 try:
38     dir(aa)
39 except:
40     aa=[None,None,None]
41
42 ####################
43 import xmlrpclib
44 import os
45
46 plc1={ 'name':'plc1 in federation',
47        'hostname':'lurch.cs.princeton.edu',
48        'url-format':'https://%s:443/PLCAPI/',
49        'builtin_admin_id':'root@localhost.localdomain',
50        'builtin_admin_password':'root',
51        'peer_admin_name':'plc1@planet-lab.org',
52        'peer_admin_password':'peer',
53        'nodename':'n11.plc1.org',
54        'plainname' : 'one',
55        }
56 plc2={ 'name':'plc2 in federation',
57        'hostname':'planetlab-devbox.inria.fr',
58        'url-format':'https://%s:443/PLCAPI/',
59        'builtin_admin_id':'root@localhost.localdomain',
60        'builtin_admin_password':'root',
61        'peer_admin_name':'plc2@planet-lab.org',
62        'peer_admin_password':'peer',
63        'nodename':'n21.plc2.org',
64        'plainname' : 'two',
65        }
66
67 ####################
68 def peer_index(i):
69     return 3-i
70
71 def site_name (i):
72     return 'site'+str(i)
73
74 def slice_name (i):
75     global plc
76     return "slice"+str(i)
77
78 def full_slice_name (i):
79     global plc
80     return plc[i]['plainname']+'_'+slice_name(i)
81
82 ####################
83 def test00_init (args=[1,2]):
84     global plc,s,a,aa
85     ## have you loaded this file already (support for reload)
86     if plc[1]:
87         pass
88     else:
89         plc=[None,plc1,plc2]
90         for i in args:
91             url=plc[i]['url-format']%plc[i]['hostname']
92             plc[i]['url']=url
93             s[i]=xmlrpclib.Server(url)
94             print 'initializing s[%d]'%i,url
95             aa[i]={'Username':plc[i]['builtin_admin_id'],
96                    'AuthMethod':'password',
97                    'AuthString':plc[i]['builtin_admin_password'],
98                    'Role':'admin'}
99             print 'initialized aa[%d]'%i, aa[i]
100             a[i]={'Username':plc[i]['peer_admin_name'],
101                   'AuthMethod':'password',
102                   'AuthString':plc[i]['peer_admin_password'],
103                   'Role':'admin'}
104             print 'initialized a[%d]'%i, a[i]
105
106 def test00_print (args=[1,2]):
107     global plc,s,a,aa
108     for i in args:
109         print 's[%d]'%i,s[i]
110         print 'aa[%d]'%i, aa[i]
111         print 'a[%d]'%i, a[i]
112
113 def check_nodes (en,ef,args=[1,2]):
114     global plc,s,a
115     for i in args:
116         n=len(s[i].GetNodes(a[i]))
117         f=len(s[i].GetForeignNodes(a[i]))
118         print '%02d: Checking connection: got %d local nodes & %d foreign nodes'%(i,n,f)
119         assert n==en
120         assert f==ef
121
122 # expected : local slices, foreign slices
123 def check_slices (els,efs,args=[1,2]):
124     global plc,s,a
125     for i in args:
126         ls=len(s[i].GetSlices(a[i]))
127         fs=len(s[i].GetForeignSlices(a[i]))
128         print '%02d: Checking connection: got %d local slices & %d foreign slices'%(i,ls,fs)
129         assert els==ls
130         assert efs==fs
131
132 def show_nodes (i,node_ids):
133     for message,nodes in [ ['LOC',s[i].GetNodes(a[i],node_ids)],
134                            ['FOR',s[i].GetForeignNodes(a[i],node_ids)] ]:
135         if nodes:
136             print '[%s:%d] : '%(message,len(nodes)),
137             for node in nodes:
138                 print node['hostname']+' ',
139             print ''
140
141 # expected : nodes on local slice
142 def check_local_slice_nodes (eln, args=[1,2]):
143     global plc,s,a
144     for i in args:
145         lname=full_slice_name(i)
146         ls=s[i].GetSlices(a[i],[lname])[0]
147         lsn=ls['node_ids']
148         print '%02d: local slice (%s) on nodes '%(i,lname),lsn
149         assert len(lsn)==eln
150         show_nodes (i,lsn)
151
152 # expected : nodes on foreign slice
153 def check_foreign_slice_nodes (efn, args=[1,2]):
154     global plc,s,a
155     for i in args:
156         peer=peer_index(i)
157         fname=full_slice_name(peer)
158         fs=s[i].GetForeignSlices(a[i],[fname])[0]
159         fsn=fs['node_ids']
160         print '%02d: foreign slice (%s) on nodes '%(i,fname),fsn
161         assert len(fsn)==efn
162         show_nodes (i,fsn)
163
164 def check_conf_files (args=[1,2]):
165     global plc,s,a
166     for i in args:
167         nodename=plc[i]['nodename']
168         ndict= s[i].GetSlivers(a[i],[nodename])[0]
169         assert ndict['hostname'] == nodename
170         conf_files = ndict['conf_files']
171         print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
172         for conf_file in conf_files:
173             print 'source=',conf_file['source'],'|',
174             print 'dest=',conf_file['dest'],'|',
175             print 'enabled=',conf_file['enabled'],'|',
176             print ''
177
178 import pprint
179 pp = pprint.PrettyPrinter(indent=3)
180
181 def check_slivers (esn,args=[1,2]):
182     global plc,s,a
183     for i in args:
184         nodename=plc[i]['nodename']
185         ndict= s[i].GetSlivers(a[i],[nodename])[0]
186         assert ndict['hostname'] == nodename
187         slivers = ndict['slivers']
188         assert len(slivers) == esn
189         print '%02d: %d  slivers in GetSlivers for node %s'%(i,len(slivers),nodename)
190         for sliver in slivers:
191             print '>>slivername = ',sliver['name']
192             pp.pprint(sliver)
193                 
194
195 ####################
196 def test00_admin (args=[1,2]):
197     global plc,s,a
198     for i in args:
199         peer=peer_index(i)
200         person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 'last_name':'PeerPoint', 'role_ids':[10],
201                                               'email':plc[i]['peer_admin_name'],'password':plc[i]['peer_admin_password']})
202         print '%02d: created peer admin account %d, %s - %s'%(i,person_id,plc[i]['peer_admin_name'],plc[i]['peer_admin_password'])
203         plc[i]['peer_admin_id']=person_id
204
205 def test00_enable (args=[1,2]):
206     global plc,s,a
207     for i in args:
208         peer=peer_index(i)
209         s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
210         s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
211         print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
212
213 def test01_node (args=[1,2]):
214     global plc,s,a
215     for i in args:
216         n=s[i].AddNode(a[i],1,{'hostname': plc[i]['nodename']})
217         print '%02d: Added node %d %s'%(i,n,plc[i]['nodename'])
218         plc[i]['node_id']=n
219
220 def test01_delnode (args=[1,2]):
221     global plc,s,a
222     for i in args:
223         retcod=s[i].DeleteNode(a[i],plc[i]['node_id'])
224         print '%02d: Deleted node %d, got %s'%(i,plc[i]['node_id'],retcod)
225         plc[i]['node_id']=None
226
227 def test01_peer_person (args=[1,2]):
228     global plc,s,a
229     for i in args:
230         peer=peer_index(i)
231         person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc[peer]['name'], 'role_ids':[3000],
232                                            'email':plc[peer]['peer_admin_name'],'password':plc[peer]['peer_admin_password']})
233         print '%02d:Created person %d as the peer person'%(i,person_id)
234         plc[i]['peer_person_id']=person_id
235
236 def test01_peer (args=[1,2]):
237     global plc,s,a
238     for i in args:
239         peer=peer_index(i)
240         peer_id=s[i].AddPeer (a[i], {'peername':plc[peer]['name'],'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
241         # NOTE : need to manually reset the encrypted password through SQL at this point
242         print '%02d:Created peer %d'%(i,peer_id)
243         plc[i]['peer_id']=peer_id
244         print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
245
246 def test01_peer_passwd (args=[1,2]):
247     global plc,s,a
248     for i in args:
249         # using an ad-hoc local command for now - never could get quotes to reach sql....
250         print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
251         retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
252         print 'got',retcod
253     
254 def test02_refresh (args=[1,2]):
255     global plc,s,a
256     for i in args:
257         print '%02d: Refreshing peer'%(i),
258         retcod=s[i].RefreshPeer(a[i],plc[i]['peer_id'])
259         print ' got ',retcod
260
261 def test03_site (args=[1,2]):
262     global plc,s,a
263     for i in args:
264         peer=peer_index(i)
265         ### create a site (required for creating a slice)
266         sitename=site_name(i)
267         abbrev_name="abbr"+str(i)
268         site_id=s[i].AddSite (a[i], {'name':plc[i]['name'],
269                                      'abbreviated_name': abbrev_name,
270                                      'login_base': plc[i]['plainname'],
271                                      'is_public': True,
272                                      'url': 'http://%s.com/'%abbrev_name,
273                                      'max_slices':10})
274         ### max_slices does not seem taken into account at that stage
275         s[i].UpdateSite(a[i],site_id,{'max_slices':10})
276         print '%02d: Created site %d with max_slices=10'%(i,site_id)
277         plc[i]['site_id']=site_id
278
279 def test03_slice (args=[1,2]):
280     global plc,s,a
281     for i in args:
282         peer=peer_index(i)
283         plain=full_slice_name(i)
284         ### create a slice
285         name=slice_name(i)
286         slice_id=s[i].AddSlice (a[i],{'name':plain,
287                                       'description':'slice %s on plc %s'%(plain,plc[i]['name']),
288                                       'url':'http://planet-lab.org/%s'%name,
289                                       'max_nodes':100,
290                                       'instanciation':'plc-instantiated',
291                                       })
292         print '%02d: created slice %d'%(i,slice_id)
293         plc[i]['slice_id']=slice_id
294         
295
296 def test04_node_slice (is_local, args=[1,2]):
297     global plc,s,a
298     for i in args:
299         peer=peer_index(i)
300         if is_local:
301             hostname=plc[i]['nodename']
302         else:
303             hostname=plc[peer]['nodename']
304         s[i].AddSliceToNodes (a[i], plc[i]['slice_id'],[hostname])
305         print '%02d: added in slice %d local node %s'%(i,plc[i]['slice_id'],hostname)
306
307 def test04_lnode_slice (args=[1,2]):
308     test04_node_slice (True,args)
309
310 def test04_fnode_slice (args=[1,2]):
311     test04_node_slice (False,args)
312
313 ####################
314 def test_all_init ():
315     test00_init ()
316     test00_print ()
317     test00_admin ()
318     test00_enable ()
319     check_nodes (0,0,)
320     test01_peer_person ()
321     test01_peer ()
322     test01_peer_passwd ()
323
324     test03_site ()
325     test03_slice ()
326
327 def test_all_nodes ():
328     # create one node on each site
329     test01_node ()
330     check_nodes (1,0,)
331     test02_refresh ()
332     check_nodes (1,1,)
333
334     # check deletions
335     test01_delnode ([2])
336     check_nodes (1,1,[1])
337     check_nodes (0,1,[2])
338     test02_refresh ()
339     check_nodes (1,0,[1])
340     check_nodes (0,1,[2])
341
342     # recreate 
343     test01_node ([2])
344     check_nodes (1,0,[1])
345     check_nodes (1,1,[2])
346     test02_refresh ()
347     check_nodes (1,1,)
348
349     # make sure node indexes differ
350     test01_delnode([2])
351     test01_node ([2])
352     test01_delnode([2])
353     test01_node ([2])
354     test01_delnode([2])
355     test01_node ([2])
356     test02_refresh ()
357     check_nodes (1,1,)
358
359 def test_all_slices ():
360     # each site has 3 local slices and 1 foreign slice
361     check_slices (3,1)
362     # no slice has any node yet
363     check_local_slice_nodes(0)
364     check_foreign_slice_nodes(0)
365
366     # insert one local node in local slice on plc1
367     test04_lnode_slice ([1])
368     # of course the change is only local
369     check_local_slice_nodes (1,[1])
370     check_local_slice_nodes (0,[2])
371     check_foreign_slice_nodes(0)
372
373     # refreshing
374     test02_refresh ()
375     # remember that foreign slices only know about LOCAL nodes
376     # so refreshing does not do anything
377     check_local_slice_nodes (1,[1])
378     check_local_slice_nodes (0,[2])
379     check_foreign_slice_nodes(0)
380
381     # now we add a foreign node into local slice
382     test04_fnode_slice ([1])
383     check_local_slice_nodes (2,[1])
384     check_foreign_slice_nodes (0,[1])
385     check_local_slice_nodes (0,[2])
386     check_foreign_slice_nodes (0,[2])
387
388     # refreshing
389     test02_refresh ()
390     # remember that foreign slices only know about LOCAL nodes
391     # so this does not do anything
392     check_local_slice_nodes (2,[1])
393     check_foreign_slice_nodes (0,[1])
394     check_local_slice_nodes (0,[2])
395     check_foreign_slice_nodes (1,[2])
396
397     check_slivers(3,[1])
398     check_slivers(3,[2])
399
400     
401 def test_all ():
402     test_all_init ()
403     test_all_nodes ()
404     test_all_slices ()
405
406 if __name__ == '__main__':
407     test_all()
408