Commit 77292586 authored by Niklas Hambuechen's avatar Niklas Hambuechen Committed by Hrvoje Ribicic

qa_utils: Make AssertCommand return stdout/stderr as well

This is usefull if the run commands output something that's needed afterwards.
Signed-off-by: default avatarNiklas Hambuechen <niklash@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>

Cherry-picked-from: b47587b0Signed-off-by: default avatarHrvoje Ribicic <riba@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>
parent 1ae08cbd
...@@ -165,7 +165,7 @@ def AssertCommand(cmd, fail=False, node=None, log_cmd=True, max_seconds=None): ...@@ -165,7 +165,7 @@ def AssertCommand(cmd, fail=False, node=None, log_cmd=True, max_seconds=None):
@type max_seconds: double @type max_seconds: double
@param max_seconds: fail if the command takes more than C{max_seconds} @param max_seconds: fail if the command takes more than C{max_seconds}
seconds seconds
@return: the return code of the command @return: the return code, stdout and stderr of the command
@raise qa_error.Error: if the command fails when it shouldn't or vice versa @raise qa_error.Error: if the command fails when it shouldn't or vice versa
""" """
...@@ -180,7 +180,10 @@ def AssertCommand(cmd, fail=False, node=None, log_cmd=True, max_seconds=None): ...@@ -180,7 +180,10 @@ def AssertCommand(cmd, fail=False, node=None, log_cmd=True, max_seconds=None):
cmdstr = utils.ShellQuoteArgs(cmd) cmdstr = utils.ShellQuoteArgs(cmd)
start = datetime.datetime.now() start = datetime.datetime.now()
rcode = StartSSH(nodename, cmdstr, log_cmd=log_cmd).wait() popen = StartSSH(nodename, cmdstr, log_cmd=log_cmd)
# Run the command
stdout, stderr = popen.communicate()
rcode = popen.returncode
duration_seconds = TimedeltaToTotalSeconds(datetime.datetime.now() - start) duration_seconds = TimedeltaToTotalSeconds(datetime.datetime.now() - start)
_AssertRetCode(rcode, fail, cmdstr, nodename) _AssertRetCode(rcode, fail, cmdstr, nodename)
...@@ -190,7 +193,7 @@ def AssertCommand(cmd, fail=False, node=None, log_cmd=True, max_seconds=None): ...@@ -190,7 +193,7 @@ def AssertCommand(cmd, fail=False, node=None, log_cmd=True, max_seconds=None):
"Cmd '%s' took %f seconds, maximum of %f was exceeded" % "Cmd '%s' took %f seconds, maximum of %f was exceeded" %
(cmdstr, duration_seconds, max_seconds)) (cmdstr, duration_seconds, max_seconds))
return rcode return rcode, stdout, stderr
def AssertRedirectedCommand(cmd, fail=False, node=None, log_cmd=True): def AssertRedirectedCommand(cmd, fail=False, node=None, log_cmd=True):
...@@ -292,7 +295,8 @@ def StartSSH(node, cmd, strict=True, log_cmd=True): ...@@ -292,7 +295,8 @@ def StartSSH(node, cmd, strict=True, log_cmd=True):
""" """
return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict), return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict),
_nolog_opts=True, log_cmd=log_cmd) _nolog_opts=True, log_cmd=log_cmd,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def StartMultiplexer(node): def StartMultiplexer(node):
...@@ -549,10 +553,10 @@ def GenericQueryTest(cmd, fields, namefield="name", test_unknown=True): ...@@ -549,10 +553,10 @@ def GenericQueryTest(cmd, fields, namefield="name", test_unknown=True):
fail=True) fail=True)
# Check exit code for listing unknown field # Check exit code for listing unknown field
AssertEqual(AssertRedirectedCommand([cmd, "list", rcode, _, _ = AssertRedirectedCommand([cmd, "list",
"--output=field/does/not/exist"], "--output=field/does/not/exist"],
fail=True), fail=True)
constants.EXIT_UNKNOWN_FIELD) AssertEqual(rcode, constants.EXIT_UNKNOWN_FIELD)
def GenericQueryFieldsTest(cmd, fields): def GenericQueryFieldsTest(cmd, fields):
...@@ -570,9 +574,9 @@ def GenericQueryFieldsTest(cmd, fields): ...@@ -570,9 +574,9 @@ def GenericQueryFieldsTest(cmd, fields):
utils.NiceSort(fields)) utils.NiceSort(fields))
# Check exit code for listing unknown field # Check exit code for listing unknown field
AssertEqual(AssertCommand([cmd, "list-fields", "field/does/not/exist"], rcode, _, _ = AssertCommand([cmd, "list-fields", "field/does/not/exist"],
fail=True), fail=True)
constants.EXIT_UNKNOWN_FIELD) AssertEqual(rcode, constants.EXIT_UNKNOWN_FIELD)
def AddToEtcHosts(hostnames): def AddToEtcHosts(hostnames):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment