5118ad628fd03dfefd1f6c09be5065323a53d6c9
[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         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
27
28         self.connection = ssh.SSH(user, ip, key_filename=key_filename)
29         self.connection.wait(timeout=600)
30         LOG.debug("ssh host success!")
31
32         self.service_name = self._config['process_name']
33         self.fault_cfg = BaseAttacker.attacker_cfgs.get('kill-process')
34
35         self.check_script = self.get_script_fullpath(
36             self.fault_cfg['check_script'])
37         self.inject_script = self.get_script_fullpath(
38             self.fault_cfg['inject_script'])
39         self.recovery_script = self.get_script_fullpath(
40             self.fault_cfg['recovery_script'])
41
42         if self.check():
43             self.setup_done = True
44
45     def check(self):
46         exit_status, stdout, stderr = self.connection.execute(
47             "/bin/sh -s {0}".format(self.service_name),
48             stdin=open(self.check_script, "r"))
49
50         if stdout and "running" in stdout:
51             LOG.info("check the envrioment success!")
52             return True
53         else:
54             LOG.error(
55                 "the host envrioment is error, stdout:%s, stderr:%s" %
56                 (stdout, stderr))
57         return False
58
59     def inject_fault(self):
60         exit_status, stdout, stderr = self.connection.execute(
61             "/bin/sh -s {0}".format(self.service_name),
62             stdin=open(self.inject_script, "r"))
63
64     def recover(self):
65         exit_status, stdout, stderr = self.connection.execute(
66             "/bin/sh -s {0} ".format(self.service_name),
67             stdin=open(self.recovery_script, "r"))