Skip to content

Commit

Permalink
Test: CTS: Avoid using the has_key() built-in as its not available in…
Browse files Browse the repository at this point in the history
… py3
  • Loading branch information
beekhof committed Jul 30, 2015
1 parent 537df3c commit 514bffa
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cts/CM_lha.py
Expand Up @@ -92,7 +92,7 @@ def install_config(self, node):
self.log("Node %s is not up." % node)
return None

if not self.CIBsync.has_key(node) and self.Env["ClobberCIB"] == 1:
if not node in self.CIBsync and self.Env["ClobberCIB"] == 1:
self.CIBsync[node] = 1
self.rsh(node, "rm -f "+CTSvars.CRM_CONFIG_DIR+"/cib*")

Expand Down
10 changes: 5 additions & 5 deletions cts/CTS.py
Expand Up @@ -157,7 +157,7 @@ def dump(self):
self.Env.dump()

def has_key(self, key):
return self.Env.has_key(key)
return key in self.Env.keys()

def __getitem__(self, key):
return self.Env[key]
Expand Down Expand Up @@ -300,7 +300,7 @@ def __getitem__(self, key):
return self.name

print("FIXME: Getting %s from %s" % (key, repr(self)))
if self.data.has_key(key):
if key in self.data:
return self.data[key]

return self.templates.get_patterns(self.Env["Name"], key)
Expand Down Expand Up @@ -500,7 +500,7 @@ def StartaCM(self, node, verbose=False):
else: self.debug("Starting %s on node %s" % (self.templates["Name"], node))
ret = 1

if not self.ShouldBeStatus.has_key(node):
if not node in self.ShouldBeStatus:
self.ShouldBeStatus[node] = "down"

if self.ShouldBeStatus[node] != "down":
Expand Down Expand Up @@ -871,7 +871,7 @@ def StatsExtract(self):

for host in self.Env["nodes"]:
log_stats_file = "%s/cts-stats.csv" % CTSvars.CRM_DAEMON_DIR
if has_log_stats.has_key(host):
if host in has_log_stats:
self.rsh(host, '''bash %s %s stop''' % (log_stats_bin, log_stats_file))
(rc, lines) = self.rsh(host, '''cat %s''' % log_stats_file, stdout=2)
self.rsh(host, '''bash %s %s delete''' % (log_stats_bin, log_stats_file))
Expand All @@ -891,7 +891,7 @@ def StatsMark(self, testnum):

for host in self.Env["nodes"]:
log_stats_file = "%s/cts-stats.csv" % CTSvars.CRM_DAEMON_DIR
if not has_log_stats.has_key(host):
if not host in has_log_stats:

global log_stats
global log_stats_bin
Expand Down
14 changes: 7 additions & 7 deletions cts/CTSaudits.py
Expand Up @@ -108,7 +108,7 @@ def TestLogging(self):
self.CM.log ("ERROR: Cannot execute remote command [%s] on %s" % (cmd, node))

for k in self.kinds:
if watch.has_key(k):
if k in watch:
w = watch[k]
if watch_pref == "any": self.CM.log("Testing for %s logs" % (k))
w.lookforall(silent=True)
Expand Down Expand Up @@ -226,7 +226,7 @@ def __call__(self):
self.known.append(line)
self.CM.log("Warning: Corosync core file on %s: %s" % (node, line))

if self.CM.ShouldBeStatus.has_key(node) and self.CM.ShouldBeStatus[node] == "down":
if node in self.CM.ShouldBeStatus and self.CM.ShouldBeStatus[node] == "down":
clean = 0
(rc, lsout) = self.CM.rsh(node, "ls -al /dev/shm | grep qb-", None)
for line in lsout:
Expand Down Expand Up @@ -532,7 +532,7 @@ def __init__(self, cm):
, "auditfail":0}

def has_key(self, key):
return self.Stats.has_key(key)
return key in self.Stats

def __setitem__(self, key, value):
self.Stats[key] = value
Expand All @@ -542,7 +542,7 @@ def __getitem__(self, key):

def incr(self, name):
'''Increment (or initialize) the value associated with the given name'''
if not self.Stats.has_key(name):
if not name in self.Stats:
self.Stats[name] = 0
self.Stats[name] = self.Stats[name]+1

Expand Down Expand Up @@ -601,7 +601,7 @@ def __init__(self, cm):
, "auditfail":0}

def has_key(self, key):
return self.Stats.has_key(key)
return key in self.Stats

def __setitem__(self, key, value):
self.Stats[key] = value
Expand All @@ -611,7 +611,7 @@ def __getitem__(self, key):

def incr(self, name):
'''Increment (or initialize) the value associated with the given name'''
if not self.Stats.has_key(name):
if not name in self.Stats:
self.Stats[name] = 0
self.Stats[name] = self.Stats[name]+1

Expand Down Expand Up @@ -726,7 +726,7 @@ def __getitem__(self, key):

def incr(self, name):
'''Increment (or initialize) the value associated with the given name'''
if not self.Stats.has_key(name):
if not name in self.Stats:
self.Stats[name] = 0
self.Stats[name] = self.Stats[name]+1

Expand Down
6 changes: 3 additions & 3 deletions cts/CTSscenarios.py
Expand Up @@ -124,7 +124,7 @@ def TearDown(self, max=None):

def incr(self, name):
'''Increment (or initialize) the value associated with the given name'''
if not self.Stats.has_key(name):
if not name in self.Stats:
self.Stats[name] = 0
self.Stats[name] = self.Stats[name]+1

Expand Down Expand Up @@ -176,7 +176,7 @@ def run_test(self, test, testcount):

elapsed_time = stoptime - starttime
test_time = stoptime - test.get_timer()
if not test.has_key("min_time"):
if not test["min_time"]:
test["elapsed_time"] = elapsed_time
test["min_time"] = test_time
test["max_time"] = test_time
Expand Down Expand Up @@ -387,7 +387,7 @@ def SetUp(self, CM):
'''Start the PingFest!'''

self.PingSize = 1024
if CM.Env.has_key("PingSize"):
if "PingSize" in CM.Env.keys():
self.PingSize = CM.Env["PingSize"]

CM.log("Starting %d byte flood pings" % self.PingSize)
Expand Down
23 changes: 14 additions & 9 deletions cts/CTStests.py
Expand Up @@ -97,13 +97,18 @@ def debug(self, args):
self.logger.debug(args)

def has_key(self, key):
return self.Stats.has_key(key)
return key in self.Stats

def __setitem__(self, key, value):
self.Stats[key] = value

def __getitem__(self, key):
return self.Stats[key]
if str(key) == "0":
raise ValueError("Bad call to 'foo in X', should reference 'foo in X.Stats' instead")

if key in self.Stats:
return self.Stats[key]
return None

def log_mark(self, msg):
self.debug("MARK: test %s %s %d" % (self.name,msg,time.time()))
Expand All @@ -128,7 +133,7 @@ def log_timer(self,key = "test"):

def incr(self, name):
'''Increment (or initialize) the value associated with the given name'''
if not self.Stats.has_key(name):
if not name in self.Stats:
self.Stats[name] = 0
self.Stats[name] = self.Stats[name]+1

Expand Down Expand Up @@ -534,7 +539,7 @@ def is_applicable(self):
if not self.is_applicable_common():
return 0

if self.Env.has_key("DoFencing"):
if "DoFencing" in self.Env.keys():
return self.Env["DoFencing"]

return 1
Expand Down Expand Up @@ -1574,7 +1579,7 @@ def __call__(self, node):
p_max = len(self.Env["nodes"])
for node in self.Env["nodes"]:
p = self.Env.RandomGen.randint(1, p_max)
if not partitions.has_key(p):
if not p in partitions:
partitions[p] = []
partitions[p].append(node)
p_max = len(partitions.keys())
Expand Down Expand Up @@ -2247,11 +2252,11 @@ def is_applicable(self):
if not self.is_applicable_common():
return None

if not self.Env.has_key("rpm-dir"):
if not "rpm-dir" in self.Env.keys():
return None
if not self.Env.has_key("current-version"):
if not "current-version" in self.Env.keys():
return None
if not self.Env.has_key("previous-version"):
if not "previous-version" in self.Env.keys():
return None

return 1
Expand Down Expand Up @@ -3109,7 +3114,7 @@ def is_applicable(self):
if not self.driver.is_applicable():
return False

if self.Env.has_key("DoFencing"):
if "DoFencing" in self.Env.keys():
return self.Env["DoFencing"]

return True
Expand Down
25 changes: 14 additions & 11 deletions cts/environment.py
Expand Up @@ -106,16 +106,19 @@ def has_key(self, key):
if key == "nodes":
return True

return self.data.has_key(key)
return key in self.data

def __getitem__(self, key):
if str(key) == "0":
raise ValueError("Bad call to 'foo in X', should reference 'foo in X.keys()' instead")

if key == "nodes":
return self.Nodes

elif key == "Name":
return self.get_stack_short()

elif self.data.has_key(key):
elif key in self.data:
return self.data[key]

else:
Expand Down Expand Up @@ -175,12 +178,12 @@ def set_stack(self, name):
self.data["Stack"] = "corosync (plugin v0)"

else:
print("Unknown stack: "+name)
raise ValueError("Unknown stack: "+name)
sys.exit(1)

def get_stack_short(self):
# Create the Cluster Manager object
if not self.data.has_key("Stack"):
if not "Stack" in self.data:
return "unknown"

elif self.data["Stack"] == "heartbeat":
Expand All @@ -202,26 +205,26 @@ def get_stack_short(self):
return "crm-plugin-v0"

else:
LogFactory().log("Unknown stack: "+self.data["stack"])
sys.exit(1)
LogFactory().log("Unknown stack: "+self["stack"])
raise ValueError("Unknown stack: "+self["stack"])

def detect_syslog(self):
# Detect syslog variant
if not self.has_key("syslogd"):
if not "syslogd" in self.data:
if self["have_systemd"]:
# Systemd
self["syslogd"] = self.rsh(self.target, "systemctl list-units | grep syslog.*\.service.*active.*running | sed 's:.service.*::'", stdout=1).strip()
else:
# SYS-V
self["syslogd"] = self.rsh(self.target, "chkconfig --list | grep syslog.*on | awk '{print $1}' | head -n 1", stdout=1).strip()

if not self.has_key("syslogd") or not self["syslogd"]:
if not "syslogd" in self.data or not self["syslogd"]:
# default
self["syslogd"] = "rsyslog"

def detect_at_boot(self):
# Detect if the cluster starts at boot
if not self.has_key("at-boot"):
if not "at-boot" in self.data:
atboot = 0

if self["have_systemd"]:
Expand All @@ -237,7 +240,7 @@ def detect_at_boot(self):

def detect_ip_offset(self):
# Try to determin an offset for IPaddr resources
if self["CIBResource"] and not self.has_key("IPBase"):
if self["CIBResource"] and not "IPBase" in self.data:
network=self.rsh(self.target, "ip addr | grep inet | grep -v -e link -e inet6 -e '/32' -e ' lo' | awk '{print $2}'", stdout=1).strip()
self["IPBase"] = self.rsh(self.target, "nmap -sn -n %s | grep 'scan report' | awk '{print $NF}' | sed 's:(::' | sed 's:)::' | sort -V | tail -n 1" % network, stdout=1).strip()
if not self["IPBase"]:
Expand Down Expand Up @@ -276,7 +279,7 @@ def discover(self):
break;
self["cts-master"] = master

if not self.has_key("have_systemd"):
if not "have_systemd" in self.data:
self["have_systemd"] = not self.rsh(self.target, "systemctl list-units")

self.detect_syslog()
Expand Down
8 changes: 4 additions & 4 deletions cts/patterns.py
Expand Up @@ -67,7 +67,7 @@ def __init__(self, name):
}

def get_component(self, key):
if self.components.has_key(key):
if key in self.components:
return self.components[key]
print("Unknown component '%s' for %s" % (key, self.name))
return []
Expand All @@ -87,9 +87,9 @@ def get_patterns(self, key):
def __getitem__(self, key):
if key == "Name":
return self.name
elif self.commands.has_key(key):
elif key in self.commands:
return self.commands[key]
elif self.search.has_key(key):
elif key in self.search:
return self.search[key]
else:
print("Unknown template '%s' for %s" % (key, self.name))
Expand Down Expand Up @@ -489,7 +489,7 @@ def __init__(self, name=None):
crm_mcp_docker(name)

def get_variant(self, variant):
if patternvariants.has_key(variant):
if variant in patternvariants:
return patternvariants[variant]
print("defaulting to crm-base for %s" % variant)
return self.base
Expand Down
2 changes: 1 addition & 1 deletion cts/watcher.py
Expand Up @@ -158,7 +158,7 @@ def __init__(self, filename, host=None, name=None):
SearchObj.__init__(self, filename, host, name)

if host is not None:
if not has_log_watcher.has_key(host):
if not host in has_log_watcher:

global log_watcher
global log_watcher_bin
Expand Down

0 comments on commit 514bffa

Please sign in to comment.