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 'peer_admin_key':'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAqK1lKNf61lAGYAXzG6xKnFQkfv3ViG0GP2Krp1zD7d/93IkmsXVEjLfGhEJQjRjzRc9/gFdATP703cDzp4Ag2eR2wdQz0e6SXOBd2sLuW3LqTwor1XMmp5f0QCOg5OSKXwozE3Tlt0+ewBNvAE8HWwZFjou5CFnrFMVZPjqfhpU= thierry.parmentelat@sophia.inria.fr',
58 'node-format':'n1%02d.plc1.org',
60 'slice-format' : 's1%02d',
62 plc2={ 'plcname':'plc2 in federation',
63 'hostname':'planetlab-devbox.inria.fr',
64 'url-format':'https://%s:443/PLCAPI/',
65 'builtin_admin_id':'root@localhost.localdomain',
66 'builtin_admin_password':'root',
67 'peer_admin_name':'plc2@planet-lab.org',
68 'peer_admin_password':'peer',
69 'peer_admin_key':'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAlsX+X+sgN/rsNizPhsXMdHzArxVdwN1KJMG4vTY1m0KQMFJSilaX6xlMIirKhFmNDrVkrOPT2On59K4fDFjGgDq9gMMfdaAfEhxDdy2x1k/H/vJRQE/CqkoRZE8mVAt/cgMsOHTLJTDxBm/0RedaBlcXBpCwqBi3n05sGkS2BjM= VTHD-tester-ssh-v2',
70 'node-format':'n2%02d.plc2.org',
72 'slice-format' : 's2%02d',
76 # set initial conditions
77 def define_test (nodes,slices):
78 global number_nodes, number_slices
85 define_test (nodes=5,slices=3)
88 system_slices_ids = (1,2)
90 return len(system_slices_ids)
91 # temporary - the myplc I use doesnt know about 'system' yet
92 def system_slivers ():
93 # return len(system_slices_ids)
97 return number_slices+system_slices()
99 return number_slices+system_slivers()
106 return plc[i]['plcname']
112 return plc[i]['node-format']%n
114 def slice_name (i,n):
115 return plc[i]['plainname']+'_'+plc[i]['slice-format']%n
117 # to have indexes start at 1
119 return range (1,n+1,1)
122 print "====================",
126 def test00_init (args=[1,2]):
128 ## have you loaded this file already (support for reload)
134 url=plc[i]['url-format']%plc[i]['hostname']
136 s[i]=xmlrpclib.ServerProxy(url,allow_none=True)
137 print 'initializing s[%d]'%i,url
138 aa[i]={'Username':plc[i]['builtin_admin_id'],
139 'AuthMethod':'password',
140 'AuthString':plc[i]['builtin_admin_password'],
142 print 'initialized aa[%d]'%i, aa[i]
143 a[i]={'Username':plc[i]['peer_admin_name'],
144 'AuthMethod':'password',
145 'AuthString':plc[i]['peer_admin_password'],
147 print 'initialized a[%d]'%i, a[i]
149 def test00_print (args=[1,2]):
153 print 'aa[%d]'%i, aa[i]
154 print 'a[%d]'%i, a[i]
156 def check_nodes (en,ef,args=[1,2]):
159 # use a single request and sort afterwards for efficiency
160 # could have used GetNodes's scope as well
161 all_nodes = s[i].GetNodes(a[i])
162 n = len ([ x for x in all_nodes if x['peer_id'] is None])
163 f = len ([ x for x in all_nodes if x['peer_id'] is not None])
164 print '%02d: Checking nodes: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,en,f,ef)
168 # expected : local slices, foreign slices
169 def check_slices (els,efs,args=[1,2]):
172 ls=len(s[i].GetSlices(a[i],{'peer_id':None}))
173 fs=len(s[i].GetSlices(a[i],{'~peer_id':None}))
174 print '%02d: Checking slices: got %d local (e=%d) & %d foreign (e=%d)'%(i,ls,els,fs,efs)
178 def show_nodes (i,node_ids):
180 all_nodes = s[i].GetNodes(a[i],node_ids)
181 loc_nodes = filter (lambda n: n['peer_id'] is None, all_nodes)
182 for_nodes = filter (lambda n: n['peer_id'] is not None, all_nodes)
184 for message,nodes in [ ['LOC',loc_nodes], ['FOR',for_nodes] ] :
186 print '[%s:%d] : '%(message,len(nodes)),
188 print node['hostname']+' ',
191 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
192 for ns in myrange(number_slices):
193 check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
195 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
200 sname=slice_name(i,ns)
201 slice=s[i].GetSlices(a[i],{'name':[sname],'peer_id':None})[0]
204 sname=slice_name(peer,ns)
205 slice=s[i].GetSlices(a[i],{'name':[sname],'~peer_id':None})[0]
207 print '%02d: %s slice %s (e=%d) '%(i,message,sname,expected_nodes),
208 slice_node_ids=slice['node_ids']
209 print 'on nodes ',slice_node_ids
210 show_nodes (i,slice_node_ids)
211 assert len(slice_node_ids)>=expected_nodes
212 if len(slice_node_ids) != expected_nodes:
215 # expected : nodes on local slice
216 def check_local_slice_nodes (expected, args=[1,2]):
217 check_slice_nodes(expected,True,args)
219 # expected : nodes on foreign slice
220 def check_foreign_slice_nodes (expected, args=[1,2]):
221 check_slice_nodes(expected,False,args)
223 def check_conf_files (args=[1,2]):
224 for nn in myrange(number_nodes):
225 check_conf_files_n (nn,args)
227 def check_conf_files_n (nn,args=[1,2]):
230 nodename=node_name(i,nn)
231 ndict= s[i].GetSlivers(a[i],[nodename])[0]
232 assert ndict['hostname'] == nodename
233 conf_files = ndict['conf_files']
234 print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
235 for conf_file in conf_files:
236 print 'source=',conf_file['source'],'|',
237 print 'dest=',conf_file['dest'],'|',
238 print 'enabled=',conf_file['enabled'],'|',
242 pp = pprint.PrettyPrinter(indent=3)
244 def check_slivers (esn,args=[1,2]):
245 for nn in myrange(number_nodes):
246 check_slivers_n (nn,esn,args)
248 # too verbose to check all nodes, let's check only the first one
249 def check_slivers_1 (esn,args=[1,2]):
250 check_slivers_n (1,esn,args)
252 def check_slivers_n (nn,esn,args=[1,2]):
255 nodename=node_name(i,nn)
256 ndict= s[i].GetSlivers(a[i],[nodename])[0]
257 assert ndict['hostname'] == nodename
258 slivers = ndict['slivers']
259 print '%02d: %d slivers (exp. %d) in GetSlivers for node %s'\
260 %(i,len(slivers),esn,nodename)
261 for sliver in slivers:
262 print '>>slivername = ',sliver['name']
264 assert len(slivers) == esn
268 def test00_admin_person (args=[1,2]):
271 email = plc[i]['peer_admin_name']
273 p=s[i].GetPersons(a[i],[email])[0]
274 plc[i]['peer_admin_id']=p['person_id']
276 person_id=s[i].AddPerson(aa[i],{'first_name':'Local',
277 'last_name':'PeerPoint',
280 'password':plc[i]['peer_admin_password']})
281 print '%02d: created peer admin account %d, %s - %s'%(i,
282 person_id,plc[i]['peer_admin_name'],
283 plc[i]['peer_admin_password'])
284 plc[i]['peer_admin_id']=person_id
285 s[i].AddPersonKey(aa[i],email,{'key_type':'ssh',
286 'key':plc[i]['peer_admin_key']})
287 print '%02d: added key to peer admin '%i
289 def test00_admin_enable (args=[1,2]):
292 s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
293 s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
294 print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
297 def test01_site (args=[1,2]):
301 ### create a site (required for creating a slice)
302 sitename=site_name(i)
303 abbrev_name="abbr"+str(i)
304 login_base=plc[i]['plainname']
305 ### should be enough - needs to check we can add/del slices
306 max_slices = number_slices
308 s[i].GetSites(a[i],{'login_base':login_base})[0]
310 site_id=s[i].AddSite (a[i], {'name':plc_name(i),
311 'abbreviated_name': abbrev_name,
312 'login_base': login_base,
314 'url': 'http://%s.com/'%abbrev_name,
315 'max_slices':max_slices})
316 ### max_slices does not seem taken into account at that stage
317 s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
318 print '%02d: Created site %d with max_slices=%d'%(i,site_id,max_slices)
319 plc[i]['site_id']=site_id
321 def test01_peer_person (args=[1,2]):
325 email=plc[peer]['peer_admin_name']
327 p=s[i].GetPersons(a[i],[email])[0]
328 plc[i]['peer_person_id']=p['person_id']
330 person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
331 'email':email,'password':plc[peer]['peer_admin_password']})
332 print '%02d:Created person %d as the peer person'%(i,person_id)
333 plc[i]['peer_person_id']=person_id
335 def test01_peer (args=[1,2]):
339 peername = plc_name(peer)
341 p=s[i].GetPeers (a[i], [peername])[0]
342 plc[i]['peer_id']=p['peer_id']
344 peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
345 # NOTE : need to manually reset the encrypted password through SQL at this point
346 print '%02d:Created peer %d'%(i,peer_id)
347 plc[i]['peer_id']=peer_id
348 print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
350 def test01_peer_passwd (args=[1,2]):
353 # using an ad-hoc local command for now - never could get quotes to reach sql....
354 print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
355 retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
356 print '-> system returns',retcod
358 ##############################
359 # this one gets cached
362 return plc[i]['peer_id']
364 peername = plc_name (peer_index(i))
365 peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
366 plc[i]['peer_id'] = peer_id
369 def test01_refresh (message,args=[1,2]):
371 print '=== refresh',message
373 print '%02d: Refreshing peer'%(i),
374 retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
378 # retrieves node_id from hostname - checks for local nodes only
379 def get_local_node_id(i,nodename):
380 return s[i].GetNodes(a[i],[nodename],None,'local')[0]['node_id']
382 # clean all local nodes - foreign nodes are not supposed to be cleaned up manually
383 def clean_all_nodes (args=[1,2]):
386 print '%02d: Cleaning all nodes'%i
387 loc_nodes = s[i].GetNodes(a[i],None,None,'local')
388 for node in loc_nodes:
389 print '%02d: > Cleaning node %d'%(i,node['node_id'])
390 s[i].DeleteNode(a[i],node['node_id'])
392 def test02_node (args=[1,2]):
393 for nn in myrange(number_nodes):
394 test02_node_n (nn,args)
396 def test02_node_n (nn,args=[1,2]):
399 nodename = node_name(i,nn)
401 get_local_node_id(i,nodename)
403 n=s[i].AddNode(a[i],1,{'hostname': nodename})
404 print '%02d: Added node %d %s'%(i,n,node_name(i,i))
406 def test02_delnode (args=[1,2]):
407 for nn in myrange(number_nodes):
408 test02_delnode_n (nn,args)
410 def test02_delnode_n (nn,args=[1,2]):
413 nodename = node_name(i,nn)
414 node_id = get_local_node_id (i,nodename)
415 retcod=s[i].DeleteNode(a[i],nodename)
416 print '%02d: Deleted node %d, returns %s'%(i,node_id,retcod)
419 def clean_all_slices (args=[1,2]):
422 print '%02d: Cleaning all slices'%i
423 for slice in s[i].GetSlices(a[i],{'peer_id':None}):
424 slice_id = slice['slice_id']
425 if slice_id not in system_slices_ids:
426 print '%02d: > Cleaning slice %d'%(i,slice_id)
427 s[i].DeleteSlice(a[i],slice_id)
429 def get_local_slice_id (i,name):
430 return s[i].GetSlices(a[i],{'name':[name],'peer_id':None})[0]['slice_id']
432 def test03_slice (args=[1,2]):
433 for n in myrange(number_slices):
434 test03_slice_n (n,args)
436 def test03_slice_n (ns,args=[1,2]):
441 slicename=slice_name(i,ns)
442 max_nodes=number_nodes
444 s[i].GetSlices(a[i],[slicename])[0]
446 slice_id=s[i].AddSlice (a[i],{'name':slicename,
447 'description':'slice %s on %s'%(slicename,plcname),
448 'url':'http://planet-lab.org/%s'%slicename,
449 'max_nodes':max_nodes,
450 'instanciation':'plc-instantiated',
452 print '%02d: created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
455 def test04_node_slice (is_local, add_if_true, args=[1,2]):
456 for ns in myrange(number_slices):
457 test04_node_slice_ns (ns,is_local, add_if_true, args)
459 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
460 test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
462 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
466 slice_id = get_local_slice_id (i,slice_name (i,ns))
469 hostnames=[node_name(i,nn) for nn in nnl]
472 hostnames=[node_name(peer,nn) for nn in nnl]
475 s[i].AddSliceToNodes (a[i], slice_id,hostnames)
478 s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
480 print '%02d: %s in slice %d %s '%(i,message,slice_id,nodetype),
483 def test04_slice_add_lnode (args=[1,2]):
484 test04_node_slice (True,True,args)
486 def test04_slice_add_fnode (args=[1,2]):
487 test04_node_slice (False,True,args)
489 def test04_slice_del_lnode (args=[1,2]):
490 test04_node_slice (True,False,args)
492 def test04_slice_del_fnode (args=[1,2]):
493 test04_node_slice (False,False,args)
496 def test_all_init ():
500 test00_admin_person ()
501 test00_admin_enable ()
502 test01_peer_person ()
504 test01_peer_passwd ()
508 def test_all_nodes ():
510 message ("RESETTING NODES")
512 test01_refresh ('cleaned nodes')
515 # create one node on each site
516 message ("CREATING NODES")
518 check_nodes(number_nodes,0)
519 test01_refresh ('after node creation')
520 check_nodes(number_nodes,number_nodes)
521 message ("2 extra del/add cycles on plc2 for different indexes")
527 check_nodes(0,number_nodes,[2])
528 test01_refresh('after deletion on plc2')
529 check_nodes(number_nodes,0,[1])
530 check_nodes(0,number_nodes,[2])
531 message ("ADD on plc2 for different indexes")
533 check_nodes (number_nodes,0,[1])
534 check_nodes (number_nodes,number_nodes,[2])
535 test01_refresh('after re-creation on plc2')
536 check_nodes (number_nodes,number_nodes,)
538 def test_all_addslices ():
541 message ("RESETTING SLICES TEST")
545 test01_refresh ("After slices init")
547 # create slices on plc1
548 message ("CREATING SLICES on plc1")
550 # each site has 3 local slices and 0 foreign slice
551 check_slices (total_slices(),0,[1])
552 check_slices (system_slices(),0,[2])
553 test01_refresh ("after slice created on plc1")
554 check_slices (total_slices(),0,[1])
555 check_slices (system_slices(),number_slices,[2])
556 # no slice has any node yet
557 check_local_slice_nodes(0,[1])
558 check_foreign_slice_nodes(0,[2])
560 # insert local nodes in local slice on plc1
561 message ("ADDING LOCAL NODES IN SLICES")
562 test04_slice_add_lnode ([1])
563 # of course the change is only local
564 check_local_slice_nodes (number_nodes,[1])
565 check_foreign_slice_nodes(0,[2])
568 test01_refresh ("After local nodes were added on plc1")
569 check_local_slice_nodes (number_nodes,[1])
570 check_foreign_slice_nodes (number_nodes,[2])
572 # now we add foreign nodes into local slice
573 message ("ADDING FOREIGN NODES IN SLICES")
574 test04_slice_add_fnode ([1])
575 check_local_slice_nodes (2*number_nodes,[1])
576 check_foreign_slice_nodes (number_nodes,[2])
579 test01_refresh ("After foreign nodes were added in plc1")
580 # remember that foreign slices only know about LOCAL nodes
581 # so this does not do anything
582 check_local_slice_nodes (2*number_nodes,[1])
583 check_foreign_slice_nodes (2*number_nodes,[2])
585 check_slivers_1(total_slivers())
587 def test_all_delslices ():
589 message ("DELETING FOREIGN NODES FROM SLICES")
590 test04_slice_del_fnode([1])
591 check_local_slice_nodes (number_nodes,[1])
592 check_foreign_slice_nodes (2*number_nodes,[2])
594 check_slivers_1(total_slivers(),[1])
596 test01_refresh ("After foreign nodes were removed on plc1")
597 check_local_slice_nodes (number_nodes,[1])
598 check_foreign_slice_nodes (number_nodes,[2])
600 message ("DELETING LOCAL NODES FROM SLICES")
601 test04_slice_del_lnode([1])
602 check_local_slice_nodes (0,[1])
603 check_foreign_slice_nodes (number_nodes,[2])
605 test01_refresh ("After local nodes were removed on plc1")
606 check_local_slice_nodes (0,[1])
607 check_foreign_slice_nodes (0,[2])
609 message ("CHECKING SLICES CLEAN UP")
610 clean_all_slices([1])
611 check_slices (system_slices(),0,[1])
612 check_slices (system_slices(),number_slices,[2])
613 test01_refresh ("After slices clenaup")
614 check_slices(system_slices(),0)
616 def test_all_slices ():
617 test_all_addslices ()
618 test_all_delslices ()
625 ### ad hoc test sequences
629 test01_refresh ("populate: refreshing peer 1",[1])
630 test04_slice_add_lnode([1])
631 test04_slice_add_fnode([1])
632 test01_refresh("populate: refresh all")
642 print "Usage: %s [-n] [-f]"%sys.argv[0]
643 print " -f runs faster (1 node - 1 slice)"
644 print " -n runs test_now instead of test_all"
650 (o,a) = getopt.getopt(sys.argv[1:], "fn")
663 print '%d nodes & %d slices'%(number_nodes,number_slices)
665 print 'Running test_now'
670 if __name__ == '__main__':