+# clean all local nodes - foreign nodes are not supposed to be cleaned up manually
+def clean_all_nodes (args=[1,2]):
+ for i in args:
+ print '%02d:== Cleaning all nodes'%i
+ local_nodes = s[i].GetNodes({'peer_id':None})
+ if local_nodes:
+ for node in local_nodes:
+ print '%02d:==== Cleaning node %d'%(i,node['node_id'])
+ s[i].DeleteNode(node['node_id'])
+
+def test03_node (args=[1,2]):
+ for nn in myrange(number_nodes):
+ test03_node_n (nn,args)
+
+def test03_node_n (nn,args=[1,2]):
+ for i in args:
+ nodename = node_name(i,nn)
+ try:
+ get_local_node_id(i,nodename)
+ except:
+ login_base=site_login_base(i,map_on_site(nn))
+ n=s[i].AddNode(login_base,{'hostname': nodename})
+ if n:
+ print '%02d:== Added node %d %s'%(i,n,node_name(i,nn))
+
+def test02_delnode (args=[1,2]):
+ for nn in myrange(number_nodes):
+ test02_delnode_n (nn,args)
+
+def test02_delnode_n (nn,args=[1,2]):
+ for i in args:
+ nodename = node_name(i,nn)
+ node_id = get_local_node_id (i,nodename)
+ retcod=s[i].DeleteNode(nodename)
+ if retcod:
+ print '%02d:== Deleted node %d, returns %s'%(i,node_id,retcod)
+
+####################
+def clean_all_slices (args=[1,2]):
+ for i in args:
+ print '%02d:== Cleaning all slices'%i
+ for slice in s[i].GetSlices({'peer_id':None}):
+ slice_id = slice['slice_id']
+ if slice_id not in system_slices_ids:
+ if s[i].DeleteSlice(slice_id):
+ print '%02d:==== Cleaned slice %d'%(i,slice_id)
+
+def test04_slice (args=[1,2]):
+ for n in myrange(number_slices):
+ test04_slice_n (n,args)
+
+def test04_slice_n (ns,args=[1,2]):
+ for i in args:
+ peer=peer_index(i)
+ plcname=plc_name(i)
+ slicename=slice_name(i,ns)
+ max_nodes=number_nodes
+ try:
+ s[i].GetSlices([slicename])[0]
+ except:
+ slice_id=s[i].AddSlice ({'name':slicename,
+ 'description':'slice %s on %s'%(slicename,plcname),
+ 'url':'http://planet-lab.org/%s'%slicename,
+ 'max_nodes':max_nodes,
+ 'instanciation':'plc-instantiated',
+ })
+ if slice_id:
+ print '%02d:== created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
+ actual_persons_per_slice = min (number_persons,number_persons_per_slice)
+ person_indexes=[map_on_person (p+ns) for p in range(actual_persons_per_slice)]
+ for np in person_indexes:
+ email = person_name (i,np)
+ retcod = s[i].AddPersonToSlice (email, slicename)
+ print '%02d:== Attached person %s to slice %s'%(i,email,slicename)
+
+
+def test04_node_slice (is_local, add_if_true, args=[1,2]):
+ for ns in myrange(number_slices):
+ test04_node_slice_ns (ns,is_local, add_if_true, args)
+
+def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
+ actual_nodes_per_slice = min (number_nodes,number_nodes_per_slice)
+ node_indexes = [ map_on_node (n+ns) for n in range(actual_nodes_per_slice)]
+ test04_node_slice_nl_n (node_indexes,ns,is_local, add_if_true, args)
+
+def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
+ for i in args:
+ peer=peer_index(i)
+ sname = slice_name (i,ns)
+
+ if is_local:
+ hostnames=[node_name(i,nn) for nn in nnl]
+ nodetype='local'
+ else:
+ hostnames=[node_name(peer,nn) for nn in nnl]
+ nodetype='foreign'
+ if add_if_true:
+ res=s[i].AddSliceToNodes (sname,hostnames)
+ message="added"
+ else:
+ res=s[i].DeleteSliceFromNodes (sname,hostnames)
+ message="deleted"
+ if res:
+ print '%02d:== %s in slice %s %s '%(i,message,sname,nodetype),
+ print hostnames
+
+def test04_slice_add_lnode (args=[1,2]):
+ test04_node_slice (True,True,args)
+
+def test04_slice_add_fnode (args=[1,2]):
+ test04_node_slice (False,True,args)
+
+def test04_slice_del_lnode (args=[1,2]):
+ test04_node_slice (True,False,args)
+
+def test04_slice_del_fnode (args=[1,2]):
+ test04_node_slice (False,False,args)
+
+####################
+def test05_sat (args=[1,2]):
+ for i in args:
+ name = sat_name(i)
+ try:
+ sat_id=s[i].GetSliceAttributeTypes ([name])[0]
+ except:
+ description="custom sat on plc%d"%i
+ min_role_id=10
+ sat_id=s[i].AddSliceAttributeType ({ 'name':name,
+ 'description': description,
+ 'min_role_id' : min_role_id})
+ if sat_id:
+ print '%02d:== created SliceAttributeType = %d'%(i,sat_id)
+
+# for test, we create 4 slice_attributes
+# on slice1 - sat=custom_made (see above) - all nodes
+# on slice1 - sat=custom_made (see above) - node=n1
+# on slice1 - sat='net_max' - all nodes
+# on slice1 - sat='net_max' - node=n1
+
+def test05_sa_atom (slice_name,sat_name,value,node,i):
+ sa_id=s[i].GetSliceAttributes({'name':sat_name,
+ 'value':value})
+ if not sa_id:
+ if node:
+ sa_id=s[i].AddSliceAttribute(slice_name,
+ sat_name,
+ value,
+ node)
+ else:
+ print 'slice_name',slice_name,'sat_name',sat_name
+ sa_id=s[i].AddSliceAttribute(slice_name,
+ sat_name,
+ value)
+ if sa_id:
+ print '%02d:== created SliceAttribute = %d'%(i,sa_id),
+ print 'On slice',slice_name,'and node',node
+
+def test05_sa (args=[1,2]):
+ for i in args:
+ test05_sa_atom (slice_name(i,1),sat_name(i),'custom sat/all nodes',None,i)
+ test05_sa_atom (slice_name(i,1),sat_name(i),'custom sat/node1',node_name(i,1),i)
+ test05_sa_atom (slice_name(i,1),'net_max','predefined sat/all nodes',None,i)
+ test05_sa_atom (slice_name(i,1),'net_max','predefined sat/node1',node_name(i,1),i)
+
+##############################
+# readable dumps
+##############################
+def p_site (s):
+ print s['site_id'],s['peer_id'],s['login_base'],s['name'],s['node_ids']
+
+def p_key (k):
+ print k['key_id'],k['peer_id'],k['key']
+
+def p_person (p):
+ print p['person_id'],p['peer_id'],p['email'],'keys:',p['key_ids'],'sites:',p['site_ids']
+
+def p_node(n):
+ print n['node_id'],n['peer_id'],n['hostname'],'sls=',n['slice_ids'],'site=',n['site_id']
+
+def p_slice(s):
+ print s['slice_id'],s['peer_id'],s['name'],'nodes=',s['node_ids'],'persons=',s['person_ids']
+ print '---','sas=',s['slice_attribute_ids'],s['name'],'crp=',s['creator_person_id']
+ print "--- 'expires':",s['expires']
+
+def p_sat(sat):
+ print sat['attribute_type_id'], sat['name'], sat['min_role_id'], sat['description']
+
+def p_sa (sa):
+ print sa['slice_attribute_id'],sa['name'],'AT_id:',sa['attribute_type_id']
+ print '---','v=',sa['value'],'sl=',sa['slice_id'],'n=',sa['node_id']
+
+import pprint
+pretty_printer=pprint.PrettyPrinter(5)
+
+def p_sliver (margin,x):
+ print margin,'SLIVERS for : hostname',x['hostname']
+ print margin,'%d config files'%len(x['conf_files'])
+ for sv in x['slivers']:
+ p_sliver_slice(margin,sv,x['hostname'])
+
+def p_sliver_slice(margin,sliver,hostname):
+ print margin,'SLIVER on hostname %s, s='%hostname,sliver['name']
+ print margin,'KEYS',
+ pretty_printer.pprint(sliver['keys'])
+ print margin,'ATTRIBUTES',
+ pretty_printer.pprint(sliver['attributes'])
+
+def dump (args=[1,2]):
+ for i in args:
+ print '%02d:============================== DUMPING'%i
+ print '%02d: SITES'%i
+ [p_site(x) for x in s[i].GetSites()]
+ print '%02d: KEYS'%i
+ [p_key(x) for x in s[i].GetKeys()]
+ print '%02d: PERSONS'%i
+ [p_person(x) for x in s[i].GetPersons()]
+ print '%02d: NODES'%i
+ [p_node(x) for x in s[i].GetNodes()]
+ print '%02d: SLICES'%i
+ [p_slice(x) for x in s[i].GetSlices()]
+ print '%02d: Slice Attribute Types'%i
+ [p_sat(x) for x in s[i].GetSliceAttributeTypes()]
+ print '%02d: Slice Attributes'%i
+ [p_sa(x) for x in s[i].GetSliceAttributes()]
+ timer_show()
+ snodes=min(3,number_nodes)
+ print '%02d: SLIVERS for first %d nodes'%(i,snodes)
+ print 'WARNING - GetSlivers needs fix'
+# for id in myrange(snodes):
+# p_sliver('%02d:'%i,s[i].GetSlivers(id))
+
+ print '%02d:============================== END DUMP'%i
+
+
+## for usage under the api
+def pt ():
+ for x in GetSites():
+ p_site(x)
+
+def pk ():
+ for x in GetKeys():
+ print (x['key_id'],x['peer_id'],x['key'])
+
+def pp ():
+ for x in GetPersons():
+ p_person(x)
+
+def pn ():
+ for x in GetNodes():
+ p_node(x)
+
+def ps ():
+ for x in GetSlices():
+ p_slice(x)
+
+def psat():
+ for x in GetSliceAttributeTypes():
+ p_sat(x)
+
+def psa():
+ for x in GetSliceAttributes():
+ p_sa(x)
+
+def pv ():
+ for s in GetSlivers():
+ p_sliver('',s)
+
+def all():
+ print 'SITES'
+ pt()
+ print 'KEYS'
+ pk()
+ print 'PERSONS'
+ pp()
+ print 'NODES'
+ pn()
+ print 'SLICES'
+ ps()
+ print 'SLICE ATTR TYPES'
+ psat()
+ print 'SLICE ATTRS'
+ psa()
+ print 'SLIVERS'
+ pv()
+
+
+####################
+def test_all_init ():
+ message ("INIT")
+ test00_init (builtin_person=True)
+ test00_print ()
+ test00_admin_person ()
+ test00_admin_enable ()
+ test00_init (builtin_person=False)
+# required before we can add peers
+# use make -f peers-test.mk peers instead
+# test00_push_public_peer_material()
+ test00_peer ()
+
+def test_all_sites ():
+ test01_site ()
+ test00_refresh ('after site creation')
+
+def test_all_persons ():
+ test02_del_person()
+ test00_refresh ('before persons&keys creation')
+ check_keys(0,0)
+ check_persons(system_persons,system_persons_cross)
+ message ("Creating persons&keys")
+ test02_person ()
+ if not fast_flag:
+ message ("1 extra del/add cycle for unique indexes")
+ test02_del_person([2])
+ test02_person([2])
+ check_keys(number_persons*number_keys_per_person,0)
+ check_persons(system_persons+number_persons,system_persons_cross)
+ test00_refresh ('after persons&keys creation')
+ check_keys(number_persons*number_keys_per_person,number_persons*number_keys_per_person)
+ check_persons(system_persons+number_persons,system_persons_cross+number_persons)
+
+def test_all_nodes ():
+
+ message ("RESETTING NODES")
+ clean_all_nodes ()
+ test00_refresh ('cleaned nodes')
+ check_nodes(0,0)
+
+ # create one node on each site
+ message ("CREATING NODES")
+ test03_node ()
+ check_nodes(number_nodes,0)
+ test00_refresh ('after node creation')
+ check_nodes(number_nodes,number_nodes)
+ test02_delnode([2])
+ if not fast_flag:
+ message ("2 extra del/add cycles on plc2 for different indexes")
+ test03_node ([2])
+ test02_delnode([2])
+ test03_node ([2])
+ test02_delnode([2])
+ check_nodes(0,number_nodes,[2])
+ test00_refresh('after deletion on plc2')
+ check_nodes(number_nodes,0,[1])
+ check_nodes(0,number_nodes,[2])
+ message ("ADD on plc2 for different indexes")
+ test03_node ([2])
+ check_nodes (number_nodes,0,[1])
+ check_nodes (number_nodes,number_nodes,[2])
+ test00_refresh('after re-creation on plc2')
+ check_nodes (number_nodes,number_nodes,)
+
+def test_all_addslices ():
+
+ # reset
+ message ("RESETTING SLICES TEST")
+ clean_all_nodes ()
+ test03_node ()
+ clean_all_slices ()
+ test00_refresh ("After slices init")
+
+ # create slices on plc1
+ message ("CREATING SLICES on plc1")
+ test04_slice ([1])
+
+ check_slices (total_slices(),system_slices(),[1])
+ check_slices (system_slices(),system_slices(),[2])
+ test00_refresh ("after slice created on plc1")
+ check_slices (total_slices(),system_slices(),[1])
+ check_slices (system_slices(),total_slices(),[2])
+ # no slice has any node yet
+ check_local_slice_nodes(0,[1])
+ check_foreign_slice_nodes(0,[2])
+
+ # insert local nodes in local slice on plc1
+ message ("ADDING LOCAL NODES IN SLICES")
+ test04_slice_add_lnode ([1])
+ # of course the change is only local
+ check_local_slice_nodes (number_nodes_per_slice,[1])
+ check_foreign_slice_nodes(0,[2])
+
+ # refreshing
+ test00_refresh ("After local nodes were added on plc1")
+ check_local_slice_nodes (number_nodes_per_slice,[1])
+ check_foreign_slice_nodes (number_nodes_per_slice,[2])
+
+ # now we add foreign nodes into local slice
+ message ("ADDING FOREIGN NODES IN SLICES")
+ test04_slice_add_fnode ([1])
+ check_local_slice_nodes (2*number_nodes_per_slice,[1])
+ check_foreign_slice_nodes (number_nodes_per_slice,[2])
+
+ # refreshing
+ test00_refresh ("After foreign nodes were added in plc1")
+ # remember that foreign slices only know about LOCAL nodes
+ # so this does not do anything
+ check_local_slice_nodes (2*number_nodes_per_slice,[1])
+ check_foreign_slice_nodes (2*number_nodes_per_slice,[2])
+
+ check_slivers_1(total_slivers())
+
+def test_all_delslices ():
+
+ message ("DELETING FOREIGN NODES FROM SLICES")
+ test04_slice_del_fnode([1])
+ check_local_slice_nodes (number_nodes_per_slice,[1])
+ check_foreign_slice_nodes (2*number_nodes_per_slice,[2])
+ # mmh?
+ check_slivers_1(total_slivers(),[1])
+
+ test00_refresh ("After foreign nodes were removed on plc1")
+ check_local_slice_nodes (number_nodes_per_slice,[1])
+ check_foreign_slice_nodes (number_nodes_per_slice,[2])
+
+ message ("DELETING LOCAL NODES FROM SLICES")
+ test04_slice_del_lnode([1])
+ check_local_slice_nodes (0,[1])
+ check_foreign_slice_nodes (number_nodes_per_slice,[2])
+
+ test00_refresh ("After local nodes were removed on plc1")
+ check_local_slice_nodes (0,[1])
+ check_foreign_slice_nodes (0,[2])
+
+ message ("CHECKING SLICES CLEAN UP")
+ clean_all_slices([1])
+ check_slices (system_slices(),system_slices(),[1])
+ check_slices (system_slices(),total_slices(),[2])
+ test00_refresh ("After slices clenaup")
+ check_slices(system_slices(),system_slices())
+
+def test_all_slices ():
+ test_all_addslices ()
+ test_all_delslices ()
+
+def test_all_sats ():
+ test05_sat ()
+ test00_refresh("after SliceAttributeType creation")
+
+def test_all ():
+ test_all_init ()
+ timer_show()
+ test_all_sites ()
+ timer_show()
+ test_all_persons ()
+ timer_show()
+ test_all_nodes ()
+ timer_show()
+ test_all_slices ()
+ timer_show()
+ test_all_sats ()
+ timer_show()
+ dump()
+ timer_show()
+ message("END")
+
+### ad hoc test sequences
+# we just create objects here so we can dump the DB
+def populate ():
+ timer_start()
+ test_all_init()
+ timer_show()
+ test01_site()
+ timer_show()
+ test02_person()
+ timer_show()
+ test03_node()
+ timer_show()
+ test04_slice([1])
+ timer_show()
+ test04_slice_add_lnode([1])
+ timer_show()
+ test05_sat()
+ timer_show()
+ test05_sa([1])
+ timer_show()
+ message("END")
+
+def populate_end():
+ test00_init(builtin_person=False)
+ test00_refresh ("Peer 1 for publishing foreign nodes from 2",[1])
+ timer_show()
+ test04_slice_add_fnode([1])
+ timer_show()
+ test00_refresh("populate: refresh all")
+ timer_show()
+ test00_refresh("empty refresh")
+ dump()
+ timer_show()
+ message("END")
+
+# temporary - scratch as needed
+def test_now ():
+ test_all_init()
+
+# populate()
+# test00_refresh('peer 1 gets plc2 nodes',[1])
+# test04_slice_add_fnode([1])
+# test00_refresh('final',[1])
+#
+# test_all_sites ()
+# clean_all_nodes()
+# clean_all_slices()
+# populate()
+
+#####
+def usage ():
+ print "Usage: %s [-n] [-f]"%sys.argv[0]
+ print " -n runs test_now instead of test_all"
+ print " -p runs populate instead of test_all"
+ print " -e runs populate_end of test_all"
+ print " -m run in mini mode (1 instance of each class)"
+ print " -b performs big run"
+ print " -H performs huge run"
+ print " -f n : increases normal sizes by <n>"
+ print " -l n : tester runs locally for peer <n>, rather than through xmlrpc"
+
+ sys.exit(1)
+
+def main ():
+ try:
+ (o,a) = getopt.getopt(sys.argv[1:], "emnpbHf:l:")
+ except:
+ usage()
+ func = test_all
+ for (opt,val) in o:
+ if opt=='-n':
+ print 'Running test_now'
+ func = test_now
+ elif opt=='-p':
+ print 'Running populate'
+ func = populate
+ elif opt=='-e':
+ print 'Running populate_end'
+ func = populate_end
+ elif opt=='-m':
+ mini()
+ elif opt=='-b':
+ big()
+ elif opt=='-H':
+ huge()
+ elif opt=='-f':
+ factor=int(val)
+ apply_factor(factor)
+ elif opt=='-l':
+ global local_peer
+ local_peer=int(val)
+ if local_peer not in (1,2):
+ usage()
+ else:
+ usage()
+ if a:
+ usage()
+ show_test()
+ func()
+ timer_show()
+ epilogue
+
+if __name__ == '__main__':
+ normal()
+ main()
+