- pull bulk of functionality into get_slivers() utility function so that
[plcapi.git] / TestPeers.py
index 0d669f8..f6b7f16 100755 (executable)
@@ -25,7 +25,7 @@ import getopt
 import sys
 import time
 
-import Shell
+from PLC.Shell import Shell
 import PLC.Methods
 
 # when running locally, we might wish to run only our local stuff
@@ -43,16 +43,13 @@ class DummyShell:
             return 0
     def __init__(self,index):
         self.index=index
-    def init(self):
         for method in PLC.Methods.methods:
             # ignore path-defined methods for now
             if "." not in method:
                 setattr(self,method,DummyShell.Callable(method,self.index))
-    def show_config(self,*args):
-        print 'DummyShell'
         
 ####################
-import xmlrpclib
+#import xmlrpclib
 import os
 
 ## try to support reload
@@ -73,16 +70,15 @@ system_persons = 4
 # among that, 1 gets refreshed - other ones have conflicting names
 system_persons_cross = 1
 
-system_slices_ids = (1,2)
+system_slices_ids = (1,)
 def system_slices ():
     return len(system_slices_ids)
 def total_slices ():
     return number_slices+system_slices()
 
-# temporary - the myplc I use doesnt know about 'system' yet
 def system_slivers ():
-#    return len(system_slices_ids)
-    return 0
+    return len(system_slices_ids)
+
 # too tedious to do the maths : how many slices attached to node 1
 expected_slivers=None
 def total_slivers ():
@@ -179,8 +175,8 @@ plain_numbers=['zero','one','two','three','four','five','six','seven','eight','n
               'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty']
 plain_digits=['a','b','c','d','e','f','g','h','i','j']
 ####################
-plc[1]={ 'plcname':'plc1 in federation',
-         'hostname':'lurch.cs.princeton.edu',
+plc[1]={ 'plcname':'Thierry plc1',
+         'hostname':'planetlab-devbox.inria.fr',
          'url-format':'https://%s:443/PLCAPI/',
          'builtin-admin-id':'root@plc1.org',
          'builtin-admin-password':'root',
@@ -192,9 +188,11 @@ plc[1]={ 'plcname':'plc1 in federation',
          'person-format' : 'user1-%d@plc1.org',
          'key-format':'ssh-rsa 11key4plc11 user%d-key%d',
          'person-password' : 'password1',
+        'gpg-keyring':'gpg_plc2.pub',
+        'api-cacert':'api_plc2.crt',
        }
-plc[2]={ 'plcname':'plc2 in federation',
-         'hostname':'planetlab-devbox.inria.fr',
+plc[2]={ 'plcname':'Thierry plc2',
+         'hostname':'lurch.cs.princeton.edu',
          'url-format':'https://%s:443/PLCAPI/',
          'builtin-admin-id':'root@plc2.org',
          'builtin-admin-password':'root',
@@ -206,6 +204,8 @@ plc[2]={ 'plcname':'plc2 in federation',
          'person-format' : 'user2-%d@plc2.org',
          'key-format':'ssh-rsa 22key4plc22 user%d-key%d',
          'person-password' : 'password2',
+        'gpg-keyring':'gpg_plc1.pub',
+        'api-cacert':'api_plc1.crt',
        }
 
 ####################
@@ -288,36 +288,52 @@ def timer_show ():
     last_time=now
 
 ####################
-def test00_init (args=[1,2]):
+errors=0
+def myassert (message,boolean):
+    if not boolean:
+        print 'ASSERTION FAILED',message
+        global errors
+        errors +=1
+
+def epilogue ():
+    if errors != 0:
+        print 'TEST FAILED with %d errors'%errors
+        assert errors == 0
+        
+####################
+# init
+def test00_init (args=[1,2],builtin_person=False):
     timer_start()
-    ## have you loaded this file already (support for reload)
     for i in args:
         url=plc[i]['url-format']%plc[i]['hostname']
         plc[i]['url']=url
         if local_peer is None:
             # the regular remote mode
-            argv=[sys.argv[0],
-                  # xxx todo - Shell.py still needs a non-empty config even if we define everything
-                  '--config','dummy-config',
-                  '--url',url,
-                  '--user',plc[i]['builtin-admin-id'],
-                  '--password',plc[i]['builtin-admin-password']]
-            print 'initializing s[%d]=>%s'%(i,url)
-            s[i]=Shell.Shell(argv)
-            s[i].init()
+            print 'initializing s[%d]=>%s'%(i,url),
+            if builtin_person:
+                user=plc[i]['builtin-admin-id']
+                password=plc[i]['builtin-admin-password']
+            else:
+                user=plc[i]['peer-admin-name']
+                password=plc[i]['peer-admin-password']
+            s[i]=Shell(url=url,
+                       user=user,
+                       password=password)
+            print 'user=',user
         elif local_peer == i:
             # local mode - use Shell's Direct mode - use /etc/planetlab/plc_config
-            s[i]=Shell.Shell([sys.argv[0]])
-            s[i].init()
+            s[i]=Shell()
         else:
             # remote peer in local mode : use dummy shell instead
             s[i]=DummyShell(i)
-            s[i].init()
+
+# use new person's account
 
 def test00_print (args=[1,2]):
     for i in args:
         print '==================== s[%d]'%i
-        s[i].show_config()
+#        s[i].show_config()
+        print 'show_config obsoleted'
     print '===================='
 
 def check_nodes (el,ef,args=[1,2]):
@@ -328,8 +344,8 @@ def check_nodes (el,ef,args=[1,2]):
         n = len ([ x for x in all_nodes if x['peer_id'] is None])
         f = len ([ x for x in all_nodes if x['peer_id'] is not None])
         print '%02d: Checking nodes: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
-        assert n==el
-        assert f==ef
+        myassert ('local nodes',n==el)
+        myassert ('foreign nodes',f==ef)
 
 def check_keys (el,ef,args=[1,2]):
     for i in args:
@@ -339,8 +355,8 @@ def check_keys (el,ef,args=[1,2]):
         n = len ([ x for x in all_keys if x['peer_id'] is None])
         f = len ([ x for x in all_keys if x['peer_id'] is not None])
         print '%02d: Checking keys: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
-        assert n==el
-        assert f==ef
+        myassert ('local keys',n==el)
+        myassert ('foreign_keys',f==ef)
 
 def check_persons (el,ef,args=[1,2]):
     for i in args:
@@ -350,8 +366,8 @@ def check_persons (el,ef,args=[1,2]):
         n = len ([ x for x in all_persons if x['peer_id'] is None])
         f = len ([ x for x in all_persons if x['peer_id'] is not None])
         print '%02d: Checking persons: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
-        assert n==el
-        assert f==ef
+        myassert ('local persons',n==el)
+        myassert ('foreign persons',f==ef)
 
 # expected : local slices, foreign slices
 def check_slices (els,efs,args=[1,2]):
@@ -359,8 +375,8 @@ def check_slices (els,efs,args=[1,2]):
         ls=len(s[i].GetSlices({'peer_id':None}))
         fs=len(s[i].GetSlices({'~peer_id':None}))
         print '%02d: Checking slices: got %d local (e=%d) & %d foreign (e=%d)'%(i,ls,els,fs,efs)
-        assert els==ls
-        assert efs==fs
+        myassert ('local slices',els==ls)
+        myassert ('foreign slices',efs==fs)
 
 def show_nodes (i,node_ids):
     # same as above
@@ -394,7 +410,7 @@ def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
         slice_node_ids=slice['node_ids']
         print 'on nodes ',slice_node_ids
         show_nodes (i,slice_node_ids)
-        assert len(slice_node_ids)>=expected_nodes
+        myassert ('slice nodes',len(slice_node_ids)>=expected_nodes)
        if len(slice_node_ids) != expected_nodes:
            print 'TEMPORARY'
 
@@ -414,7 +430,7 @@ def check_conf_files_n (nn,args=[1,2]):
     for i in args:
         nodename=node_name(i,nn)
         ndict= s[i].GetSlivers([nodename])[0]
-        assert ndict['hostname'] == nodename
+        myassert ('conf files',ndict['hostname'] == nodename)
         conf_files = ndict['conf_files']
         print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
         for conf_file in conf_files:
@@ -437,15 +453,15 @@ def check_slivers_1 (esn,args=[1,2]):
 def check_slivers_n (nn,esn,args=[1,2]):
     for i in args:
         nodename=node_name(i,nn)
-        ndict= s[i].GetSlivers([nodename])[0]
-        assert ndict['hostname'] == nodename
+        ndict= s[i].GetSlivers(nodename)
+        myassert ('slivers hostname',ndict['hostname'] == nodename)
         slivers = ndict['slivers']
         print '%02d: %d slivers (exp. %d) in GetSlivers for node %s'\
               %(i,len(slivers),esn,nodename)
         for sliver in slivers:
             print '>>slivername = ',sliver['name']
             pretty_printer.pprint(sliver)
-        assert len(slivers) == esn
+        myassert ('slivers count',len(slivers) == esn)
                 
 
 ####################
@@ -473,22 +489,18 @@ def test00_admin_enable (args=[1,2]):
             s[i].AddRoleToPerson('admin',plc[i]['peer-admin-id'])
             print '%02d:== enabled+admin on account %d:%s'%(i,plc[i]['peer-admin-id'],plc[i]['peer-admin-name'])
 
-def test00_peer_person (args=[1,2]):
-    global plc
-    for i in args:
-        peer=peer_index(i)
-        email=plc[peer]['peer-admin-name']
-        try:
-            p=s[i].GetPersons([email])[0]
-            plc[i]['peer_person_id']=p['person_id']
-        except:
-            person_id = s[i].AddPerson ( {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
-                                               'email':email,'password':plc[peer]['peer-admin-password']})
-            if person_id:
-                print '%02d:== Created person %d as the peer person'%(i,person_id)
-            plc[i]['peer_person_id']=person_id
-
 ####################
+def locate_key (filename):
+     " tries to locate a key file, either in . or in /etc/planetlab"
+     try:
+         return file("./"+filename).read()
+     except:
+         try:
+             return file("/etc/planetlab/"+filename).read()
+         except:
+             raise Exception,"Could not locate key %s"%filename
+             
+
 def test00_peer (args=[1,2]):
     global plc
     for i in args:
@@ -496,28 +508,57 @@ def test00_peer (args=[1,2]):
         peername = plc_name(peer)
         try:
             p=s[i].GetPeers ( [peername])[0]
-            plc[i]['peer_id']=p['peer_id']
         except:
-            peer_id=s[i].AddPeer ( {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
-            # NOTE : need to manually reset the encrypted password through SQL at this point
-            if peer_id:
+            try:
+                keyringname=plc[i]['gpg-keyring']
+                cacertname=plc[i]['api-cacert']
+                print 'Trying to locate keys for peer on plc[%d]'%i,
+                print 'in %s and %s'%(keyringname,cacertname)
+
+                keyring=locate_key(keyringname)
+                cacert=locate_key(cacertname)
+                peer_id=s[i].AddPeer ( {'peername':peername,
+                                        'peer_url':plc[peer]['url'],
+                                        'key':keyring,
+                                        'cacert': cacert,
+                                        })
                 print '%02d:Created peer %d'%(i,peer_id)
-                print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
-            plc[i]['peer_id']=peer_id
-
-def test00_peer_passwd (args=[1,2]):
-    if local_peer is None:
-        for i in args:
-            # using an ad-hoc local command for now - never could get quotes to reach sql....
-            print "Attempting to remotely set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
-            retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
-            print '-> system returns',retcod
-    else:
-        i=local_peer
-        print "Locally setting passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
-        retcod=os.system("./person-password.sh -l %d"%(plc[i]['peer_person_id']))
-        print '-> system returns',retcod
+            except Exception,e:
+                print 'Could not create peer,',e
     
+# push various stuff across hosts through external ssh/scp
+# this is broken, use peers-test.mk instead
+#def test00_push_public_peer_material (args=[1,2]):
+#    for i in args:
+#      peer=peer_index(i)
+#
+#      ### the gpg keyring
+#      # refresh
+#      local_keyring="/etc/planetlab/gpg_keyring.pub"
+#      command="ssh root@%s gpg --homedir=/etc/planetlab --export --armor > %s"\
+#             %(plc[i]['hostname'],local_keyring)
+#      retcod=os.system(command)
+#      print '#',command,'->',retcod
+#
+#    for i in args:
+#      peer=peer_index(i)
+#      # push
+#      src_url='root@%s:%s'%(plc[i]['hostname'],local_keyring)
+#      dst_url='root@%s:%s'%(plc[peer]['hostname'], plc[i]['gpg-keyring'])
+#      command = 'scp %s %s'%(src_url,dst_url)
+#      retcod=os.system(command)
+#      print '#',command,'->',retcod
+#      
+#    for i in args:
+#      peer=peer_index(i)
+#      # push cacert
+#      local_cacert='/etc/planetlab/api_ca_ssl.crt'
+#      src_url='root@%s:%s'%(plc[i]['hostname'],local_cacert)
+#      dst_url='root@%s:%s'%(plc[peer]['hostname'], plc[i]['api-cacert'])
+#      command = 'scp %s %s'%(src_url,dst_url)
+#      retcod=os.system(command)
+#      print '#',command,'->',retcod
+
 # this one gets cached 
 def get_peer_id (i):
     try:
@@ -543,7 +584,7 @@ def test00_refresh (message,args=[1,2]):
                 print key,retcod[key],
         print "}"
         print "+++ ellapsed: {",
-        timers=retcod['timers']
+        timers=retcod
         keys=timers.keys()
         keys.sort()
         for key in keys:
@@ -809,10 +850,10 @@ def p_slice(s):
     print "--- 'expires':",s['expires']
 
 def p_sat(sat):
-    print sat['attribute_type_id'],sat['peer_id'], sat['name'], sat['min_role_id'], sat['description']
+    print sat['attribute_type_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 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
@@ -848,8 +889,13 @@ def dump (args=[1,2]):
         [p_sat(x) for x in s[i].GetSliceAttributeTypes()]
         print '%02d: Slice Attributes'%i
         [p_sa(x) for x in s[i].GetSliceAttributes()]
-        print '%02d: SLIVERS'%i
-        [p_sliver('%02d:'%i,x) for x in s[i].GetSlivers()]
+        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
     
 
@@ -908,13 +954,15 @@ def all():
 ####################
 def test_all_init ():
     message ("INIT")
-    test00_init ()
+    test00_init (builtin_person=True)
     test00_print ()
     test00_admin_person ()
     test00_admin_enable ()
-    test00_peer_person ()
+    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 ()
-    test00_peer_passwd ()
 
 def test_all_sites ():
     test01_site ()
@@ -980,12 +1028,12 @@ def test_all_addslices ():
     # 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])
+
+    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(),0,[1])
-    check_slices (system_slices(),number_slices,[2])
+    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])
@@ -1041,10 +1089,10 @@ def test_all_delslices ():
 
     message ("CHECKING SLICES CLEAN UP")
     clean_all_slices([1])
-    check_slices (system_slices(),0,[1])
-    check_slices (system_slices(),number_slices,[2])
+    check_slices (system_slices(),system_slices(),[1])
+    check_slices (system_slices(),total_slices(),[2])
     test00_refresh ("After slices clenaup")
-    check_slices(system_slices(),0)
+    check_slices(system_slices(),system_slices())
 
 def test_all_slices ():
     test_all_addslices ()
@@ -1094,8 +1142,8 @@ def populate ():
     message("END")
 
 def populate_end():
-    test00_init()
-    test00_refresh ("populate: refreshing peer 1",[1])
+    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()
@@ -1109,7 +1157,13 @@ def populate_end():
 # temporary - scratch as needed
 def test_now ():
     test_all_init()
-    test_all_sites ()
+
+#    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()
@@ -1165,6 +1219,7 @@ def main ():
     show_test()
     func()   
     timer_show()
+    epilogue
 
 if __name__ == '__main__':
     normal()