diff options
author | wym_libra <yimin.wang@huawei.com> | 2015-12-31 20:02:09 +0800 |
---|---|---|
committer | qi liang <liangqi1@huawei.com> | 2016-01-10 03:22:44 +0000 |
commit | 4f4edd840823ff6a0151e3f5220241183e27e560 (patch) | |
tree | c483de22d9abbf14f7ebf932170f666d89e6035a /yardstick/benchmark/scenarios/availability/attacker | |
parent | 2733defda816a84d2b9c2e361a5970b9de4923f4 (diff) |
The secondi HA test case-shutdown controller
1) add "attacker_baremetal.py" for fault injection
2) modify the monitor to excute on remote node after ssh connection
3) move all shell scripts together
JIRA: YARDSTICK-182
Change-Id: Ibb9dc908224ddb8b99a0140b75c1a046503f6dfb
Signed-off-by: wym_libra <yimin.wang@huawei.com>
Diffstat (limited to 'yardstick/benchmark/scenarios/availability/attacker')
6 files changed, 130 insertions, 67 deletions
diff --git a/yardstick/benchmark/scenarios/availability/attacker/attacker_baremetal.py b/yardstick/benchmark/scenarios/availability/attacker/attacker_baremetal.py new file mode 100644 index 000000000..b35869d07 --- /dev/null +++ b/yardstick/benchmark/scenarios/availability/attacker/attacker_baremetal.py @@ -0,0 +1,129 @@ +############################################################################## +# Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others +# +# 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 +############################################################################## +import logging +import traceback +import subprocess +import yardstick.ssh as ssh +from baseattacker import BaseAttacker + +LOG = logging.getLogger(__name__) + + +def _execute_shell_command(command, stdin=None): + '''execute shell script with error handling''' + exitcode = 0 + output = [] + try: + output = subprocess.check_output(command, stdin=stdin, 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 + + +class BaremetalAttacker(BaseAttacker): + + __attacker_type__ = 'bare-metal-down' + + def setup(self): + LOG.debug("config:%s context:%s" % (self._config, self._context)) + host = self._context.get(self._config['host'], None) + ip = host.get("ip", None) + user = host.get("user", "root") + key_filename = host.get("key_filename", "~/.ssh/id_rsa") + + self.connection = ssh.SSH(user, ip, key_filename=key_filename) + self.connection.wait(timeout=600) + LOG.debug("ssh host success!") + self.host_ip = ip + + self.ipmi_ip = host.get("ipmi_ip", None) + self.ipmi_user = host.get("ipmi_user", "root") + self.ipmi_pwd = host.get("ipmi_pwd", None) + + self.fault_cfg = BaseAttacker.attacker_cfgs.get('bare-metal-down') + self.check_script = self.get_script_fullpath( + self.fault_cfg['check_script']) + self.recovery_script = self.get_script_fullpath( + self.fault_cfg['recovery_script']) + + if self.check(): + self.setup_done = True + + def check(self): + exit_status, stdout, stderr = self.connection.execute( + "/bin/sh -s {0} -W 10".format(self.host_ip), + stdin=open(self.check_script, "r")) + + LOG.debug("check ret: %s out:%s err:%s" % + (exit_status, stdout, stderr)) + if not stdout or "running" not in stdout: + LOG.info("the host (ipmi_ip:%s) is not running!" % self.ipmi_ip) + return False + + return True + + def inject_fault(self): + exit_status, stdout, stderr = self.connection.execute( + "shutdown -h now") + LOG.debug("inject fault ret: %s out:%s err:%s" % + (exit_status, stdout, stderr)) + if not exit_status: + LOG.info("inject fault success") + + def recover(self): + jump_host_name = self._config.get("jump_host", None) + self.jump_connection = None + if jump_host_name is not None: + host = self._context.get(jump_host_name, None) + ip = host.get("ip", None) + user = host.get("user", "root") + pwd = host.get("pwd", None) + + LOG.debug("jump_host ip:%s user:%s" % (ip, user)) + self.jump_connection = ssh.SSH(user, ip, password=pwd) + self.jump_connection.wait(timeout=600) + LOG.debug("ssh jump host success!") + + if self.jump_connection is not None: + exit_status, stdout, stderr = self.jump_connection.execute( + "/bin/bash -s {0} {1} {2} {3}".format( + self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"), + stdin=open(self.recovery_script, "r")) + else: + exit_status, stdout = _execute_shell_command( + "/bin/bash -s {0} {1} {2} {3}".format( + self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"), + stdin=open(self.recovery_script, "r")) + + +def _test(): # pragma: no cover + host = { + "ipmi_ip": "10.20.0.5", + "ipmi_user": "root", + "ipmi_pwd": "123456", + "ip": "10.20.0.5", + "user": "root", + "key_filename": "/root/.ssh/id_rsa" + } + context = {"node1": host} + attacker_cfg = { + 'fault_type': 'bear-metal-down', + 'host': 'node1', + } + ins = BaremetalAttacker(attacker_cfg, context) + ins.setup() + ins.inject_fault() + + +if __name__ == '__main__': # pragma: no cover + _test() diff --git a/yardstick/benchmark/scenarios/availability/attacker/attacker_conf.yaml b/yardstick/benchmark/scenarios/availability/attacker/attacker_conf.yaml deleted file mode 100644 index 44f06038b..000000000 --- a/yardstick/benchmark/scenarios/availability/attacker/attacker_conf.yaml +++ /dev/null @@ -1,9 +0,0 @@ ---- -# sample config file for ha test -# -schema: "yardstick:task:0.1" - -kill-process: - inject_script: scripts/stop_service.bash - recovery_script: scripts/start_service.bash - check_script: scripts/check_service.bash diff --git a/yardstick/benchmark/scenarios/availability/attacker/baseattacker.py b/yardstick/benchmark/scenarios/availability/attacker/baseattacker.py index ddaf09969..a1c6999e5 100644 --- a/yardstick/benchmark/scenarios/availability/attacker/baseattacker.py +++ b/yardstick/benchmark/scenarios/availability/attacker/baseattacker.py @@ -16,7 +16,7 @@ import yardstick.common.utils as utils LOG = logging.getLogger(__name__) attacker_conf_path = pkg_resources.resource_filename( - "yardstick.benchmark.scenarios.availability.attacker", + "yardstick.benchmark.scenarios.availability", "attacker_conf.yaml") diff --git a/yardstick/benchmark/scenarios/availability/attacker/scripts/check_service.bash b/yardstick/benchmark/scenarios/availability/attacker/scripts/check_service.bash deleted file mode 100755 index cc898a859..000000000 --- a/yardstick/benchmark/scenarios/availability/attacker/scripts/check_service.bash +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -############################################################################## -# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others. -# -# 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 -############################################################################## - -# check the status of a service - -set -e - -service_name=$1 - -service $service_name status diff --git a/yardstick/benchmark/scenarios/availability/attacker/scripts/start_service.bash b/yardstick/benchmark/scenarios/availability/attacker/scripts/start_service.bash deleted file mode 100755 index c1bf8b7eb..000000000 --- a/yardstick/benchmark/scenarios/availability/attacker/scripts/start_service.bash +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -############################################################################## -# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others. -# -# 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 -############################################################################## - -# Start a service and check the service is started - -set -e - -service_name=$1 - -service $service_name start diff --git a/yardstick/benchmark/scenarios/availability/attacker/scripts/stop_service.bash b/yardstick/benchmark/scenarios/availability/attacker/scripts/stop_service.bash deleted file mode 100755 index a8901784e..000000000 --- a/yardstick/benchmark/scenarios/availability/attacker/scripts/stop_service.bash +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -############################################################################## -# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others. -# -# 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 -############################################################################## - -# Stop a service and check the service is stoped - -set -e - -service_name=$1 - -service $service_name stop - -# TODO -# check the service status |