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
51 plc1={ 'plcname':'plc1 in federation',
52 'hostname':'lurch.cs.princeton.edu',
53 'url-format':'https://%s:443/PLCAPI/',
54 'builtin-admin-id':'root@plc1.org',
55 'builtin-admin-password':'root',
56 'peer-admin-name':'plc1@planet-lab.org',
57 'peer-admin-password':'peer',
58 'node-format':'n1%02d.plc1.org',
60 'slice-format' : 's1%02d',
61 'person-format' : 'user1-%d@plc1.org',
62 'key-format':'ssh-rsa 1111111111111111 user%d-key%d',
63 'person-password' : 'password1',
65 plc2={ 'plcname':'plc2 in federation',
66 'hostname':'planetlab-devbox.inria.fr',
67 'url-format':'https://%s:443/PLCAPI/',
68 'builtin-admin-id':'root@plc2.org',
69 'builtin-admin-password':'root',
70 'peer-admin-name':'plc2@planet-lab.org',
71 'peer-admin-password':'peer',
72 'node-format':'n2%02d.plc2.org',
74 'slice-format' : 's2%02d',
75 'person-format' : 'user2-%d@plc2.org',
76 'key-format':'ssh-rsa 2222222222222222 user%d-key%d',
77 'person-password' : 'password2',
81 # set initial conditions
82 def define_test (keys,persons,nodes,slices,fast_mode):
83 global number_keys, number_persons, number_nodes, number_slices, fast_flag
85 number_persons=persons
91 define_test(1,1,1,1,True)
93 define_test (keys=4,persons=2,nodes=5,slices=3,fast_mode=False)
96 # number of 'system' persons
97 # builtin maint, local root, 2 persons for the peering
99 # among that, 1 gets refreshed - other ones have conflicting names
100 system_persons_cross = 1
102 system_slices_ids = (1,2)
103 def system_slices ():
104 return len(system_slices_ids)
106 return number_slices+system_slices()
108 # temporary - the myplc I use doesnt know about 'system' yet
109 def system_slivers ():
110 # return len(system_slices_ids)
112 def total_slivers ():
113 return number_slices+system_slivers()
120 return plc[i]['plcname']
126 return plc[i]['node-format']%n
128 def slice_name (i,n):
129 return plc[i]['plainname']+'_'+plc[i]['slice-format']%n
131 def person_name (i,n):
132 return plc[i]['person-format']%n
134 def key_name (i,n,k):
135 return plc[i]['key-format']%(n,k)
137 # to have indexes start at 1
139 return range (1,n+1,1)
142 print "====================",
149 print '+++ timer start'
152 print '+++ %d seconds ellapsed'%(time.time()-epoch)
155 def test00_init (args=[1,2]):
157 ## have you loaded this file already (support for reload)
163 url=plc[i]['url-format']%plc[i]['hostname']
165 s[i]=xmlrpclib.ServerProxy(url,allow_none=True)
166 print 'initializing s[%d]'%i,url
167 aa[i]={'Username':plc[i]['builtin-admin-id'],
168 'AuthMethod':'password',
169 'AuthString':plc[i]['builtin-admin-password'],
171 print 'initialized aa[%d]'%i, aa[i]
172 a[i]={'Username':plc[i]['peer-admin-name'],
173 'AuthMethod':'password',
174 'AuthString':plc[i]['peer-admin-password'],
176 print 'initialized a[%d]'%i, a[i]
178 def test00_print (args=[1,2]):
181 print 'aa[%d]'%i, aa[i]
182 print 'a[%d]'%i, a[i]
184 def check_nodes (el,ef,args=[1,2]):
186 # use a single request and sort afterwards for efficiency
187 # could have used GetNodes's scope as well
188 all_nodes = s[i].GetNodes(a[i])
189 n = len ([ x for x in all_nodes if x['peer_id'] is None])
190 f = len ([ x for x in all_nodes if x['peer_id'] is not None])
191 print '%02d: Checking nodes: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
195 def check_keys (el,ef,args=[1,2]):
197 # use a single request and sort afterwards for efficiency
198 # could have used GetKeys's scope as well
199 all_keys = s[i].GetKeys(a[i])
200 n = len ([ x for x in all_keys if x['peer_id'] is None])
201 f = len ([ x for x in all_keys if x['peer_id'] is not None])
202 print '%02d: Checking keys: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
206 def check_persons (el,ef,args=[1,2]):
208 # use a single request and sort afterwards for efficiency
209 # could have used GetPersons's scope as well
210 all_persons = s[i].GetPersons(a[i])
211 n = len ([ x for x in all_persons if x['peer_id'] is None])
212 f = len ([ x for x in all_persons if x['peer_id'] is not None])
213 print '%02d: Checking persons: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
217 # expected : local slices, foreign slices
218 def check_slices (els,efs,args=[1,2]):
220 ls=len(s[i].GetSlices(a[i],{'peer_id':None}))
221 fs=len(s[i].GetSlices(a[i],{'~peer_id':None}))
222 print '%02d: Checking slices: got %d local (e=%d) & %d foreign (e=%d)'%(i,ls,els,fs,efs)
226 def show_nodes (i,node_ids):
228 all_nodes = s[i].GetNodes(a[i],node_ids)
229 loc_nodes = filter (lambda n: n['peer_id'] is None, all_nodes)
230 for_nodes = filter (lambda n: n['peer_id'] is not None, all_nodes)
232 for message,nodes in [ ['LOC',loc_nodes], ['FOR',for_nodes] ] :
234 print '[%s:%d] : '%(message,len(nodes)),
236 print node['hostname']+' ',
239 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
240 for ns in myrange(number_slices):
241 check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
243 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
247 sname=slice_name(i,ns)
248 slice=s[i].GetSlices(a[i],{'name':[sname],'peer_id':None})[0]
251 sname=slice_name(peer,ns)
252 slice=s[i].GetSlices(a[i],{'name':[sname],'~peer_id':None})[0]
254 print '%02d: %s slice %s (e=%d) '%(i,message,sname,expected_nodes),
255 slice_node_ids=slice['node_ids']
256 print 'on nodes ',slice_node_ids
257 show_nodes (i,slice_node_ids)
258 assert len(slice_node_ids)>=expected_nodes
259 if len(slice_node_ids) != expected_nodes:
262 # expected : nodes on local slice
263 def check_local_slice_nodes (expected, args=[1,2]):
264 check_slice_nodes(expected,True,args)
266 # expected : nodes on foreign slice
267 def check_foreign_slice_nodes (expected, args=[1,2]):
268 check_slice_nodes(expected,False,args)
270 def check_conf_files (args=[1,2]):
271 for nn in myrange(number_nodes):
272 check_conf_files_n (nn,args)
274 def check_conf_files_n (nn,args=[1,2]):
276 nodename=node_name(i,nn)
277 ndict= s[i].GetSlivers(a[i],[nodename])[0]
278 assert ndict['hostname'] == nodename
279 conf_files = ndict['conf_files']
280 print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
281 for conf_file in conf_files:
282 print 'source=',conf_file['source'],'|',
283 print 'dest=',conf_file['dest'],'|',
284 print 'enabled=',conf_file['enabled'],'|',
288 pp = pprint.PrettyPrinter(indent=3)
290 def check_slivers (esn,args=[1,2]):
291 for nn in myrange(number_nodes):
292 check_slivers_n (nn,esn,args)
294 # too verbose to check all nodes, let's check only the first one
295 def check_slivers_1 (esn,args=[1,2]):
296 check_slivers_n (1,esn,args)
298 def check_slivers_n (nn,esn,args=[1,2]):
300 nodename=node_name(i,nn)
301 ndict= s[i].GetSlivers(a[i],[nodename])[0]
302 assert ndict['hostname'] == nodename
303 slivers = ndict['slivers']
304 print '%02d: %d slivers (exp. %d) in GetSlivers for node %s'\
305 %(i,len(slivers),esn,nodename)
306 for sliver in slivers:
307 print '>>slivername = ',sliver['name']
309 assert len(slivers) == esn
313 def test00_admin_person (args=[1,2]):
316 email = plc[i]['peer-admin-name']
318 p=s[i].GetPersons(a[i],[email])[0]
319 plc[i]['peer-admin-id']=p['person_id']
321 person_id=s[i].AddPerson(aa[i],{'first_name':'Local',
322 'last_name':'PeerPoint',
325 'password':plc[i]['peer-admin-password']})
326 print '%02d:== created peer admin account %d, %s - %s'%(i,
327 person_id,plc[i]['peer-admin-name'],
328 plc[i]['peer-admin-password'])
329 plc[i]['peer-admin-id']=person_id
331 def test00_admin_enable (args=[1,2]):
333 s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer-admin-id'],True)
334 s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer-admin-id'])
335 print '%02d:== enabled+admin on account %d:%s'%(i,plc[i]['peer-admin-id'],plc[i]['peer-admin-name'])
337 def test01_peer_person (args=[1,2]):
341 email=plc[peer]['peer-admin-name']
343 p=s[i].GetPersons(a[i],[email])[0]
344 plc[i]['peer_person_id']=p['person_id']
346 person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
347 'email':email,'password':plc[peer]['peer-admin-password']})
348 print '%02d:Created person %d as the peer person'%(i,person_id)
349 plc[i]['peer_person_id']=person_id
352 def test01_peer (args=[1,2]):
356 peername = plc_name(peer)
358 p=s[i].GetPeers (a[i], [peername])[0]
359 plc[i]['peer_id']=p['peer_id']
361 peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
362 # NOTE : need to manually reset the encrypted password through SQL at this point
363 print '%02d:Created peer %d'%(i,peer_id)
364 plc[i]['peer_id']=peer_id
365 print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
367 def test01_peer_passwd (args=[1,2]):
369 # using an ad-hoc local command for now - never could get quotes to reach sql....
370 print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
371 retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
372 print '-> system returns',retcod
374 def test01_site (args=[1,2]):
378 ### create a site (required for creating a slice)
379 sitename=site_name(i)
380 abbrev_name="abbr"+str(i)
381 login_base=plc[i]['plainname']
382 ### should be enough - needs to check we can add/del slices
383 max_slices = number_slices
385 s[i].GetSites(a[i],{'login_base':login_base})[0]
387 site_id=s[i].AddSite (a[i], {'name':plc_name(i),
388 'abbreviated_name': abbrev_name,
389 'login_base': login_base,
391 'url': 'http://%s.com/'%abbrev_name,
392 'max_slices':max_slices})
393 ### max_slices does not seem taken into account at that stage
394 s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
395 print '%02d:== Created site %d with max_slices=%d'%(i,site_id,max_slices)
396 plc[i]['site_id']=site_id
398 ##############################
399 # this one gets cached
402 return plc[i]['peer_id']
404 peername = plc_name (peer_index(i))
405 peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
406 plc[i]['peer_id'] = peer_id
409 def test01_refresh (message,args=[1,2]):
410 print '=== refresh',message
413 print '%02d:== Refreshing peer'%(i),
414 retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
419 # retrieves node_id from hostname - checks for local nodes only
420 def get_local_node_id(i,nodename):
421 return s[i].GetNodes(a[i],[nodename],None,'local')[0]['node_id']
423 # clean all local nodes - foreign nodes are not supposed to be cleaned up manually
424 def clean_all_nodes (args=[1,2]):
426 print '%02d:== Cleaning all nodes'%i
427 loc_nodes = s[i].GetNodes(a[i],None,None,'local')
428 for node in loc_nodes:
429 print '%02d:==== Cleaning node %d'%(i,node['node_id'])
430 s[i].DeleteNode(a[i],node['node_id'])
432 def test02_node (args=[1,2]):
433 for nn in myrange(number_nodes):
434 test02_node_n (nn,args)
436 def test02_node_n (nn,args=[1,2]):
438 nodename = node_name(i,nn)
440 get_local_node_id(i,nodename)
442 n=s[i].AddNode(a[i],1,{'hostname': nodename})
443 print '%02d:== Added node %d %s'%(i,n,node_name(i,i))
445 def test02_delnode (args=[1,2]):
446 for nn in myrange(number_nodes):
447 test02_delnode_n (nn,args)
449 def test02_delnode_n (nn,args=[1,2]):
451 nodename = node_name(i,nn)
452 node_id = get_local_node_id (i,nodename)
453 retcod=s[i].DeleteNode(a[i],nodename)
454 print '%02d:== Deleted node %d, returns %s'%(i,node_id,retcod)
457 def test05_person (args=[1,2]):
458 for np in myrange(number_persons):
459 test05_person_n (np,True,args)
461 def test05_del_person (args=[1,2]):
462 for np in myrange(number_persons):
463 test05_person_n (np,False,args)
465 def test05_person_n (np,add_if_true,args=[1,2]):
466 test05_person_n_ks (np, myrange(number_keys),add_if_true,args)
468 def test05_person_n_ks (np,nks,add_if_true,args=[1,2]):
470 email = person_name(i,np)
472 person_id=s[i].GetPersons(a[i],[email])[0]['person_id']
474 s[i].DeletePerson(a[i],person_id)
475 print "%02d:== deleted person_id %d"%(i,person_id)
478 password = plc[i]['person-password']
479 person_id=s[i].AddPerson(a[i],{'first_name':'Your average',
480 'last_name':'User%d'%np,
483 'password': password })
484 print '%02d:== created user account %d, %s - %s'%(i, person_id,email,password)
486 key=key_name(i,np,nk)
487 s[i].AddPersonKey(aa[i],email,{'key_type':'ssh', 'key':key})
488 print '%02d:== added key %s to person %s'%(i,key,email)
491 def clean_all_slices (args=[1,2]):
493 print '%02d:== Cleaning all slices'%i
494 for slice in s[i].GetSlices(a[i],{'peer_id':None}):
495 slice_id = slice['slice_id']
496 if slice_id not in system_slices_ids:
497 print '%02d:==== Cleaning slice %d'%(i,slice_id)
498 s[i].DeleteSlice(a[i],slice_id)
500 def get_local_slice_id (i,name):
501 return s[i].GetSlices(a[i],{'name':[name],'peer_id':None})[0]['slice_id']
503 def test03_slice (args=[1,2]):
504 for n in myrange(number_slices):
505 test03_slice_n (n,args)
507 def test03_slice_n (ns,args=[1,2]):
511 slicename=slice_name(i,ns)
512 max_nodes=number_nodes
514 s[i].GetSlices(a[i],[slicename])[0]
516 slice_id=s[i].AddSlice (a[i],{'name':slicename,
517 'description':'slice %s on %s'%(slicename,plcname),
518 'url':'http://planet-lab.org/%s'%slicename,
519 'max_nodes':max_nodes,
520 'instanciation':'plc-instantiated',
522 print '%02d:== created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
523 for np in myrange(number_persons):
524 email = person_name (i,np)
525 retcod = s[i].AddPersonToSlice (a[i], email, slicename)
526 print '%02d:== Attached person %s to slice %s'%(i,email,slicename)
529 def test04_node_slice (is_local, add_if_true, args=[1,2]):
530 for ns in myrange(number_slices):
531 test04_node_slice_ns (ns,is_local, add_if_true, args)
533 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
534 test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
536 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
539 slice_id = get_local_slice_id (i,slice_name (i,ns))
542 hostnames=[node_name(i,nn) for nn in nnl]
545 hostnames=[node_name(peer,nn) for nn in nnl]
548 s[i].AddSliceToNodes (a[i], slice_id,hostnames)
551 s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
553 print '%02d:== %s in slice %d %s '%(i,message,slice_id,nodetype),
556 def test04_slice_add_lnode (args=[1,2]):
557 test04_node_slice (True,True,args)
559 def test04_slice_add_fnode (args=[1,2]):
560 test04_node_slice (False,True,args)
562 def test04_slice_del_lnode (args=[1,2]):
563 test04_node_slice (True,False,args)
565 def test04_slice_del_fnode (args=[1,2]):
566 test04_node_slice (False,False,args)
569 def test_all_init ():
573 test00_admin_person ()
574 test00_admin_enable ()
575 test01_peer_person ()
577 test01_peer_passwd ()
581 def test_all_nodes ():
583 message ("RESETTING NODES")
585 test01_refresh ('cleaned nodes')
588 # create one node on each site
589 message ("CREATING NODES")
591 check_nodes(number_nodes,0)
592 test01_refresh ('after node creation')
593 check_nodes(number_nodes,number_nodes)
596 message ("2 extra del/add cycles on plc2 for different indexes")
601 check_nodes(0,number_nodes,[2])
602 test01_refresh('after deletion on plc2')
603 check_nodes(number_nodes,0,[1])
604 check_nodes(0,number_nodes,[2])
605 message ("ADD on plc2 for different indexes")
607 check_nodes (number_nodes,0,[1])
608 check_nodes (number_nodes,number_nodes,[2])
609 test01_refresh('after re-creation on plc2')
610 check_nodes (number_nodes,number_nodes,)
612 def test_all_addslices ():
615 message ("RESETTING SLICES TEST")
619 test01_refresh ("After slices init")
621 # create slices on plc1
622 message ("CREATING SLICES on plc1")
624 # each site has 3 local slices and 0 foreign slice
625 check_slices (total_slices(),0,[1])
626 check_slices (system_slices(),0,[2])
627 test01_refresh ("after slice created on plc1")
628 check_slices (total_slices(),0,[1])
629 check_slices (system_slices(),number_slices,[2])
630 # no slice has any node yet
631 check_local_slice_nodes(0,[1])
632 check_foreign_slice_nodes(0,[2])
634 # insert local nodes in local slice on plc1
635 message ("ADDING LOCAL NODES IN SLICES")
636 test04_slice_add_lnode ([1])
637 # of course the change is only local
638 check_local_slice_nodes (number_nodes,[1])
639 check_foreign_slice_nodes(0,[2])
642 test01_refresh ("After local nodes were added on plc1")
643 check_local_slice_nodes (number_nodes,[1])
644 check_foreign_slice_nodes (number_nodes,[2])
646 # now we add foreign nodes into local slice
647 message ("ADDING FOREIGN NODES IN SLICES")
648 test04_slice_add_fnode ([1])
649 check_local_slice_nodes (2*number_nodes,[1])
650 check_foreign_slice_nodes (number_nodes,[2])
653 test01_refresh ("After foreign nodes were added in plc1")
654 # remember that foreign slices only know about LOCAL nodes
655 # so this does not do anything
656 check_local_slice_nodes (2*number_nodes,[1])
657 check_foreign_slice_nodes (2*number_nodes,[2])
659 check_slivers_1(total_slivers())
661 def test_all_delslices ():
663 message ("DELETING FOREIGN NODES FROM SLICES")
664 test04_slice_del_fnode([1])
665 check_local_slice_nodes (number_nodes,[1])
666 check_foreign_slice_nodes (2*number_nodes,[2])
668 check_slivers_1(total_slivers(),[1])
670 test01_refresh ("After foreign nodes were removed on plc1")
671 check_local_slice_nodes (number_nodes,[1])
672 check_foreign_slice_nodes (number_nodes,[2])
674 message ("DELETING LOCAL NODES FROM SLICES")
675 test04_slice_del_lnode([1])
676 check_local_slice_nodes (0,[1])
677 check_foreign_slice_nodes (number_nodes,[2])
679 test01_refresh ("After local nodes were removed on plc1")
680 check_local_slice_nodes (0,[1])
681 check_foreign_slice_nodes (0,[2])
683 message ("CHECKING SLICES CLEAN UP")
684 clean_all_slices([1])
685 check_slices (system_slices(),0,[1])
686 check_slices (system_slices(),number_slices,[2])
687 test01_refresh ("After slices clenaup")
688 check_slices(system_slices(),0)
690 def test_all_slices ():
691 test_all_addslices ()
692 test_all_delslices ()
694 def test_all_persons ():
696 test01_refresh ('before persons&keys creation')
698 check_persons(system_persons,system_persons_cross)
699 message ("Creating persons&keys")
702 message ("1 extra del/add cycle for unique indexes")
703 test05_del_person([2])
705 check_keys(number_persons*number_keys,0)
706 check_persons(system_persons+number_persons,system_persons_cross)
707 test01_refresh ('after persons&keys creation')
708 check_keys(number_persons*number_keys,number_persons*number_keys)
709 check_persons(system_persons+number_persons,system_persons_cross+number_persons)
721 ### ad hoc test sequences
726 test01_refresh ("populate: refreshing peer 1",[1])
727 test04_slice_add_lnode([1])
728 test04_slice_add_fnode([1])
729 test01_refresh("populate: refresh all")
740 print "Usage: %s [-n] [-f]"%sys.argv[0]
741 print " -f runs faster (1 node - 1 slice)"
742 print " -n runs test_now instead of test_all"
743 print " -p runs populate instead of test_all"
749 (o,a) = getopt.getopt(sys.argv[1:], "fnp")
757 print 'Running test_now'
760 print 'Running populate'
766 print '%d nodes & %d slices'%(number_nodes,number_slices)
770 if __name__ == '__main__':