3 ##############################
5 ### preparation / requirements
7 ### two separate instances of myplc
8 ### for now they are located on the same box on lurch
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
15 ##############################
17 # support reloading without wiping everything off
18 # dunno how to do (defvar plc)
23 ## we use indexes 1 and 2
33 ## the authentication objects
39 ## the builtin root user for bootstrapping
50 plc1={ 'plcname':'plc1 in federation',
51 'hostname':'lurch.cs.princeton.edu',
52 'url-format':'https://%s:443/PLCAPI/',
53 'builtin_admin_id':'root@localhost.localdomain',
54 'builtin_admin_password':'root',
55 'peer_admin_name':'plc1@planet-lab.org',
56 'peer_admin_password':'peer',
57 'node-format':'n1%02d.plc1.org',
59 'slice-format' : 's1%02d',
61 plc2={ 'plcname':'plc2 in federation',
62 'hostname':'planetlab-devbox.inria.fr',
63 'url-format':'https://%s:443/PLCAPI/',
64 'builtin_admin_id':'root@localhost.localdomain',
65 'builtin_admin_password':'root',
66 'peer_admin_name':'plc2@planet-lab.org',
67 'peer_admin_password':'peer',
68 'node-format':'n2%02d.plc2.org',
70 'slice-format' : 's2%02d',
74 # set initial conditions
75 def define_test (nodes,slices):
76 global number_nodes, number_slices
83 define_test (nodes=5,slices=3)
86 system_slices_ids = (1,2)
88 return len(system_slices_ids)
89 # temporary - the myplc I use doesnt know about 'system' yet
90 def system_slivers ():
91 # return len(system_slices_ids)
95 return number_slices+system_slices()
97 return number_slices+system_slivers()
104 return plc[i]['plcname']
110 return plc[i]['node-format']%n
112 def slice_name (i,n):
113 return plc[i]['plainname']+'_'+plc[i]['slice-format']%n
115 # to have indexes start at 1
117 return range (1,n+1,1)
120 print "XXXXXXXXXXXXXXXXXXXX",
124 def test00_init (args=[1,2]):
126 ## have you loaded this file already (support for reload)
132 url=plc[i]['url-format']%plc[i]['hostname']
134 s[i]=xmlrpclib.ServerProxy(url,allow_none=True)
135 print 'initializing s[%d]'%i,url
136 aa[i]={'Username':plc[i]['builtin_admin_id'],
137 'AuthMethod':'password',
138 'AuthString':plc[i]['builtin_admin_password'],
140 print 'initialized aa[%d]'%i, aa[i]
141 a[i]={'Username':plc[i]['peer_admin_name'],
142 'AuthMethod':'password',
143 'AuthString':plc[i]['peer_admin_password'],
145 print 'initialized a[%d]'%i, a[i]
147 def test00_print (args=[1,2]):
151 print 'aa[%d]'%i, aa[i]
152 print 'a[%d]'%i, a[i]
154 def check_nodes (en,ef,args=[1,2]):
157 # use a single request and sort afterwards for efficiency
158 # could have used GetNodes's scope as well
159 all_nodes = s[i].GetNodes(a[i])
160 n = len ([ x for x in all_nodes if x['peer_id'] is None])
161 f = len ([ x for x in all_nodes if x['peer_id'] is not None])
162 print '%02d: Checking nodes: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,en,f,ef)
166 # expected : local slices, foreign slices
167 def check_slices (els,efs,args=[1,2]):
170 ls=len(s[i].GetSlices(a[i],{'peer_id':None}))
171 fs=len(s[i].GetSlices(a[i],{'~peer_id':None}))
172 print '%02d: Checking slices: got %d local (e=%d) & %d foreign (e=%d)'%(i,ls,els,fs,efs)
176 def show_nodes (i,node_ids):
178 all_nodes = s[i].GetNodes(a[i],node_ids)
179 loc_nodes = filter (lambda n: n['peer_id'] is None, all_nodes)
180 for_nodes = filter (lambda n: n['peer_id'] is not None, all_nodes)
182 for message,nodes in [ ['LOC',loc_nodes], ['FOR',for_nodes] ] :
184 print '[%s:%d] : '%(message,len(nodes)),
186 print node['hostname']+' ',
189 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
190 for ns in myrange(number_slices):
191 check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
193 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
198 sname=slice_name(i,ns)
199 slice=s[i].GetSlices(a[i],{'name':[sname],'peer_id':None})[0]
202 sname=slice_name(peer,ns)
203 slice=s[i].GetSlices(a[i],{'name':[sname],'~peer_id':None})[0]
205 print '%02d: %s slice %s (e=%d) '%(i,message,sname,expected_nodes),
206 slice_node_ids=slice['node_ids']
207 print 'on nodes ',slice_node_ids
208 show_nodes (i,slice_node_ids)
209 assert len(slice_node_ids)>=expected_nodes
210 if len(slice_node_ids) != expected_nodes:
213 # expected : nodes on local slice
214 def check_local_slice_nodes (expected, args=[1,2]):
215 check_slice_nodes(expected,True,args)
217 # expected : nodes on foreign slice
218 def check_foreign_slice_nodes (expected, args=[1,2]):
219 check_slice_nodes(expected,False,args)
221 def check_conf_files (args=[1,2]):
222 for nn in myrange(number_nodes):
223 check_conf_files_n (nn,args)
225 def check_conf_files_n (nn,args=[1,2]):
228 nodename=node_name(i,nn)
229 ndict= s[i].GetSlivers(a[i],[nodename])[0]
230 assert ndict['hostname'] == nodename
231 conf_files = ndict['conf_files']
232 print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
233 for conf_file in conf_files:
234 print 'source=',conf_file['source'],'|',
235 print 'dest=',conf_file['dest'],'|',
236 print 'enabled=',conf_file['enabled'],'|',
240 pp = pprint.PrettyPrinter(indent=3)
242 def check_slivers (esn,args=[1,2]):
243 for nn in myrange(number_nodes):
244 check_slivers_n (nn,esn,args)
246 # too verbose to check all nodes, let's check only the first one
247 def check_slivers_1 (esn,args=[1,2]):
248 check_slivers_n (1,esn,args)
250 def check_slivers_n (nn,esn,args=[1,2]):
253 nodename=node_name(i,nn)
254 ndict= s[i].GetSlivers(a[i],[nodename])[0]
255 assert ndict['hostname'] == nodename
256 slivers = ndict['slivers']
257 print '%02d: %d slivers (exp. %d) in GetSlivers for node %s'\
258 %(i,len(slivers),esn,nodename)
259 for sliver in slivers:
260 print '>>slivername = ',sliver['name']
262 assert len(slivers) == esn
266 def test00_admin_person (args=[1,2]):
269 email = plc[i]['peer_admin_name']
271 p=s[i].GetPersons(a[i],[email])[0]
272 plc[i]['peer_admin_id']=p['person_id']
274 person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 'last_name':'PeerPoint', 'role_ids':[10],
275 'email':email,'password':plc[i]['peer_admin_password']})
276 print '%02d: created peer admin account %d, %s - %s'%(i,person_id,plc[i]['peer_admin_name'],plc[i]['peer_admin_password'])
277 plc[i]['peer_admin_id']=person_id
279 def test00_admin_enable (args=[1,2]):
282 s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
283 s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
284 print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
287 def test01_site (args=[1,2]):
291 ### create a site (required for creating a slice)
292 sitename=site_name(i)
293 abbrev_name="abbr"+str(i)
294 login_base=plc[i]['plainname']
295 ### should be enough - needs to check we can add/del slices
296 max_slices = number_slices
298 s[i].GetSites(a[i],{'login_base':login_base})[0]
300 site_id=s[i].AddSite (a[i], {'name':plc_name(i),
301 'abbreviated_name': abbrev_name,
302 'login_base': login_base,
304 'url': 'http://%s.com/'%abbrev_name,
305 'max_slices':max_slices})
306 ### max_slices does not seem taken into account at that stage
307 s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
308 print '%02d: Created site %d with max_slices=%d'%(i,site_id,max_slices)
309 plc[i]['site_id']=site_id
311 def test01_peer_person (args=[1,2]):
315 email=plc[peer]['peer_admin_name']
317 p=s[i].GetPersons(a[i],[email])[0]
318 plc[i]['peer_person_id']=p['person_id']
320 person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
321 'email':email,'password':plc[peer]['peer_admin_password']})
322 print '%02d:Created person %d as the peer person'%(i,person_id)
323 plc[i]['peer_person_id']=person_id
325 def test01_peer (args=[1,2]):
329 peername = plc_name(peer)
331 p=s[i].GetPeers (a[i], [peername])[0]
332 plc[i]['peer_id']=p['peer_id']
334 peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
335 # NOTE : need to manually reset the encrypted password through SQL at this point
336 print '%02d:Created peer %d'%(i,peer_id)
337 plc[i]['peer_id']=peer_id
338 print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
340 def test01_peer_passwd (args=[1,2]):
343 # using an ad-hoc local command for now - never could get quotes to reach sql....
344 print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
345 retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
346 print '-> system returns',retcod
348 ##############################
349 # this one gets cached
352 return plc[i]['peer_id']
354 peername = plc_name (peer_index(i))
355 peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
356 plc[i]['peer_id'] = peer_id
359 def test01_refresh (message,args=[1,2]):
361 print 'XXX refresh',message
363 print '%02d: Refreshing peer'%(i),
364 retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
368 # retrieves node_id from hostname - checks for local nodes only
369 def get_local_node_id(i,nodename):
370 return s[i].GetNodes(a[i],[nodename],None,'local')[0]['node_id']
372 # clean all local nodes - foreign nodes are not supposed to be cleaned up manually
373 def clean_all_nodes (args=[1,2]):
376 print '%02d: Cleaning all nodes'%i
377 loc_nodes = s[i].GetNodes(a[i],None,None,'local')
378 for node in loc_nodes:
379 print '%02d: > Cleaning node %d'%(i,node['node_id'])
380 s[i].DeleteNode(a[i],node['node_id'])
382 def test02_node (args=[1,2]):
383 for nn in myrange(number_nodes):
384 test02_node_n (nn,args)
386 def test02_node_n (nn,args=[1,2]):
389 nodename = node_name(i,nn)
391 get_local_node_id(i,nodename)
393 n=s[i].AddNode(a[i],1,{'hostname': nodename})
394 print '%02d: Added node %d %s'%(i,n,node_name(i,i))
396 def test02_delnode (args=[1,2]):
397 for nn in myrange(number_nodes):
398 test02_delnode_n (nn,args)
400 def test02_delnode_n (nn,args=[1,2]):
403 nodename = node_name(i,nn)
404 node_id = get_local_node_id (i,nodename)
405 retcod=s[i].DeleteNode(a[i],nodename)
406 print '%02d: Deleted node %d, returns %s'%(i,node_id,retcod)
409 def clean_all_slices (args=[1,2]):
412 print '%02d: Cleaning all slices'%i
413 for slice in s[i].GetSlices(a[i],{'peer_id':None}):
414 slice_id = slice['slice_id']
415 if slice_id not in system_slices_ids:
416 print '%02d: > Cleaning slice %d'%(i,slice_id)
417 s[i].DeleteSlice(a[i],slice_id)
419 def get_local_slice_id (i,name):
420 return s[i].GetSlices(a[i],{'name':[name],'peer_id':None})[0]['slice_id']
422 def test03_slice (args=[1,2]):
423 for n in myrange(number_slices):
424 test03_slice_n (n,args)
426 def test03_slice_n (ns,args=[1,2]):
431 slicename=slice_name(i,ns)
432 max_nodes=number_nodes
434 s[i].GetSlices(a[i],[slicename])[0]
436 slice_id=s[i].AddSlice (a[i],{'name':slicename,
437 'description':'slice %s on %s'%(slicename,plcname),
438 'url':'http://planet-lab.org/%s'%slicename,
439 'max_nodes':max_nodes,
440 'instanciation':'plc-instantiated',
442 print '%02d: created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
445 def test04_node_slice (is_local, add_if_true, args=[1,2]):
446 for ns in myrange(number_slices):
447 test04_node_slice_ns (ns,is_local, add_if_true, args)
449 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
450 test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
452 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
456 slice_id = get_local_slice_id (i,slice_name (i,ns))
459 hostnames=[node_name(i,nn) for nn in nnl]
462 hostnames=[node_name(peer,nn) for nn in nnl]
465 s[i].AddSliceToNodes (a[i], slice_id,hostnames)
468 s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
470 print '%02d: %s in slice %d %s '%(i,message,slice_id,nodetype),
473 def test04_slice_add_lnode (args=[1,2]):
474 test04_node_slice (True,True,args)
476 def test04_slice_add_fnode (args=[1,2]):
477 test04_node_slice (False,True,args)
479 def test04_slice_del_lnode (args=[1,2]):
480 test04_node_slice (True,False,args)
482 def test04_slice_del_fnode (args=[1,2]):
483 test04_node_slice (False,False,args)
486 def test_all_init ():
490 test00_admin_person ()
491 test00_admin_enable ()
492 test01_peer_person ()
494 test01_peer_passwd ()
498 def test_all_nodes ():
500 message ("RESETTING NODES")
502 test01_refresh ('cleaned nodes')
505 # create one node on each site
506 message ("CREATING NODES")
508 check_nodes(number_nodes,0)
509 test01_refresh ('after node creation')
510 check_nodes(number_nodes,number_nodes)
511 message ("2 extra del/add cycles on plc2 for different indexes")
517 check_nodes(0,number_nodes,[2])
518 test01_refresh('after deletion on plc2')
519 check_nodes(number_nodes,0,[1])
520 check_nodes(0,number_nodes,[2])
521 message ("ADD on plc2 for different indexes")
523 check_nodes (number_nodes,0,[1])
524 check_nodes (number_nodes,number_nodes,[2])
525 test01_refresh('after re-creation on plc2')
526 check_nodes (number_nodes,number_nodes,)
528 def test_all_addslices ():
531 message ("RESETTING SLICES TEST")
535 test01_refresh ("After slices init")
537 # create slices on plc1
538 message ("CREATING SLICES on plc1")
540 # each site has 3 local slices and 0 foreign slice
541 check_slices (total_slices(),0,[1])
542 check_slices (system_slices(),0,[2])
543 test01_refresh ("after slice created on plc1")
544 check_slices (total_slices(),0,[1])
545 check_slices (system_slices(),number_slices,[2])
546 # no slice has any node yet
547 check_local_slice_nodes(0,[1])
548 check_foreign_slice_nodes(0,[2])
550 # insert local nodes in local slice on plc1
551 message ("ADDING LOCAL NODES IN SLICES")
552 test04_slice_add_lnode ([1])
553 # of course the change is only local
554 check_local_slice_nodes (number_nodes,[1])
555 check_foreign_slice_nodes(0,[2])
558 test01_refresh ("After local nodes were added on plc1")
559 check_local_slice_nodes (number_nodes,[1])
560 check_foreign_slice_nodes (number_nodes,[2])
562 # now we add foreign nodes into local slice
563 message ("ADDING FOREIGN NODES IN SLICES")
564 test04_slice_add_fnode ([1])
565 check_local_slice_nodes (2*number_nodes,[1])
566 check_foreign_slice_nodes (number_nodes,[2])
569 test01_refresh ("After foreign nodes were added in plc1")
570 # remember that foreign slices only know about LOCAL nodes
571 # so this does not do anything
572 check_local_slice_nodes (2*number_nodes,[1])
573 check_foreign_slice_nodes (2*number_nodes,[2])
575 check_slivers_1(total_slivers())
577 def test_all_delslices ():
579 message ("DELETING FOREIGN NODES FROM SLICES")
580 test04_slice_del_fnode([1])
581 check_local_slice_nodes (number_nodes,[1])
582 check_foreign_slice_nodes (2*number_nodes,[2])
584 check_slivers_1(total_slivers(),[1])
586 test01_refresh ("After foreign nodes were removed on plc1")
587 check_local_slice_nodes (number_nodes,[1])
588 check_foreign_slice_nodes (number_nodes,[2])
590 message ("DELETING LOCAL NODES FROM SLICES")
591 test04_slice_del_lnode([1])
592 check_local_slice_nodes (0,[1])
593 check_foreign_slice_nodes (number_nodes,[2])
595 test01_refresh ("After local nodes were removed on plc1")
596 check_local_slice_nodes (0,[1])
597 check_foreign_slice_nodes (0,[2])
599 message ("CHECKING SLICES CLEAN UP")
600 clean_all_slices([1])
601 check_slices (system_slices(),0,[1])
602 check_slices (system_slices(),number_slices,[2])
603 test01_refresh ("After slices clenaup")
604 check_slices(system_slices(),0)
606 def test_all_slices ():
607 test_all_addslices ()
608 test_all_delslices ()
615 ### ad hoc test sequences
619 test01_refresh ("populate: refreshing peer 1",[1])
620 test04_slice_add_lnode([1])
621 test04_slice_add_fnode([1])
622 test01_refresh("populate: refresh all")
632 print "Usage: %s [-n] [-f]"%sys.argv[0]
633 print " -f runs faster (1 node - 1 slice)"
634 print " -n runs test_now instead of test_all"
640 (o,a) = getopt.getopt(sys.argv[1:], "fn")
653 print '%d nodes & %d slices'%(number_nodes,number_slices)
655 print 'Running test_now'
660 if __name__ == '__main__':