]> git.proxmox.com Git - mirror_corosync.git/commitdiff
cts: Make code compatible with Python 3
authorBin Liu <bliu@suse.com>
Thu, 4 Jan 2018 03:12:41 +0000 (11:12 +0800)
committerJan Friesse <jfriesse@redhat.com>
Mon, 15 Jan 2018 14:03:34 +0000 (15:03 +0100)
Signed-off-by: Bin Liu <bliu@suse.com>
Reviewed-by: Jan Friesse <jfriesse@redhat.com>
cts/CTSvars.py.in
cts/corolab.py
cts/corosync.py
cts/corotests.py

index af2fcc410b26fa68bed0ecacb898d28b941d747f..cb59119fa1d6dd309cb2320eb61984eccd32c615 100644 (file)
@@ -1,3 +1,3 @@
-class CTSvars:
+class CTSvars(object):
     CTS_home="@prefix@/share/pacemaker/tests/cts"
     INITDIR="/etc/init.d"
index 8f1ebb5ed0aa6c92a4e6475a3557e7ecda70f3ad..9856441935b5fc2219911c965ffe3d2078de4de8 100755 (executable)
@@ -3,6 +3,8 @@
 '''CTS: Cluster Testing System: Lab environment module
  '''
 
+from __future__ import print_function
+
 __copyright__='''
 Copyright (c) 2010 Red Hat, Inc.
 
@@ -63,35 +65,35 @@ DefaultFacility = "daemon"
 
 
 def usage(arg):
-    print "Illegal argument " + arg
-    print "usage: " + sys.argv[0] +" [options] number-of-iterations" 
-    print "\nCommon options: "  
-    print "\t [--at-boot (1|0)],         does the cluster software start at boot time" 
-    print "\t [--nodes 'node list'],     list of cluster nodes separated by whitespace" 
-    print "\t [--limit-nodes max],       only use the first 'max' cluster nodes supplied with --nodes" 
-    print "\t [--logfile path],          where should the test software look for logs from cluster nodes" 
-    print "\t [--rrp-bindaddr addr],       extra interface used for rrp, provide the bindaddr" 
-    print "\t [--outputfile path],       optional location for the test software to write logs to" 
-    print "\t [--syslog-facility name],  which syslog facility should the test software log to" 
-    print "\t [--choose testcase-name],  run only the named test" 
-    print "\t [--list-tests],            list the valid tests" 
-    print "\t [--benchmark],             add the timing information" 
-    print "\t "
-    print "Additional (less common) options: "  
-    print "\t [--trunc (truncate logfile before starting)]" 
-    print "\t [--xmit-loss lost-rate(0.0-1.0)]" 
-    print "\t [--recv-loss lost-rate(0.0-1.0)]" 
-    print "\t [--standby (1 | 0 | yes | no)]" 
-    print "\t [--fencing (1 | 0 | yes | no)]" 
-    print "\t [--once],                 run all valid tests once" 
-    print "\t [--no-loop-tests],        dont run looping/time-based tests" 
-    print "\t [--no-unsafe-tests],      dont run tests that are unsafe for use with ocfs2/drbd" 
-    print "\t [--valgrind-tests],       include tests using valgrind" 
-    print "\t [--experimental-tests],   include experimental tests" 
-    print "\t [--oprofile 'node list'], list of cluster nodes to run oprofile on]" 
-    print "\t [--qarsh]                 Use the QARSH backdoor to access nodes instead of SSH"
-    print "\t [--seed random_seed]"
-    print "\t [--set option=value]"
+    print("Illegal argument " + arg)
+    print("usage: " + sys.argv[0] +" [options] number-of-iterations")
+    print("\nCommon options: ")
+    print("\t [--at-boot (1|0)],         does the cluster software start at boot time")
+    print("\t [--nodes 'node list'],     list of cluster nodes separated by whitespace")
+    print("\t [--limit-nodes max],       only use the first 'max' cluster nodes supplied with --nodes")
+    print("\t [--logfile path],          where should the test software look for logs from cluster nodes")
+    print("\t [--rrp-bindaddr addr],       extra interface used for rrp, provide the bindaddr")
+    print("\t [--outputfile path],       optional location for the test software to write logs to")
+    print("\t [--syslog-facility name],  which syslog facility should the test software log to")
+    print("\t [--choose testcase-name],  run only the named test")
+    print("\t [--list-tests],            list the valid tests")
+    print("\t [--benchmark],             add the timing information")
+    print("\t ")
+    print("Additional (less common) options: ")
+    print("\t [--trunc (truncate logfile before starting)]")
+    print("\t [--xmit-loss lost-rate(0.0-1.0)]")
+    print("\t [--recv-loss lost-rate(0.0-1.0)]")
+    print("\t [--standby (1 | 0 | yes | no)]")
+    print("\t [--fencing (1 | 0 | yes | no)]")
+    print("\t [--once],                 run all valid tests once")
+    print("\t [--no-loop-tests],        dont run looping/time-based tests")
+    print("\t [--no-unsafe-tests],      dont run tests that are unsafe for use with ocfs2/drbd")
+    print("\t [--valgrind-tests],       include tests using valgrind")
+    print("\t [--experimental-tests],   include experimental tests")
+    print("\t [--oprofile 'node list'], list of cluster nodes to run oprofile on]")
+    print("\t [--qarsh]                 Use the QARSH backdoor to access nodes instead of SSH")
+    print("\t [--seed random_seed]")
+    print("\t [--set option=value]")
     sys.exit(1)
 
 class CoroLabEnvironment(CtsLab):
@@ -271,7 +273,7 @@ if __name__ == '__main__':
         Environment["logger"].append(FileLog(Environment, Environment["OutputFile"]))
 
     if len(node_list) < 1:
-        print "No nodes specified!"
+        print("No nodes specified!")
         sys.exit(1)
 
     if LimitNodes > 0:
index 21898511d9fd63b92274e606a57eb26e62ac5d8a..4c0774176c0bd74bcc4ce2fc12af6237b1e53112 100644 (file)
@@ -164,7 +164,7 @@ class corosync_needle(ClusterManager):
         for c in self.applied_config:
             if 'bindnetaddr' in c:
                 continue
-            elif not self.config.original.has_key(c):
+            elif c not in self.config.original:
                 # new config option (non default)
                 pass
             elif self.applied_config[c] is not self.config.original[c]:
@@ -207,13 +207,13 @@ class corosync_needle(ClusterManager):
             self.install_all_config()
 
     def key_for_node(self, node):
-        if not self.node_to_ip.has_key(node):
+        if node not in self.node_to_ip:
             self.node_to_ip[node] = socket.gethostbyname (node)
         return self.node_to_ip[node]
 
     def StartaCM(self, node, verbose=False):
 
-        if not self.ShouldBeStatus.has_key(node):
+        if node not in self.ShouldBeStatus:
             self.ShouldBeStatus[node] = "down"
 
         if self.ShouldBeStatus[node] != "down":
@@ -222,19 +222,19 @@ class corosync_needle(ClusterManager):
         self.debug('starting corosync on : ' + node)
         ret = ClusterManager.StartaCM(self, node)
         if self.start_cpg:
-            if self.cpg_agent.has_key(node):
+            if node in self.cpg_agent:
                 self.cpg_agent[node].restart()
             else:
                 self.cpg_agent[node] = CpgTestAgent(node, self.Env)
                 self.cpg_agent[node].start()
 
-        if self.sam_agent.has_key(node):
+        if node in self.sam_agent:
             self.sam_agent[node].restart()
 
         # votequorum agent started as needed.
-        if self.applied_config.has_key('quorum/provider'):
+        if 'quorum/provider' in self.applied_config:
             if self.applied_config['quorum/provider'] is 'corosync_votequorum':
-                if self.votequorum_agent.has_key(node):
+                if node in self.votequorum_agent:
                     self.votequorum_agent[node].restart()
                 else:
                     self.votequorum_agent[node] = VoteQuorumTestAgent(node, self.Env)
@@ -247,11 +247,11 @@ class corosync_needle(ClusterManager):
             return 1
 
         self.debug('stoping corosync on : ' + node)
-        if self.cpg_agent.has_key(node):
+        if node in self.cpg_agent:
             self.cpg_agent[node].stop()
-        if self.sam_agent.has_key(node):
+        if node in self.sam_agent:
             self.sam_agent[node].stop()
-        if self.votequorum_agent.has_key(node):
+        if node in self.votequorum_agent:
             self.votequorum_agent[node].stop()
         return ClusterManager.StopaCM(self, node)
 
@@ -381,18 +381,18 @@ class TestAgentComponent(ScenarioComponent):
                 raise RuntimeError ("corosync not up")
 
             if self.CM.start_cpg:
-                if self.CM.cpg_agent.has_key(node):
+                if node in self.CM.cpg_agent:
                     self.CM.cpg_agent[node].clean_start()
                 else:
                     self.CM.cpg_agent[node] = CpgTestAgent(node, CM.Env)
                     self.CM.cpg_agent[node].start()
-            if self.CM.sam_agent.has_key(node):
+            if node in self.CM.sam_agent:
                 self.CM.sam_agent[node].clean_start()
             else:
                 self.CM.sam_agent[node] = SamTestAgent(node, CM.Env)
                 self.CM.sam_agent[node].start()
             # votequorum agent started as needed.
-            if self.CM.applied_config.has_key('quorum/provider'):
+            if 'quorum/provider' in self.CM.applied_config:
                 if CM.applied_config['quorum/provider'] is 'corosync_votequorum':
                     self.CM.votequorum_agent[node] = VoteQuorumTestAgent(node, CM.Env)
                     self.CM.votequorum_agent[node].start()
@@ -402,10 +402,10 @@ class TestAgentComponent(ScenarioComponent):
         '''Tear down (undo) the given ScenarioComponent'''
         self.CM = CM
         for node in self.Env["nodes"]:
-            if self.CM.cpg_agent.has_key(node):
+            if node in self.CM.cpg_agent:
                 self.CM.cpg_agent[node].stop()
             self.CM.sam_agent[node].stop()
-            if self.CM.votequorum_agent.has_key(node):
+            if node in self.CM.votequorum_agent:
                 self.CM.votequorum_agent[node].stop()
 
 ###################################################################
@@ -420,7 +420,7 @@ class TestAgent(object):
         self.started = False
         resh = RemoteFactory.rsh
         self.rsh = RemoteExec(resh)
-        self.func_name = None
+        self.__name__ = None
         self.used = False
         self.env = Env
         self.send_recv = False
@@ -445,7 +445,7 @@ class TestAgent(object):
             self.read()
             self.started = True
             return True
-        except RuntimeError, msg:
+        except RuntimeError as msg:
             self.started = False
             return False
 
@@ -465,7 +465,7 @@ class TestAgent(object):
                 retries = retries + 1
                 self.sock.connect ((ip, self.port))
                 is_connected = True
-            except socket.error, msg:
+            except socket.error as msg:
                 if retries > 10:
                     LogFactory().log("%s:%s Tried connecting %d times. %s" % (self.node, self.binary, retries, str(msg)))
                 if retries > 30:
@@ -502,9 +502,11 @@ class TestAgent(object):
             a_str = str(a)
             real_msg += ":" + str (len (a_str)) + ":" + a_str
         real_msg += ";"
+        if sys.version_info > (3,):
+            real_msg = real_msg.encode("utf8")
         try:
             return self.sock.send (real_msg)
-        except socket.error, msg:
+        except socket.error as msg:
             LogFactory().log("send(%s): %s; error: %s" % (self.node, real_msg, msg))
             return 0
 
@@ -523,7 +525,7 @@ class TestAgent(object):
         try:
             return object.__getattribute__(self, name)
         except:
-            self.func_name = name
+            self.__name__ = name
             if self.send_recv:
                 return self.send_recv_dynamic
             else:
@@ -534,9 +536,9 @@ class TestAgent(object):
 
         try:
             res = self.read ()
-        except RuntimeError, msg:
+        except RuntimeError as msg:
             res = None
-            LogFactory().log("send_recv_dynamic: %s(); error: %s" % (self.func_name, msg))
+            LogFactory().log("send_recv_dynamic: %s(); error: %s" % (self.__name__, msg))
 
         return res
 
@@ -546,15 +548,17 @@ class TestAgent(object):
             raise RuntimeError ("agent not started")
 
         # number of args+func
-        real_msg = str (len (args) + 1) + ":" + str(len(self.func_name)) + ":" + self.func_name
+        real_msg = str (len (args) + 1) + ":" + str(len(self.__name__)) + ":" + self.__name__
         for a in args:
             a_str = str(a)
             real_msg += ":" + str (len (a_str)) + ":" + a_str
         real_msg += ";"
         sent = 0
+        if sys.version_info > (3,):
+            real_msg = bytes(real_msg, encoding = "utf8")
         try:
             sent = self.sock.send (real_msg)
-        except socket.error, msg:
+        except socket.error as msg:
             LogFactory().log("send_dynamic(%s): %s; error: %s" % (self.node, real_msg, msg))
 
         if sent == 0:
@@ -565,15 +569,17 @@ class TestAgent(object):
 
         try:
             msg = self.sock.recv (4096)
-        except socket.error, msg:
+        except socket.error as msg:
             raise RuntimeError(msg)
+        if sys.version_info > (3,):
+            msg = msg.decode("utf8")
 
         if msg == '':
             raise RuntimeError("socket connection broken")
         return msg
 
 
-class CpgConfigEvent:
+class CpgConfigEvent(object):
     def __init__(self, msg):
         info = msg.split(',')
         self.group_name = info[0]
index 5778100acc2b3d4b7e62f1c98de6dfa172bd433b..6a983fe47fb15ff6b68a03a6429029c84c8caf34 100644 (file)
@@ -1,3 +1,6 @@
+from __future__ import division
+from __future__ import print_function
+
 __copyright__='''
 Copyright (c) 2010 Red Hat, Inc.
 '''
@@ -34,7 +37,11 @@ Copyright (c) 2010 Red Hat, Inc.
 
 import random
 import socket
-from UserDict import UserDict
+import sys
+if sys.version_info < (3,):
+    from UserDict import UserDict
+else:
+    from collections import UserDict
 from cts.CTStests import *
 from corosync import CpgTestAgent
 
@@ -134,9 +141,9 @@ class CpgConfigChangeBase(CoroTest):
             elif self.listener is None:
                 self.listener = n
 
-        if self.CM.cpg_agent.has_key(self.wobbly):
+        if self.wobbly in self.CM.cpg_agent:
             self.wobbly_id = self.CM.cpg_agent[self.wobbly].cpg_local_get()
-        if self.CM.cpg_agent.has_key(self.listener):
+        if self.listener in self.CM.cpg_agent:
             self.CM.cpg_agent[self.listener].record_config_events(truncate=True)
 
         return ret
@@ -162,7 +169,7 @@ class CpgConfigChangeBase(CoroTest):
                     waited = waited + 1
                     printit = printit + 1
                     if printit is 60:
-                        print 'waited ' + str(waited) + ' seconds'
+                        print('waited ' + str(waited) + ' seconds')
                         printit = 0
 
             elif str(event.node_id) in str(self.wobbly_id) and not event.is_member:
@@ -318,7 +325,7 @@ class CpgCfgChgOnNodeIsolate(CpgConfigChangeBase):
         self.name="CpgCfgChgOnNodeIsolate"
 
     def config_valid(self, config):
-        if config.has_key('totem/rrp_mode'):
+        if 'totem/rrp_mode' in config:
             return False
         else:
             return True
@@ -345,12 +352,12 @@ class CpgCfgChgOnNodeRestart(CpgConfigChangeBase):
         self.CM.start_cpg = False
 
     def config_valid(self, config):
-        if config.has_key('totem/secauth'):
+        if 'totem/secauth' in config:
             if config['totem/secauth'] is 'on':
                 return False
             else:
                 return True
-        if config.has_key('totem/rrp_mode'):
+        if 'totem/rrp_mode' in config:
             return False
         else:
             return True
@@ -682,6 +689,8 @@ class QuorumState(object):
         assert(info != 'NOT_SUPPORTED')
 
         #self.CM.log('refresh: ' + info)
+        if info is None:
+            return
         params = info.split(':')
 
         self.node_votes = int(params[0])
@@ -707,9 +716,9 @@ class VoteQuorumBase(CoroTest):
         return ret
 
     def config_valid(self, config):
-        if config.has_key('totem/rrp_mode'):
+        if 'totem/rrp_mode' in config:
             return False
-        if config.has_key('quorum/provider'):
+        if 'quorum/provider' in config:
             return False
         return True
 
@@ -766,7 +775,7 @@ class VoteQuorumGoDown(VoteQuorumBase):
             if state.total_votes != nodes_alive:
                 self.failure('unexpected number of total votes:%d, nodes_alive:%d' % (state.total_votes, nodes_alive))
 
-            min = ((len(self.CM.Env["nodes"]) + 2) / 2)
+            min = int((len(self.CM.Env["nodes"]) + 2) / 2)
             if min != state.quorum:
                 self.failure('we should have %d (not %d) as quorum' % (min, state.quorum))
 
@@ -958,8 +967,8 @@ class GenSimulStart(CoroTest):
         ret = self.stopall(None)
         if not ret:
             return self.failure("Setup failed")
-
-        self.CM.clear_all_caches()
+        #clear_all_caches was removed
+        #self.CM.clear_all_caches()
 
         if not self.startall(None):
             return self.failure("Startall failed")
@@ -1112,7 +1121,7 @@ Setup: no config, kmod inserted
         self.need_all_up = False
 
     def config_valid(self, config):
-        return not config.has_key('resources')
+        return 'resources' not in config
 
     def __call__(self, node):
         '''Perform the 'NoWDConfig' test. '''
@@ -1597,7 +1606,7 @@ def CoroTestList(cm, audits):
         h['totem/interface[2]/mcastport'] = '5405'
         configs.append(h)
     else:
-        print 'Not including rrp tests. Use --rrp-binaddr to enable them.'
+        print('Not including rrp tests. Use --rrp-binaddr to enable them.')
 
     num=1
     for cfg in configs:
@@ -1605,7 +1614,7 @@ def CoroTestList(cm, audits):
             bound_test = testclass(cm)
             if bound_test.is_applicable() and bound_test.config_valid(cfg):
                 bound_test.Audits = audits
-                for c in cfg.keys():
+                for c in list(cfg.keys()):
                     bound_test.config[c] = cfg[c]
                 bound_test.name = bound_test.name + '_' + cfg.name
                 result.append(bound_test)