(*) implements validate_ methods for all timestamp objects
[plcapi.git] / TestPeers.py
index 18d2866..a3b2e8b 100755 (executable)
 ###
 ##############################
 
+### xxx todo
+# check sites
+# check persons
+
 # support reloading without wiping everything off
 # dunno how to do (defvar plc)
 
@@ -70,35 +74,57 @@ def total_slivers ():
 
 ####################
 # set initial conditions
-def define_test (sites,keys,persons,nodes,slices,fast_mode):
-    global number_sites, number_keys, number_persons, number_nodes, number_slices, fast_flag
-    number_sites = sites
+def define_test (keys,sites,persons,nodes,slices,fast_mode=None):
+    global number_keys, number_sites, number_persons, number_nodes, number_slices, fast_flag
     number_keys=keys
+    number_sites = sites
     number_persons=persons
     number_nodes=nodes
     number_slices=slices
-    fast_flag=fast_mode
+    if fast_mode is not None:
+        fast_flag=fast_mode
+
+local_index=None
 
 def show_test():
-    print '%d sites, %d keys, %d persons, %d nodes & %d slices'%(number_sites, number_keys,number_persons,
-                                                                number_nodes,number_slices)
+    print '%d keys, %d sites, %d persons, %d nodes & %d slices'%(
+        number_keys, number_sites,number_persons,number_nodes,number_slices),
+    print 'fast_flag',fast_flag
+    if local_index is not None:
+        print 'Running locally on index %d'%local_index
 
-def fast():
+def mini():
     define_test(1,1,1,1,1,True)
     
 def normal():
-    define_test (sites=4,keys=2,persons=4,nodes=5,slices=4,fast_mode=False)
+    define_test (keys=2,sites=4,persons=4,nodes=5,slices=4,fast_mode=False)
 
+# use only 1 key in this case
+big_factor=4
 def big():
-    define_test (sites=16,keys=8,persons=16,nodes=20,slices=16,fast_mode=False)
+    global number_keys, number_sites, number_persons, number_nodes, number_slices
+    normal()
+    (number_sites,number_persons,number_nodes,number_slices) = [
+        big_factor * x for x in (number_sites,number_persons,number_nodes,number_slices)]
+    number_keys=1
+
+huge_factor=50
+def huge():
+    global number_keys, number_sites, number_persons, number_nodes, number_slices
+    normal()
+    (number_sites,number_persons,number_nodes,number_slices) = [
+        huge_factor * x for x in (number_sites,number_persons,number_nodes,number_slices)]
+    number_keys=1
 
-fast()
+# use fast by default in interactive mode
+mini()
 #normal()
 
 ####################
 # argh, for login_name that doesn't accept digits
 plain_numbers=['zero','one','two','three','four','five','six','seven','eight','nine','ten',
               'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty']
+plain_digits=['a','b','c','d','e','f','g','h','i','j']
 ####################
 plc1={ 'plcname':'plc1 in federation',
        'hostname':'lurch.cs.princeton.edu',
@@ -111,7 +137,7 @@ plc1={ 'plcname':'plc1 in federation',
        'plainname' : 'one',
        'site-format':'one%s',
        'person-format' : 'user1-%d@plc1.org',
-       'key-format':'ssh-rsa 1111111111111111 user%d-key%d',
+       'key-format':'ssh-rsa 11key4plc11 user%d-key%d',
        'person-password' : 'password1',
        }
 plc2={ 'plcname':'plc2 in federation',
@@ -125,7 +151,7 @@ plc2={ 'plcname':'plc2 in federation',
        'plainname' : 'two',
        'site-format':'two%s',
        'person-format' : 'user2-%d@plc2.org',
-       'key-format':'ssh-rsa 2222222222222222 user%d-key%d',
+       'key-format':'ssh-rsa 22key4plc22 user%d-key%d',
        'person-password' : 'password2',
        }
 
@@ -141,7 +167,20 @@ def site_name (i,n):
     return 'Site fullname '+x
 
 def site_login_base (i,n):
-    return plc[i]['site-format']%plain_numbers[n]
+    # for huge
+    if number_sites<len(plain_numbers):
+        return plc[i]['site-format']%plain_numbers[n]
+    else:
+        string=''
+        while True:
+            quo=n/10
+            rem=n%10
+            string=plain_digits[rem]+string
+            if quo == 0:
+                break
+            else:
+                n=quo
+        return plc[i]['site-format']%string
 
 def person_name (i,n):
     return plc[i]['person-format']%n
@@ -162,6 +201,9 @@ def slice_name (i,n):
     site_index=map_on_site(n)
     return "%s_slice%d"%(site_login_base(i,site_index),n)
 
+def sat_name (i):
+    return 'sat_%d'%i
+
 # to have indexes start at 1
 def myrange (n):
     return range (1,n+1,1)
@@ -172,12 +214,16 @@ def message (*args):
     
 ##########
 def timer_start ():
-    global epoch
+    global epoch,last_time
     epoch = time.time()
+    last_time=epoch
     print '+++ timer start'
 
 def timer_show ():
-    print '+++ %d seconds ellapsed'%(time.time()-epoch)
+    global last_time
+    now=time.time()
+    print '+++ %.02f seconds ellapsed (%.02f)'%(now-epoch,now-last_time)
+    last_time=now
 
 ####################
 def test00_init (args=[1,2]):
@@ -334,7 +380,7 @@ def check_slivers_n (nn,esn,args=[1,2]):
               %(i,len(slivers),esn,nodename)
         for sliver in slivers:
             print '>>slivername = ',sliver['name']
-            pp.pprint(sliver)
+            pretty_printer.pprint(sliver)
         assert len(slivers) == esn
                 
 
@@ -489,13 +535,13 @@ def test02_person_n_ks (np,nks,add_if_true,args=[1,2]):
 ####################
 # retrieves node_id from hostname - checks for local nodes only
 def get_local_node_id(i,nodename):
-    return s[i].GetNodes(a[i],[nodename],None,'local')[0]['node_id']
+    return s[i].GetNodes(a[i],{'hostname':nodename,'peer_id':None})[0]['node_id']
 
 # 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
-        loc_nodes = s[i].GetNodes(a[i],None,None,'local')
+        loc_nodes = s[i].GetNodes(a[i],{'peer_id':None})
         for node in loc_nodes:
             print '%02d:==== Cleaning node %d'%(i,node['node_id'])
             s[i].DeleteNode(a[i],node['node_id'])
@@ -535,9 +581,6 @@ def clean_all_slices (args=[1,2]):
                 print '%02d:==== Cleaning slice %d'%(i,slice_id)
                 s[i].DeleteSlice(a[i],slice_id)
 
-def get_local_slice_id (i,name):
-    return s[i].GetSlices(a[i],{'name':[name],'peer_id':None})[0]['slice_id']
-
 def test04_slice (args=[1,2]):
     for n in myrange(number_slices):
        test04_slice_n (n,args)
@@ -574,7 +617,7 @@ def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
     for i in args:
         peer=peer_index(i)
-        slice_id = get_local_slice_id (i,slice_name (i,ns))
+        sname = slice_name (i,ns)
         
         if is_local:
             hostnames=[node_name(i,nn) for nn in nnl]
@@ -583,12 +626,12 @@ def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
             hostnames=[node_name(peer,nn) for nn in nnl]
             nodetype='foreign'
         if add_if_true:
-            s[i].AddSliceToNodes (a[i], slice_id,hostnames)
+            s[i].AddSliceToNodes (a[i], sname,hostnames)
             message="added"
         else:
-            s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
+            s[i].DeleteSliceFromNodes (a[i], sname,hostnames)
             message="deleted"
-        print '%02d:== %s in slice %d %s '%(i,message,slice_id,nodetype),
+        print '%02d:== %s in slice %s %s '%(i,message,sname,nodetype),
         print hostnames
 
 def test04_slice_add_lnode (args=[1,2]):
@@ -603,6 +646,169 @@ def test04_slice_del_lnode (args=[1,2]):
 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 (a[i],[name])[0]
+        except:
+            description="custom sat on plc%d"%i
+            min_role_id=10
+            sat_id=s[i].AddSliceAttributeType (a[i],
+                                               { 'name':name,
+                                                 'description': description,
+                                                 'min_role_id' : min_role_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(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'],'e=',s['expires']
+
+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']
+
+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(a[i])]
+        print '%02d: KEYS'%i
+        [p_key(x) for x in s[i].GetKeys(a[i])]
+        print '%02d: PERSONS'%i
+        [p_person(x) for x in s[i].GetPersons(a[i])]
+        print '%02d: NODES'%i
+        [p_node(x) for x in s[i].GetNodes(a[i])]
+        print '%02d: SLICES'%i
+        [p_slice(x) for x in s[i].GetSlices(a[i])]
+        print '%02d: Slice Attribute Types'%i
+        [p_sat(x) for x in s[i].GetSliceAttributeTypes(a[i])]
+        print '%02d: Slice Attributes'%i
+        [p_sa(x) for x in s[i].GetSliceAttributes(a[i])]
+        print '%02d: SLIVERS'%i
+        [p_sliver('%02d:'%i,x) for x in s[i].GetSlivers(a[i])]
+        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")
@@ -748,6 +954,10 @@ 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()
@@ -759,20 +969,40 @@ def test_all ():
     timer_show()
     test_all_slices ()
     timer_show()
+    test_all_sats ()
+    timer_show()
+    dump()
+    timer_show()
     message("END")
 
 ### ad hoc test sequences
 def populate ():
+    timer_start()
     test_all_init()
+    timer_show()
     test01_site()
+    timer_show()
     test02_person()
+    timer_show()
     test03_node()
+    timer_show()
     test04_slice([1])
-    test00_refresh ("populate: refreshing peer 1",[1])
+    timer_show()
     test04_slice_add_lnode([1])
+    timer_show()
+    test05_sat()
+    timer_show()
+    test05_sa([1])
+    timer_show()
+    test00_refresh ("populate: refreshing peer 1",[1])
+    timer_show()
     test04_slice_add_fnode([1])
+    timer_show()
     test00_refresh("populate: refresh all")
+    dump()
+    timer_show()
 
+# temporary - scratch as needed
 def test_now ():
     test_all_init()
     test_all_sites ()
@@ -783,37 +1013,50 @@ def test_now ():
 #####
 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"
+    print " -m run in mini mode (1 instance of each class)"
+    print " -b performs big run (%d times as large as normal)"%big_factor
+    print " -H performs huge run (%d times as large as normal)"%huge_factor
+    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:], "fnpb")
+        (o,a) = getopt.getopt(sys.argv[1:], "mnpbHl:")
     except:
         usage()
     func = test_all
     for (opt,val) in o:
-        if opt=='-f':
-            fast()
-       elif opt=='-b':
-            big()
-        elif opt=='-n':
+        if opt=='-n':
            print 'Running test_now'
             func = test_now
         elif opt=='-p':
            print 'Running populate'
             func = populate
+        elif opt=='-m':
+            mini()
+       elif opt=='-b':
+            big()
+       elif opt=='-H':
+            huge()
+        elif opt=='-l':
+            if val in (1,2):
+                local_index=val
+                print '-l option not implemented yet'
+                # need to figure a way to use Shell.py-like calling paradigm
+                sys.exit(1)
+            else:
+                usage()
         else:
             usage()
     if a:
         usage()
     show_test()
     func()   
+    timer_show()
+
 if __name__ == '__main__':
     normal()
     main()