added in view site_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 number_nodes=5
47 number_slices=3
48 magic_slices = (1,2)
49 number_magic=len(magic_slices)
50 number_all_slices=number_slices+number_magic
51
52 plc1={ 'plcname':'plc1 in federation',
53        'hostname':'lurch.cs.princeton.edu',
54        'url-format':'https://%s:443/PLCAPI/',
55        'builtin_admin_id':'root@localhost.localdomain',
56        'builtin_admin_password':'root',
57        'peer_admin_name':'plc1@planet-lab.org',
58        'peer_admin_password':'peer',
59        'node-format':'n1%02d.plc1.org',
60        'plainname' : 'one',
61        'slice-format' : 's1%02d',
62        }
63 plc2={ 'plcname':'plc2 in federation',
64        'hostname':'planetlab-devbox.inria.fr',
65        'url-format':'https://%s:443/PLCAPI/',
66        'builtin_admin_id':'root@localhost.localdomain',
67        'builtin_admin_password':'root',
68        'peer_admin_name':'plc2@planet-lab.org',
69        'peer_admin_password':'peer',
70        'node-format':'n2%02d.plc2.org',
71        'plainname' : 'two',
72        'slice-format' : 's2%02d',
73        }
74
75 ####################
76 def peer_index(i):
77     return 3-i
78
79 def plc_name (i):
80     return plc[i]['plcname']
81
82 def site_name (i):
83     return 'site'+str(i)
84
85 def node_name (i,n):
86     return plc[i]['node-format']%n
87
88 def slice_name (i,n):
89     return plc[i]['plainname']+'_'+plc[i]['slice-format']%n
90
91 # to have indexes start at 1
92 def myrange (n):
93     return range (1,n+1,1)
94
95 def message (*args):
96     print "XXXXXXXXXXXXXXXXXXXX",
97     print args
98     
99 ####################
100 def test00_init (args=[1,2]):
101     global plc,s,a,aa
102     ## have you loaded this file already (support for reload)
103     if plc[1]:
104         pass
105     else:
106         plc=[None,plc1,plc2]
107         for i in args:
108             url=plc[i]['url-format']%plc[i]['hostname']
109             plc[i]['url']=url
110             s[i]=xmlrpclib.Server(url)
111             print 'initializing s[%d]'%i,url
112             aa[i]={'Username':plc[i]['builtin_admin_id'],
113                    'AuthMethod':'password',
114                    'AuthString':plc[i]['builtin_admin_password'],
115                    'Role':'admin'}
116             print 'initialized aa[%d]'%i, aa[i]
117             a[i]={'Username':plc[i]['peer_admin_name'],
118                   'AuthMethod':'password',
119                   'AuthString':plc[i]['peer_admin_password'],
120                   'Role':'admin'}
121             print 'initialized a[%d]'%i, a[i]
122
123 def test00_print (args=[1,2]):
124     global plc,s,a,aa
125     for i in args:
126         print 's[%d]'%i,s[i]
127         print 'aa[%d]'%i, aa[i]
128         print 'a[%d]'%i, a[i]
129
130 def check_nodes (en,ef,args=[1,2]):
131     global plc,s,a
132     for i in args:
133         n=len(s[i].GetNodes(a[i]))
134         f=len(s[i].GetForeignNodes(a[i]))
135         print '%02d: Checking nodes: got %d local nodes & %d foreign nodes'%(i,n,f)
136         assert n==en
137         assert f==ef
138
139 # expected : local slices, foreign slices
140 def check_slices (els,efs,args=[1,2]):
141     global plc,s,a
142     for i in args:
143         ls=len(s[i].GetSlices(a[i]))
144         fs=len(s[i].GetForeignSlices(a[i]))
145         print '%02d: Checking slices: got %d local slices & %d foreign slices'%(i,ls,fs)
146         assert els==ls
147         assert efs==fs
148
149 def show_nodes (i,node_ids):
150     for message,nodes in [ ['LOC',s[i].GetNodes(a[i],node_ids)],
151                            ['FOR',s[i].GetForeignNodes(a[i],node_ids)] ]:
152         if nodes:
153             print '[%s:%d] : '%(message,len(nodes)),
154             for node in nodes:
155                 print node['hostname']+' ',
156             print ''
157
158 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
159     for ns in myrange(number_slices):
160         check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
161
162 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
163     global plc,s,a
164     for i in args:
165         peer=peer_index(i)
166         if is_local_slice:
167             sname=slice_name(i,ns)
168             slice=s[i].GetSlices(a[i],[sname])[0]
169             message='local'
170         else:
171             sname=slice_name(peer,ns)
172             slice=s[i].GetForeignSlices(a[i],[sname])[0]
173             message='foreign'
174         print '%02d: %s slice (%s) '%(i,message,sname),
175         slice_node_ids=slice['node_ids']
176         print 'on nodes ',slice_node_ids
177         assert len(slice_node_ids)==expected_nodes
178         show_nodes (i,slice_node_ids)
179
180 # expected : nodes on local slice
181 def check_local_slice_nodes (expected, args=[1,2]):
182     check_slice_nodes(expected,True,args)
183
184 # expected : nodes on foreign slice
185 def check_foreign_slice_nodes (expected, args=[1,2]):
186     check_slice_nodes(expected,False,args)
187
188 def check_conf_files (args=[1,2]):
189     for nn in myrange(number_nodes):
190         check_conf_files_n (nn,args)
191
192 def check_conf_files_n (nn,args=[1,2]):
193     global plc,s,a
194     for i in args:
195         nodename=node_name(i,nn)
196         ndict= s[i].GetSlivers(a[i],[nodename])[0]
197         assert ndict['hostname'] == nodename
198         conf_files = ndict['conf_files']
199         print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
200         for conf_file in conf_files:
201             print 'source=',conf_file['source'],'|',
202             print 'dest=',conf_file['dest'],'|',
203             print 'enabled=',conf_file['enabled'],'|',
204             print ''
205
206 import pprint
207 pp = pprint.PrettyPrinter(indent=3)
208
209 def check_slivers (esn,args=[1,2]):
210     for nn in myrange(number_nodes):
211         check_slivers_n (nn,esn,args)
212
213 # too verbose to check all nodes, let's check only the first one
214 def check_slivers_1 (esn,args=[1,2]):
215     check_slivers_n (1,esn,args)
216
217 def check_slivers_n (nn,esn,args=[1,2]):
218     global plc,s,a
219     for i in args:
220         nodename=node_name(i,i)
221         ndict= s[i].GetSlivers(a[i],[nodename])[0]
222         assert ndict['hostname'] == nodename
223         slivers = ndict['slivers']
224         assert len(slivers) == esn
225         print '%02d: %d  slivers in GetSlivers for node %s'%(i,len(slivers),nodename)
226         for sliver in slivers:
227             print '>>slivername = ',sliver['name']
228             pp.pprint(sliver)
229                 
230
231 ####################
232 def test00_admin_person (args=[1,2]):
233     global plc,s,a
234     for i in args:
235         email = plc[i]['peer_admin_name']
236         try:
237             p=s[i].GetPersons(a[i],[email])[0]
238             plc[i]['peer_admin_id']=p['person_id']
239         except:
240             person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 'last_name':'PeerPoint', 'role_ids':[10],
241                                             'email':email,'password':plc[i]['peer_admin_password']})
242             print '%02d: created peer admin account %d, %s - %s'%(i,person_id,plc[i]['peer_admin_name'],plc[i]['peer_admin_password'])
243             plc[i]['peer_admin_id']=person_id
244
245 def test00_admin_enable (args=[1,2]):
246     global plc,s,a
247     for i in args:
248         s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
249         s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
250         print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
251
252 ####################
253 def test01_site (args=[1,2]):
254     global plc,s,a
255     for i in args:
256         peer=peer_index(i)
257         ### create a site (required for creating a slice)
258         sitename=site_name(i)
259         abbrev_name="abbr"+str(i)
260         login_base=plc[i]['plainname']
261         ### should be enough - needs to check we can add/del slices
262         max_slices = number_slices 
263         try:
264             s[i].GetSites(a[i],{'login_base':login_base})[0]
265         except:
266             site_id=s[i].AddSite (a[i], {'name':plc_name(i),
267                                          'abbreviated_name': abbrev_name,
268                                          'login_base': login_base,
269                                          'is_public': True,
270                                          'url': 'http://%s.com/'%abbrev_name,
271                                          'max_slices':max_slices})
272         ### max_slices does not seem taken into account at that stage
273             s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
274             print '%02d: Created site %d with max_slices=%d'%(i,site_id,max_slices)
275             plc[i]['site_id']=site_id
276
277 def test01_peer_person (args=[1,2]):
278     global plc,s,a
279     for i in args:
280         peer=peer_index(i)
281         email=plc[peer]['peer_admin_name']
282         try:
283             p=s[i].GetPersons(a[i],[email])[0]
284             plc[i]['peer_person_id']=p['person_id']
285         except:
286             person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
287                                                'email':email,'password':plc[peer]['peer_admin_password']})
288             print '%02d:Created person %d as the peer person'%(i,person_id)
289             plc[i]['peer_person_id']=person_id
290
291 def test01_peer (args=[1,2]):
292     global plc,s,a
293     for i in args:
294         peer=peer_index(i)
295         peername = plc_name(peer)
296         try:
297             p=s[i].GetPeers (a[i], [peername])[0]
298             plc[i]['peer_id']=p['peer_id']
299         except:
300             peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
301             # NOTE : need to manually reset the encrypted password through SQL at this point
302             print '%02d:Created peer %d'%(i,peer_id)
303             plc[i]['peer_id']=peer_id
304             print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
305
306 def test01_peer_passwd (args=[1,2]):
307     global plc,s,a
308     for i in args:
309         # using an ad-hoc local command for now - never could get quotes to reach sql....
310         print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
311         retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
312         print '-> system returns',retcod
313     
314 ##############################
315 # this one gets cached 
316 def get_peer_id (i):
317     try:
318         return plc[i]['peer_id']
319     except:
320         peername = plc_name (peer_index(i))
321         peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
322         plc[i]['peer_id'] = peer_id
323         return peer_id
324
325 def test01_refresh (args=[1,2]):
326     global plc,s,a
327     for i in args:
328         print '%02d: Refreshing peer'%(i),
329         retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
330         print 'got ',retcod
331
332 ####################
333 def get_node_id(i,nodename):
334     return s[i].GetNodes(a[i],[nodename])[0]['node_id']
335
336 def clean_all_nodes (args=[1,2]):
337     global plc,s,a
338     for i in args:
339         print '%02d: Cleaning all nodes'%i
340         for node in s[i].GetNodes(a[i]):
341             print '%02d: > Cleaning node %d'%(i,node['node_id'])
342             s[i].DeleteNode(a[i],node['node_id'])
343
344 def test02_node (args=[1,2]):
345     for nn in myrange(number_nodes):
346         test02_node_n (nn,args)
347
348 def test02_node_n (nn,args=[1,2]):
349     global plc,s,a
350     for i in args:
351         nodename = node_name(i,nn)
352         try:
353             get_node_id(i,nodename)
354         except:
355             n=s[i].AddNode(a[i],1,{'hostname': nodename})
356             print '%02d: Added node %d %s'%(i,n,node_name(i,i))
357
358 def test02_delnode (args=[1,2]):
359     for nn in myrange(number_nodes):
360         test02_delnode_n (nn,args)
361
362 def test02_delnode_n (nn,args=[1,2]):
363     global plc,s,a
364     for i in args:
365         nodename = node_name(i,nn)
366         node_id = get_node_id (i,nodename)
367         retcod=s[i].DeleteNode(a[i],nodename)
368         print '%02d: Deleted node %d, returns %s'%(i,node_id,retcod)
369
370 ####################
371 def clean_all_slices (args=[1,2]):
372     global plc,s,a
373     for i in args:
374         print '%02d: Cleaning all slices'%i
375         for slice in s[i].GetSlices(a[i]):
376             slice_id = slice['slice_id']
377             if slice_id not in magic_slices:
378                 print '%02d: > Cleaning slice %d'%(i,slice_id)
379                 s[i].DeleteSlice(a[i],slice_id)
380
381 def get_slice_id (i,name):
382     return s[i].GetSlices(a[i],[name])[0]['slice_id']
383
384 def test03_slice (args=[1,2]):
385     for n in myrange(number_slices):
386         test03_slice_n (n,args)
387
388 def test03_slice_n (ns,args=[1,2]):
389     global plc,s,a
390     for i in args:
391         peer=peer_index(i)
392         plcname=plc_name(i)
393         slicename=slice_name(i,ns)
394         max_nodes=number_nodes
395         try:
396             s[i].GetSlices(a[i],[slicename])[0]
397         except:
398             slice_id=s[i].AddSlice (a[i],{'name':slicename,
399                                           'description':'slice %s on %s'%(slicename,plcname),
400                                           'url':'http://planet-lab.org/%s'%slicename,
401                                           'max_nodes':max_nodes,
402                                           'instanciation':'plc-instantiated',
403                                           })
404             print '%02d: created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
405         
406
407 def test04_node_slice (is_local, add_if_true, args=[1,2]):
408     for ns in myrange(number_slices):
409         test04_node_slice_ns (ns,is_local, add_if_true, args)
410
411 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
412     test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
413
414 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
415     global plc,s,a
416     for i in args:
417         peer=peer_index(i)
418         slice_id = get_slice_id (i,slice_name (i,ns))
419         
420         if is_local:
421             hostnames=[node_name(i,nn) for nn in nnl]
422             nodetype='local'
423         else:
424             hostnames=[node_name(peer,nn) for nn in nnl]
425             nodetype='foreign'
426         if add_if_true:
427             s[i].AddSliceToNodes (a[i], slice_id,hostnames)
428             message="added"
429         else:
430             s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
431             message="deleted"
432         print '%02d: %s in slice %d %s '%(i,message,slice_id,nodetype),
433         print hostnames
434
435 def test04_slice_add_lnode (args=[1,2]):
436     test04_node_slice (True,True,args)
437
438 def test04_slice_add_fnode (args=[1,2]):
439     test04_node_slice (False,True,args)
440
441 def test04_slice_del_lnode (args=[1,2]):
442     test04_node_slice (True,False,args)
443
444 def test04_slice_del_fnode (args=[1,2]):
445     test04_node_slice (False,False,args)
446
447 ####################
448 def test_all_init ():
449     message ("INIT")
450     test00_init ()
451     test00_print ()
452     test00_admin_person ()
453     test00_admin_enable ()
454     test01_peer_person ()
455     test01_peer ()
456     test01_peer_passwd ()
457
458     test01_site ()
459
460 def test_all_nodes ():
461
462     message ("RESETTING NODES")
463     clean_all_nodes ()
464     test01_refresh ()
465     check_nodes(0,0)
466     # create one node on each site
467     message ("CREATING NODES")
468     test02_node ()
469     check_nodes (number_nodes,0,)
470     test01_refresh ()
471     check_nodes (number_nodes,number_nodes,)
472
473     # check deletions
474     message ("DELETING NODES")
475     test02_delnode ([2])
476     check_nodes (number_nodes,number_nodes,[1])
477     check_nodes (0,number_nodes,[2])
478     test01_refresh ()
479     check_nodes (number_nodes,0,[1])
480     check_nodes (0,number_nodes,[2])
481
482     # recreate 
483     message ("RECREATING NODES")
484     test02_node ([2])
485     check_nodes (number_nodes,0,[1])
486     check_nodes (number_nodes,number_nodes,[2])
487     test01_refresh ()
488     check_nodes (number_nodes,number_nodes,)
489
490     # make sure node indexes differ
491     message ("DUMMY DEL/ADD for different indexes")
492     test02_delnode([2])
493     test02_node ([2])
494     check_nodes (number_nodes,number_nodes,)
495     test01_refresh ()
496     check_nodes (number_nodes,number_nodes,)
497
498 def test_all_addslices ():
499
500     # reset
501     message ("RESETTING SLICES TEST")
502     clean_all_nodes ()
503     test02_node ()
504     clean_all_slices ()
505     test01_refresh ()
506
507     # create slices on plc1
508     message ("CREATING SLICES on plc1")
509     test03_slice ([1])
510     # each site has 3 local slices and 0 foreign slice
511     check_slices (number_all_slices,0,[1])
512     check_slices (number_magic,0,[2])
513     test01_refresh ()
514     check_slices (number_all_slices,0,[1])
515     check_slices (number_magic,number_slices,[2])
516     # no slice has any node yet
517     check_local_slice_nodes(0,[1])
518     check_foreign_slice_nodes(0,[2])
519
520     # insert local nodes in local slice on plc1
521     message ("ADDING LOCAL NODES IN SLICES")
522     test04_slice_add_lnode ([1])
523     # of course the change is only local
524     check_local_slice_nodes (number_nodes,[1])
525     check_foreign_slice_nodes(0,[2])
526
527     # refreshing
528     test01_refresh ()
529     # remember that foreign slices only know about LOCAL nodes
530     # so refreshing does not do anything
531     check_local_slice_nodes (number_nodes,[1])
532     check_foreign_slice_nodes (0,[2])
533
534     # now we add foreign nodes into local slice
535     message ("ADDING FOREIGN NODES IN SLICES")
536     test04_slice_add_fnode ([1])
537     check_local_slice_nodes (2*number_nodes,[1])
538     check_foreign_slice_nodes (0,[2])
539
540     # refreshing
541     test01_refresh ()
542     # remember that foreign slices only know about LOCAL nodes
543     # so this does not do anything
544     check_local_slice_nodes (2*number_nodes,[1])
545     check_foreign_slice_nodes (number_nodes,[2])
546
547     check_slivers_1(number_all_slices)
548
549 def test_all_delslices ():
550
551     message ("DELETING FOREIGN NODES FROM SLICES")
552     test04_slice_del_fnode([1])
553     check_local_slice_nodes (number_nodes,[1])
554     check_foreign_slice_nodes (number_nodes,[2])
555     # mmh?
556     check_slivers_1(number_all_slices,[1])
557
558     test01_refresh ()
559     check_local_slice_nodes (number_nodes,[1])
560     check_foreign_slice_nodes (0,[2])
561     
562     message ("DELETING LOCAL NODES FROM SLICES")
563     test04_slice_del_lnode([1])
564     check_local_slice_nodes (0,[1])
565     check_foreign_slice_nodes (0,[2])
566     test01_refresh ()
567     check_local_slice_nodes (0,[1])
568     check_foreign_slice_nodes (0,[2])
569
570     message ("CHECKING SLICES CLEAN UP")
571     clean_all_slices([1])
572     check_slices (number_magic,0,[1])
573     check_slices (number_magic,number_slices,[2])
574     test01_refresh ()
575     check_slices(number_magic,0)
576
577 def test_all_slices ():
578     test_all_addslices ()
579     test_all_delslices ()
580     
581 def test_all ():
582     test_all_init ()
583     test_all_nodes ()
584     test_all_slices ()
585
586 if __name__ == '__main__':
587     test_all()
588