Merge "Open storperf testcase to huawei-pod2"
[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
27         self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
28         self.connection.wait(timeout=600)
29         LOG.debug("ssh host success!")
30
31         self.service_name = self._config['process_name']
32         self.fault_cfg = BaseAttacker.attacker_cfgs.get('kill-process')
33
34         self.check_script = self.get_script_fullpath(
35             self.fault_cfg['check_script'])
36         self.inject_script = self.get_script_fullpath(
37             self.fault_cfg['inject_script'])
38         self.recovery_script = self.get_script_fullpath(
39             self.fault_cfg['recovery_script'])
40
41         self.data[self.service_name] = self.check()
42
43     def check(self):
44         with open(self.check_script, "r") as stdin_file:
45             exit_status, stdout, stderr = self.connection.execute(
46                 "sudo /bin/sh -s {0}".format(self.service_name),
47                 stdin=stdin_file)
48
49         if stdout:
50             LOG.info("check the environment success!")
51             return int(stdout.strip('\n'))
52         else:
53             LOG.error(
54                 "the host environment is error, stdout:%s, stderr:%s",
55                 stdout, stderr)
56         return False
57
58     def inject_fault(self):
59         with open(self.inject_script, "r") as stdin_file:
60             exit_status, stdout, stderr = self.connection.execute(
61                 "sudo /bin/sh -s {0}".format(self.service_name),
62                 stdin=stdin_file)
63
64     def recover(self):
65         with open(self.recovery_script, "r") as stdin_file:
66             exit_status, stdout, stderr = self.connection.execute(
67                 "sudo /bin/bash -s {0} ".format(self.service_name),
68                 stdin=stdin_file)
69         if exit_status:
70             LOG.info("Fail to restart service!")