X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=yardstick%2Fbenchmark%2Fscenarios%2Favailability%2Fattacker%2Fattacker_process.py;h=cb171eafad1d5eb64ea2dc2e8ed3862f10aed5b2;hb=15d807c22f33fab8e1f9b61e1d0c041a8144ee89;hp=0a844f56cb0858eb02bb9336757fe5196d26b955;hpb=492f64b990e8487894f30f7cbce4cbff7e720d16;p=yardstick.git diff --git a/yardstick/benchmark/scenarios/availability/attacker/attacker_process.py b/yardstick/benchmark/scenarios/availability/attacker/attacker_process.py index 0a844f56c..cb171eafa 100644 --- a/yardstick/benchmark/scenarios/availability/attacker/attacker_process.py +++ b/yardstick/benchmark/scenarios/availability/attacker/attacker_process.py @@ -6,10 +6,12 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +from __future__ import absolute_import import logging -from baseattacker import BaseAttacker import yardstick.ssh as ssh +from yardstick.benchmark.scenarios.availability.attacker.baseattacker import \ + BaseAttacker LOG = logging.getLogger(__name__) @@ -19,15 +21,10 @@ class ProcessAttacker(BaseAttacker): __attacker_type__ = 'kill-process' def setup(self): - LOG.debug("config:%s context:%s" % (self._config, self._context)) + LOG.debug("config:%s context:%s", self._config, self._context) host = self._context.get(self._config['host'], None) - ip = host.get("ip", None) - user = host.get("user", "root") - ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT) - key_filename = host.get("key_filename", "~/.ssh/id_rsa") - self.connection = ssh.SSH(user, ip, key_filename=key_filename, - port=ssh_port) + self.connection = ssh.SSH.from_node(host, defaults={"user": "root"}) self.connection.wait(timeout=600) LOG.debug("ssh host success!") @@ -41,29 +38,33 @@ class ProcessAttacker(BaseAttacker): self.recovery_script = self.get_script_fullpath( self.fault_cfg['recovery_script']) - if self.check(): - self.setup_done = True + self.data[self.service_name] = self.check() def check(self): - exit_status, stdout, stderr = self.connection.execute( - "/bin/sh -s {0}".format(self.service_name), - stdin=open(self.check_script, "r")) + with open(self.check_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + "sudo /bin/sh -s {0}".format(self.service_name), + stdin=stdin_file) - if stdout and "running" in stdout: - LOG.info("check the envrioment success!") - return True + if stdout: + LOG.info("check the environment success!") + return int(stdout.strip('\n')) else: LOG.error( - "the host envrioment is error, stdout:%s, stderr:%s" % - (stdout, stderr)) + "the host environment is error, stdout:%s, stderr:%s", + stdout, stderr) return False def inject_fault(self): - exit_status, stdout, stderr = self.connection.execute( - "/bin/sh -s {0}".format(self.service_name), - stdin=open(self.inject_script, "r")) + with open(self.inject_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + "sudo /bin/sh -s {0}".format(self.service_name), + stdin=stdin_file) def recover(self): - exit_status, stdout, stderr = self.connection.execute( - "/bin/sh -s {0} ".format(self.service_name), - stdin=open(self.recovery_script, "r")) + with open(self.recovery_script, "r") as stdin_file: + exit_status, stdout, stderr = self.connection.execute( + "sudo /bin/bash -s {0} ".format(self.service_name), + stdin=stdin_file) + if exit_status: + LOG.info("Fail to restart service!")