Add intermediate variable for HA test 07/35307/7
authorqiujuan <juan_qiu@tongji.edu.cn>
Thu, 25 May 2017 05:18:28 +0000 (13:18 +0800)
committerJing Lu <lvjing5@huawei.com>
Tue, 25 Jul 2017 06:10:56 +0000 (06:10 +0000)
JIRA: YARDSTICK-397

Change-Id: I3489893caa5b8194b63cb844325ec0b2c554aecc
Signed-off-by: qiujuan <juan_qiu@tongji.edu.cn>
tests/unit/benchmark/scenarios/availability/test_util.py
yardstick/benchmark/scenarios/availability/actionplayers.py
yardstick/benchmark/scenarios/availability/director.py
yardstick/benchmark/scenarios/availability/operation/baseoperation.py
yardstick/benchmark/scenarios/availability/operation/operation_general.py
yardstick/benchmark/scenarios/availability/scenario_general.py
yardstick/benchmark/scenarios/availability/util.py

index bb0e6bc..2e4fff4 100644 (file)
@@ -19,6 +19,25 @@ from yardstick.benchmark.scenarios.availability import util
 @mock.patch('yardstick.benchmark.scenarios.availability.util.subprocess')
 class ExecuteShellTestCase(unittest.TestCase):
 
+    def setUp(self):
+        self.param_config = {'serviceName': '$serviceName', 'value': 1}
+        self.intermediate_variables = {'$serviceName': 'nova-api'}
+        self.std_output = '| id       | 1                     |'
+        self.cmd_config = {'cmd':'ls','param':'-a'}
+
+    def test_util_build_command_shell(self,mock_subprocess):
+        result = util.build_shell_command(self.param_config, True,
+                                          self.intermediate_variables)
+        self.assertEqual("nova-api" in result, True)
+
+    def test_read_stdout_item(self,mock_subprocess):
+        result = util.read_stdout_item(self.std_output,'id')
+        self.assertEquals('1',result)
+
+    def test_buildshellparams(self,mock_subprocess):
+        result = util.buildshellparams(self.cmd_config,True)
+        self.assertEquals('/bin/bash -s {0} {1}', result)
+
     def test__fun_execute_shell_command_successful(self, mock_subprocess):
         cmd = "env"
         mock_subprocess.check_output.return_value = (0, 'unittest')
index 4206264..c5e199b 100644 (file)
@@ -29,8 +29,10 @@ class AttackerPlayer(ActionPlayer):
 
 class OperationPlayer(ActionPlayer):
 
-    def __init__(self, operation):
+    def __init__(self, operation, intermediate_variables):
         self.underlyingOperation = operation
+        self.underlyingOperation.intermediate_variables \
+            = intermediate_variables
 
     def action(self):
         self.underlyingOperation.run()
index e0d05eb..c9187c3 100644 (file)
@@ -65,7 +65,9 @@ class Director(object):
             self.resultCheckerMgr = baseresultchecker.ResultCheckerMgr()
             self.resultCheckerMgr.init_ResultChecker(result_check_cfgs, nodes)
 
-    def createActionPlayer(self, type, key):
+    def createActionPlayer(self, type, key, intermediate_variables=None):
+        if intermediate_variables is None:
+            intermediate_variables = {}
         LOG.debug(
             "the type of current action is %s, the key is %s", type, key)
         if type == ActionType.ATTACKER:
@@ -76,7 +78,8 @@ class Director(object):
             return actionplayers.ResultCheckerPlayer(
                 self.resultCheckerMgr[key])
         if type == ActionType.OPERATION:
-            return actionplayers.OperationPlayer(self.operationMgr[key])
+            return actionplayers.OperationPlayer(self.operationMgr[key],
+                                                 intermediate_variables)
         LOG.debug("something run when creatactionplayer")
 
     def createActionRollbacker(self, type, key):
index be286b8..88ca9e2 100644 (file)
@@ -58,6 +58,7 @@ class BaseOperation(object):
         self.key = ''
         self._config = config
         self._context = context
+        self.intermediate_variables = {}
 
     @staticmethod
     def get_operation_cls(type):
index 8fd387e..af1ae74 100644 (file)
@@ -15,7 +15,8 @@ from yardstick.benchmark.scenarios.availability.operation.baseoperation \
 
 import yardstick.ssh as ssh
 from yardstick.benchmark.scenarios.availability.util \
-    import buildshellparams, execute_shell_command
+    import buildshellparams, execute_shell_command, \
+    read_stdout_item, build_shell_command
 
 LOG = logging.getLogger(__name__)
 
@@ -39,11 +40,7 @@ class GeneralOperaion(BaseOperation):
         self.operation_key = self._config['operation_key']
 
         if "action_parameter" in self._config:
-            actionParameter = self._config['action_parameter']
-            str = buildshellparams(
-                actionParameter, True if self.connection else False)
-            l = list(item for item in actionParameter.values())
-            self.action_param = str.format(*l)
+            self.actionParameter_config = self._config['action_parameter']
 
         if "rollback_parameter" in self._config:
             rollbackParameter = self._config['rollback_parameter']
@@ -61,6 +58,11 @@ class GeneralOperaion(BaseOperation):
 
     def run(self):
         if "action_parameter" in self._config:
+            self.action_param = \
+                build_shell_command(
+                    self.actionParameter_config,
+                    True if self.connection else False,
+                    self.intermediate_variables)
             if self.connection:
                 with open(self.action_script, "r") as stdin_file:
                     exit_status, stdout, stderr = self.connection.execute(
@@ -83,6 +85,12 @@ class GeneralOperaion(BaseOperation):
 
         if exit_status == 0:
             LOG.debug("success,the operation's output is: %s", stdout)
+            if "return_parameter" in self._config:
+                returnParameter = self._config['return_parameter']
+                for key, item in returnParameter.items():
+                    value = read_stdout_item(stdout, key)
+                    LOG.debug("intermediate variables %s: %s", item, value)
+                    self.intermediate_variables[item] = value
         else:
             LOG.error(
                 "the operation's error, stdout:%s, stderr:%s",
index 28bec8a..17ad79f 100644 (file)
@@ -25,6 +25,7 @@ class ScenarioGeneral(base.Scenario):
             "scenario_cfg:%s context_cfg:%s", scenario_cfg, context_cfg)
         self.scenario_cfg = scenario_cfg
         self.context_cfg = context_cfg
+        self.intermediate_variables = {}
 
     def setup(self):
         self.director = Director(self.scenario_cfg, self.context_cfg)
@@ -38,7 +39,8 @@ class ScenarioGeneral(base.Scenario):
                 orderedSteps.index(step) + 1)
             try:
                 actionPlayer = self.director.createActionPlayer(
-                    step['actionType'], step['actionKey'])
+                    step['actionType'], step['actionKey'],
+                    self.intermediate_variables)
                 actionPlayer.action()
                 actionRollbacker = self.director.createActionRollbacker(
                     step['actionType'], step['actionKey'])
index eadbfa5..6fef622 100644 (file)
@@ -14,13 +14,8 @@ LOG = logging.getLogger(__name__)
 
 
 def buildshellparams(param, remote=True):
-    i = 0
-    values = []
     result = '/bin/bash -s' if remote else ''
-    for key in param.keys():
-        values.append(param[key])
-        result += " {%d}" % i
-        i = i + 1
+    result += "".join(" {%d}" % i for i in range(len(param)))
     return result
 
 
@@ -36,5 +31,29 @@ def execute_shell_command(command):
         output = traceback.format_exc()
         LOG.error("exec command '%s' error:\n ", command)
         LOG.error(traceback.format_exc())
-
     return exitcode, output
+
+PREFIX = '$'
+
+
+def build_shell_command(param_config, remote=True, intermediate_variables=None):
+    param_template = '/bin/bash -s' if remote else ''
+    if intermediate_variables:
+        for key, val in param_config.items():
+            if str(val).startswith(PREFIX):
+                try:
+                    param_config[key] = intermediate_variables[val]
+                except KeyError:
+                    pass
+    result = param_template + "".join(" {}".format(v) for v in param_config.values())
+    LOG.debug("THE RESULT OF build_shell_command IS: %s", result)
+    return result
+
+
+def read_stdout_item(stdout, key):
+    for item in stdout.splitlines():
+        if key in item:
+            attributes = item.split("|")
+            if attributes[1].lstrip().startswith(key):
+                return attributes[2].strip()
+    return None