提交 69971505 编写于 作者: L Lucas Meneghel Rodrigues

avocado.utils.remote: Changes to the .run() method

Make the run() method to return a CmdResult object,
to make it symmetrical to the process.run() result.
Also, turn on logging of outputs for commands executed
on remote connections.

The reason why we want to do that is to increase the
debugging output we have for commands executed over
the fabric SSH connections. This way we can obtain
better, richer outputs in test logs that use our
fabric wrappers.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 24d119f2
......@@ -72,13 +72,13 @@ class VMTestRunner(TestRunner):
urls = urls.split()
avocado_cmd = ('cd %s; avocado run --force-job-id %s --json - --archive %s' %
(self.remote_test_dir, self.result.stream.job_unique_id, " ".join(urls)))
stdout = self.result.vm.remote.run(avocado_cmd)
result = self.result.vm.remote.run(avocado_cmd, ignore_status=True)
try:
results = json.loads(stdout)
results = json.loads(result.stdout)
except Exception, details:
raise ValueError('Error loading JSON '
'(full output below): %s\n"""\n%s\n"""' %
(details, stdout))
(details, result.stdout))
return results
def run(self, params_list):
......
......@@ -18,6 +18,7 @@ Module to provide remote operations.
import getpass
import logging
import time
log = logging.getLogger('avocado.test')
......@@ -30,6 +31,10 @@ except ImportError:
else:
remote_capable = True
from avocado.core import output
from avocado.core import exceptions
from avocado.utils import process
class Remote(object):
......@@ -38,7 +43,7 @@ class Remote(object):
"""
def __init__(self, hostname, username=None, password=None,
port=22, timeout=60, attempts=3, quiet=True):
port=22, timeout=60, attempts=3, quiet=False):
"""
Creates an instance of :class:`Remote`.
......@@ -62,23 +67,45 @@ class Remote(object):
password=password,
port=port,
connection_timeout=timeout,
connection_attempts=attempts)
connection_attempts=attempts,
linewise=True)
def _setup_environment(self, **kwargs):
fabric.api.env.update(kwargs)
def run(self, command):
def run(self, command, ignore_status=False):
"""
Run a remote command.
:param command: the command string to execute.
:return: the result of the remote program's output.
:rtype: :class:`fabric.operations._AttributeString`.
:rtype: :class:`avocado.utils.process.CmdResult`.
"""
return fabric.operations.run(command,
quiet=self.quiet,
warn_only=True)
if not self.quiet:
log.info('[%s] Running command %s', self.hostname, command)
result = process.CmdResult()
stdout = output.LoggingFile(logger=logging.getLogger('avocado.test'))
stderr = output.LoggingFile(logger=logging.getLogger('avocado.test'))
start_time = time.time()
fabric_result = fabric.operations.run(command=command,
quiet=self.quiet,
stdout=stdout,
stderr=stderr,
warn_only=True)
end_time = time.time()
duration = end_time - start_time
result.command = command
result.stdout = str(fabric_result)
result.stderr = fabric_result.stderr
result.duration = duration
result.exit_status = fabric_result.return_code
result.failed = fabric_result.failed
result.succeeded = fabric_result.succeeded
if not ignore_status:
if result.failed:
raise exceptions.CmdError(command=command, result=result)
return result
def uptime(self):
"""
......@@ -86,8 +113,8 @@ class Remote(object):
:return: the uptime string or empty string if fails.
"""
res = self.run('uptime')
if res.succeeded:
res = self.run('uptime', ignore_status=True)
if res.exit_status == 0:
return res
else:
return ''
......@@ -107,6 +134,9 @@ class Remote(object):
:param local_path: the local path.
:param remote_path: the remote path.
"""
if not self.quiet:
log.info('[%s] Receive remote files %s -> %s', self.hostname,
local_path, remote_path)
with fabric.context_managers.quiet():
try:
fabric.operations.put(local_path,
......@@ -122,6 +152,9 @@ class Remote(object):
:param local_path: the local path.
:param remote_path: the remote path.
"""
if not self.quiet:
log.info('[%s] Receive remote files %s -> %s', self.hostname,
local_path, remote_path)
with fabric.context_managers.quiet():
try:
fabric.operations.get(remote_path,
......
......@@ -272,7 +272,8 @@ class VM(object):
:param password: the password.
"""
if not self.logged:
self.remote = remote.Remote(hostname, username, password)
self.remote = remote.Remote(hostname, username, password,
quiet=True)
res = self.remote.uptime()
if res.succeeded:
self.logged = True
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册