Merge "Disable spoof check on vfs in sriov setup"
[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 from yardstick.benchmark.scenarios.availability.util \
17     import read_stdout_item, build_shell_command
18
19 LOG = logging.getLogger(__name__)
20
21
22 class GeneralAttacker(BaseAttacker):
23
24     __attacker_type__ = 'general-attacker'
25
26     def setup(self):
27         LOG.debug("config:%s context:%s", self._config, self._context)
28         host = self._context.get(self._config['host'], None)
29
30         self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
31         self.connection.wait(timeout=600)
32         LOG.debug("ssh host success!")
33
34         self.key = self._config['key']
35         self.attack_key = self._config['attack_key']
36
37         if "action_parameter" in self._config:
38             self.actionParameter_config = self._config['action_parameter']
39
40         if "rollback_parameter" in self._config:
41             rollbackParameter = self._config['rollback_parameter']
42             str = util.buildshellparams(rollbackParameter)
43             LOG.debug("recover parameter is: %s", rollbackParameter)
44             LOG.debug("recover parameter values are: %s",
45                       list(rollbackParameter.values()))
46             l = list(rollbackParameter.values())
47             self.rollback_param = str.format(*l)
48
49         self.fault_cfg = BaseAttacker.attacker_cfgs.get(self.attack_key)
50         self.inject_script = self.get_script_fullpath(
51             self.fault_cfg['inject_script'])
52         self.recovery_script = self.get_script_fullpath(
53             self.fault_cfg['recovery_script'])
54
55     def inject_fault(self):
56         LOG.debug("%s starting inject!", self.key)
57         LOG.debug("the inject_script path:%s", self.inject_script)
58         if "action_parameter" in self._config:
59             self.action_param = \
60                 build_shell_command(
61                     self.actionParameter_config,
62                     bool(self.connection),
63                     self.intermediate_variables)
64             LOG.debug("the shell command is: %s", self.action_param)
65             with open(self.inject_script, "r") as stdin_file:
66                 exit_status, stdout, stderr = self.connection.execute(
67                     "sudo {}".format(self.action_param),
68                     stdin=stdin_file)
69         else:
70             with open(self.inject_script, "r") as stdin_file:
71                 exit_status, stdout, stderr = self.connection.execute(
72                     "sudo /bin/bash -s ",
73                     stdin=stdin_file)
74
75         LOG.debug("the inject_fault's exit status is: %s", exit_status)
76         if exit_status == 0:
77             LOG.debug("success,the inject_fault's output is: %s", stdout)
78             if "return_parameter" in self._config:
79                 returnParameter = self._config['return_parameter']
80                 for key, item in returnParameter.items():
81                     value = read_stdout_item(stdout, key)
82                     LOG.debug("intermediate variables %s: %s", item, value)
83                     self.intermediate_variables[item] = value
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                     "sudo {}".format(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                     "sudo /bin/bash -s ",
100                     stdin=stdin_file)