From 9e3525df867ba1dbb3e5b1ff55f088f8287e9f9a Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Fri, 10 Sep 2010 10:25:49 -0600 Subject: [PATCH] tests: silence qemuargv2xmltest noise Before this patch, the testsuite was noisy: TEST: qemuargv2xmltest ........................................ 40 ................20:41:28.046: warning : qemuParseCommandLine:6565 : unknown QEMU argument '-unknown', adding to the qemu namespace 20:41:28.046: warning : qemuParseCommandLine:6565 : unknown QEMU argument 'parameter', adding to the qemu namespace . 57 OK PASS: qemuargv2xmltest It's not a real failure (which is why the test was completing successfully), so much as an intentional warning to the user that use of the qemu namespace has the potential for undefined effects that leaked through the default logging behavior. After this patch series, all tests can access any logged data, and this particular test can explicitly check for the presence or absence of the warning, such that the test output becomes: TEST: qemuargv2xmltest ........................................ 40 ................. 57 OK PASS: qemuargv2xmltest * tests/testutils.h (virtTestLogContentAndReset): New prototype. * tests/testutils.c (struct virtTestLogData): New struct. (virtTestLogOutput, virtTestLogClose, virtTestLogContentAndReset): New functions. (virtTestMain): Always capture log data emitted during tests. * tests/qemuargv2xmltest.c (testCompareXMLToArgvHelper, mymain): Use flag to mark which tests expect noisy stderr. (testCompareXMLToArgvFiles): Add parameter to test whether stderr was appropriately silent. --- tests/qemuargv2xmltest.c | 17 ++++++++++++--- tests/testutils.c | 46 +++++++++++++++++++++++++++++++++++++++- tests/testutils.h | 4 +++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/tests/qemuargv2xmltest.c b/tests/qemuargv2xmltest.c index b75d2c52e0..4f9ec8454d 100644 --- a/tests/qemuargv2xmltest.c +++ b/tests/qemuargv2xmltest.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -35,7 +36,8 @@ static int blankProblemElements(char *data) } static int testCompareXMLToArgvFiles(const char *xml, - const char *cmdfile) { + const char *cmdfile, + bool expect_warning) { char xmlData[MAX_FILE]; char cmdData[MAX_FILE]; char *expectxml = &(xmlData[0]); @@ -43,6 +45,7 @@ static int testCompareXMLToArgvFiles(const char *xml, char *cmd = &(cmdData[0]); int ret = -1; virDomainDefPtr vmdef = NULL; + char *log; if (virtTestLoadFile(cmdfile, &cmd, MAX_FILE) < 0) goto fail; @@ -52,6 +55,14 @@ static int testCompareXMLToArgvFiles(const char *xml, if (!(vmdef = qemuParseCommandLineString(driver.caps, cmd))) goto fail; + if ((log = virtTestLogContentAndReset()) == NULL) + goto fail; + if ((*log != '\0') != expect_warning) { + free(log); + goto fail; + } + free(log); + if (!(actualxml = virDomainDefFormat(vmdef, 0))) goto fail; @@ -87,7 +98,7 @@ static int testCompareXMLToArgvHelper(const void *data) { abs_srcdir, info->name); snprintf(args, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.args", abs_srcdir, info->name); - return testCompareXMLToArgvFiles(xml, args); + return testCompareXMLToArgvFiles(xml, args, !!info->extraFlags); } @@ -215,7 +226,7 @@ mymain(int argc, char **argv) DO_TEST_FULL("restore-v2", 0, "exec:cat"); DO_TEST_FULL("migrate", 0, "tcp:10.0.0.1:5000"); - DO_TEST("qemu-ns-no-env"); + DO_TEST_FULL("qemu-ns-no-env", 1, NULL); free(driver.stateDir); virCapabilitiesFree(driver.caps); diff --git a/tests/testutils.c b/tests/testutils.c index 2f61aadc84..8171f103b6 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -1,7 +1,7 @@ /* * testutils.c: basic test utils * - * Copyright (C) 2005-2009 Red Hat, Inc. + * Copyright (C) 2005-2010 Red Hat, Inc. * * See COPYING.LIB for the License of this software * @@ -31,6 +31,8 @@ #include "util.h" #include "threads.h" #include "virterror_internal.h" +#include "buf.h" +#include "logging.h" #if TEST_OOM_TRACE # include @@ -351,6 +353,45 @@ virtTestErrorFuncQuiet(void *data ATTRIBUTE_UNUSED, { } #endif +struct virtTestLogData { + virBuffer buf; +}; + +static struct virtTestLogData testLog = { VIR_BUFFER_INITIALIZER }; + +static int +virtTestLogOutput(const char *category ATTRIBUTE_UNUSED, + int priority ATTRIBUTE_UNUSED, + const char *funcname ATTRIBUTE_UNUSED, + long long lineno ATTRIBUTE_UNUSED, + const char *str, int len, void *data) +{ + struct virtTestLogData *log = data; + virBufferAdd(&log->buf, str, len); + return len; +} + +static void +virtTestLogClose(void *data) +{ + struct virtTestLogData *log = data; + + virBufferFreeAndReset(&log->buf); +} + +/* Return a malloc'd string (possibly with strlen of 0) of all data + * logged since the last call to this function, or NULL on failure. */ +char * +virtTestLogContentAndReset(void) +{ + char *ret; + + if (virBufferError(&testLog.buf)) + return NULL; + ret = virBufferContentAndReset(&testLog.buf); + return ret ? ret : strdup(""); +} + #if TEST_OOM_TRACE static void virtTestErrorHook(int n, void *data ATTRIBUTE_UNUSED) @@ -425,6 +466,9 @@ int virtTestMain(int argc, virRandomInitialize(time(NULL) ^ getpid())) return 1; + if (virLogDefineOutput(virtTestLogOutput, virtTestLogClose, &testLog, + 0, 0, NULL, 0) < 0) + return 1; #if TEST_OOM if ((oomStr = getenv("VIR_TEST_OOM")) != NULL) { diff --git a/tests/testutils.h b/tests/testutils.h index 95f1680464..88603a1f0b 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -1,7 +1,7 @@ /* * utils.c: test utils * - * Copyright (C) 2005, 2008-2009 Red Hat, Inc. + * Copyright (C) 2005, 2008-2010 Red Hat, Inc. * * See COPYING.LIB for the License of this software * @@ -40,6 +40,8 @@ int virtTestDifference(FILE *stream, unsigned int virTestGetDebug(void); unsigned int virTestGetVerbose(void); +char *virtTestLogContentAndReset(void); + int virtTestMain(int argc, char **argv, int (*func)(int, char **));