From afd5ca0470c7c8779debd0d9901e59686d343c42 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 5 Feb 2013 16:24:05 +0100
Subject: [PATCH] Add function to execute QA commands with redirected output

Some QA commands generate non-trivial output: for a full QA, the gnt-*
list commands generate more than 300MB of data. To make the QA logs
more parseable by humans, we need support for redirecting output of
commands to a different place.

This patch adds a 'AssertRedirectedCommand' that does this,
redirecting the output to a (fixed) log file in the Ganeti log
directory.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 qa/qa_utils.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/qa/qa_utils.py b/qa/qa_utils.py
index 6f37aecbc..948dd471f 100644
--- a/qa/qa_utils.py
+++ b/qa/qa_utils.py
@@ -55,6 +55,9 @@ _MULTIPLEXERS = {}
 #: Unique ID per QA run
 _RUN_UUID = utils.NewUUID()
 
+#: Path to the QA query output log file
+_QA_OUTPUT = pathutils.GetLogFilename("qa-output")
+
 
 (INST_DOWN,
  INST_UP) = range(500, 502)
@@ -192,6 +195,27 @@ def AssertCommand(cmd, fail=False, node=None, log_cmd=True):
   return rcode
 
 
+def AssertRedirectedCommand(cmd, fail=False, node=None, log_cmd=True):
+  """Executes a command with redirected output.
+
+  The log will go to the qa-output log file in the ganeti log
+  directory on the node where the command is executed. The fail and
+  node parameters are passed unchanged to AssertCommand.
+
+  @param cmd: the command to be executed, as a list; a string is not
+      supported
+
+  """
+  if not isinstance(cmd, list):
+    raise qa_error.Error("Non-list passed to AssertRedirectedCommand")
+  ofile = utils.ShellQuote(_QA_OUTPUT)
+  cmdstr = utils.ShellQuoteArgs(cmd)
+  AssertCommand("echo ---- $(date) %s ---- >> %s" % (cmdstr, ofile),
+                fail=False, node=node, log_cmd=False)
+  return AssertCommand(cmdstr + " >> %s" % ofile,
+                       fail=fail, node=node, log_cmd=log_cmd)
+
+
 def GetSSHCommand(node, cmd, strict=True, opts=None, tty=None):
   """Builds SSH command to be executed.
 
-- 
GitLab