1 ##############################################################################
2 # Copyright (c) 2016 Juan Qiu and others
3 # juan_ qiu@tongji.edu.cn
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
12 import yardstick.ssh as ssh
13 from yardstick.benchmark.scenarios.availability import util
14 from yardstick.benchmark.scenarios.availability.attacker.baseattacker import \
17 LOG = logging.getLogger(__name__)
20 class GeneralAttacker(BaseAttacker):
22 __attacker_type__ = 'general-attacker'
25 LOG.debug("config:%s context:%s", self._config, self._context)
26 host = self._context.get(self._config['host'], None)
27 ip = host.get("ip", None)
28 user = host.get("user", "root")
29 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
30 key_filename = host.get("key_filename", "~/.ssh/id_rsa")
32 self.connection = ssh.SSH(user, ip, key_filename=key_filename,
34 self.connection.wait(timeout=600)
35 LOG.debug("ssh host success!")
37 self.key = self._config['key']
38 self.attack_key = self._config['attack_key']
40 if "action_parameter" in self._config:
41 actionParameter = self._config['action_parameter']
42 str = util.buildshellparams(actionParameter)
43 LOG.debug("inject parameter is: {0}".format(actionParameter))
44 LOG.debug("inject parameter values are: {0}"
45 .format(list(actionParameter.values())))
46 l = list(item for item in actionParameter.values())
47 self.action_param = str.format(*l)
49 if "rollback_parameter" in self._config:
50 rollbackParameter = self._config['rollback_parameter']
51 str = util.buildshellparams(rollbackParameter)
52 LOG.debug("recover parameter is: {0}".format(rollbackParameter))
53 LOG.debug("recover parameter values are: {0}".
54 format(list(rollbackParameter.values())))
55 l = list(item for item in rollbackParameter.values())
56 self.rollback_param = str.format(*l)
58 self.fault_cfg = BaseAttacker.attacker_cfgs.get(self.attack_key)
59 self.inject_script = self.get_script_fullpath(
60 self.fault_cfg['inject_script'])
61 self.recovery_script = self.get_script_fullpath(
62 self.fault_cfg['recovery_script'])
64 def inject_fault(self):
65 LOG.debug("{0} starting inject!".format(self.key))
66 LOG.debug("the inject_script path:{0}".format(self.inject_script))
68 if "action_parameter" in self._config:
69 LOG.debug("the shell command is: {0}".format(self.action_param))
70 with open(self.inject_script, "r") as stdin_file:
71 exit_status, stdout, stderr = self.connection.execute(
75 with open(self.inject_script, "r") as stdin_file:
76 exit_status, stdout, stderr = self.connection.execute(
80 LOG.debug("the inject_fault's exit status is: {0}".format(exit_status))
82 LOG.debug("success,the inject_fault's output is: {0}"
86 "the inject_fault's error, stdout:%s, stderr:%s",
90 if "rollback_parameter" in self._config:
91 LOG.debug("the shell command is: {0}".format(self.rollback_param))
92 with open(self.recovery_script, "r") as stdin_file:
93 exit_status, stdout, stderr = self.connection.execute(
97 with open(self.recovery_script, "r") as stdin_file:
98 exit_status, stdout, stderr = self.connection.execute(