diff options
5 files changed, 136 insertions, 42 deletions
diff --git a/tests/opnfv/test_cases/opnfv_yardstick_tc052.yaml b/tests/opnfv/test_cases/opnfv_yardstick_tc052.yaml index 6e060b15d..4254e79b6 100644 --- a/tests/opnfv/test_cases/opnfv_yardstick_tc052.yaml +++ b/tests/opnfv/test_cases/opnfv_yardstick_tc052.yaml @@ -40,7 +40,6 @@ scenarios: operation_type: "general-operation" key: "create-flavor" operation_key: "nova-create-flavor" - host: node1 action_parameter: flavorconfig: "test-001 test-001 100 1 1" rollback_parameter: @@ -50,7 +49,6 @@ scenarios: - checker_type: "general-result-checker" key: "check-flavor" - host: node1 checker_key: "nova-flavor-checker" expectedValue: "test-001" condition: "in" diff --git a/tests/unit/benchmark/scenarios/availability/test_util.py b/tests/unit/benchmark/scenarios/availability/test_util.py new file mode 100644 index 000000000..bb0e6bc79 --- /dev/null +++ b/tests/unit/benchmark/scenarios/availability/test_util.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +############################################################################## +# Copyright (c) 2016 Kanglin Yin and others +# 14_ykl@tongji.edu.cn +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +# Unittest for yardstick.benchmark.scenarios.availability.utils + +import mock +import unittest + +from yardstick.benchmark.scenarios.availability import util + +@mock.patch('yardstick.benchmark.scenarios.availability.util.subprocess') +class ExecuteShellTestCase(unittest.TestCase): + + def test__fun_execute_shell_command_successful(self, mock_subprocess): + cmd = "env" + mock_subprocess.check_output.return_value = (0, 'unittest') + exitcode, output = util.execute_shell_command(cmd) + self.assertEqual(exitcode, 0) + + def test__fun_execute_shell_command_fail_cmd_exception(self, mock_subprocess): + cmd = "env" + mock_subprocess.check_output.side_effect = RuntimeError + exitcode, output = util.execute_shell_command(cmd) + self.assertEqual(exitcode, -1) diff --git a/yardstick/benchmark/scenarios/availability/operation/operation_general.py b/yardstick/benchmark/scenarios/availability/operation/operation_general.py index 49c63cc75..6ec8601af 100644 --- a/yardstick/benchmark/scenarios/availability/operation/operation_general.py +++ b/yardstick/benchmark/scenarios/availability/operation/operation_general.py @@ -14,7 +14,8 @@ from yardstick.benchmark.scenarios.availability.operation.baseoperation \ BaseOperation import yardstick.ssh as ssh -from yardstick.benchmark.scenarios.availability.util import buildshellparams +from yardstick.benchmark.scenarios.availability.util \ + import buildshellparams, execute_shell_command LOG = logging.getLogger(__name__) @@ -25,24 +26,29 @@ class GeneralOperaion(BaseOperation): def setup(self): LOG.debug("config:%s context:%s", self._config, self._context) - host = self._context.get(self._config['host'], None) + host = self._context.get(self._config.get('host', None), None) - self.connection = ssh.SSH.from_node(host, defaults={"user": "root"}) - self.connection.wait(timeout=600) - LOG.debug("ssh host success!") + self.connection = None + if host: + self.connection = ssh.SSH.from_node( + host, defaults={"user": "root"}) + self.connection.wait(timeout=600) + LOG.debug("ssh host success!") self.key = self._config['key'] self.operation_key = self._config['operation_key'] if "action_parameter" in self._config: actionParameter = self._config['action_parameter'] - str = buildshellparams(actionParameter) + str = buildshellparams( + actionParameter, True if self.connection else False) l = list(item for item in actionParameter.values()) self.action_param = str.format(*l) if "rollback_parameter" in self._config: rollbackParameter = self._config['rollback_parameter'] - str = buildshellparams(rollbackParameter) + str = buildshellparams( + rollbackParameter, True if self.connection else False) l = list(item for item in rollbackParameter.values()) self.rollback_param = str.format(*l) @@ -55,15 +61,25 @@ class GeneralOperaion(BaseOperation): def run(self): if "action_parameter" in self._config: - with open(self.action_script, "r") as stdin_file: - exit_status, stdout, stderr = self.connection.execute( - self.action_param, - stdin=stdin_file) + if self.connection: + with open(self.action_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + self.action_param, + stdin=stdin_file) + else: + exit_status, stdout = \ + execute_shell_command( + "/bin/bash {0} {1}".format( + self.action_script, self.action_param)) else: - with open(self.action_script, "r") as stdin_file: - exit_status, stdout, stderr = self.connection.execute( - "/bin/sh -s ", - stdin=stdin_file) + if self.connection: + with open(self.action_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + "/bin/sh -s ", + stdin=stdin_file) + else: + exit_status, stdout = execute_shell_command( + "/bin/bash {0}".format(self.action_script)) if exit_status == 0: LOG.debug("success,the operation's output is: %s", stdout) @@ -74,12 +90,22 @@ class GeneralOperaion(BaseOperation): def rollback(self): if "rollback_parameter" in self._config: - with open(self.rollback_script, "r") as stdin_file: - exit_status, stdout, stderr = self.connection.execute( - self.rollback_param, - stdin=stdin_file) + if self.connection: + with open(self.rollback_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + self.rollback_param, + stdin=stdin_file) + else: + exit_status, stdout = \ + execute_shell_command( + "/bin/bash {0} {1}".format( + self.rollback_script, self.rollback_param)) else: - with open(self.rollback_script, "r") as stdin_file: - exit_status, stdout, stderr = self.connection.execute( - "/bin/sh -s ", - stdin=stdin_file) + if self.connection: + with open(self.rollback_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + "/bin/sh -s ", + stdin=stdin_file) + else: + exit_status, stdout = execute_shell_command( + "/bin/bash {0}".format(self.rollback_script)) diff --git a/yardstick/benchmark/scenarios/availability/result_checker/result_checker_general.py b/yardstick/benchmark/scenarios/availability/result_checker/result_checker_general.py index ff6017b88..0f2e382ae 100644 --- a/yardstick/benchmark/scenarios/availability/result_checker/result_checker_general.py +++ b/yardstick/benchmark/scenarios/availability/result_checker/result_checker_general.py @@ -9,13 +9,13 @@ from __future__ import absolute_import import logging - from yardstick.benchmark.scenarios.availability.result_checker \ .baseresultchecker import \ BaseResultChecker from yardstick.benchmark.scenarios.availability import Condition import yardstick.ssh as ssh -from yardstick.benchmark.scenarios.availability.util import buildshellparams +from yardstick.benchmark.scenarios.availability.util \ + import buildshellparams, execute_shell_command LOG = logging.getLogger(__name__) @@ -25,11 +25,14 @@ class GeneralResultChecker(BaseResultChecker): def setup(self): LOG.debug("config:%s context:%s", self._config, self._context) - host = self._context.get(self._config['host'], None) + host = self._context.get(self._config.get('host', None), None) - self.connection = ssh.SSH.from_node(host, defaults={"user": "root"}) - self.connection.wait(timeout=600) - LOG.debug("ssh host success!") + self.connection = None + if host: + self.connection = ssh.SSH.from_node( + host, defaults={"user": "root"}) + self.connection.wait(timeout=600) + LOG.debug("ssh host success!") self.key = self._config['key'] self.resultchecker_key = self._config['checker_key'] @@ -41,7 +44,8 @@ class GeneralResultChecker(BaseResultChecker): self.key = self._config['key'] if "parameter" in self._config: parameter = self._config['parameter'] - str = buildshellparams(parameter) + str = buildshellparams( + parameter, True if self.connection else False) l = list(item for item in parameter.values()) self.shell_cmd = str.format(*l) @@ -52,19 +56,32 @@ class GeneralResultChecker(BaseResultChecker): def verify(self): if "parameter" in self._config: - with open(self.verify_script, "r") as stdin_file: - exit_status, stdout, stderr = self.connection.execute( - self.shell_cmd, - stdin=stdin_file) + if self.connection: + with open(self.verify_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + self.shell_cmd, + stdin=stdin_file) + else: + exit_status, stdout = \ + execute_shell_command( + "/bin/bash {0} {1}".format( + self.verify_script, + self.rollback_param)) + LOG.debug("action script of the operation is: %s", self.verify_script) LOG.debug("action parameter the of operation is: %s", self.shell_cmd) else: - with open(self.verify_script, "r") as stdin_file: - exit_status, stdout, stderr = self.connection.execute( - "/bin/bash -s ", - stdin=stdin_file) + if self.connection: + with open(self.verify_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + "/bin/bash -s ", + stdin=stdin_file) + else: + exit_status, stdout = execute_shell_command( + "/bin/bash {0}".format(self.verify_script)) + LOG.debug("action script of the operation is: %s", self.verify_script) diff --git a/yardstick/benchmark/scenarios/availability/util.py b/yardstick/benchmark/scenarios/availability/util.py index 2addef8ef..eadbfa53b 100644 --- a/yardstick/benchmark/scenarios/availability/util.py +++ b/yardstick/benchmark/scenarios/availability/util.py @@ -6,14 +6,35 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import logging +import subprocess +import traceback +LOG = logging.getLogger(__name__) -def buildshellparams(param): + +def buildshellparams(param, remote=True): i = 0 values = [] - result = '/bin/bash -s' + result = '/bin/bash -s' if remote else '' for key in param.keys(): values.append(param[key]) result += " {%d}" % i i = i + 1 return result + + +def execute_shell_command(command): + """execute shell script with error handling""" + exitcode = 0 + output = [] + try: + LOG.debug("the command is: %s", command) + output = subprocess.check_output(command, shell=True) + except Exception: + exitcode = -1 + output = traceback.format_exc() + LOG.error("exec command '%s' error:\n ", command) + LOG.error(traceback.format_exc()) + + return exitcode, output |