+# 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(a[i],{'name':sat_name,
+ 'value':value})
+ if not sa_id:
+ if node:
+ sa_id=s[i].AddSliceAttribute(a[i],
+ slice_name,
+ sat_name,
+ value,
+ node)
+ else:
+ print 'slice_name',slice_name,'sat_name',sat_name
+ sa_id=s[i].AddSliceAttribute(a[i],
+ slice_name,
+ sat_name,
+ value)
+ 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'])
+
+def p_sat(sat):
+ print (sat['attribute_type_id'],sat['peer_id'], sat['name'], sat['min_role_id'], sat['description'])
+
+def p_sa (sa):
+ print (sa['slice_attribute_id'],sa['peer_id'],sa['name'],'AT_id:',sa['attribute_type_id'])
+ print '---',('v=',sa['value'],'sl=',sa['slice_id'],'n=',sa['node_id'])
+
+def p_sliver (x):
+ print ('SLIVERS for : hostname',x['hostname'])
+ print ('%d config files'%len(x['conf_files']))
+ for sv in x['slivers']:
+ p_sliver_slice(sv,x['hostname'])
+
+import pprint
+pretty_printer=pprint.PrettyPrinter(5)
+
+def p_sliver_slice(sliver,hostname):
+ print 'SLIVER on hostname %s, s='%hostname,sliver['name']
+ print 'KEYS',
+ pretty_printer.pprint(sliver['keys'])
+ print 'ATTRIBUTES',
+ pretty_printer.pprint(sliver['attributes'])
+
+def dump (args=[1,2]):
+ for i in args:
+ print 'SITES'
+ [p_site(x) for x in s[i].GetSites(a[i])]
+ print 'KEYS'
+ [p_key(x) for x in s[i].GetKeys(a[i])]
+ print 'PERSONS'
+ [p_person(x) for x in s[i].GetPersons(a[i])]
+ print 'NODES'
+ [p_node(x) for x in s[i].GetNodes(a[i])]
+ print 'SLICES'
+ [p_slice(x) for x in s[i].GetSlices(a[i])]
+ print 'Slice Attribute Types'
+ [p_sat(x) for x in s[i].GetSliceAttributeTypes(a[i])]
+ print 'Slice Attributes'
+ [p_sa(x) for x in s[i].GetSliceAttributes(a[i])]
+ print 'SLIVERS'
+ [p_sliver(x) for x in s[i].GetSlivers(a[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 ()
+ test00_print ()
+ test00_admin_person ()
+ test00_admin_enable ()
+ test00_peer_person ()
+ test00_peer ()
+ test00_peer_passwd ()
+
+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,0)
+ check_persons(system_persons+number_persons,system_persons_cross)
+ test00_refresh ('after persons&keys creation')
+ check_keys(number_persons*number_keys,number_persons*number_keys)
+ 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])
+ # each site has 3 local slices and 0 foreign slice
+ check_slices (total_slices(),0,[1])
+ check_slices (system_slices(),0,[2])
+ test00_refresh ("after slice created on plc1")
+ check_slices (total_slices(),0,[1])
+ check_slices (system_slices(),number_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,[1])
+ check_foreign_slice_nodes(0,[2])
+
+ # refreshing
+ test00_refresh ("After local nodes were added on plc1")
+ check_local_slice_nodes (number_nodes,[1])
+ check_foreign_slice_nodes (number_nodes,[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,[1])
+ check_foreign_slice_nodes (number_nodes,[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,[1])
+ check_foreign_slice_nodes (2*number_nodes,[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,[1])
+ check_foreign_slice_nodes (2*number_nodes,[2])
+ # mmh?
+ check_slivers_1(total_slivers(),[1])
+
+ test00_refresh ("After foreign nodes were removed on plc1")
+ check_local_slice_nodes (number_nodes,[1])
+ check_foreign_slice_nodes (number_nodes,[2])
+
+ message ("DELETING LOCAL NODES FROM SLICES")
+ test04_slice_del_lnode([1])
+ check_local_slice_nodes (0,[1])
+ check_foreign_slice_nodes (number_nodes,[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(),0,[1])
+ check_slices (system_slices(),number_slices,[2])
+ test00_refresh ("After slices clenaup")
+ check_slices(system_slices(),0)
+
+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
+def populate ():
+ test_all_init()
+ test01_site()
+ test02_person()
+ test03_node()
+ test04_slice([1])
+ test04_slice_add_lnode([1])
+ test05_sat()
+ test05_sa([1])
+ test00_refresh ("populate: refreshing peer 1",[1])
+ test04_slice_add_fnode([1])
+ test00_refresh("populate: refresh all")
+ dump()
+
+def test_now ():
+ test_all_init()
+ test_all_sites ()
+# clean_all_nodes()
+# clean_all_slices()
+# populate()
+
+#####
+def usage ():
+ print "Usage: %s [-n] [-f]"%sys.argv[0]
+ print " -f runs faster (1 node - 1 slice)"
+ print " -b performs big run (4 times as large as normal)"
+ print " -n runs test_now instead of test_all"
+ print " -p runs populate instead of test_all"
+
+ sys.exit(1)
+
+def main ():
+ try:
+ (o,a) = getopt.getopt(sys.argv[1:], "fnpb")
+ except:
+ usage()
+ func = test_all
+ for (opt,val) in o:
+ if opt=='-f':
+ fast()
+ elif opt=='-b':
+ big()
+ elif opt=='-n':
+ print 'Running test_now'
+ func = test_now
+ elif opt=='-p':
+ print 'Running populate'
+ func = populate
+ else:
+ usage()
+ if a:
+ usage()
+ show_test()
+ func()
+ timer_show()