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)
20 ## we use indexes 1 and 2
30 ## the authentication objects
36 ## the builtin root user for bootstrapping
47 plc1={ 'plcname':'plc1 in federation',
48 'hostname':'lurch.cs.princeton.edu',
49 'url-format':'https://%s:443/PLCAPI/',
50 'builtin_admin_id':'root@localhost.localdomain',
51 'builtin_admin_password':'root',
52 'peer_admin_name':'plc1@planet-lab.org',
53 'peer_admin_password':'peer',
54 'node-format':'n1%02d.plc1.org',
56 'slice-format' : 's1%02d',
58 plc2={ 'plcname':'plc2 in federation',
59 'hostname':'planetlab-devbox.inria.fr',
60 'url-format':'https://%s:443/PLCAPI/',
61 'builtin_admin_id':'root@localhost.localdomain',
62 'builtin_admin_password':'root',
63 'peer_admin_name':'plc2@planet-lab.org',
64 'peer_admin_password':'peer',
65 'node-format':'n2%02d.plc2.org',
67 'slice-format' : 's2%02d',
71 # set initial conditions
72 def define_test (nodes,slices):
73 global number_nodes, number_slices
80 define_test (nodes=5,slices=3)
83 system_slices_ids = (1,2)
85 return len(system_slices_ids)
86 # temporary - the myplc I use doesnt know about 'system' yet
87 def system_slivers ():
88 # return len(system_slices_ids)
92 return number_slices+system_slices()
94 return number_slices+system_slivers()
101 return plc[i]['plcname']
107 return plc[i]['node-format']%n
109 def slice_name (i,n):
110 return plc[i]['plainname']+'_'+plc[i]['slice-format']%n
112 # to have indexes start at 1
114 return range (1,n+1,1)
117 print "XXXXXXXXXXXXXXXXXXXX",
121 def test00_init (args=[1,2]):
123 ## have you loaded this file already (support for reload)
129 url=plc[i]['url-format']%plc[i]['hostname']
131 s[i]=xmlrpclib.ServerProxy(url,allow_none=True)
132 print 'initializing s[%d]'%i,url
133 aa[i]={'Username':plc[i]['builtin_admin_id'],
134 'AuthMethod':'password',
135 'AuthString':plc[i]['builtin_admin_password'],
137 print 'initialized aa[%d]'%i, aa[i]
138 a[i]={'Username':plc[i]['peer_admin_name'],
139 'AuthMethod':'password',
140 'AuthString':plc[i]['peer_admin_password'],
142 print 'initialized a[%d]'%i, a[i]
144 def test00_print (args=[1,2]):
148 print 'aa[%d]'%i, aa[i]
149 print 'a[%d]'%i, a[i]
151 def check_nodes (en,ef,args=[1,2]):
154 # use a single request and sort afterwards for efficiency
155 # could have used GetNodes's scope as well
156 all_nodes = s[i].GetNodes(a[i])
157 n = len ([ x for x in all_nodes if x['peer_id'] is None])
158 f = len ([ x for x in all_nodes if x['peer_id'] is not None])
159 print '%02d: Checking nodes: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,en,f,ef)
163 # expected : local slices, foreign slices
164 def check_slices (els,efs,args=[1,2]):
167 ls=len(s[i].GetSlices(a[i],{'peer_id':None}))
168 fs=len(s[i].GetSlices(a[i],{'~peer_id':None}))
169 print '%02d: Checking slices: got %d local (e=%d) & %d foreign (e=%d)'%(i,ls,els,fs,efs)
173 def show_nodes (i,node_ids):
175 all_nodes = s[i].GetNodes(a[i],node_ids)
176 loc_nodes = filter (lambda n: n['peer_id'] is None, all_nodes)
177 for_nodes = filter (lambda n: n['peer_id'] is not None, all_nodes)
179 for message,nodes in [ ['LOC',loc_nodes], ['FOR',for_nodes] ] :
181 print '[%s:%d] : '%(message,len(nodes)),
183 print node['hostname']+' ',
186 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
187 for ns in myrange(number_slices):
188 check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
190 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
195 sname=slice_name(i,ns)
196 slice=s[i].GetSlices(a[i],{'name':[sname],'peer_id':None})[0]
199 sname=slice_name(peer,ns)
200 slice=s[i].GetSlices(a[i],{'name':[sname],'~peer_id':None})[0]
202 print '%02d: %s slice %s (e=%d) '%(i,message,sname,expected_nodes),
203 slice_node_ids=slice['node_ids']
204 print 'on nodes ',slice_node_ids
205 show_nodes (i,slice_node_ids)
206 assert len(slice_node_ids)==expected_nodes
208 # expected : nodes on local slice
209 def check_local_slice_nodes (expected, args=[1,2]):
210 check_slice_nodes(expected,True,args)
212 # expected : nodes on foreign slice
213 def check_foreign_slice_nodes (expected, args=[1,2]):
214 check_slice_nodes(expected,False,args)
216 def check_conf_files (args=[1,2]):
217 for nn in myrange(number_nodes):
218 check_conf_files_n (nn,args)
220 def check_conf_files_n (nn,args=[1,2]):
223 nodename=node_name(i,nn)
224 ndict= s[i].GetSlivers(a[i],[nodename])[0]
225 assert ndict['hostname'] == nodename
226 conf_files = ndict['conf_files']
227 print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
228 for conf_file in conf_files:
229 print 'source=',conf_file['source'],'|',
230 print 'dest=',conf_file['dest'],'|',
231 print 'enabled=',conf_file['enabled'],'|',
235 pp = pprint.PrettyPrinter(indent=3)
237 def check_slivers (esn,args=[1,2]):
238 for nn in myrange(number_nodes):
239 check_slivers_n (nn,esn,args)
241 # too verbose to check all nodes, let's check only the first one
242 def check_slivers_1 (esn,args=[1,2]):
243 check_slivers_n (1,esn,args)
245 def check_slivers_n (nn,esn,args=[1,2]):
248 nodename=node_name(i,nn)
249 ndict= s[i].GetSlivers(a[i],[nodename])[0]
250 assert ndict['hostname'] == nodename
251 slivers = ndict['slivers']
252 print '%02d: %d slivers (exp. %d) in GetSlivers for node %s'\
253 %(i,len(slivers),esn,nodename)
254 for sliver in slivers:
255 print '>>slivername = ',sliver['name']
257 assert len(slivers) == esn
261 def test00_admin_person (args=[1,2]):
264 email = plc[i]['peer_admin_name']
266 p=s[i].GetPersons(a[i],[email])[0]
267 plc[i]['peer_admin_id']=p['person_id']
269 person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 'last_name':'PeerPoint', 'role_ids':[10],
270 'email':email,'password':plc[i]['peer_admin_password']})
271 print '%02d: created peer admin account %d, %s - %s'%(i,person_id,plc[i]['peer_admin_name'],plc[i]['peer_admin_password'])
272 plc[i]['peer_admin_id']=person_id
274 def test00_admin_enable (args=[1,2]):
277 s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
278 s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
279 print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
282 def test01_site (args=[1,2]):
286 ### create a site (required for creating a slice)
287 sitename=site_name(i)
288 abbrev_name="abbr"+str(i)
289 login_base=plc[i]['plainname']
290 ### should be enough - needs to check we can add/del slices
291 max_slices = number_slices
293 s[i].GetSites(a[i],{'login_base':login_base})[0]
295 site_id=s[i].AddSite (a[i], {'name':plc_name(i),
296 'abbreviated_name': abbrev_name,
297 'login_base': login_base,
299 'url': 'http://%s.com/'%abbrev_name,
300 'max_slices':max_slices})
301 ### max_slices does not seem taken into account at that stage
302 s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
303 print '%02d: Created site %d with max_slices=%d'%(i,site_id,max_slices)
304 plc[i]['site_id']=site_id
306 def test01_peer_person (args=[1,2]):
310 email=plc[peer]['peer_admin_name']
312 p=s[i].GetPersons(a[i],[email])[0]
313 plc[i]['peer_person_id']=p['person_id']
315 person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
316 'email':email,'password':plc[peer]['peer_admin_password']})
317 print '%02d:Created person %d as the peer person'%(i,person_id)
318 plc[i]['peer_person_id']=person_id
320 def test01_peer (args=[1,2]):
324 peername = plc_name(peer)
326 p=s[i].GetPeers (a[i], [peername])[0]
327 plc[i]['peer_id']=p['peer_id']
329 peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
330 # NOTE : need to manually reset the encrypted password through SQL at this point
331 print '%02d:Created peer %d'%(i,peer_id)
332 plc[i]['peer_id']=peer_id
333 print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
335 def test01_peer_passwd (args=[1,2]):
338 # using an ad-hoc local command for now - never could get quotes to reach sql....
339 print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
340 retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
341 print '-> system returns',retcod
343 ##############################
344 # this one gets cached
347 return plc[i]['peer_id']
349 peername = plc_name (peer_index(i))
350 peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
351 plc[i]['peer_id'] = peer_id
354 def test01_refresh (args=[1,2]):
357 print '%02d: Refreshing peer'%(i),
358 retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
362 # retrieves node_id from hostname - checks for local nodes only
363 def get_local_node_id(i,nodename):
364 return s[i].GetNodes(a[i],[nodename],None,'local')[0]['node_id']
366 # clean all local nodes - foreign nodes are not supposed to be cleaned up manually
367 def clean_all_nodes (args=[1,2]):
370 print '%02d: Cleaning all nodes'%i
371 loc_nodes = s[i].GetNodes(a[i],None,None,'local')
372 for node in loc_nodes:
373 print '%02d: > Cleaning node %d'%(i,node['node_id'])
374 s[i].DeleteNode(a[i],node['node_id'])
376 def test02_node (args=[1,2]):
377 for nn in myrange(number_nodes):
378 test02_node_n (nn,args)
380 def test02_node_n (nn,args=[1,2]):
383 nodename = node_name(i,nn)
385 get_local_node_id(i,nodename)
387 n=s[i].AddNode(a[i],1,{'hostname': nodename})
388 print '%02d: Added node %d %s'%(i,n,node_name(i,i))
390 def test02_delnode (args=[1,2]):
391 for nn in myrange(number_nodes):
392 test02_delnode_n (nn,args)
394 def test02_delnode_n (nn,args=[1,2]):
397 nodename = node_name(i,nn)
398 node_id = get_local_node_id (i,nodename)
399 retcod=s[i].DeleteNode(a[i],nodename)
400 print '%02d: Deleted node %d, returns %s'%(i,node_id,retcod)
403 def clean_all_slices (args=[1,2]):
406 print '%02d: Cleaning all slices'%i
407 for slice in s[i].GetSlices(a[i],{'peer_id':None}):
408 slice_id = slice['slice_id']
409 if slice_id not in system_slices_ids:
410 print '%02d: > Cleaning slice %d'%(i,slice_id)
411 s[i].DeleteSlice(a[i],slice_id)
413 def get_local_slice_id (i,name):
414 return s[i].GetSlices(a[i],{'name':[name],'peer_id':None})[0]['slice_id']
416 def test03_slice (args=[1,2]):
417 for n in myrange(number_slices):
418 test03_slice_n (n,args)
420 def test03_slice_n (ns,args=[1,2]):
425 slicename=slice_name(i,ns)
426 max_nodes=number_nodes
428 s[i].GetSlices(a[i],[slicename])[0]
430 slice_id=s[i].AddSlice (a[i],{'name':slicename,
431 'description':'slice %s on %s'%(slicename,plcname),
432 'url':'http://planet-lab.org/%s'%slicename,
433 'max_nodes':max_nodes,
434 'instanciation':'plc-instantiated',
436 print '%02d: created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
439 def test04_node_slice (is_local, add_if_true, args=[1,2]):
440 for ns in myrange(number_slices):
441 test04_node_slice_ns (ns,is_local, add_if_true, args)
443 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
444 test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
446 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
450 slice_id = get_local_slice_id (i,slice_name (i,ns))
453 hostnames=[node_name(i,nn) for nn in nnl]
456 hostnames=[node_name(peer,nn) for nn in nnl]
459 s[i].AddSliceToNodes (a[i], slice_id,hostnames)
462 s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
464 print '%02d: %s in slice %d %s '%(i,message,slice_id,nodetype),
467 def test04_slice_add_lnode (args=[1,2]):
468 test04_node_slice (True,True,args)
470 def test04_slice_add_fnode (args=[1,2]):
471 test04_node_slice (False,True,args)
473 def test04_slice_del_lnode (args=[1,2]):
474 test04_node_slice (True,False,args)
476 def test04_slice_del_fnode (args=[1,2]):
477 test04_node_slice (False,False,args)
480 def test_all_init ():
484 test00_admin_person ()
485 test00_admin_enable ()
486 test01_peer_person ()
488 test01_peer_passwd ()
492 def test_all_nodes ():
494 message ("RESETTING NODES")
498 # create one node on each site
499 message ("CREATING NODES")
501 check_nodes (number_nodes,0,)
503 check_nodes (number_nodes,number_nodes,)
506 message ("DELETING NODES")
508 check_nodes (number_nodes,number_nodes,[1])
509 check_nodes (0,number_nodes,[2])
511 check_nodes (number_nodes,0,[1])
512 check_nodes (0,number_nodes,[2])
515 message ("RECREATING NODES")
517 check_nodes (number_nodes,0,[1])
518 check_nodes (number_nodes,number_nodes,[2])
520 check_nodes (number_nodes,number_nodes,)
522 # make sure node indexes differ
523 message ("DUMMY DEL/ADD for different indexes")
526 check_nodes (number_nodes,number_nodes,)
528 check_nodes (number_nodes,number_nodes,)
534 test04_slice_add_lnode([1])
535 test04_slice_add_fnode([1])
538 def test_all_addslices ():
541 message ("RESETTING SLICES TEST")
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])
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])
569 # remember that foreign slices only know about LOCAL nodes
570 # so refreshing does not do anything
571 check_local_slice_nodes (number_nodes,[1])
572 check_foreign_slice_nodes (0,[2])
574 # now we add foreign nodes into local slice
575 message ("ADDING FOREIGN NODES IN SLICES")
576 test04_slice_add_fnode ([1])
577 check_local_slice_nodes (2*number_nodes,[1])
578 check_foreign_slice_nodes (0,[2])
582 # remember that foreign slices only know about LOCAL nodes
583 # so this does not do anything
584 check_local_slice_nodes (2*number_nodes,[1])
585 check_foreign_slice_nodes (number_nodes,[2])
587 check_slivers_1(total_slivers())
589 def test_all_delslices ():
591 message ("DELETING FOREIGN NODES FROM SLICES")
592 test04_slice_del_fnode([1])
593 check_local_slice_nodes (number_nodes,[1])
594 check_foreign_slice_nodes (number_nodes,[2])
596 check_slivers_1(total_slivers(),[1])
599 check_local_slice_nodes (number_nodes,[1])
600 check_foreign_slice_nodes (0,[2])
602 message ("DELETING LOCAL NODES FROM SLICES")
603 test04_slice_del_lnode([1])
604 check_local_slice_nodes (0,[1])
605 check_foreign_slice_nodes (0,[2])
607 check_local_slice_nodes (0,[1])
608 check_foreign_slice_nodes (0,[2])
610 message ("CHECKING SLICES CLEAN UP")
611 clean_all_slices([1])
612 check_slices (system_slices(),0,[1])
613 check_slices (system_slices(),number_slices,[2])
615 check_slices(system_slices(),0)
617 def test_all_slices ():
618 test_all_addslices ()
619 test_all_delslices ()
626 if __name__ == '__main__':