Merge "Bugfix: debug should be default off"
[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 import logging
10
11 from baseattacker import BaseAttacker
12 import yardstick.ssh as ssh
13
14 LOG = logging.getLogger(__name__)
15
16
17 class ProcessAttacker(BaseAttacker):
18
19     __attacker_type__ = 'kill-process'
20
21     def setup(self):
22         LOG.debug("config:%s context:%s", self._config, self._context)
23         host = self._context.get(self._config['host'], None)
24         ip = host.get("ip", None)
25         user = host.get("user", "root")
26         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
27         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
28
29         self.connection = ssh.SSH(user, ip, key_filename=key_filename,
30                                   port=ssh_port)
31         self.connection.wait(timeout=600)
32         LOG.debug("ssh host success!")
33
34         self.service_name = self._config['process_name']
35         self.fault_cfg = BaseAttacker.attacker_cfgs.get('kill-process')
36
37         self.check_script = self.get_script_fullpath(
38             self.fault_cfg['check_script'])
39         self.inject_script = self.get_script_fullpath(
40             self.fault_cfg['inject_script'])
41         self.recovery_script = self.get_script_fullpath(
42             self.fault_cfg['recovery_script'])
43
44         if self.check():
45             self.setup_done = True
46
47     def check(self):
48         exit_status, stdout, stderr = self.connection.execute(
49             "/bin/sh -s {0}".format(self.service_name),
50             stdin=open(self.check_script, "r"))
51
52         if stdout and "running" in stdout:
53             LOG.info("check the envrioment success!")
54             return True
55         else:
56             LOG.error(
57                 "the host envrioment is error, stdout:%s, stderr:%s",
58                 stdout, stderr)
59         return False
60
61     def inject_fault(self):
62         exit_status, stdout, stderr = self.connection.execute(
63             "/bin/sh -s {0}".format(self.service_name),
64             stdin=open(self.inject_script, "r"))
65
66     def recover(self):
67         exit_status, stdout, stderr = self.connection.execute(
68             "/bin/sh -s {0} ".format(self.service_name),
69             stdin=open(self.recovery_script, "r"))