Merge "Point to user guide to get started on VNF testing"
[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
28         self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
29         self.connection.wait(timeout=600)
30         LOG.debug("ssh host success!")
31
32         self.key = self._config['key']
33         self.attack_key = self._config['attack_key']
34
35         if "action_parameter" in self._config:
36             actionParameter = self._config['action_parameter']
37             str = util.buildshellparams(actionParameter)
38             LOG.debug("inject parameter is: %s", actionParameter)
39             LOG.debug("inject parameter values are: %s",
40                       list(actionParameter.values()))
41             l = list(actionParameter.values())
42             self.action_param = str.format(*l)
43
44         if "rollback_parameter" in self._config:
45             rollbackParameter = self._config['rollback_parameter']
46             str = util.buildshellparams(rollbackParameter)
47             LOG.debug("recover parameter is: %s", rollbackParameter)
48             LOG.debug("recover parameter values are: %s",
49                       list(rollbackParameter.values()))
50             l = list(rollbackParameter.values())
51             self.rollback_param = str.format(*l)
52
53         self.fault_cfg = BaseAttacker.attacker_cfgs.get(self.attack_key)
54         self.inject_script = self.get_script_fullpath(
55             self.fault_cfg['inject_script'])
56         self.recovery_script = self.get_script_fullpath(
57             self.fault_cfg['recovery_script'])
58
59     def inject_fault(self):
60         LOG.debug("%s starting inject!", self.key)
61         LOG.debug("the inject_script path:%s", self.inject_script)
62
63         if "action_parameter" in self._config:
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                     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                     "/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         else:
79             LOG.error(
80                 "the inject_fault's error, stdout:%s, stderr:%s",
81                 stdout, stderr)
82
83     def recover(self):
84         if "rollback_parameter" in self._config:
85             LOG.debug("the shell command is: %s", self.rollback_param)
86             with open(self.recovery_script, "r") as stdin_file:
87                 exit_status, stdout, stderr = self.connection.execute(
88                     self.rollback_param,
89                     stdin=stdin_file)
90         else:
91             with open(self.recovery_script, "r") as stdin_file:
92                 exit_status, stdout, stderr = self.connection.execute(
93                     "/bin/bash -s ",
94                     stdin=stdin_file)