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
49 number_magic=len(magic_slices)
50 number_all_slices=number_slices+number_magic
52 def set_env (nodes,slices):
53 global number_nodes, number_slices, magic_slices, number_magic, number_all_slices
57 number_magic=len(magic_slices)
58 number_all_slices=number_slices+number_magic
61 plc1={ 'plcname':'plc1 in federation',
62 'hostname':'lurch.cs.princeton.edu',
63 'url-format':'https://%s:443/PLCAPI/',
64 'builtin_admin_id':'root@localhost.localdomain',
65 'builtin_admin_password':'root',
66 'peer_admin_name':'plc1@planet-lab.org',
67 'peer_admin_password':'peer',
68 'node-format':'n1%02d.plc1.org',
70 'slice-format' : 's1%02d',
72 plc2={ 'plcname':'plc2 in federation',
73 'hostname':'planetlab-devbox.inria.fr',
74 'url-format':'https://%s:443/PLCAPI/',
75 'builtin_admin_id':'root@localhost.localdomain',
76 'builtin_admin_password':'root',
77 'peer_admin_name':'plc2@planet-lab.org',
78 'peer_admin_password':'peer',
79 'node-format':'n2%02d.plc2.org',
81 'slice-format' : 's2%02d',
89 return plc[i]['plcname']
95 return plc[i]['node-format']%n
98 return plc[i]['plainname']+'_'+plc[i]['slice-format']%n
100 # to have indexes start at 1
102 return range (1,n+1,1)
105 print "XXXXXXXXXXXXXXXXXXXX",
109 def test00_init (args=[1,2]):
111 ## have you loaded this file already (support for reload)
117 url=plc[i]['url-format']%plc[i]['hostname']
119 s[i]=xmlrpclib.Server(url)
120 print 'initializing s[%d]'%i,url
121 aa[i]={'Username':plc[i]['builtin_admin_id'],
122 'AuthMethod':'password',
123 'AuthString':plc[i]['builtin_admin_password'],
125 print 'initialized aa[%d]'%i, aa[i]
126 a[i]={'Username':plc[i]['peer_admin_name'],
127 'AuthMethod':'password',
128 'AuthString':plc[i]['peer_admin_password'],
130 print 'initialized a[%d]'%i, a[i]
132 def test00_print (args=[1,2]):
136 print 'aa[%d]'%i, aa[i]
137 print 'a[%d]'%i, a[i]
139 def check_nodes (en,ef,args=[1,2]):
142 n=len(s[i].GetNodes(a[i]))
143 f=len(s[i].GetForeignNodes(a[i]))
144 print '%02d: Checking nodes: got %d local nodes & %d foreign nodes'%(i,n,f)
148 # expected : local slices, foreign slices
149 def check_slices (els,efs,args=[1,2]):
152 ls=len(s[i].GetSlices(a[i]))
153 fs=len(s[i].GetForeignSlices(a[i]))
154 print '%02d: Checking slices: got %d local slices & %d foreign slices'%(i,ls,fs)
158 def show_nodes (i,node_ids):
159 for message,nodes in [ ['LOC',s[i].GetNodes(a[i],node_ids)],
160 ['FOR',s[i].GetForeignNodes(a[i],node_ids)] ]:
162 print '[%s:%d] : '%(message,len(nodes)),
164 print node['hostname']+' ',
167 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
168 for ns in myrange(number_slices):
169 check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
171 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
176 sname=slice_name(i,ns)
177 slice=s[i].GetSlices(a[i],[sname])[0]
180 sname=slice_name(peer,ns)
181 slice=s[i].GetForeignSlices(a[i],[sname])[0]
183 print '%02d: %s slice (%s) '%(i,message,sname),
184 slice_node_ids=slice['node_ids']
185 print 'on nodes ',slice_node_ids
186 assert len(slice_node_ids)==expected_nodes
187 show_nodes (i,slice_node_ids)
189 # expected : nodes on local slice
190 def check_local_slice_nodes (expected, args=[1,2]):
191 check_slice_nodes(expected,True,args)
193 # expected : nodes on foreign slice
194 def check_foreign_slice_nodes (expected, args=[1,2]):
195 check_slice_nodes(expected,False,args)
197 def check_conf_files (args=[1,2]):
198 for nn in myrange(number_nodes):
199 check_conf_files_n (nn,args)
201 def check_conf_files_n (nn,args=[1,2]):
204 nodename=node_name(i,nn)
205 ndict= s[i].GetSlivers(a[i],[nodename])[0]
206 assert ndict['hostname'] == nodename
207 conf_files = ndict['conf_files']
208 print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
209 for conf_file in conf_files:
210 print 'source=',conf_file['source'],'|',
211 print 'dest=',conf_file['dest'],'|',
212 print 'enabled=',conf_file['enabled'],'|',
216 pp = pprint.PrettyPrinter(indent=3)
218 def check_slivers (esn,args=[1,2]):
219 for nn in myrange(number_nodes):
220 check_slivers_n (nn,esn,args)
222 # too verbose to check all nodes, let's check only the first one
223 def check_slivers_1 (esn,args=[1,2]):
224 check_slivers_n (1,esn,args)
226 def check_slivers_n (nn,esn,args=[1,2]):
229 nodename=node_name(i,i)
230 ndict= s[i].GetSlivers(a[i],[nodename])[0]
231 assert ndict['hostname'] == nodename
232 slivers = ndict['slivers']
233 assert len(slivers) == esn
234 print '%02d: %d slivers in GetSlivers for node %s'%(i,len(slivers),nodename)
235 for sliver in slivers:
236 print '>>slivername = ',sliver['name']
241 def test00_admin_person (args=[1,2]):
244 email = plc[i]['peer_admin_name']
246 p=s[i].GetPersons(a[i],[email])[0]
247 plc[i]['peer_admin_id']=p['person_id']
249 person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 'last_name':'PeerPoint', 'role_ids':[10],
250 'email':email,'password':plc[i]['peer_admin_password']})
251 print '%02d: created peer admin account %d, %s - %s'%(i,person_id,plc[i]['peer_admin_name'],plc[i]['peer_admin_password'])
252 plc[i]['peer_admin_id']=person_id
254 def test00_admin_enable (args=[1,2]):
257 s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
258 s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
259 print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
262 def test01_site (args=[1,2]):
266 ### create a site (required for creating a slice)
267 sitename=site_name(i)
268 abbrev_name="abbr"+str(i)
269 login_base=plc[i]['plainname']
270 ### should be enough - needs to check we can add/del slices
271 max_slices = number_slices
273 s[i].GetSites(a[i],{'login_base':login_base})[0]
275 site_id=s[i].AddSite (a[i], {'name':plc_name(i),
276 'abbreviated_name': abbrev_name,
277 'login_base': login_base,
279 'url': 'http://%s.com/'%abbrev_name,
280 'max_slices':max_slices})
281 ### max_slices does not seem taken into account at that stage
282 s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
283 print '%02d: Created site %d with max_slices=%d'%(i,site_id,max_slices)
284 plc[i]['site_id']=site_id
286 def test01_peer_person (args=[1,2]):
290 email=plc[peer]['peer_admin_name']
292 p=s[i].GetPersons(a[i],[email])[0]
293 plc[i]['peer_person_id']=p['person_id']
295 person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
296 'email':email,'password':plc[peer]['peer_admin_password']})
297 print '%02d:Created person %d as the peer person'%(i,person_id)
298 plc[i]['peer_person_id']=person_id
300 def test01_peer (args=[1,2]):
304 peername = plc_name(peer)
306 p=s[i].GetPeers (a[i], [peername])[0]
307 plc[i]['peer_id']=p['peer_id']
309 peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
310 # NOTE : need to manually reset the encrypted password through SQL at this point
311 print '%02d:Created peer %d'%(i,peer_id)
312 plc[i]['peer_id']=peer_id
313 print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
315 def test01_peer_passwd (args=[1,2]):
318 # using an ad-hoc local command for now - never could get quotes to reach sql....
319 print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
320 retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
321 print '-> system returns',retcod
323 ##############################
324 # this one gets cached
327 return plc[i]['peer_id']
329 peername = plc_name (peer_index(i))
330 peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
331 plc[i]['peer_id'] = peer_id
334 def test01_refresh (args=[1,2]):
337 print '%02d: Refreshing peer'%(i),
338 retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
342 def get_node_id(i,nodename):
343 return s[i].GetNodes(a[i],[nodename])[0]['node_id']
345 def clean_all_nodes (args=[1,2]):
348 print '%02d: Cleaning all nodes'%i
349 for node in s[i].GetNodes(a[i]):
350 print '%02d: > Cleaning node %d'%(i,node['node_id'])
351 s[i].DeleteNode(a[i],node['node_id'])
353 def test02_node (args=[1,2]):
354 for nn in myrange(number_nodes):
355 test02_node_n (nn,args)
357 def test02_node_n (nn,args=[1,2]):
360 nodename = node_name(i,nn)
362 get_node_id(i,nodename)
364 n=s[i].AddNode(a[i],1,{'hostname': nodename})
365 print '%02d: Added node %d %s'%(i,n,node_name(i,i))
367 def test02_delnode (args=[1,2]):
368 for nn in myrange(number_nodes):
369 test02_delnode_n (nn,args)
371 def test02_delnode_n (nn,args=[1,2]):
374 nodename = node_name(i,nn)
375 node_id = get_node_id (i,nodename)
376 retcod=s[i].DeleteNode(a[i],nodename)
377 print '%02d: Deleted node %d, returns %s'%(i,node_id,retcod)
380 def clean_all_slices (args=[1,2]):
383 print '%02d: Cleaning all slices'%i
384 for slice in s[i].GetSlices(a[i]):
385 slice_id = slice['slice_id']
386 if slice_id not in magic_slices:
387 print '%02d: > Cleaning slice %d'%(i,slice_id)
388 s[i].DeleteSlice(a[i],slice_id)
390 def get_slice_id (i,name):
391 return s[i].GetSlices(a[i],[name])[0]['slice_id']
393 def test03_slice (args=[1,2]):
394 for n in myrange(number_slices):
395 test03_slice_n (n,args)
397 def test03_slice_n (ns,args=[1,2]):
402 slicename=slice_name(i,ns)
403 max_nodes=number_nodes
405 s[i].GetSlices(a[i],[slicename])[0]
407 slice_id=s[i].AddSlice (a[i],{'name':slicename,
408 'description':'slice %s on %s'%(slicename,plcname),
409 'url':'http://planet-lab.org/%s'%slicename,
410 'max_nodes':max_nodes,
411 'instanciation':'plc-instantiated',
413 print '%02d: created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
416 def test04_node_slice (is_local, add_if_true, args=[1,2]):
417 for ns in myrange(number_slices):
418 test04_node_slice_ns (ns,is_local, add_if_true, args)
420 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
421 test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
423 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
427 slice_id = get_slice_id (i,slice_name (i,ns))
430 hostnames=[node_name(i,nn) for nn in nnl]
433 hostnames=[node_name(peer,nn) for nn in nnl]
436 s[i].AddSliceToNodes (a[i], slice_id,hostnames)
439 s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
441 print '%02d: %s in slice %d %s '%(i,message,slice_id,nodetype),
444 def test04_slice_add_lnode (args=[1,2]):
445 test04_node_slice (True,True,args)
447 def test04_slice_add_fnode (args=[1,2]):
448 test04_node_slice (False,True,args)
450 def test04_slice_del_lnode (args=[1,2]):
451 test04_node_slice (True,False,args)
453 def test04_slice_del_fnode (args=[1,2]):
454 test04_node_slice (False,False,args)
457 def test_all_init ():
461 test00_admin_person ()
462 test00_admin_enable ()
463 test01_peer_person ()
465 test01_peer_passwd ()
469 def test_all_nodes ():
471 message ("RESETTING NODES")
475 # create one node on each site
476 message ("CREATING NODES")
478 check_nodes (number_nodes,0,)
480 check_nodes (number_nodes,number_nodes,)
483 message ("DELETING NODES")
485 check_nodes (number_nodes,number_nodes,[1])
486 check_nodes (0,number_nodes,[2])
488 check_nodes (number_nodes,0,[1])
489 check_nodes (0,number_nodes,[2])
492 message ("RECREATING NODES")
494 check_nodes (number_nodes,0,[1])
495 check_nodes (number_nodes,number_nodes,[2])
497 check_nodes (number_nodes,number_nodes,)
499 # make sure node indexes differ
500 message ("DUMMY DEL/ADD for different indexes")
503 check_nodes (number_nodes,number_nodes,)
505 check_nodes (number_nodes,number_nodes,)
507 def test_all_addslices ():
510 message ("RESETTING SLICES TEST")
516 # create slices on plc1
517 message ("CREATING SLICES on plc1")
519 # each site has 3 local slices and 0 foreign slice
520 check_slices (number_all_slices,0,[1])
521 check_slices (number_magic,0,[2])
523 check_slices (number_all_slices,0,[1])
524 check_slices (number_magic,number_slices,[2])
525 # no slice has any node yet
526 check_local_slice_nodes(0,[1])
527 check_foreign_slice_nodes(0,[2])
529 # insert local nodes in local slice on plc1
530 message ("ADDING LOCAL NODES IN SLICES")
531 test04_slice_add_lnode ([1])
532 # of course the change is only local
533 check_local_slice_nodes (number_nodes,[1])
534 check_foreign_slice_nodes(0,[2])
538 # remember that foreign slices only know about LOCAL nodes
539 # so refreshing does not do anything
540 check_local_slice_nodes (number_nodes,[1])
541 check_foreign_slice_nodes (0,[2])
543 # now we add foreign nodes into local slice
544 message ("ADDING FOREIGN NODES IN SLICES")
545 test04_slice_add_fnode ([1])
546 check_local_slice_nodes (2*number_nodes,[1])
547 check_foreign_slice_nodes (0,[2])
551 # remember that foreign slices only know about LOCAL nodes
552 # so this does not do anything
553 check_local_slice_nodes (2*number_nodes,[1])
554 check_foreign_slice_nodes (number_nodes,[2])
556 check_slivers_1(number_all_slices)
558 def test_all_delslices ():
560 message ("DELETING FOREIGN NODES FROM SLICES")
561 test04_slice_del_fnode([1])
562 check_local_slice_nodes (number_nodes,[1])
563 check_foreign_slice_nodes (number_nodes,[2])
565 check_slivers_1(number_all_slices,[1])
568 check_local_slice_nodes (number_nodes,[1])
569 check_foreign_slice_nodes (0,[2])
571 message ("DELETING LOCAL NODES FROM SLICES")
572 test04_slice_del_lnode([1])
573 check_local_slice_nodes (0,[1])
574 check_foreign_slice_nodes (0,[2])
576 check_local_slice_nodes (0,[1])
577 check_foreign_slice_nodes (0,[2])
579 message ("CHECKING SLICES CLEAN UP")
580 clean_all_slices([1])
581 check_slices (number_magic,0,[1])
582 check_slices (number_magic,number_slices,[2])
584 check_slices(number_magic,0)
586 def test_all_slices ():
587 test_all_addslices ()
588 test_all_delslices ()
595 if __name__ == '__main__':