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@plc1.org',
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',
60 'person-format' : 'user1-%d@plc1.org',
61 'key-format':'ssh-rsa 1111111111111111 user%d-key%d',
62 'person-password' : 'password1',
64 plc2={ 'plcname':'plc2 in federation',
65 'hostname':'planetlab-devbox.inria.fr',
66 'url-format':'https://%s:443/PLCAPI/',
67 'builtin-admin-id':'root@plc2.org',
68 'builtin-admin-password':'root',
69 'peer-admin-name':'plc2@planet-lab.org',
70 'peer-admin-password':'peer',
71 'node-format':'n2%02d.plc2.org',
73 'slice-format' : 's2%02d',
74 'person-format' : 'user2-%d@plc2.org',
75 'key-format':'ssh-rsa 2222222222222222 user%d-key%d',
76 'person-password' : 'password2',
80 # set initial conditions
81 def define_test (keys,persons,nodes,slices):
82 global number_keys, number_persons, number_nodes, number_slices
84 number_persons=persons
91 define_test (keys=4,persons=2,nodes=5,slices=3)
94 # number of 'system' persons
95 # builtin maint, local root, 2 persons for the peering
97 # among that, 1 gets refreshed - other ones have conflicting names
98 system_persons_cross = 1
100 system_slices_ids = (1,2)
101 def system_slices ():
102 return len(system_slices_ids)
104 return number_slices+system_slices()
106 # temporary - the myplc I use doesnt know about 'system' yet
107 def system_slivers ():
108 # return len(system_slices_ids)
110 def total_slivers ():
111 return number_slices+system_slivers()
118 return plc[i]['plcname']
124 return plc[i]['node-format']%n
126 def slice_name (i,n):
127 return plc[i]['plainname']+'_'+plc[i]['slice-format']%n
129 def person_name (i,n):
130 return plc[i]['person-format']%n
132 def key_name (i,n,k):
133 return plc[i]['key-format']%(n,k)
135 # to have indexes start at 1
137 return range (1,n+1,1)
140 print "====================",
144 def test00_init (args=[1,2]):
146 ## have you loaded this file already (support for reload)
152 url=plc[i]['url-format']%plc[i]['hostname']
154 s[i]=xmlrpclib.ServerProxy(url,allow_none=True)
155 print 'initializing s[%d]'%i,url
156 aa[i]={'Username':plc[i]['builtin-admin-id'],
157 'AuthMethod':'password',
158 'AuthString':plc[i]['builtin-admin-password'],
160 print 'initialized aa[%d]'%i, aa[i]
161 a[i]={'Username':plc[i]['peer-admin-name'],
162 'AuthMethod':'password',
163 'AuthString':plc[i]['peer-admin-password'],
165 print 'initialized a[%d]'%i, a[i]
167 def test00_print (args=[1,2]):
170 print 'aa[%d]'%i, aa[i]
171 print 'a[%d]'%i, a[i]
173 def check_nodes (el,ef,args=[1,2]):
175 # use a single request and sort afterwards for efficiency
176 # could have used GetNodes's scope as well
177 all_nodes = s[i].GetNodes(a[i])
178 n = len ([ x for x in all_nodes if x['peer_id'] is None])
179 f = len ([ x for x in all_nodes if x['peer_id'] is not None])
180 print '%02d: Checking nodes: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
184 def check_keys (el,ef,args=[1,2]):
186 # use a single request and sort afterwards for efficiency
187 # could have used GetKeys's scope as well
188 all_keys = s[i].GetKeys(a[i])
189 n = len ([ x for x in all_keys if x['peer_id'] is None])
190 f = len ([ x for x in all_keys if x['peer_id'] is not None])
191 print '%02d: Checking keys: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
195 def check_persons (el,ef,args=[1,2]):
197 # use a single request and sort afterwards for efficiency
198 # could have used GetPersons's scope as well
199 all_persons = s[i].GetPersons(a[i])
200 n = len ([ x for x in all_persons if x['peer_id'] is None])
201 f = len ([ x for x in all_persons if x['peer_id'] is not None])
202 print '%02d: Checking persons: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
206 # expected : local slices, foreign slices
207 def check_slices (els,efs,args=[1,2]):
209 ls=len(s[i].GetSlices(a[i],{'peer_id':None}))
210 fs=len(s[i].GetSlices(a[i],{'~peer_id':None}))
211 print '%02d: Checking slices: got %d local (e=%d) & %d foreign (e=%d)'%(i,ls,els,fs,efs)
215 def show_nodes (i,node_ids):
217 all_nodes = s[i].GetNodes(a[i],node_ids)
218 loc_nodes = filter (lambda n: n['peer_id'] is None, all_nodes)
219 for_nodes = filter (lambda n: n['peer_id'] is not None, all_nodes)
221 for message,nodes in [ ['LOC',loc_nodes], ['FOR',for_nodes] ] :
223 print '[%s:%d] : '%(message,len(nodes)),
225 print node['hostname']+' ',
228 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
229 for ns in myrange(number_slices):
230 check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
232 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
236 sname=slice_name(i,ns)
237 slice=s[i].GetSlices(a[i],{'name':[sname],'peer_id':None})[0]
240 sname=slice_name(peer,ns)
241 slice=s[i].GetSlices(a[i],{'name':[sname],'~peer_id':None})[0]
243 print '%02d: %s slice %s (e=%d) '%(i,message,sname,expected_nodes),
244 slice_node_ids=slice['node_ids']
245 print 'on nodes ',slice_node_ids
246 show_nodes (i,slice_node_ids)
247 assert len(slice_node_ids)>=expected_nodes
248 if len(slice_node_ids) != expected_nodes:
251 # expected : nodes on local slice
252 def check_local_slice_nodes (expected, args=[1,2]):
253 check_slice_nodes(expected,True,args)
255 # expected : nodes on foreign slice
256 def check_foreign_slice_nodes (expected, args=[1,2]):
257 check_slice_nodes(expected,False,args)
259 def check_conf_files (args=[1,2]):
260 for nn in myrange(number_nodes):
261 check_conf_files_n (nn,args)
263 def check_conf_files_n (nn,args=[1,2]):
265 nodename=node_name(i,nn)
266 ndict= s[i].GetSlivers(a[i],[nodename])[0]
267 assert ndict['hostname'] == nodename
268 conf_files = ndict['conf_files']
269 print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
270 for conf_file in conf_files:
271 print 'source=',conf_file['source'],'|',
272 print 'dest=',conf_file['dest'],'|',
273 print 'enabled=',conf_file['enabled'],'|',
277 pp = pprint.PrettyPrinter(indent=3)
279 def check_slivers (esn,args=[1,2]):
280 for nn in myrange(number_nodes):
281 check_slivers_n (nn,esn,args)
283 # too verbose to check all nodes, let's check only the first one
284 def check_slivers_1 (esn,args=[1,2]):
285 check_slivers_n (1,esn,args)
287 def check_slivers_n (nn,esn,args=[1,2]):
289 nodename=node_name(i,nn)
290 ndict= s[i].GetSlivers(a[i],[nodename])[0]
291 assert ndict['hostname'] == nodename
292 slivers = ndict['slivers']
293 print '%02d: %d slivers (exp. %d) in GetSlivers for node %s'\
294 %(i,len(slivers),esn,nodename)
295 for sliver in slivers:
296 print '>>slivername = ',sliver['name']
298 assert len(slivers) == esn
302 def test00_admin_person (args=[1,2]):
305 email = plc[i]['peer-admin-name']
307 p=s[i].GetPersons(a[i],[email])[0]
308 plc[i]['peer-admin-id']=p['person_id']
310 person_id=s[i].AddPerson(aa[i],{'first_name':'Local',
311 'last_name':'PeerPoint',
314 'password':plc[i]['peer-admin-password']})
315 print '%02d:== created peer admin account %d, %s - %s'%(i,
316 person_id,plc[i]['peer-admin-name'],
317 plc[i]['peer-admin-password'])
318 plc[i]['peer-admin-id']=person_id
320 def test00_admin_enable (args=[1,2]):
322 s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer-admin-id'],True)
323 s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer-admin-id'])
324 print '%02d:== enabled+admin on account %d:%s'%(i,plc[i]['peer-admin-id'],plc[i]['peer-admin-name'])
326 def test01_peer_person (args=[1,2]):
330 email=plc[peer]['peer-admin-name']
332 p=s[i].GetPersons(a[i],[email])[0]
333 plc[i]['peer_person_id']=p['person_id']
335 person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
336 'email':email,'password':plc[peer]['peer-admin-password']})
337 print '%02d:Created person %d as the peer person'%(i,person_id)
338 plc[i]['peer_person_id']=person_id
341 def test01_peer (args=[1,2]):
345 peername = plc_name(peer)
347 p=s[i].GetPeers (a[i], [peername])[0]
348 plc[i]['peer_id']=p['peer_id']
350 peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
351 # NOTE : need to manually reset the encrypted password through SQL at this point
352 print '%02d:Created peer %d'%(i,peer_id)
353 plc[i]['peer_id']=peer_id
354 print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
356 def test01_peer_passwd (args=[1,2]):
358 # using an ad-hoc local command for now - never could get quotes to reach sql....
359 print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
360 retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
361 print '-> system returns',retcod
363 def test01_site (args=[1,2]):
367 ### create a site (required for creating a slice)
368 sitename=site_name(i)
369 abbrev_name="abbr"+str(i)
370 login_base=plc[i]['plainname']
371 ### should be enough - needs to check we can add/del slices
372 max_slices = number_slices
374 s[i].GetSites(a[i],{'login_base':login_base})[0]
376 site_id=s[i].AddSite (a[i], {'name':plc_name(i),
377 'abbreviated_name': abbrev_name,
378 'login_base': login_base,
380 'url': 'http://%s.com/'%abbrev_name,
381 'max_slices':max_slices})
382 ### max_slices does not seem taken into account at that stage
383 s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
384 print '%02d:== Created site %d with max_slices=%d'%(i,site_id,max_slices)
385 plc[i]['site_id']=site_id
387 ##############################
388 # this one gets cached
391 return plc[i]['peer_id']
393 peername = plc_name (peer_index(i))
394 peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
395 plc[i]['peer_id'] = peer_id
398 def test01_refresh (message,args=[1,2]):
399 print '=== refresh',message
401 print '%02d:== Refreshing peer'%(i),
402 retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
406 # retrieves node_id from hostname - checks for local nodes only
407 def get_local_node_id(i,nodename):
408 return s[i].GetNodes(a[i],[nodename],None,'local')[0]['node_id']
410 # clean all local nodes - foreign nodes are not supposed to be cleaned up manually
411 def clean_all_nodes (args=[1,2]):
413 print '%02d:== Cleaning all nodes'%i
414 loc_nodes = s[i].GetNodes(a[i],None,None,'local')
415 for node in loc_nodes:
416 print '%02d:==== Cleaning node %d'%(i,node['node_id'])
417 s[i].DeleteNode(a[i],node['node_id'])
419 def test02_node (args=[1,2]):
420 for nn in myrange(number_nodes):
421 test02_node_n (nn,args)
423 def test02_node_n (nn,args=[1,2]):
425 nodename = node_name(i,nn)
427 get_local_node_id(i,nodename)
429 n=s[i].AddNode(a[i],1,{'hostname': nodename})
430 print '%02d:== Added node %d %s'%(i,n,node_name(i,i))
432 def test02_delnode (args=[1,2]):
433 for nn in myrange(number_nodes):
434 test02_delnode_n (nn,args)
436 def test02_delnode_n (nn,args=[1,2]):
438 nodename = node_name(i,nn)
439 node_id = get_local_node_id (i,nodename)
440 retcod=s[i].DeleteNode(a[i],nodename)
441 print '%02d:== Deleted node %d, returns %s'%(i,node_id,retcod)
444 def test05_person (args=[1,2]):
445 for np in myrange(number_persons):
446 test05_person_n (np,True,args)
448 def test05_del_person (args=[1,2]):
449 for np in myrange(number_persons):
450 test05_person_n (np,False,args)
452 def test05_person_n (np,add_if_true,args=[1,2]):
453 test05_person_n_ks (np, myrange(number_keys),add_if_true,args)
455 def test05_person_n_ks (np,nks,add_if_true,args=[1,2]):
457 email = person_name(i,np)
459 person_id=s[i].GetPersons(a[i],[email])[0]['person_id']
461 s[i].DeletePerson(a[i],person_id)
462 print "%02d:== deleted person_id %d"%(i,person_id)
465 password = plc[i]['person-password']
466 person_id=s[i].AddPerson(a[i],{'first_name':'Your average',
467 'last_name':'User%d'%np,
470 'password': password })
471 print '%02d:== created user account %d, %s - %s'%(i, person_id,email,password)
473 key=key_name(i,np,nk)
474 s[i].AddPersonKey(aa[i],email,{'key_type':'ssh', 'key':key})
475 print '%02d:== added key %s to person %s'%(i,key,email)
478 def clean_all_slices (args=[1,2]):
480 print '%02d:== Cleaning all slices'%i
481 for slice in s[i].GetSlices(a[i],{'peer_id':None}):
482 slice_id = slice['slice_id']
483 if slice_id not in system_slices_ids:
484 print '%02d:==== Cleaning slice %d'%(i,slice_id)
485 s[i].DeleteSlice(a[i],slice_id)
487 def get_local_slice_id (i,name):
488 return s[i].GetSlices(a[i],{'name':[name],'peer_id':None})[0]['slice_id']
490 def test03_slice (args=[1,2]):
491 for n in myrange(number_slices):
492 test03_slice_n (n,args)
494 def test03_slice_n (ns,args=[1,2]):
498 slicename=slice_name(i,ns)
499 max_nodes=number_nodes
501 s[i].GetSlices(a[i],[slicename])[0]
503 slice_id=s[i].AddSlice (a[i],{'name':slicename,
504 'description':'slice %s on %s'%(slicename,plcname),
505 'url':'http://planet-lab.org/%s'%slicename,
506 'max_nodes':max_nodes,
507 'instanciation':'plc-instantiated',
509 print '%02d:== created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
512 def test04_node_slice (is_local, add_if_true, args=[1,2]):
513 for ns in myrange(number_slices):
514 test04_node_slice_ns (ns,is_local, add_if_true, args)
516 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
517 test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
519 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
522 slice_id = get_local_slice_id (i,slice_name (i,ns))
525 hostnames=[node_name(i,nn) for nn in nnl]
528 hostnames=[node_name(peer,nn) for nn in nnl]
531 s[i].AddSliceToNodes (a[i], slice_id,hostnames)
534 s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
536 print '%02d:== %s in slice %d %s '%(i,message,slice_id,nodetype),
539 def test04_slice_add_lnode (args=[1,2]):
540 test04_node_slice (True,True,args)
542 def test04_slice_add_fnode (args=[1,2]):
543 test04_node_slice (False,True,args)
545 def test04_slice_del_lnode (args=[1,2]):
546 test04_node_slice (True,False,args)
548 def test04_slice_del_fnode (args=[1,2]):
549 test04_node_slice (False,False,args)
552 def test_all_init ():
556 test00_admin_person ()
557 test00_admin_enable ()
558 test01_peer_person ()
560 test01_peer_passwd ()
564 def test_all_nodes ():
566 message ("RESETTING NODES")
568 test01_refresh ('cleaned nodes')
571 # create one node on each site
572 message ("CREATING NODES")
574 check_nodes(number_nodes,0)
575 test01_refresh ('after node creation')
576 check_nodes(number_nodes,number_nodes)
577 message ("2 extra del/add cycles on plc2 for different indexes")
583 check_nodes(0,number_nodes,[2])
584 test01_refresh('after deletion on plc2')
585 check_nodes(number_nodes,0,[1])
586 check_nodes(0,number_nodes,[2])
587 message ("ADD on plc2 for different indexes")
589 check_nodes (number_nodes,0,[1])
590 check_nodes (number_nodes,number_nodes,[2])
591 test01_refresh('after re-creation on plc2')
592 check_nodes (number_nodes,number_nodes,)
594 def test_all_addslices ():
597 message ("RESETTING SLICES TEST")
601 test01_refresh ("After slices init")
603 # create slices on plc1
604 message ("CREATING SLICES on plc1")
606 # each site has 3 local slices and 0 foreign slice
607 check_slices (total_slices(),0,[1])
608 check_slices (system_slices(),0,[2])
609 test01_refresh ("after slice created on plc1")
610 check_slices (total_slices(),0,[1])
611 check_slices (system_slices(),number_slices,[2])
612 # no slice has any node yet
613 check_local_slice_nodes(0,[1])
614 check_foreign_slice_nodes(0,[2])
616 # insert local nodes in local slice on plc1
617 message ("ADDING LOCAL NODES IN SLICES")
618 test04_slice_add_lnode ([1])
619 # of course the change is only local
620 check_local_slice_nodes (number_nodes,[1])
621 check_foreign_slice_nodes(0,[2])
624 test01_refresh ("After local nodes were added on plc1")
625 check_local_slice_nodes (number_nodes,[1])
626 check_foreign_slice_nodes (number_nodes,[2])
628 # now we add foreign nodes into local slice
629 message ("ADDING FOREIGN NODES IN SLICES")
630 test04_slice_add_fnode ([1])
631 check_local_slice_nodes (2*number_nodes,[1])
632 check_foreign_slice_nodes (number_nodes,[2])
635 test01_refresh ("After foreign nodes were added in plc1")
636 # remember that foreign slices only know about LOCAL nodes
637 # so this does not do anything
638 check_local_slice_nodes (2*number_nodes,[1])
639 check_foreign_slice_nodes (2*number_nodes,[2])
641 check_slivers_1(total_slivers())
643 def test_all_delslices ():
645 message ("DELETING FOREIGN NODES FROM SLICES")
646 test04_slice_del_fnode([1])
647 check_local_slice_nodes (number_nodes,[1])
648 check_foreign_slice_nodes (2*number_nodes,[2])
650 check_slivers_1(total_slivers(),[1])
652 test01_refresh ("After foreign nodes were removed on plc1")
653 check_local_slice_nodes (number_nodes,[1])
654 check_foreign_slice_nodes (number_nodes,[2])
656 message ("DELETING LOCAL NODES FROM SLICES")
657 test04_slice_del_lnode([1])
658 check_local_slice_nodes (0,[1])
659 check_foreign_slice_nodes (number_nodes,[2])
661 test01_refresh ("After local nodes were removed on plc1")
662 check_local_slice_nodes (0,[1])
663 check_foreign_slice_nodes (0,[2])
665 message ("CHECKING SLICES CLEAN UP")
666 clean_all_slices([1])
667 check_slices (system_slices(),0,[1])
668 check_slices (system_slices(),number_slices,[2])
669 test01_refresh ("After slices clenaup")
670 check_slices(system_slices(),0)
672 def test_all_slices ():
673 test_all_addslices ()
674 test_all_delslices ()
676 def test_all_persons ():
679 check_persons(system_persons,0)
680 test01_refresh ('before persons&keys creation')
682 check_persons(system_persons,system_persons_cross)
683 message ("Creating persons&keys - 1 extra del/add cycle for unique indexes")
685 test05_del_person([2])
687 check_keys(number_persons*number_keys,0)
688 check_persons(system_persons+number_persons,system_persons_cross)
689 test01_refresh ('after persons&keys creation')
690 check_keys(number_persons*number_keys,number_persons*number_keys)
691 check_persons(system_persons+number_persons,system_persons_cross+number_persons)
699 ### ad hoc test sequences
703 test01_refresh ("populate: refreshing peer 1",[1])
704 test04_slice_add_lnode([1])
705 test04_slice_add_fnode([1])
706 test01_refresh("populate: refresh all")
717 print "Usage: %s [-n] [-f]"%sys.argv[0]
718 print " -f runs faster (1 node - 1 slice)"
719 print " -n runs test_now instead of test_all"
725 (o,a) = getopt.getopt(sys.argv[1:], "fn")
738 print '%d nodes & %d slices'%(number_nodes,number_slices)
740 print 'Running test_now'
745 if __name__ == '__main__':