Merge "Add SRIOV support"
[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: %s", actionParameter)
44             LOG.debug("inject parameter values are: %s",
45                       list(actionParameter.values()))
46             l = list(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: %s", rollbackParameter)
53             LOG.debug("recover parameter values are: %s",
54                       list(rollbackParameter.values()))
55             l = list(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: %s", exit_status)
81         if exit_status == 0:
82             LOG.debug("success,the inject_fault's output is: %s", stdout)
83         else:
84             LOG.error(
85                 "the inject_fault's error, stdout:%s, stderr:%s",
86                 stdout, stderr)
87
88     def recover(self):
89         if "rollback_parameter" in self._config:
90             LOG.debug("the shell command is: %s", self.rollback_param)
91             with open(self.recovery_script, "r") as stdin_file:
92                 exit_status, stdout, stderr = self.connection.execute(
93                     self.rollback_param,
94                     stdin=stdin_file)
95         else:
96             with open(self.recovery_script, "r") as stdin_file:
97                 exit_status, stdout, stderr = self.connection.execute(
98                     "/bin/bash -s ",
99                     stdin=stdin_file)