Merge "Add a new runner to test end-to-end fast data path"
[yardstick.git] / yardstick / benchmark / scenarios / availability / attacker / attacker_process.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
3 #
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.attacker.baseattacker import \
14     BaseAttacker
15
16 LOG = logging.getLogger(__name__)
17
18
19 class ProcessAttacker(BaseAttacker):
20
21     __attacker_type__ = 'kill-process'
22
23     def setup(self):
24         LOG.debug("config:%s context:%s", self._config, self._context)
25         host = self._context.get(self._config['host'], None)
26         ip = host.get("ip", None)
27         user = host.get("user", "root")
28         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
29         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
30
31         self.connection = ssh.SSH(user, ip, key_filename=key_filename,
32                                   port=ssh_port)
33         self.connection.wait(timeout=600)
34         LOG.debug("ssh host success!")
35
36         self.service_name = self._config['process_name']
37         self.fault_cfg = BaseAttacker.attacker_cfgs.get('kill-process')
38
39         self.check_script = self.get_script_fullpath(
40             self.fault_cfg['check_script'])
41         self.inject_script = self.get_script_fullpath(
42             self.fault_cfg['inject_script'])
43         self.recovery_script = self.get_script_fullpath(
44             self.fault_cfg['recovery_script'])
45
46         if self.check():
47             self.setup_done = True
48
49     def check(self):
50         with open(self.check_script, "r") as stdin_file:
51             exit_status, stdout, stderr = self.connection.execute(
52                 "/bin/sh -s {0}".format(self.service_name),
53                 stdin=stdin_file)
54
55         if stdout and "running" in stdout:
56             LOG.info("check the envrioment success!")
57             return True
58         else:
59             LOG.error(
60                 "the host envrioment is error, stdout:%s, stderr:%s",
61                 stdout, stderr)
62         return False
63
64     def inject_fault(self):
65         with open(self.inject_script, "r") as stdin_file:
66             exit_status, stdout, stderr = self.connection.execute(
67                 "/bin/sh -s {0}".format(self.service_name),
68                 stdin=stdin_file)
69
70     def recover(self):
71         with open(self.recovery_script, "r") as stdin_file:
72             exit_status, stdout, stderr = self.connection.execute(
73                 "/bin/sh -s {0} ".format(self.service_name),
74                 stdin=stdin_file)