added a shell command with result, leveraged it usage to get output for failures during addfile and mounting within nodes

This commit is contained in:
Blake J. Harnden 2018-02-07 19:13:51 -08:00
parent 78537d8e95
commit 127d0b0666
3 changed files with 30 additions and 5 deletions

View file

@ -172,8 +172,10 @@ class SimpleLxcNode(PyCoreNode):
source = os.path.abspath(source)
logger.info("mounting %s at %s" % (source, target))
try:
shcmd = 'mkdir -p "%s" && %s -n --bind "%s" "%s"' % (target, constants.MOUNT_BIN, source, target)
self.client.shcmd(shcmd)
cmd = 'mkdir -p "%s" && %s -n --bind "%s" "%s"' % (target, constants.MOUNT_BIN, source, target)
status, output = self.client.shcmd_result(cmd)
if status:
raise IOError("error during mount: %s" % output)
self._mounts.append((source, target))
except IOError:
logger.exception("mounting failed for %s at %s", source, target)
@ -446,8 +448,16 @@ class SimpleLxcNode(PyCoreNode):
:param str filename: file name to add
:return: nothing
"""
shcmd = 'mkdir -p $(dirname "%s") && mv "%s" "%s" && sync' % (filename, srcname, filename)
self.client.shcmd(shcmd)
logger.info("adding file from %s to %s", srcname, filename)
directory = os.path.dirname(filename)
try:
cmd = 'mkdir -p "%s" && mv "%s" "%s" && sync' % (directory, srcname, filename)
status, output = self.client.shcmd_result(cmd)
if status:
logger.error("error adding file: %s", output)
except IOError:
logger.exception("error during addfile")
def getaddr(self, ifname, rescan=False):
"""

View file

@ -6,6 +6,7 @@ by invoking the vcmd shell command.
"""
import os
import shlex
import vcmd
@ -93,6 +94,10 @@ class VnodeClient(object):
"""
self._verify_connection()
# split shell string to shell array for convenience
if type(args) == str:
args = shlex.split(args)
p, stdin, stdout, stderr = self.popen(args)
output = stdout.read() + stderr.read()
stdin.close()
@ -184,6 +189,17 @@ class VnodeClient(object):
"""
return self.cmd([sh, "-c", cmdstr])
def shcmd_result(self, cmd, sh="/bin/sh"):
"""
Execute a shell command and return the exist status and combined output.
:param str cmd: shell command to run
:param str sh: shell to run command in
:return: exist status and combined output
:rtype: tuple[int, str]
"""
return self.cmdresult([sh, "-c", cmd])
def getaddr(self, ifname, rescan=False):
"""
Get address for interface on node.

View file

@ -186,7 +186,6 @@ class TestCore:
assert not client.shcmd(command[0])
# check various command using command line
vnodeclient.USE_VCMD_MODULE = False
assert not client.cmd(command)
status, output = client.cmdresult(command)
assert not status