checks for node removal in slices
[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 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
142     global plc,s,a
143     for i in args:
144         peer=peer_index(i)
145         if is_local_slice:
146             sname=full_slice_name(i)
147             slice=s[i].GetSlices(a[i],[sname])[0]
148             message='local'
149         else:
150             sname=full_slice_name(peer)
151             slice=s[i].GetForeignSlices(a[i],[sname])[0]
152             message='foreign'
153         print '%02d: %s slice (%s) '%(i,message,sname),
154         slice_node_ids=slice['node_ids']
155         print 'on nodes ',slice_node_ids
156         assert len(slice_node_ids)==expected_nodes
157         show_nodes (i,slice_node_ids)
158
159 # expected : nodes on local slice
160 def check_local_slice_nodes (expected, args=[1,2]):
161     check_slice_nodes(expected,True,args)
162
163 # expected : nodes on foreign slice
164 def check_foreign_slice_nodes (expected, args=[1,2]):
165     check_slice_nodes(expected,False,args)
166
167 def check_conf_files (args=[1,2]):
168     global plc,s,a
169     for i in args:
170         nodename=plc[i]['nodename']
171         ndict= s[i].GetSlivers(a[i],[nodename])[0]
172         assert ndict['hostname'] == nodename
173         conf_files = ndict['conf_files']
174         print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
175         for conf_file in conf_files:
176             print 'source=',conf_file['source'],'|',
177             print 'dest=',conf_file['dest'],'|',
178             print 'enabled=',conf_file['enabled'],'|',
179             print ''
180
181 import pprint
182 pp = pprint.PrettyPrinter(indent=3)
183
184 def check_slivers (esn,args=[1,2]):
185     global plc,s,a
186     for i in args:
187         nodename=plc[i]['nodename']
188         ndict= s[i].GetSlivers(a[i],[nodename])[0]
189         assert ndict['hostname'] == nodename
190         slivers = ndict['slivers']
191         assert len(slivers) == esn
192         print '%02d: %d  slivers in GetSlivers for node %s'%(i,len(slivers),nodename)
193         for sliver in slivers:
194             print '>>slivername = ',sliver['name']
195             pp.pprint(sliver)
196                 
197
198 ####################
199 def test00_admin (args=[1,2]):
200     global plc,s,a
201     for i in args:
202         peer=peer_index(i)
203         person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 'last_name':'PeerPoint', 'role_ids':[10],
204                                               'email':plc[i]['peer_admin_name'],'password':plc[i]['peer_admin_password']})
205         print '%02d: created peer admin account %d, %s - %s'%(i,person_id,plc[i]['peer_admin_name'],plc[i]['peer_admin_password'])
206         plc[i]['peer_admin_id']=person_id
207
208 def test00_enable (args=[1,2]):
209     global plc,s,a
210     for i in args:
211         peer=peer_index(i)
212         s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
213         s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
214         print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
215
216 def test01_node (args=[1,2]):
217     global plc,s,a
218     for i in args:
219         n=s[i].AddNode(a[i],1,{'hostname': plc[i]['nodename']})
220         print '%02d: Added node %d %s'%(i,n,plc[i]['nodename'])
221         plc[i]['node_id']=n
222
223 def test01_delnode (args=[1,2]):
224     global plc,s,a
225     for i in args:
226         retcod=s[i].DeleteNode(a[i],plc[i]['node_id'])
227         print '%02d: Deleted node %d, got %s'%(i,plc[i]['node_id'],retcod)
228         plc[i]['node_id']=None
229
230 def test01_peer_person (args=[1,2]):
231     global plc,s,a
232     for i in args:
233         peer=peer_index(i)
234         person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc[peer]['name'], 'role_ids':[3000],
235                                            'email':plc[peer]['peer_admin_name'],'password':plc[peer]['peer_admin_password']})
236         print '%02d:Created person %d as the peer person'%(i,person_id)
237         plc[i]['peer_person_id']=person_id
238
239 def test01_peer (args=[1,2]):
240     global plc,s,a
241     for i in args:
242         peer=peer_index(i)
243         peer_id=s[i].AddPeer (a[i], {'peername':plc[peer]['name'],'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
244         # NOTE : need to manually reset the encrypted password through SQL at this point
245         print '%02d:Created peer %d'%(i,peer_id)
246         plc[i]['peer_id']=peer_id
247         print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
248
249 def test01_peer_passwd (args=[1,2]):
250     global plc,s,a
251     for i in args:
252         # using an ad-hoc local command for now - never could get quotes to reach sql....
253         print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
254         retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
255         print 'got',retcod
256     
257 def test02_refresh (args=[1,2]):
258     global plc,s,a
259     for i in args:
260         print '%02d: Refreshing peer'%(i),
261         retcod=s[i].RefreshPeer(a[i],plc[i]['peer_id'])
262         print ' got ',retcod
263
264 def test03_site (args=[1,2]):
265     global plc,s,a
266     for i in args:
267         peer=peer_index(i)
268         ### create a site (required for creating a slice)
269         sitename=site_name(i)
270         abbrev_name="abbr"+str(i)
271         site_id=s[i].AddSite (a[i], {'name':plc[i]['name'],
272                                      'abbreviated_name': abbrev_name,
273                                      'login_base': plc[i]['plainname'],
274                                      'is_public': True,
275                                      'url': 'http://%s.com/'%abbrev_name,
276                                      'max_slices':10})
277         ### max_slices does not seem taken into account at that stage
278         s[i].UpdateSite(a[i],site_id,{'max_slices':10})
279         print '%02d: Created site %d with max_slices=10'%(i,site_id)
280         plc[i]['site_id']=site_id
281
282 def test03_slice (args=[1,2]):
283     global plc,s,a
284     for i in args:
285         peer=peer_index(i)
286         plain=full_slice_name(i)
287         ### create a slice
288         name=slice_name(i)
289         slice_id=s[i].AddSlice (a[i],{'name':plain,
290                                       'description':'slice %s on plc %s'%(plain,plc[i]['name']),
291                                       'url':'http://planet-lab.org/%s'%name,
292                                       'max_nodes':100,
293                                       'instanciation':'plc-instantiated',
294                                       })
295         print '%02d: created slice %d'%(i,slice_id)
296         plc[i]['slice_id']=slice_id
297         
298
299 def test04_node_slice (is_local, add_if_true, args=[1,2]):
300     global plc,s,a
301     for i in args:
302         peer=peer_index(i)
303         if is_local:
304             hostname=plc[i]['nodename']
305             nodetype='local'
306         else:
307             hostname=plc[peer]['nodename']
308             nodetype='foreign'
309         if add_if_true:
310             s[i].AddSliceToNodes (a[i], plc[i]['slice_id'],[hostname])
311             message="added"
312         else:
313             s[i].DeleteSliceFromNodes (a[i], plc[i]['slice_id'],[hostname])
314             message="deleted"
315         print '%02d: %s in slice %d %s node %s'%(i,message,plc[i]['slice_id'],nodetype,hostname)
316
317 def test04_slice_add_lnode (args=[1,2]):
318     test04_node_slice (True,True,args)
319
320 def test04_slice_add_fnode (args=[1,2]):
321     test04_node_slice (False,True,args)
322
323 def test04_slice_del_lnode (args=[1,2]):
324     test04_node_slice (True,False,args)
325
326 def test04_slice_del_fnode (args=[1,2]):
327     test04_node_slice (False,False,args)
328
329 ####################
330 def test_all_init ():
331     test00_init ()
332     test00_print ()
333     test00_admin ()
334     test00_enable ()
335     check_nodes (0,0,)
336     test01_peer_person ()
337     test01_peer ()
338     test01_peer_passwd ()
339
340     test03_site ()
341     test03_slice ()
342
343 def test_all_nodes ():
344     # create one node on each site
345     test01_node ()
346     check_nodes (1,0,)
347     test02_refresh ()
348     check_nodes (1,1,)
349
350     # check deletions
351     test01_delnode ([2])
352     check_nodes (1,1,[1])
353     check_nodes (0,1,[2])
354     test02_refresh ()
355     check_nodes (1,0,[1])
356     check_nodes (0,1,[2])
357
358     # recreate 
359     test01_node ([2])
360     check_nodes (1,0,[1])
361     check_nodes (1,1,[2])
362     test02_refresh ()
363     check_nodes (1,1,)
364
365     # make sure node indexes differ
366     test01_delnode([2])
367     test01_node ([2])
368     test01_delnode([2])
369     test01_node ([2])
370     test01_delnode([2])
371     test01_node ([2])
372     test02_refresh ()
373     check_nodes (1,1,)
374
375 def test_all_addslices ():
376     # each site has 3 local slices and 1 foreign slice
377     check_slices (3,1)
378     # no slice has any node yet
379     check_local_slice_nodes(0)
380     check_foreign_slice_nodes(0)
381
382     # insert one local node in local slice on plc1
383     test04_slice_add_lnode ([1])
384     # of course the change is only local
385     check_local_slice_nodes (1,[1])
386     check_local_slice_nodes (0,[2])
387     check_foreign_slice_nodes(0)
388
389     # refreshing
390     test02_refresh ()
391     # remember that foreign slices only know about LOCAL nodes
392     # so refreshing does not do anything
393     check_local_slice_nodes (1,[1])
394     check_local_slice_nodes (0,[2])
395     check_foreign_slice_nodes(0)
396
397     # now we add a foreign node into local slice
398     test04_slice_add_fnode ([1])
399     check_local_slice_nodes (2,[1])
400     check_foreign_slice_nodes (0,[1])
401     check_local_slice_nodes (0,[2])
402     check_foreign_slice_nodes (0,[2])
403
404     # refreshing
405     test02_refresh ()
406     # remember that foreign slices only know about LOCAL nodes
407     # so this does not do anything
408     check_local_slice_nodes (2,[1])
409     check_foreign_slice_nodes (0,[1])
410     check_local_slice_nodes (0,[2])
411     check_foreign_slice_nodes (1,[2])
412
413     check_slivers(3,[1])
414     check_slivers(3,[2])
415
416 def test_all_delslices ():
417     test04_slice_del_fnode([1])
418     check_local_slice_nodes (1,[1])
419     check_foreign_slice_nodes (1,[2])
420     test02_refresh ()
421     check_local_slice_nodes (1,[1])
422     check_foreign_slice_nodes (0,[2])
423     
424     test04_slice_del_lnode([1])
425     check_local_slice_nodes (0,[1])
426     check_foreign_slice_nodes (0,[2])
427     test02_refresh ()
428     check_local_slice_nodes (0,[1])
429     check_foreign_slice_nodes (0,[2])
430
431 def test_all ():
432     test_all_init ()
433     test_all_nodes ()
434     test_all_addslices ()
435     test_all_delslices ()
436
437 if __name__ == '__main__':
438     test_all()
439