summaryrefslogtreecommitdiffstats
path: root/libs
diff options
context:
space:
mode:
authorShwetha-H-Panduranga <shwetha@gluster.com>2011-12-20 18:04:44 +0530
committerShwetha-H-Panduranga <shwetha@gluster.com>2011-12-20 18:04:44 +0530
commita36ddc28bded0507d90d0ffc9d0a04831d13f47f (patch)
treef000f8c4c7fd7f7dc05efaa78e64522efc3b83a0 /libs
parent4c595fd98ae5daf31ae35eb24e1cdcef1fc156f3 (diff)
Changes made to '_substitute_value_for_variables' function. The function now handles substitution of variable values. (Ex:- <volume1.volumename> replaces volumename of volume1. The variable value is the value defined in testenv.cfg for that testunit). The function returns commands wiith values substituted for variables. The function 'execute_command' now returns 'output'(dictionary output from ssh.executecommand)
Diffstat (limited to 'libs')
-rw-r--r--libs/utils/hostutils.py93
1 files changed, 56 insertions, 37 deletions
diff --git a/libs/utils/hostutils.py b/libs/utils/hostutils.py
index 9992bb6..32741ea 100644
--- a/libs/utils/hostutils.py
+++ b/libs/utils/hostutils.py
@@ -10,6 +10,7 @@ Supported Wrappers:
"""
import re
+from collections import namedtuple
import atfutils
from atfglobals import GlobalObj
import pdb
@@ -147,63 +148,81 @@ def execute_command(hostkey, command, commandInput=None):
logger.error("SSH Connection Not established to host '%s' "
% hostkey)
return 1
- new_command = _substitute_value_for_variables(hostkey, command)
+ new_command = _substitute_value_for_variables(command)
logger.debug('%s: Executing Command: %s' % (hostkey, command))
output = host_connection.executecommand(new_command, commandInput)
- return_status = atfutils.assert_success(**output)
atfutils.print_stdout(output['stdoutdata'])
atfutils.print_stderr(output['stderrdata'])
- return return_status
+ return output
-def _substitute_value_for_variables(hostkey, command):
+def _substitute_value_for_variables(command):
"""
"""
logger = GlobalObj.getLoggerObj()
- pattern_for_variables = re.compile("<[a-z]+\d*>")
- pattern_for_hosts = re.compile('(server|client|master)*')
- variables_to_replace = []
- replace_values = {}
+ env = GlobalObj.getTestenvObj()
new_command = command
- Functions_Map = {
- "server" : "getserver",
- "client" : "getclient",
- "master" : "getmaster"
- }
+ pattern_for_variables = re.compile("<[a-z]+\d*\.[a-z]*>")
+ name_to_functions_mapping = {
+ "export" : "getExportdir",
+ "server" : "getServer",
+ "brick" : "getBrick",
+ "volume" : "getVolume",
+ "client" : "getClient",
+ "mountdevice" : "getMountDevice",
+ "mount" : "getMount"}
+ name_pattern = re.compile('(export|server|brick|volume|client|mountdevice|mount)*')
+
+ variable_to_replace_named_tuple = namedtuple("Vars", ["actual_var",
+ "name",
+ "option"])
+ variables_to_replace = []
+
variables = pattern_for_variables.findall(command)
- host = None
-
if not variables:
return new_command
else:
- result = pattern_for_hosts.match(hostkey.lower()).group(0)
- if result:
- funcname = Functions_Map[result]
- function = getattr(env, funcname)
- if re.match("master", result):
- host = function()
- else:
- host = function(hostkey)
-
- if not host:
- logger.error("No Host to execute the command\n")
- return 1
+ active_volume = env.getActiveVolume()
for variable in variables:
if variable not in variables_to_replace:
- variables_to_replace.append(variable.strip("<>"))
-
+ name, option = (variable.strip("<>")).split(".")
+ variables_to_replace.append(variable_to_replace_named_tuple(variable,
+ name,
+ option))
for variable in variables_to_replace:
- value = host.__getattribute__(variable)
- replace_values[variable] = value
-
- for key in replace_values.keys():
- value = replace_values[key]
- key = "<" + key + ">"
- pattern = re.compile(key)
- new_command = pattern.sub(value, new_command)
+ actual_var = variable.actual_var
+ name = variable.name
+ option = variable.option
+ matched_obj = name_pattern.match(name.lower())
+ result = matched_obj.group(0)
+ if result is '':
+ continue
+ else:
+ funcname = name_to_functions_mapping[result]
+ function = getattr(env, funcname)
+ obj_value = function(name)
+ if obj_value is None:
+ continue
+ else:
+ try:
+ option_value = obj_value.__getattribute__(option)
+
+ except AttributeError:
+ logger.error("Attribute Error: %s object has no attribute '%s'" %
+ (name, option))
+ continue
+ else:
+ actual_var_pattern = re.compile(actual_var)
+ new_command = actual_var_pattern.sub(option_value,
+ new_command)
+
+ variables = pattern_for_variables.findall(command)
+ if not variables:
+ new_command = ""
+ return new_command
return new_command