Merge "New reliability/availability testcase - IP datagram error rate and etc."
[yardstick.git] / yardstick / benchmark / scenarios / availability / attacker / attacker_general.py
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
10 import logging
11
12 import yardstick.ssh as ssh
13 from yardstick.benchmark.scenarios.availability import util
14 from yardstick.benchmark.scenarios.availability.attacker.baseattacker import \
15     BaseAttacker
16
17 LOG = logging.getLogger(__name__)
18
19
20 class GeneralAttacker(BaseAttacker):
21
22     __attacker_type__ = 'general-attacker'
23
24     def setup(self):
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")
31
32         self.connection = ssh.SSH(user, ip, key_filename=key_filename,
33                                   port=ssh_port)
34         self.connection.wait(timeout=600)
35         LOG.debug("ssh host success!")
36
37         self.key = self._config['key']
38         self.attack_key = self._config['attack_key']
39
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)
48
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)
57
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'])
63
64     def inject_fault(self):
65         LOG.debug("%s starting inject!", self.key)
66         LOG.debug("the inject_script path:%s", self.inject_script)
67
68         if "action_parameter" in self._config:
69             LOG.debug("the shell command is: %s", self.action_param)
70             with open(self.inject_script, "r") as stdin_file:
71                 exit_status, stdout, stderr = self.connection.execute(
72                     self.action_param,
73                     stdin=stdin_file)
74         else:
75             with open(self.inject_script, "r") as stdin_file:
76                 exit_status, stdout, stderr = self.connection.execute(
77                     "/bin/bash -s ",
78                     stdin=stdin_file)
79
80         LOG.debug("the inject_fault's exit status is: {0}".format(exit_status))
81         if exit_status == 0:
82             LOG.debug("success,the inject_fault's output is: {0}"
83                       .format(stdout))
84         else:
85             LOG.error(
86                 "the inject_fault's error, stdout:%s, stderr:%s",
87                 stdout, stderr)
88
89     def recover(self):
90         if "rollback_parameter" in self._config:
91             LOG.debug("the shell command is: %s", self.rollback_param)
92             with open(self.recovery_script, "r") as stdin_file:
93                 exit_status, stdout, stderr = self.connection.execute(
94                     self.rollback_param,
95                     stdin=stdin_file)
96         else:
97             with open(self.recovery_script, "r") as stdin_file:
98                 exit_status, stdout, stderr = self.connection.execute(
99                     "/bin/bash -s ",
100                     stdin=stdin_file)