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)
24 ## we use indexes 1 and 2
34 ## the authentication objects
40 ## the builtin root user for bootstrapping
52 # number of 'system' persons
53 # builtin maint, local root, 2 persons for the peering
55 # among that, 1 gets refreshed - other ones have conflicting names
56 system_persons_cross = 1
58 system_slices_ids = (1,2)
60 return len(system_slices_ids)
62 return number_slices+system_slices()
64 # temporary - the myplc I use doesnt know about 'system' yet
65 def system_slivers ():
66 # return len(system_slices_ids)
69 return number_slices+system_slivers()
72 # set initial conditions
73 def define_test (sites,keys,persons,nodes,slices,fast_mode):
74 global number_sites, number_keys, number_persons, number_nodes, number_slices, fast_flag
77 number_persons=persons
83 print '%d sites, %d keys, %d persons, %d nodes & %d slices'%(number_sites, number_keys,number_persons,
84 number_nodes,number_slices)
87 define_test(1,1,1,1,1,True)
90 define_test (sites=4,keys=2,persons=4,nodes=5,slices=4,fast_mode=False)
93 define_test (sites=16,keys=8,persons=16,nodes=20,slices=16,fast_mode=False)
99 # argh, for login_name that doesn't accept digits
100 plain_numbers=['zero','one','two','three','four','five','six','seven','eight','nine','ten',
101 'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty']
103 plc1={ 'plcname':'plc1 in federation',
104 'hostname':'lurch.cs.princeton.edu',
105 'url-format':'https://%s:443/PLCAPI/',
106 'builtin-admin-id':'root@plc1.org',
107 'builtin-admin-password':'root',
108 'peer-admin-name':'peer1@planet-lab.org',
109 'peer-admin-password':'peer',
110 'node-format':'n1%02d.plc1.org',
112 'site-format':'one%s',
113 'person-format' : 'user1-%d@plc1.org',
114 'key-format':'ssh-rsa 1111111111111111 user%d-key%d',
115 'person-password' : 'password1',
117 plc2={ 'plcname':'plc2 in federation',
118 'hostname':'planetlab-devbox.inria.fr',
119 'url-format':'https://%s:443/PLCAPI/',
120 'builtin-admin-id':'root@plc2.org',
121 'builtin-admin-password':'root',
122 'peer-admin-name':'peer2@planet-lab.org',
123 'peer-admin-password':'peer',
124 'node-format':'n2%02d.plc2.org',
126 'site-format':'two%s',
127 'person-format' : 'user2-%d@plc2.org',
128 'key-format':'ssh-rsa 2222222222222222 user%d-key%d',
129 'person-password' : 'password2',
137 return plc[i]['plcname']
140 x=site_login_base(i,n)
141 return 'Site fullname '+x
143 def site_login_base (i,n):
144 return plc[i]['site-format']%plain_numbers[n]
146 def person_name (i,n):
147 return plc[i]['person-format']%n
149 def key_name (i,n,k):
150 return plc[i]['key-format']%(n,k)
153 return plc[i]['node-format']%n
156 result=(n%number_sites)
161 def slice_name (i,n):
162 site_index=map_on_site(n)
163 return "%s_slice%d"%(site_login_base(i,site_index),n)
165 # to have indexes start at 1
167 return range (1,n+1,1)
170 print "====================",
177 print '+++ timer start'
180 print '+++ %.02f seconds ellapsed'%(time.time()-epoch)
183 def test00_init (args=[1,2]):
186 ## have you loaded this file already (support for reload)
192 url=plc[i]['url-format']%plc[i]['hostname']
194 s[i]=xmlrpclib.ServerProxy(url,allow_none=True)
195 print 'initializing s[%d]'%i,url
196 aa[i]={'Username':plc[i]['builtin-admin-id'],
197 'AuthMethod':'password',
198 'AuthString':plc[i]['builtin-admin-password'],
200 print 'initialized aa[%d]'%i, aa[i]
201 a[i]={'Username':plc[i]['peer-admin-name'],
202 'AuthMethod':'password',
203 'AuthString':plc[i]['peer-admin-password'],
205 print 'initialized a[%d]'%i, a[i]
207 def test00_print (args=[1,2]):
210 print 'aa[%d]'%i, aa[i]
211 print 'a[%d]'%i, a[i]
213 def check_nodes (el,ef,args=[1,2]):
215 # use a single request and sort afterwards for efficiency
216 # could have used GetNodes's scope as well
217 all_nodes = s[i].GetNodes(a[i])
218 n = len ([ x for x in all_nodes if x['peer_id'] is None])
219 f = len ([ x for x in all_nodes if x['peer_id'] is not None])
220 print '%02d: Checking nodes: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
224 def check_keys (el,ef,args=[1,2]):
226 # use a single request and sort afterwards for efficiency
227 # could have used GetKeys's scope as well
228 all_keys = s[i].GetKeys(a[i])
229 n = len ([ x for x in all_keys if x['peer_id'] is None])
230 f = len ([ x for x in all_keys if x['peer_id'] is not None])
231 print '%02d: Checking keys: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
235 def check_persons (el,ef,args=[1,2]):
237 # use a single request and sort afterwards for efficiency
238 # could have used GetPersons's scope as well
239 all_persons = s[i].GetPersons(a[i])
240 n = len ([ x for x in all_persons if x['peer_id'] is None])
241 f = len ([ x for x in all_persons if x['peer_id'] is not None])
242 print '%02d: Checking persons: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
246 # expected : local slices, foreign slices
247 def check_slices (els,efs,args=[1,2]):
249 ls=len(s[i].GetSlices(a[i],{'peer_id':None}))
250 fs=len(s[i].GetSlices(a[i],{'~peer_id':None}))
251 print '%02d: Checking slices: got %d local (e=%d) & %d foreign (e=%d)'%(i,ls,els,fs,efs)
255 def show_nodes (i,node_ids):
257 all_nodes = s[i].GetNodes(a[i],node_ids)
258 loc_nodes = filter (lambda n: n['peer_id'] is None, all_nodes)
259 for_nodes = filter (lambda n: n['peer_id'] is not None, all_nodes)
261 for message,nodes in [ ['LOC',loc_nodes], ['FOR',for_nodes] ] :
263 print '[%s:%d] : '%(message,len(nodes)),
265 print node['hostname']+' ',
268 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
269 for ns in myrange(number_slices):
270 check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
272 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
276 sname=slice_name(i,ns)
277 slice=s[i].GetSlices(a[i],{'name':[sname],'peer_id':None})[0]
280 sname=slice_name(peer,ns)
281 slice=s[i].GetSlices(a[i],{'name':[sname],'~peer_id':None})[0]
283 print '%02d: %s slice %s (e=%d) '%(i,message,sname,expected_nodes),
284 slice_node_ids=slice['node_ids']
285 print 'on nodes ',slice_node_ids
286 show_nodes (i,slice_node_ids)
287 assert len(slice_node_ids)>=expected_nodes
288 if len(slice_node_ids) != expected_nodes:
291 # expected : nodes on local slice
292 def check_local_slice_nodes (expected, args=[1,2]):
293 check_slice_nodes(expected,True,args)
295 # expected : nodes on foreign slice
296 def check_foreign_slice_nodes (expected, args=[1,2]):
297 check_slice_nodes(expected,False,args)
299 def check_conf_files (args=[1,2]):
300 for nn in myrange(number_nodes):
301 check_conf_files_n (nn,args)
303 def check_conf_files_n (nn,args=[1,2]):
305 nodename=node_name(i,nn)
306 ndict= s[i].GetSlivers(a[i],[nodename])[0]
307 assert ndict['hostname'] == nodename
308 conf_files = ndict['conf_files']
309 print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
310 for conf_file in conf_files:
311 print 'source=',conf_file['source'],'|',
312 print 'dest=',conf_file['dest'],'|',
313 print 'enabled=',conf_file['enabled'],'|',
317 pp = pprint.PrettyPrinter(indent=3)
319 def check_slivers (esn,args=[1,2]):
320 for nn in myrange(number_nodes):
321 check_slivers_n (nn,esn,args)
323 # too verbose to check all nodes, let's check only the first one
324 def check_slivers_1 (esn,args=[1,2]):
325 check_slivers_n (1,esn,args)
327 def check_slivers_n (nn,esn,args=[1,2]):
329 nodename=node_name(i,nn)
330 ndict= s[i].GetSlivers(a[i],[nodename])[0]
331 assert ndict['hostname'] == nodename
332 slivers = ndict['slivers']
333 print '%02d: %d slivers (exp. %d) in GetSlivers for node %s'\
334 %(i,len(slivers),esn,nodename)
335 for sliver in slivers:
336 print '>>slivername = ',sliver['name']
338 assert len(slivers) == esn
342 def test00_admin_person (args=[1,2]):
345 email = plc[i]['peer-admin-name']
347 p=s[i].GetPersons(a[i],[email])[0]
348 plc[i]['peer-admin-id']=p['person_id']
350 person_id=s[i].AddPerson(aa[i],{'first_name':'Local',
351 'last_name':'PeerPoint',
354 'password':plc[i]['peer-admin-password']})
355 print '%02d:== created peer admin account %d, %s - %s'%(i,
356 person_id,plc[i]['peer-admin-name'],
357 plc[i]['peer-admin-password'])
358 plc[i]['peer-admin-id']=person_id
360 def test00_admin_enable (args=[1,2]):
362 s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer-admin-id'],True)
363 s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer-admin-id'])
364 print '%02d:== enabled+admin on account %d:%s'%(i,plc[i]['peer-admin-id'],plc[i]['peer-admin-name'])
366 def test00_peer_person (args=[1,2]):
370 email=plc[peer]['peer-admin-name']
372 p=s[i].GetPersons(a[i],[email])[0]
373 plc[i]['peer_person_id']=p['person_id']
375 person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
376 'email':email,'password':plc[peer]['peer-admin-password']})
377 print '%02d:Created person %d as the peer person'%(i,person_id)
378 plc[i]['peer_person_id']=person_id
381 def test00_peer (args=[1,2]):
385 peername = plc_name(peer)
387 p=s[i].GetPeers (a[i], [peername])[0]
388 plc[i]['peer_id']=p['peer_id']
390 peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
391 # NOTE : need to manually reset the encrypted password through SQL at this point
392 print '%02d:Created peer %d'%(i,peer_id)
393 plc[i]['peer_id']=peer_id
394 print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
396 def test00_peer_passwd (args=[1,2]):
398 # using an ad-hoc local command for now - never could get quotes to reach sql....
399 print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
400 retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
401 print '-> system returns',retcod
403 # this one gets cached
406 return plc[i]['peer_id']
408 peername = plc_name (peer_index(i))
409 peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
410 plc[i]['peer_id'] = peer_id
413 ##############################
414 def test00_refresh (message,args=[1,2]):
415 print '=== refresh',message
418 print '%02d:== Refreshing peer'%(i),
419 retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
424 def test01_site (args=[1,2]):
425 for ns in myrange(number_sites):
426 test01_site_n (ns,True,args)
428 def test01_del_site (args=[1,2]):
429 for ns in myrange(number_sites):
430 test01_site_n (ns,False,args)
432 def test01_site_n (ns,add_if_true,args=[1,2]):
434 login_base = site_login_base (i,ns)
436 site_id = s[i].GetSites(a[i],[login_base])[0]['site_id']
438 s[i].DeleteSite(a[i],site_id)
439 print "%02d:== deleted site_id %d"%(i,site_id)
442 sitename=site_name(i,ns)
443 abbrev_name="abbr"+str(i)
444 max_slices = number_slices
445 site_id=s[i].AddSite (a[i], {'name':plc_name(i),
446 'abbreviated_name': abbrev_name,
447 'login_base': login_base,
449 'url': 'http://%s.com/'%abbrev_name,
450 'max_slices':max_slices})
451 ### max_slices does not seem taken into account at that stage
452 s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
453 print '%02d:== Created site %d with max_slices=%d'%(i,site_id,max_slices)
456 def test02_person (args=[1,2]):
457 for np in myrange(number_persons):
458 test02_person_n (np,True,args)
460 def test02_del_person (args=[1,2]):
461 for np in myrange(number_persons):
462 test02_person_n (np,False,args)
464 def test02_person_n (np,add_if_true,args=[1,2]):
465 test02_person_n_ks (np, myrange(number_keys),add_if_true,args)
467 def test02_person_n_ks (np,nks,add_if_true,args=[1,2]):
469 email = person_name(i,np)
471 person_id=s[i].GetPersons(a[i],[email])[0]['person_id']
473 s[i].DeletePerson(a[i],person_id)
474 print "%02d:== deleted person_id %d"%(i,person_id)
477 password = plc[i]['person-password']
478 person_id=s[i].AddPerson(a[i],{'first_name':'Your average',
479 'last_name':'User%d'%np,
482 'password': password })
483 print '%02d:== created user account %d, %s - %s'%(i, person_id,email,password)
485 key=key_name(i,np,nk)
486 s[i].AddPersonKey(aa[i],email,{'key_type':'ssh', 'key':key})
487 print '%02d:== added key %s to person %s'%(i,key,email)
490 # retrieves node_id from hostname - checks for local nodes only
491 def get_local_node_id(i,nodename):
492 return s[i].GetNodes(a[i],[nodename],None,'local')[0]['node_id']
494 # clean all local nodes - foreign nodes are not supposed to be cleaned up manually
495 def clean_all_nodes (args=[1,2]):
497 print '%02d:== Cleaning all nodes'%i
498 loc_nodes = s[i].GetNodes(a[i],None,None,'local')
499 for node in loc_nodes:
500 print '%02d:==== Cleaning node %d'%(i,node['node_id'])
501 s[i].DeleteNode(a[i],node['node_id'])
503 def test03_node (args=[1,2]):
504 for nn in myrange(number_nodes):
505 test03_node_n (nn,args)
507 def test03_node_n (nn,args=[1,2]):
509 nodename = node_name(i,nn)
511 get_local_node_id(i,nodename)
513 login_base=site_login_base(i,map_on_site(nn))
514 n=s[i].AddNode(a[i],login_base,{'hostname': nodename})
515 print '%02d:== Added node %d %s'%(i,n,node_name(i,i))
517 def test02_delnode (args=[1,2]):
518 for nn in myrange(number_nodes):
519 test02_delnode_n (nn,args)
521 def test02_delnode_n (nn,args=[1,2]):
523 nodename = node_name(i,nn)
524 node_id = get_local_node_id (i,nodename)
525 retcod=s[i].DeleteNode(a[i],nodename)
526 print '%02d:== Deleted node %d, returns %s'%(i,node_id,retcod)
529 def clean_all_slices (args=[1,2]):
531 print '%02d:== Cleaning all slices'%i
532 for slice in s[i].GetSlices(a[i],{'peer_id':None}):
533 slice_id = slice['slice_id']
534 if slice_id not in system_slices_ids:
535 print '%02d:==== Cleaning slice %d'%(i,slice_id)
536 s[i].DeleteSlice(a[i],slice_id)
538 def get_local_slice_id (i,name):
539 return s[i].GetSlices(a[i],{'name':[name],'peer_id':None})[0]['slice_id']
541 def test04_slice (args=[1,2]):
542 for n in myrange(number_slices):
543 test04_slice_n (n,args)
545 def test04_slice_n (ns,args=[1,2]):
549 slicename=slice_name(i,ns)
550 max_nodes=number_nodes
552 s[i].GetSlices(a[i],[slicename])[0]
554 slice_id=s[i].AddSlice (a[i],{'name':slicename,
555 'description':'slice %s on %s'%(slicename,plcname),
556 'url':'http://planet-lab.org/%s'%slicename,
557 'max_nodes':max_nodes,
558 'instanciation':'plc-instantiated',
560 print '%02d:== created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
561 for np in myrange(number_persons):
562 email = person_name (i,np)
563 retcod = s[i].AddPersonToSlice (a[i], email, slicename)
564 print '%02d:== Attached person %s to slice %s'%(i,email,slicename)
567 def test04_node_slice (is_local, add_if_true, args=[1,2]):
568 for ns in myrange(number_slices):
569 test04_node_slice_ns (ns,is_local, add_if_true, args)
571 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
572 test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
574 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
577 slice_id = get_local_slice_id (i,slice_name (i,ns))
580 hostnames=[node_name(i,nn) for nn in nnl]
583 hostnames=[node_name(peer,nn) for nn in nnl]
586 s[i].AddSliceToNodes (a[i], slice_id,hostnames)
589 s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
591 print '%02d:== %s in slice %d %s '%(i,message,slice_id,nodetype),
594 def test04_slice_add_lnode (args=[1,2]):
595 test04_node_slice (True,True,args)
597 def test04_slice_add_fnode (args=[1,2]):
598 test04_node_slice (False,True,args)
600 def test04_slice_del_lnode (args=[1,2]):
601 test04_node_slice (True,False,args)
603 def test04_slice_del_fnode (args=[1,2]):
604 test04_node_slice (False,False,args)
607 def test_all_init ():
611 test00_admin_person ()
612 test00_admin_enable ()
613 test00_peer_person ()
615 test00_peer_passwd ()
617 def test_all_sites ():
619 test00_refresh ('after site creation')
621 def test_all_persons ():
623 test00_refresh ('before persons&keys creation')
625 check_persons(system_persons,system_persons_cross)
626 message ("Creating persons&keys")
629 message ("1 extra del/add cycle for unique indexes")
630 test02_del_person([2])
632 check_keys(number_persons*number_keys,0)
633 check_persons(system_persons+number_persons,system_persons_cross)
634 test00_refresh ('after persons&keys creation')
635 check_keys(number_persons*number_keys,number_persons*number_keys)
636 check_persons(system_persons+number_persons,system_persons_cross+number_persons)
638 def test_all_nodes ():
640 message ("RESETTING NODES")
642 test00_refresh ('cleaned nodes')
645 # create one node on each site
646 message ("CREATING NODES")
648 check_nodes(number_nodes,0)
649 test00_refresh ('after node creation')
650 check_nodes(number_nodes,number_nodes)
653 message ("2 extra del/add cycles on plc2 for different indexes")
658 check_nodes(0,number_nodes,[2])
659 test00_refresh('after deletion on plc2')
660 check_nodes(number_nodes,0,[1])
661 check_nodes(0,number_nodes,[2])
662 message ("ADD on plc2 for different indexes")
664 check_nodes (number_nodes,0,[1])
665 check_nodes (number_nodes,number_nodes,[2])
666 test00_refresh('after re-creation on plc2')
667 check_nodes (number_nodes,number_nodes,)
669 def test_all_addslices ():
672 message ("RESETTING SLICES TEST")
676 test00_refresh ("After slices init")
678 # create slices on plc1
679 message ("CREATING SLICES on plc1")
681 # each site has 3 local slices and 0 foreign slice
682 check_slices (total_slices(),0,[1])
683 check_slices (system_slices(),0,[2])
684 test00_refresh ("after slice created on plc1")
685 check_slices (total_slices(),0,[1])
686 check_slices (system_slices(),number_slices,[2])
687 # no slice has any node yet
688 check_local_slice_nodes(0,[1])
689 check_foreign_slice_nodes(0,[2])
691 # insert local nodes in local slice on plc1
692 message ("ADDING LOCAL NODES IN SLICES")
693 test04_slice_add_lnode ([1])
694 # of course the change is only local
695 check_local_slice_nodes (number_nodes,[1])
696 check_foreign_slice_nodes(0,[2])
699 test00_refresh ("After local nodes were added on plc1")
700 check_local_slice_nodes (number_nodes,[1])
701 check_foreign_slice_nodes (number_nodes,[2])
703 # now we add foreign nodes into local slice
704 message ("ADDING FOREIGN NODES IN SLICES")
705 test04_slice_add_fnode ([1])
706 check_local_slice_nodes (2*number_nodes,[1])
707 check_foreign_slice_nodes (number_nodes,[2])
710 test00_refresh ("After foreign nodes were added in plc1")
711 # remember that foreign slices only know about LOCAL nodes
712 # so this does not do anything
713 check_local_slice_nodes (2*number_nodes,[1])
714 check_foreign_slice_nodes (2*number_nodes,[2])
716 check_slivers_1(total_slivers())
718 def test_all_delslices ():
720 message ("DELETING FOREIGN NODES FROM SLICES")
721 test04_slice_del_fnode([1])
722 check_local_slice_nodes (number_nodes,[1])
723 check_foreign_slice_nodes (2*number_nodes,[2])
725 check_slivers_1(total_slivers(),[1])
727 test00_refresh ("After foreign nodes were removed on plc1")
728 check_local_slice_nodes (number_nodes,[1])
729 check_foreign_slice_nodes (number_nodes,[2])
731 message ("DELETING LOCAL NODES FROM SLICES")
732 test04_slice_del_lnode([1])
733 check_local_slice_nodes (0,[1])
734 check_foreign_slice_nodes (number_nodes,[2])
736 test00_refresh ("After local nodes were removed on plc1")
737 check_local_slice_nodes (0,[1])
738 check_foreign_slice_nodes (0,[2])
740 message ("CHECKING SLICES CLEAN UP")
741 clean_all_slices([1])
742 check_slices (system_slices(),0,[1])
743 check_slices (system_slices(),number_slices,[2])
744 test00_refresh ("After slices clenaup")
745 check_slices(system_slices(),0)
747 def test_all_slices ():
748 test_all_addslices ()
749 test_all_delslices ()
764 ### ad hoc test sequences
771 test00_refresh ("populate: refreshing peer 1",[1])
772 test04_slice_add_lnode([1])
773 test04_slice_add_fnode([1])
774 test00_refresh("populate: refresh all")
785 print "Usage: %s [-n] [-f]"%sys.argv[0]
786 print " -f runs faster (1 node - 1 slice)"
787 print " -b performs big run (4 times as large as normal)"
788 print " -n runs test_now instead of test_all"
789 print " -p runs populate instead of test_all"
795 (o,a) = getopt.getopt(sys.argv[1:], "fnpb")
805 print 'Running test_now'
808 print 'Running populate'
817 if __name__ == '__main__':