Bugfix: Local Openstack Operation in HA test frameworks 47/34247/1 danube.2.0 danube.2.RC1
authortjuyinkanglin <14_ykl@tongji.edu.cn>
Thu, 4 May 2017 07:58:55 +0000 (15:58 +0800)
committerJing Lu <lvjing5@huawei.com>
Thu, 4 May 2017 12:59:21 +0000 (12:59 +0000)
JIRA: YARDSTICK-635

Change-Id: Ic27517714db9325e7a3b1ef623c49af61c36b2b5
Signed-off-by: tjuyinkanglin <14_ykl@tongji.edu.cn>
(cherry picked from commit 2fb95fbb9380ea7ce92dcb32cd2f0789b9f91bdc)

tests/opnfv/test_cases/opnfv_yardstick_tc052.yaml
tests/unit/benchmark/scenarios/availability/test_util.py [new file with mode: 0644]
yardstick/benchmark/scenarios/availability/operation/operation_general.py
yardstick/benchmark/scenarios/availability/result_checker/result_checker_general.py
yardstick/benchmark/scenarios/availability/util.py

index 6e060b1..4254e79 100644 (file)
@@ -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 (file)
index 0000000..bb0e6bc
--- /dev/null
@@ -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)
index 49c63cc..6ec8601 100644 (file)
@@ -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))
index ff6017b..0f2e382 100644 (file)
@@ -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)
 
index 2addef8..eadbfa5 100644 (file)
@@ -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