tcptest.py runs on top of python3, and uses ArgumentParser
[tests.git] / system / plcsh_stress_test.py
index ef862bc..fa4551b 100755 (executable)
@@ -1,11 +1,20 @@
 #!/usr/bin/env plcsh
 #
+# WARNING: as opposed to the rest of the python code in this repo
+# the current script runs on top of plcsh and so it is for now
+# pinned as python2 code
+#
+#
 # Test script utility class
 #
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id$
+
+# NOTE on porting to python3
+#
+# this file gets fed to plcsh on the tested myplc, so
+# it needs to remain python2 for now
 #
 
 from pprint import pprint
@@ -108,15 +117,10 @@ def random_peer():
         }
 
 def random_site(namelengths):
-    try:
-        sitename=randstr(namelengths['sitename'],namelengths['sitename_contents'])
-    except:
-        sitename=randstr(namelengths['sitename'])
-    try:
-        abbreviated_name=randstr(namelengths['abbreviated_name'],namelengths['abbreviated_name_contents'])
-    except:
-        abbreviated_name=randstr(namelengths['abbreviated_name'])
+    sitename=randstr(namelengths['sitename'],namelengths['sitename_contents'])
+    abbreviated_name=randstr(namelengths['abbreviated_name'],namelengths['abbreviated_name_contents'])
 
+    print 'nl[a] in random_site',namelengths['abbreviated_name'],'actual',len(abbreviated_name)
     return {
         'name': sitename,
         'abbreviated_name': abbreviated_name,
@@ -124,7 +128,7 @@ def random_site(namelengths):
         'latitude': int(randfloat(-90.0, 90.0) * 1000) / 1000.0,
         'longitude': int(randfloat(-180.0, 180.0) * 1000) / 1000.0,
         }
-            
+
 def random_address_type():
     return {
         'name': randstr(20),
@@ -162,13 +166,16 @@ def random_key(key_types,namelengths):
 def random_tag_type (role_ids):
     return  {'tagname': randstr(12,letters+digits),
              'category':randstr(4,letters+digits)+'/'+randstr(6,letters+digits),
-             'min_role_id': random.sample(role_ids, 1)[0],
              'description' : randstr(128,letters+digits+whitespace+punctuation),
              }
 
 def random_nodegroup():
     return {'groupname' : randstr(30, letters+digits+whitespace) }
 
+def random_roles(role_ids):
+    nb_roles=len(role_ids)
+    return random.sample(role_ids,random.choice(range(1,nb_roles+1)))
+
 tag_fields=['arch']
 def random_node(node_types,boot_states,namelengths):
     return {
@@ -313,7 +320,9 @@ class Test:
         'hostname2':5,
         'login_base':20,
         'sitename':254,
+        'sitename_contents':letters+digits,
         'abbreviated_name':50,
+        'abbreviated_name_contents':letters+digits+whitespace+punctuation,
         'model':255,
         'first_name':128,
         'last_name':128,
@@ -325,7 +334,7 @@ class Test:
         'hostname2':3,
         'login_base':8,
         'sitename':64,
-        'sitename_contents':letters+digits+whitespace+punctuation,
+        'sitename_contents':letters+digits,
         'abbreviated_name':24,
         'abbreviated_name_contents':letters+digits+whitespace+punctuation,
         'model':40,
@@ -340,7 +349,7 @@ class Test:
         self.verbose = verbose
         self.preserve = preserve
         self.federating = federating
-        
+
         self.site_ids = []
         self.address_type_ids = []
         self.address_ids = []
@@ -359,7 +368,7 @@ class Test:
         self.slice_tag_ids = []
 
     def Cardinals (self):
-        return [len(x) for x in ( 
+        return [len(x) for x in (
                 self.api.GetNodes({},['node_id']),
                 self.api.GetSites({},['site_id']),
                 self.api.GetPersons({},['person_id']),
@@ -421,7 +430,7 @@ class Test:
             self.AddConfFiles(sizes['conf_files'])
             self.AddSlices(sizes['slices_per_site'])
             self.AddSliceTags(sizes['attributes_per_slice'])
-        
+
         else:
             self.RecordStatus()
             self.AddSites(sizes['sites'])
@@ -465,7 +474,7 @@ class Test:
         self.DeleteAddressTypes()
         self.DeleteSites()
 
-    # record current (old) objects 
+    # record current (old) objects
     def RecordStatus (self):
         self.old_site_ids = [ s['site_id'] for s in self.api.GetSites({},['site_id']) ]
         self.old_person_ids = [ s['person_id'] for s in self.api.GetPersons({},['person_id']) ]
@@ -830,7 +839,7 @@ class Test:
             self.api.DeletePerson(person_id)
 
             if self.check:
-                assert not self.api.GetPersons([person_id])                         
+                assert not self.api.GetPersons([person_id])
 
             if self.verbose:
                 print "Deleted user", person_id
@@ -935,7 +944,7 @@ class Test:
             # locate tag type
             tag_type_id = self.nodegroup_type_ids[i]
             tagname=self.api.GetTagTypes([tag_type_id])[0]['tagname']
-            
+
             # Add node group
             groupname = random_nodegroup() ['groupname']
             value = 'yes'
@@ -1001,7 +1010,7 @@ class Test:
         be added to a random node group if AddNodeGroups() was
         previously run.
         """
-        
+
         node_types = self.api.GetNodeTypes()
         if not node_types:
             raise Exception, "No node types"
@@ -1051,7 +1060,7 @@ class Test:
             # Update node
             node_fields = random_node(node_types,boot_states,self.namelengths)
             self.api.UpdateNode(node_id, node_fields)
-            
+
             node = self.api.GetNodes([node_id])[0]
 
             # Add to a random set of node groups
@@ -1129,7 +1138,7 @@ class Test:
         network_methods = self.api.GetNetworkMethods()
         if not network_methods:
             raise Exception, "No network methods"
-        
+
         network_types = self.api.GetNetworkTypes()
         if not network_types:
             raise Exception, "No network types"
@@ -1164,7 +1173,7 @@ class Test:
         network_methods = self.api.GetNetworkMethods()
         if not network_methods:
             raise Exception, "No network methods"
-        
+
         network_types = self.api.GetNetworkTypes()
         if not network_types:
             raise Exception, "No network types"
@@ -1204,7 +1213,7 @@ class Test:
             assert not self.api.GetInterfaces(self.interface_ids)
 
         self.interface_ids = []
-        
+
     def AddIlinks (self, n):
         """
         Add random links between interfaces.
@@ -1438,7 +1447,7 @@ class Test:
 
     def AddTagTypes(self,n_sa,n_ng,n_il):
         """
-        Add as many tag types as there are nodegroups, 
+        Add as many tag types as there are nodegroups,
         will use value=yes for each nodegroup
         """
 
@@ -1455,7 +1464,11 @@ class Test:
                 self.slice_type_ids + \
                 self.nodegroup_type_ids + \
                 self.ilink_type_ids
-            
+
+            tt_role_ids=random_roles(role_ids)
+            for tt_role_id in tt_role_ids:
+                self.api.AddRoleToTagType(tt_role_id,tag_type_id)
+
             if i < n_sa:
                 self.slice_type_ids.append(tag_type_id)
             elif i < n_sa+n_ng :
@@ -1467,8 +1480,10 @@ class Test:
                 tag_type = self.api.GetTagTypes([tag_type_id])[0]
                 for field in tag_type_fields:
                     assert tag_type[field] == tag_type_fields[field]
+                for tt_role_id in tt_role_ids:
+                    assert tt_role_id in tag_type['role_ids']
             if self.verbose:
-                print "Updated slice attribute type", tag_type_id
+                print "Created tag type", tag_type_id
 
     def UpdateTagTypes(self):
         """
@@ -1491,7 +1506,7 @@ class Test:
                 for field in tag_type_fields:
                     assert tag_type[field] == tag_type_fields[field]
             if self.verbose:
-                print "Updated slice attribute type", tag_type_id
+                print "Updated tag type", tag_type_id
 
     def DeleteTagTypes(self):
         """
@@ -1505,7 +1520,7 @@ class Test:
                 assert not self.api.GetTagTypes([tag_type_id])
 
             if self.verbose:
-                print "Deleted slice attribute type", tag_type_id
+                print "Deleted tag type", tag_type_id
 
         if self.check:
             assert not self.api.GetTagTypes(self.slice_type_ids+self.nodegroup_type_ids+self.ilink_type_ids)
@@ -1655,7 +1670,7 @@ class Test:
                         if node_id is not None:
                             print "to node", node_id,
                         print
-                        
+
     def UpdateSliceTags(self):
         """
         Make random changes to any slice attributes we may have added.
@@ -1692,24 +1707,37 @@ class Test:
 
         self.slice_tag_ids = []
 
+    # convenience for cleaning up
+    # not exactly accurate -- use on test plcs only
+    def WipeSitesFromLength(self):
+        for site in self.api.GetSites():
+            abbrev=site['abbreviated_name']
+#            print 'matching',len(abbrev),'against',self.namelengths['abbreviated_name']
+            if len(abbrev)==self.namelengths['abbreviated_name']:
+#            if len(abbrev)==17:
+                print 'wiping site %d (%s)'%(site['site_id'],site['name'])
+                self.api.DeleteSite(site['site_id'])
+
 def main():
     parser = OptionParser()
-    parser.add_option("-c", "--check", action = "store_true", default = False, 
+    parser.add_option("-c", "--check", action = "store_true", default = False,
                       help = "Check most actions (default: %default)")
-    parser.add_option("-q", "--quiet", action = "store_true", default = False, 
+    parser.add_option("-q", "--quiet", action = "store_true", default = False,
                       help = "Be quiet (default: %default)")
     parser.add_option("-p","--preserve", action="store_true", default =False,
                       help = "Do not delete created objects")
-    parser.add_option("-t", "--tiny", action = "store_true", default = False, 
+    parser.add_option("-t", "--tiny", action = "store_true", default = False,
                       help = "Run a tiny test (default: %default)")
-    parser.add_option("-l", "--large", action = "store_true", default = False, 
+    parser.add_option("-l", "--large", action = "store_true", default = False,
                       help = "Run a large test (default: %default)")
-    parser.add_option("-x", "--xlarge", action = "store_true", default = False, 
+    parser.add_option("-x", "--xlarge", action = "store_true", default = False,
                       help = "Run an XL test (default: %default)")
-    parser.add_option("-s", "--short-names", action="store_true", dest="short_names", default = False, 
+    parser.add_option("-s", "--short-names", action="store_true", dest="short_names", default = False,
                       help = "Generate smaller names for checking UI rendering")
     parser.add_option ("-f", "--foreign", action="store_true", dest="federating", default = False,
                        help = "Create a fake peer and add items in it (no update, no delete)")
+    parser.add_option ("-w", "--wipe", action="store_true", dest="wipe", default = False,
+                       help = "Wipe sites whose abbrev matches what the tests created")
     (options, args) = parser.parse_args()
 
     test = Test(api = Shell(),
@@ -1723,6 +1751,10 @@ def main():
     else:
         test.namelengths = Test.namelengths_default
 
+    if options.wipe:
+        test.WipeSitesFromLength()
+        return
+
     if options.tiny:
         sizes = Test.sizes_tiny
     elif options.large:
@@ -1731,7 +1763,6 @@ def main():
         sizes = Test.sizes_xlarge
     else:
         sizes = Test.sizes_default
-
     test.Run(**sizes)
 
 if __name__ == "__main__":