[libvirt] [PATCH 0/4] tests: Cleanups and improvements

First 3 patches are uninteresting cleanups, removing a bunch of boiler plate code. The last patch adds a way to easily regenerate test output, like when adding a new test case (plop in the qemu XML, use the env var and the test suite will create foo.args for you), or when a code changes causes test suite output to expectedly change. Cole Robinson (4): tests: Add VIR_TEST_DEBUG and VIR_TEST_VERBOSE tests: Use *DefParseFile more tests: Add virtTestCompareToFile tests: Add VIR_TEST_REGENERATE_OUTPUT HACKING | 7 ++ docs/hacking.html.in | 12 +++ tests/bhyvexml2argvtest.c | 41 ++-------- tests/bhyvexml2xmltest.c | 18 +---- tests/cputest.c | 45 ++++------- tests/domaincapstest.c | 9 +-- tests/domainconftest.c | 7 +- tests/jsontest.c | 48 +++++------- tests/lxcxml2xmltest.c | 17 +---- tests/networkxml2conftest.c | 16 +--- tests/networkxml2firewalltest.c | 9 +-- tests/networkxml2xmltest.c | 15 +--- tests/networkxml2xmlupdatetest.c | 15 +--- tests/nodeinfotest.c | 20 +---- tests/nwfilterxml2firewalltest.c | 11 +-- tests/nwfilterxml2xmltest.c | 15 +--- tests/qemuagenttest.c | 9 +-- tests/qemuargv2xmltest.c | 15 ++-- tests/qemucaps2xmltest.c | 27 +------ tests/qemuhelptest.c | 6 +- tests/qemuhotplugtest.c | 19 ++--- tests/qemumonitorjsontest.c | 9 +-- tests/qemumonitortest.c | 24 +++--- tests/qemuxml2argvtest.c | 27 ++----- tests/qemuxmlnstest.c | 19 +---- tests/secretxml2xmltest.c | 15 +--- tests/securityselinuxlabeltest.c | 13 +--- tests/sexpr2xmltest.c | 9 +-- tests/statstest.c | 3 +- tests/storagebackendsheepdogtest.c | 20 +---- tests/storagepoolxml2xmltest.c | 15 +--- tests/storagevolxml2argvtest.c | 40 ++-------- tests/storagevolxml2xmltest.c | 21 +---- tests/sysinfotest.c | 10 +-- tests/testutils.c | 74 ++++++++++++++---- tests/testutils.h | 14 ++++ tests/testutilslxc.c | 2 +- tests/testutilsqemu.c | 2 +- tests/utiltest.c | 30 +++----- tests/virbuftest.c | 60 +++++++-------- tests/vircaps2xmltest.c | 10 +-- tests/vircgrouptest.c | 9 +-- tests/virhashtest.c | 152 ++++++++++++------------------------- tests/virpcitest.c | 4 +- tests/virportallocatortest.c | 35 +++------ tests/vmx2xmltest.c | 9 +-- tests/xencapstest.c | 9 +-- tests/xlconfigtest.c | 27 +------ tests/xmconfigtest.c | 27 +------ tests/xml2sexprtest.c | 18 +---- tests/xml2vmxtest.c | 19 +---- 51 files changed, 340 insertions(+), 767 deletions(-) -- 2.3.5

To remove a bunch of TestGetDebug()/TestGetVerbose() checks --- tests/cputest.c | 34 ++++----- tests/jsontest.c | 48 +++++-------- tests/nodeinfotest.c | 2 +- tests/qemuargv2xmltest.c | 15 ++-- tests/qemuhelptest.c | 6 +- tests/qemuhotplugtest.c | 19 +++-- tests/qemumonitortest.c | 24 +++---- tests/qemuxml2argvtest.c | 14 ++-- tests/qemuxmlnstest.c | 6 +- tests/securityselinuxlabeltest.c | 6 +- tests/statstest.c | 3 +- tests/testutils.c | 2 +- tests/testutils.h | 12 ++++ tests/testutilslxc.c | 2 +- tests/testutilsqemu.c | 2 +- tests/utiltest.c | 30 +++----- tests/virbuftest.c | 60 +++++++--------- tests/virhashtest.c | 152 +++++++++++++-------------------------- tests/virpcitest.c | 4 +- tests/virportallocatortest.c | 35 ++++----- 20 files changed, 188 insertions(+), 288 deletions(-) diff --git a/tests/cputest.c b/tests/cputest.c index bf7a48f..56bcc90 100644 --- a/tests/cputest.c +++ b/tests/cputest.c @@ -235,13 +235,11 @@ cpuTestCompare(const void *arg) virResetLastError(); if (data->result != result) { - if (virTestGetVerbose()) { - fprintf(stderr, "\nExpected result %s, got %s\n", + VIR_TEST_VERBOSE("\nExpected result %s, got %s\n", cpuTestCompResStr(data->result), cpuTestCompResStr(result)); - /* Pad to line up with test name ... in virTestRun */ - fprintf(stderr, "%74s", "... "); - } + /* Pad to line up with test name ... in virTestRun */ + VIR_TEST_VERBOSE("%74s", "... "); goto cleanup; } @@ -337,8 +335,8 @@ cpuTestBaseline(const void *arg) virResetLastError(); if (!baseline) { ret = 0; - } else if (virTestGetVerbose()) { - fprintf(stderr, "\n%-70s... ", + } else { + VIR_TEST_VERBOSE("\n%-70s... ", "cpuBaseline was expected to fail but it succeeded"); } goto cleanup; @@ -364,11 +362,9 @@ cpuTestBaseline(const void *arg) cmp = cpuCompare(cpus[i], baseline, false); if (cmp != VIR_CPU_COMPARE_SUPERSET && cmp != VIR_CPU_COMPARE_IDENTICAL) { - if (virTestGetVerbose()) { - fprintf(stderr, - "\nbaseline CPU is incompatible with CPU %zu\n", i); - fprintf(stderr, "%74s", "... "); - } + VIR_TEST_VERBOSE("\nbaseline CPU is incompatible with CPU %zu\n", + i); + VIR_TEST_VERBOSE("%74s", "... "); ret = -1; goto cleanup; } @@ -438,13 +434,11 @@ cpuTestHasFeature(const void *arg) virResetLastError(); if (data->result != result) { - if (virTestGetVerbose()) { - fprintf(stderr, "\nExpected result %s, got %s\n", - cpuTestBoolWithErrorStr(data->result), - cpuTestBoolWithErrorStr(result)); - /* Pad to line up with test name ... in virTestRun */ - fprintf(stderr, "%74s", "... "); - } + VIR_TEST_VERBOSE("\nExpected result %s, got %s\n", + cpuTestBoolWithErrorStr(data->result), + cpuTestBoolWithErrorStr(result)); + /* Pad to line up with test name ... in virTestRun */ + VIR_TEST_VERBOSE("%74s", "... "); goto cleanup; } @@ -483,7 +477,7 @@ cpuTestRun(const char *name, const struct data *data) char *log; if ((log = virtTestLogContentAndReset()) && strlen(log) > 0) - fprintf(stderr, "\n%s\n", log); + VIR_TEST_DEBUG("\n%s\n", log); VIR_FREE(log); } diff --git a/tests/jsontest.c b/tests/jsontest.c index a2a42e3..f27943e 100644 --- a/tests/jsontest.c +++ b/tests/jsontest.c @@ -27,23 +27,19 @@ testJSONFromString(const void *data) if (info->pass) { if (!json) { - if (virTestGetVerbose()) - fprintf(stderr, "Fail to parse %s\n", info->doc); + VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc); ret = -1; goto cleanup; } else { - if (virTestGetDebug()) - fprintf(stderr, "Parsed %s\n", info->doc); + VIR_TEST_DEBUG("Parsed %s\n", info->doc); } } else { if (json) { - if (virTestGetVerbose()) - fprintf(stderr, "Should not have parsed %s\n", info->doc); + VIR_TEST_VERBOSE("Should not have parsed %s\n", info->doc); ret = -1; goto cleanup; } else { - if (virTestGetDebug()) - fprintf(stderr, "Fail to parse %s\n", info->doc); + VIR_TEST_DEBUG("Fail to parse %s\n", info->doc); } } @@ -66,8 +62,7 @@ testJSONAddRemove(const void *data) json = virJSONValueFromString(info->doc); if (!json) { - if (virTestGetVerbose()) - fprintf(stderr, "Fail to parse %s\n", info->doc); + VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc); ret = -1; goto cleanup; } @@ -75,49 +70,42 @@ testJSONAddRemove(const void *data) switch (virJSONValueObjectRemoveKey(json, "name", &name)) { case 1: if (!info->pass) { - if (virTestGetVerbose()) - fprintf(stderr, "should not remove from non-object %s\n", - info->doc); + VIR_TEST_VERBOSE("should not remove from non-object %s\n", + info->doc); goto cleanup; } break; case -1: if (!info->pass) ret = 0; - else if (virTestGetVerbose()) - fprintf(stderr, "Fail to recognize non-object %s\n", info->doc); + else + VIR_TEST_VERBOSE("Fail to recognize non-object %s\n", info->doc); goto cleanup; default: - if (virTestGetVerbose()) - fprintf(stderr, "unexpected result when removing from %s\n", - info->doc); + VIR_TEST_VERBOSE("unexpected result when removing from %s\n", + info->doc); goto cleanup; } if (STRNEQ_NULLABLE(virJSONValueGetString(name), "sample")) { - if (virTestGetVerbose()) - fprintf(stderr, "unexpected value after removing name: %s\n", - NULLSTR(virJSONValueGetString(name))); + VIR_TEST_VERBOSE("unexpected value after removing name: %s\n", + NULLSTR(virJSONValueGetString(name))); goto cleanup; } if (virJSONValueObjectRemoveKey(json, "name", NULL)) { - if (virTestGetVerbose()) - fprintf(stderr, "%s", - "unexpected success when removing missing key\n"); + VIR_TEST_VERBOSE("%s", + "unexpected success when removing missing key\n"); goto cleanup; } if (virJSONValueObjectAppendString(json, "newname", "foo") < 0) { - if (virTestGetVerbose()) - fprintf(stderr, "%s", "unexpected failure adding new key\n"); + VIR_TEST_VERBOSE("%s", "unexpected failure adding new key\n"); goto cleanup; } if (!(result = virJSONValueToString(json, false))) { - if (virTestGetVerbose()) - fprintf(stderr, "%s", "failed to stringize result\n"); + VIR_TEST_VERBOSE("%s", "failed to stringize result\n"); goto cleanup; } if (STRNEQ(info->expect, result)) { - if (virTestGetVerbose()) - virtTestDifference(stderr, info->expect, result); + virtTestDifference(stderr, info->expect, result); goto cleanup; } ret = 0; diff --git a/tests/nodeinfotest.c b/tests/nodeinfotest.c index 1004174..e53a604 100644 --- a/tests/nodeinfotest.c +++ b/tests/nodeinfotest.c @@ -50,7 +50,7 @@ linuxTestCompareFiles(const char *cpuinfofile, if (virTestGetDebug()) { virErrorPtr error = virSaveLastError(); if (error && error->code != VIR_ERR_OK) - fprintf(stderr, "\n%s\n", error->message); + VIR_TEST_DEBUG("\n%s\n", error->message); virFreeError(error); } VIR_FORCE_FCLOSE(cpuinfo); diff --git a/tests/qemuargv2xmltest.c b/tests/qemuargv2xmltest.c index cf4a6a8..d6df116 100644 --- a/tests/qemuargv2xmltest.c +++ b/tests/qemuargv2xmltest.c @@ -64,22 +64,17 @@ static int testCompareXMLToArgvFiles(const char *xml, goto fail; if (flags & FLAG_EXPECT_WARNING) { if (*log) { - if (virTestGetDebug() > 1) - fprintf(stderr, - "Got expected warning from " + VIR_TEST_DEBUG("Got expected warning from " "qemuParseCommandLineString:\n%s", log); } else { - if (virTestGetDebug()) - fprintf(stderr, "qemuParseCommandLineString " - "should have logged a warning\n"); + VIR_TEST_DEBUG("qemuParseCommandLineString " + "should have logged a warning\n"); goto fail; } } else { /* didn't expect a warning */ if (*log) { - if (virTestGetDebug()) - fprintf(stderr, - "Got unexpected warning from " + VIR_TEST_DEBUG("Got unexpected warning from " "qemuParseCommandLineString:\n%s", log); goto fail; @@ -88,7 +83,7 @@ static int testCompareXMLToArgvFiles(const char *xml, } if (!virDomainDefCheckABIStability(vmdef, vmdef)) { - fprintf(stderr, "ABI stability check failed on %s", xml); + VIR_TEST_DEBUG("ABI stability check failed on %s", xml); goto fail; } diff --git a/tests/qemuhelptest.c b/tests/qemuhelptest.c index 6d9525c..507831c 100644 --- a/tests/qemuhelptest.c +++ b/tests/qemuhelptest.c @@ -90,10 +90,8 @@ static int testHelpStrParsing(const void *data) goto cleanup; if (STRNEQ(got, expected)) { - if (virTestGetVerbose() || virTestGetDebug()) - fprintf(stderr, - "%s: computed flags do not match: got %s, expected %s\n", - info->name, got, expected); + VIR_TEST_DEBUG("%s: computed flags do not match: got %s, expected %s\n", + info->name, got, expected); if (virTestGetDebug()) printMismatchedFlags(flags, info->flags); diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c index 08f764b..368a5e7 100644 --- a/tests/qemuhotplugtest.c +++ b/tests/qemuhotplugtest.c @@ -115,9 +115,8 @@ testQemuHotplugAttach(virDomainObjPtr vm, ret = qemuDomainAttachChrDevice(&driver, vm, dev->data.chr); break; default: - if (virTestGetVerbose()) - fprintf(stderr, "device type '%s' cannot be attached\n", - virDomainDeviceTypeToString(dev->type)); + VIR_TEST_VERBOSE("device type '%s' cannot be attached\n", + virDomainDeviceTypeToString(dev->type)); break; } @@ -138,9 +137,8 @@ testQemuHotplugDetach(virDomainObjPtr vm, ret = qemuDomainDetachChrDevice(&driver, vm, dev->data.chr); break; default: - if (virTestGetVerbose()) - fprintf(stderr, "device type '%s' cannot be detached\n", - virDomainDeviceTypeToString(dev->type)); + VIR_TEST_VERBOSE("device type '%s' cannot be detached\n", + virDomainDeviceTypeToString(dev->type)); break; } @@ -163,9 +161,8 @@ testQemuHotplugUpdate(virDomainObjPtr vm, ret = qemuDomainChangeGraphics(&driver, vm, dev->data.graphics); break; default: - if (virTestGetVerbose()) - fprintf(stderr, "device type '%s' cannot be updated\n", - virDomainDeviceTypeToString(dev->type)); + VIR_TEST_VERBOSE("device type '%s' cannot be updated\n", + virDomainDeviceTypeToString(dev->type)); break; } @@ -187,8 +184,8 @@ testQemuHotplugCheckResult(virDomainObjPtr vm, vm->def->id = QEMU_HOTPLUG_TEST_DOMAIN_ID; if (STREQ(expected, actual)) { - if (fail && virTestGetVerbose()) - fprintf(stderr, "domain XML should not match the expected result\n"); + if (fail) + VIR_TEST_VERBOSE("domain XML should not match the expected result\n"); ret = 0; } else { if (!fail) diff --git a/tests/qemumonitortest.c b/tests/qemumonitortest.c index d73bbf1..0125962 100644 --- a/tests/qemumonitortest.c +++ b/tests/qemumonitortest.c @@ -43,13 +43,11 @@ static int testEscapeArg(const void *data ATTRIBUTE_UNUSED) for (i = 0; i < ARRAY_CARDINALITY(escapeStrings); ++i) { escaped = qemuMonitorEscapeArg(escapeStrings[i].unescaped); if (!escaped) { - if (virTestGetDebug() > 0) { - fprintf(stderr, "\nUnescaped string [%s]\n", - escapeStrings[i].unescaped); - fprintf(stderr, "Expect result [%s]\n", - escapeStrings[i].escaped); - fprintf(stderr, "Actual result [(null)]\n"); - } + VIR_TEST_DEBUG("\nUnescaped string [%s]\n", + escapeStrings[i].unescaped); + VIR_TEST_DEBUG("Expect result [%s]\n", + escapeStrings[i].escaped); + VIR_TEST_DEBUG("Actual result [(null)]\n"); return -1; } if (STRNEQ(escapeStrings[i].escaped, escaped)) { @@ -70,13 +68,11 @@ static int testUnescapeArg(const void *data ATTRIBUTE_UNUSED) for (i = 0; i < ARRAY_CARDINALITY(escapeStrings); ++i) { unescaped = qemuMonitorUnescapeArg(escapeStrings[i].escaped); if (!unescaped) { - if (virTestGetDebug() > 0) { - fprintf(stderr, "\nEscaped string [%s]\n", - escapeStrings[i].escaped); - fprintf(stderr, "Expect result [%s]\n", - escapeStrings[i].unescaped); - fprintf(stderr, "Actual result [(null)]\n"); - } + VIR_TEST_DEBUG("\nEscaped string [%s]\n", + escapeStrings[i].escaped); + VIR_TEST_DEBUG("Expect result [%s]\n", + escapeStrings[i].unescaped); + VIR_TEST_DEBUG("Actual result [(null)]\n"); return -1; } if (STRNEQ(escapeStrings[i].unescaped, unescaped)) { diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index acd6126..e489a13 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -282,7 +282,7 @@ static int testCompareXMLToArgvFiles(const char *xml, } if (!virDomainDefCheckABIStability(vmdef, vmdef)) { - fprintf(stderr, "ABI stability check failed on %s", xml); + VIR_TEST_DEBUG("ABI stability check failed on %s", xml); goto out; } @@ -353,22 +353,20 @@ static int testCompareXMLToArgvFiles(const char *xml, if (!virtTestOOMActive() && (flags & FLAG_EXPECT_FAILURE)) { ret = 0; - if (virTestGetDebug() > 1) - fprintf(stderr, "Got expected error: %s\n", - virGetLastErrorMessage()); + VIR_TEST_DEBUG("Got expected error: %s\n", + virGetLastErrorMessage()); virResetLastError(); } goto out; } else if (flags & FLAG_EXPECT_FAILURE) { - if (virTestGetDebug()) - fprintf(stderr, "qemuBuildCommandLine should have failed\n"); + VIR_TEST_DEBUG("qemuBuildCommandLine should have failed\n"); goto out; } if (!virtTestOOMActive() && (!!virGetLastError() != !!(flags & FLAG_EXPECT_ERROR))) { - if (virTestGetDebug() && (log = virtTestLogContentAndReset())) - fprintf(stderr, "\n%s", log); + if ((log = virtTestLogContentAndReset())) + VIR_TEST_DEBUG("\n%s", log); goto out; } diff --git a/tests/qemuxmlnstest.c b/tests/qemuxmlnstest.c index 97a809a..4d7a142 100644 --- a/tests/qemuxmlnstest.c +++ b/tests/qemuxmlnstest.c @@ -59,7 +59,7 @@ static int testCompareXMLToArgvFiles(const char *xml, goto fail; if (!virDomainDefCheckABIStability(vmdef, vmdef)) { - fprintf(stderr, "ABI stability check failed on %s", xml); + VIR_TEST_DEBUG("ABI stability check failed on %s", xml); goto fail; } @@ -121,8 +121,8 @@ static int testCompareXMLToArgvFiles(const char *xml, if (!virtTestOOMActive()) { if (!!virGetLastError() != expectError) { - if (virTestGetDebug() && (log = virtTestLogContentAndReset())) - fprintf(stderr, "\n%s", log); + if ((log = virtTestLogContentAndReset())) + VIR_TEST_DEBUG("\n%s", log); goto fail; } diff --git a/tests/securityselinuxlabeltest.c b/tests/securityselinuxlabeltest.c index c5e96e2..730f7be 100644 --- a/tests/securityselinuxlabeltest.c +++ b/tests/securityselinuxlabeltest.c @@ -336,9 +336,9 @@ testSELinuxLabeling(const void *opaque) VIR_FREE(files[i].context); } VIR_FREE(files); - if (ret < 0 && virTestGetVerbose()) { + if (ret < 0) { virErrorPtr err = virGetLastError(); - fprintf(stderr, "%s\n", err ? err->message : "<unknown>"); + VIR_TEST_VERBOSE("%s\n", err ? err->message : "<unknown>"); } return ret; } @@ -358,7 +358,7 @@ mymain(void) if (!(mgr = virSecurityManagerNew("selinux", "QEMU", false, true, false))) { virErrorPtr err = virGetLastError(); - fprintf(stderr, "Unable to initialize security driver: %s\n", + VIR_TEST_VERBOSE("Unable to initialize security driver: %s\n", err->message); return EXIT_FAILURE; } diff --git a/tests/statstest.c b/tests/statstest.c index f9b4573..46e5560 100644 --- a/tests/statstest.c +++ b/tests/statstest.c @@ -18,8 +18,7 @@ static int testDevice(const char *path, int expect) if (actual == expect) { return 0; } else { - if (virTestGetDebug()) - fprintf(stderr, "Expect %-6d Actual %-6d\n", expect, actual); + VIR_TEST_DEBUG("Expect %-6d Actual %-6d\n", expect, actual); return -1; } } diff --git a/tests/testutils.c b/tests/testutils.c index aca2fd0..3274e40 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -960,7 +960,7 @@ virCapsPtr virTestGenericCapsInit(void) if (!caps_str) goto error; - fprintf(stderr, "Generic driver capabilities:\n%s", caps_str); + VIR_TEST_DEBUG("Generic driver capabilities:\n%s", caps_str); VIR_FREE(caps_str); } diff --git a/tests/testutils.h b/tests/testutils.h index 155b30f..8c05d85 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -78,6 +78,18 @@ unsigned int virTestGetDebug(void); unsigned int virTestGetVerbose(void); unsigned int virTestGetExpensive(void); +#define VIR_TEST_DEBUG(...) \ + do { \ + if (virTestGetDebug()) \ + fprintf(stderr, __VA_ARGS__); \ + } while (0) + +#define VIR_TEST_VERBOSE(...) \ + do { \ + if (virTestGetVerbose()) \ + fprintf(stderr, __VA_ARGS__); \ + } while (0) + char *virtTestLogContentAndReset(void); void virtTestQuiesceLibvirtErrors(bool always); diff --git a/tests/testutilslxc.c b/tests/testutilslxc.c index 3e4d948..e4489f3 100644 --- a/tests/testutilslxc.c +++ b/tests/testutilslxc.c @@ -44,7 +44,7 @@ virCapsPtr testLXCCapsInit(void) if (!caps_str) goto error; - fprintf(stderr, "LXC driver capabilities:\n%s", caps_str); + VIR_TEST_DEBUG("LXC driver capabilities:\n%s", caps_str); VIR_FREE(caps_str); } diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index bbf58ee..14743be 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -427,7 +427,7 @@ virCapsPtr testQemuCapsInit(void) if (!caps_str) goto cleanup; - fprintf(stderr, "QEMU driver capabilities:\n%s", caps_str); + VIR_TEST_DEBUG("QEMU driver capabilities:\n%s", caps_str); VIR_FREE(caps_str); } diff --git a/tests/utiltest.c b/tests/utiltest.c index 2dbd291..dfa4290 100644 --- a/tests/utiltest.c +++ b/tests/utiltest.c @@ -63,10 +63,8 @@ testDiskNameToIndex(const void *data ATTRIBUTE_UNUSED) idx = virDiskNameToIndex(diskName); if (idx < 0 || idx != i) { - if (virTestGetDebug() > 0) { - fprintf(stderr, "\nExpect [%zu]\n", i); - fprintf(stderr, "Actual [%d]\n", idx); - } + VIR_TEST_DEBUG("\nExpect [%zu]\n", i); + VIR_TEST_DEBUG("Actual [%d]\n", idx); VIR_FREE(diskName); @@ -115,11 +113,9 @@ testParseVersionString(const void *data ATTRIBUTE_UNUSED) versions[i].allowMissing); if (result != versions[i].result) { - if (virTestGetDebug() > 0) { - fprintf(stderr, "\nVersion string [%s]\n", versions[i].string); - fprintf(stderr, "Expect result [%d]\n", versions[i].result); - fprintf(stderr, "Actual result [%d]\n", result); - } + VIR_TEST_DEBUG("\nVersion string [%s]\n", versions[i].string); + VIR_TEST_DEBUG("Expect result [%d]\n", versions[i].result); + VIR_TEST_DEBUG("Actual result [%d]\n", result); return -1; } @@ -128,11 +124,9 @@ testParseVersionString(const void *data ATTRIBUTE_UNUSED) continue; if (version != versions[i].version) { - if (virTestGetDebug() > 0) { - fprintf(stderr, "\nVersion string [%s]\n", versions[i].string); - fprintf(stderr, "Expect version [%lu]\n", versions[i].version); - fprintf(stderr, "Actual version [%lu]\n", version); - } + VIR_TEST_DEBUG("\nVersion string [%s]\n", versions[i].string); + VIR_TEST_DEBUG("Expect version [%lu]\n", versions[i].version); + VIR_TEST_DEBUG("Actual version [%lu]\n", version); return -1; } @@ -166,11 +160,9 @@ testRoundValueToPowerOfTwo(const void *data ATTRIBUTE_UNUSED) for (i = 0; i < ARRAY_CARDINALITY(roundData); i++) { result = VIR_ROUND_UP_POWER_OF_TWO(roundData[i].input); if (roundData[i].output != result) { - if (virTestGetDebug() > 0) { - fprintf(stderr, "\nInput number [%u]\n", roundData[i].input); - fprintf(stderr, "Expected number [%u]\n", roundData[i].output); - fprintf(stderr, "Actual number [%u]\n", result); - } + VIR_TEST_DEBUG("\nInput number [%u]\n", roundData[i].input); + VIR_TEST_DEBUG("Expected number [%u]\n", roundData[i].output); + VIR_TEST_DEBUG("Actual number [%u]\n", result); return -1; } diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 10398d5..1c48ee0 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -12,12 +12,6 @@ #define VIR_FROM_THIS VIR_FROM_NONE -#define TEST_ERROR(...) \ - do { \ - if (virTestGetDebug()) \ - fprintf(stderr, __VA_ARGS__); \ - } while (0) - struct testInfo { int doEscape; }; @@ -50,7 +44,7 @@ static int testBufInfiniteLoop(const void *data) out: bufret = virBufferContentAndReset(buf); if (!bufret) { - TEST_ERROR("Buffer had error set"); + VIR_TEST_DEBUG("Buffer had error set"); ret = -1; } @@ -70,54 +64,54 @@ static int testBufAutoIndent(const void *data ATTRIBUTE_UNUSED) if (virBufferGetIndent(buf, false) != 0 || virBufferGetIndent(buf, true) != 0) { - TEST_ERROR("Wrong indentation"); + VIR_TEST_DEBUG("Wrong indentation"); ret = -1; } virBufferAdjustIndent(buf, 3); if (STRNEQ(virBufferCurrentContent(buf), "")) { - TEST_ERROR("Wrong content"); + VIR_TEST_DEBUG("Wrong content"); ret = -1; } if (virBufferGetIndent(buf, false) != 3 || virBufferGetIndent(buf, true) != 3 || virBufferError(buf)) { - TEST_ERROR("Wrong indentation"); + VIR_TEST_DEBUG("Wrong indentation"); ret = -1; } virBufferAdjustIndent(buf, -2); if (virBufferGetIndent(buf, false) != 1 || virBufferGetIndent(buf, true) != 1 || virBufferError(buf)) { - TEST_ERROR("Wrong indentation"); + VIR_TEST_DEBUG("Wrong indentation"); ret = -1; } virBufferAdjustIndent(buf, -3); if (virBufferGetIndent(buf, false) != -1 || virBufferGetIndent(buf, true) != -1 || virBufferError(buf) != -1) { - TEST_ERROR("Usage error not flagged"); + VIR_TEST_DEBUG("Usage error not flagged"); ret = -1; } virBufferFreeAndReset(buf); if (virBufferGetIndent(buf, false) != 0 || virBufferGetIndent(buf, true) != 0 || virBufferError(buf)) { - TEST_ERROR("Reset didn't clear indentation"); + VIR_TEST_DEBUG("Reset didn't clear indentation"); ret = -1; } virBufferAdjustIndent(buf, 2); virBufferAddLit(buf, "1"); if (virBufferError(buf)) { - TEST_ERROR("Buffer had error"); + VIR_TEST_DEBUG("Buffer had error"); return -1; } if (STRNEQ(virBufferCurrentContent(buf), " 1")) { - TEST_ERROR("Wrong content"); + VIR_TEST_DEBUG("Wrong content"); ret = -1; } if (virBufferGetIndent(buf, false) != 2 || virBufferGetIndent(buf, true) != 0) { - TEST_ERROR("Wrong indentation"); + VIR_TEST_DEBUG("Wrong indentation"); ret = -1; } virBufferAddLit(buf, "\n"); @@ -138,7 +132,7 @@ static int testBufAutoIndent(const void *data ATTRIBUTE_UNUSED) virBufferAddChar(buf, '\n'); if (virBufferError(buf)) { - TEST_ERROR("Buffer had error"); + VIR_TEST_DEBUG("Buffer had error"); return -1; } @@ -175,7 +169,7 @@ static int testBufTrim(const void *data ATTRIBUTE_UNUSED) virBufferTrim(buf, ",", -1); if (virBufferError(buf)) { - TEST_ERROR("Buffer had error"); + VIR_TEST_DEBUG("Buffer had error"); return -1; } @@ -187,7 +181,7 @@ static int testBufTrim(const void *data ATTRIBUTE_UNUSED) virBufferTrim(buf, NULL, -1); if (virBufferError(buf) != -1) { - TEST_ERROR("Usage error not flagged"); + VIR_TEST_DEBUG("Usage error not flagged"); goto cleanup; } @@ -223,17 +217,17 @@ static int testBufAddBuffer(const void *data ATTRIBUTE_UNUSED) " destroy an entire planet.\n"; if (virBufferUse(&buf1)) { - TEST_ERROR("buf1 already in use"); + VIR_TEST_DEBUG("buf1 already in use"); goto cleanup; } if (virBufferUse(&buf2)) { - TEST_ERROR("buf2 already in use"); + VIR_TEST_DEBUG("buf2 already in use"); goto cleanup; } if (virBufferUse(&buf3)) { - TEST_ERROR("buf3 already in use"); + VIR_TEST_DEBUG("buf3 already in use"); goto cleanup; } @@ -258,41 +252,41 @@ static int testBufAddBuffer(const void *data ATTRIBUTE_UNUSED) virBufferAddLit(&buf3, "destroy an entire planet.\n"); if (!virBufferUse(&buf1)) { - TEST_ERROR("Error adding to buf1"); + VIR_TEST_DEBUG("Error adding to buf1"); goto cleanup; } if (!virBufferUse(&buf2)) { - TEST_ERROR("Error adding to buf2"); + VIR_TEST_DEBUG("Error adding to buf2"); goto cleanup; } if (!virBufferUse(&buf3)) { - TEST_ERROR("Error adding to buf3"); + VIR_TEST_DEBUG("Error adding to buf3"); goto cleanup; } virBufferAddBuffer(&buf2, &buf3); if (!virBufferUse(&buf2)) { - TEST_ERROR("buf2 cleared mistakenly"); + VIR_TEST_DEBUG("buf2 cleared mistakenly"); goto cleanup; } if (virBufferUse(&buf3)) { - TEST_ERROR("buf3 is not clear even though it should be"); + VIR_TEST_DEBUG("buf3 is not clear even though it should be"); goto cleanup; } virBufferAddBuffer(&buf1, &buf2); if (!virBufferUse(&buf1)) { - TEST_ERROR("buf1 cleared mistakenly"); + VIR_TEST_DEBUG("buf1 cleared mistakenly"); goto cleanup; } if (virBufferUse(&buf2)) { - TEST_ERROR("buf2 is not clear even though it should be"); + VIR_TEST_DEBUG("buf2 is not clear even though it should be"); goto cleanup; } @@ -330,12 +324,12 @@ testBufAddStr(const void *opaque ATTRIBUTE_UNUSED) virBufferAddLit(&buf, "</c>"); if (!(actual = virBufferContentAndReset(&buf))) { - TEST_ERROR("buf is empty"); + VIR_TEST_DEBUG("buf is empty"); goto cleanup; } if (STRNEQ_NULLABLE(actual, data->expect)) { - TEST_ERROR("testBufAddStr(): Strings don't match:\n"); + VIR_TEST_DEBUG("testBufAddStr(): Strings don't match:\n"); virtTestDifference(stderr, data->expect, actual); goto cleanup; } @@ -363,12 +357,12 @@ testBufEscapeStr(const void *opaque ATTRIBUTE_UNUSED) virBufferAddLit(&buf, "</c>"); if (!(actual = virBufferContentAndReset(&buf))) { - TEST_ERROR("buf is empty"); + VIR_TEST_DEBUG("buf is empty"); goto cleanup; } if (STRNEQ_NULLABLE(actual, data->expect)) { - TEST_ERROR("testBufEscapeStr(): Strings don't match:\n"); + VIR_TEST_DEBUG("testBufEscapeStr(): Strings don't match:\n"); virtTestDifference(stderr, data->expect, actual); goto cleanup; } diff --git a/tests/virhashtest.c b/tests/virhashtest.c index c9d189b..41f0e76 100644 --- a/tests/virhashtest.c +++ b/tests/virhashtest.c @@ -17,18 +17,6 @@ VIR_LOG_INIT("tests.hashtest"); -#define testError(...) \ - do { \ - char *str; \ - if (virAsprintfQuiet(&str, __VA_ARGS__) >= 0) { \ - fprintf(stderr, "%s", str); \ - VIR_FREE(str); \ - } \ - /* Pad to line up with test name ... in virTestRun */ \ - fprintf(stderr, "%74s", "... "); \ - } while (0) - - static virHashTablePtr testHashInit(int size) { @@ -48,25 +36,22 @@ testHashInit(int size) return NULL; } - if (virHashTableSize(hash) != oldsize && virTestGetDebug()) { - VIR_WARN("hash grown from %zd to %zd", + if (virHashTableSize(hash) != oldsize) { + VIR_TEST_DEBUG("hash grown from %zd to %zd", (size_t)oldsize, (size_t)virHashTableSize(hash)); } } for (i = 0; i < ARRAY_CARDINALITY(uuids); i++) { if (!virHashLookup(hash, uuids[i])) { - if (virTestGetVerbose()) { - VIR_WARN("\nentry \"%s\" could not be found\n", - uuids[i]); - } + VIR_TEST_VERBOSE("\nentry \"%s\" could not be found\n", uuids[i]); virHashFree(hash); return NULL; } } - if (size && size != virHashTableSize(hash) && virTestGetDebug()) - fprintf(stderr, "\n"); + if (size && size != virHashTableSize(hash)) + VIR_TEST_DEBUG("\n"); return hash; } @@ -84,14 +69,14 @@ testHashCheckCount(virHashTablePtr hash, size_t count) ssize_t iter_count = 0; if (virHashSize(hash) != count) { - testError("\nhash contains %zu instead of %zu elements\n", + VIR_TEST_VERBOSE("\nhash contains %zu instead of %zu elements\n", (size_t)virHashSize(hash), count); return -1; } iter_count = virHashForEach(hash, testHashCheckForEachCount, NULL); if (count != iter_count) { - testError("\nhash claims to have %zu elements but iteration finds %zu\n", + VIR_TEST_VERBOSE("\nhash claims to have %zu elements but iteration finds %zu\n", count, (size_t)iter_count); return -1; } @@ -140,20 +125,16 @@ testHashUpdate(const void *data ATTRIBUTE_UNUSED) for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) { if (virHashUpdateEntry(hash, uuids_subset[i], (void *) 1) < 0) { - if (virTestGetVerbose()) { - fprintf(stderr, "\nentry \"%s\" could not be updated\n", - uuids_subset[i]); - } + VIR_TEST_VERBOSE("\nentry \"%s\" could not be updated\n", + uuids_subset[i]); goto cleanup; } } for (i = 0; i < ARRAY_CARDINALITY(uuids_new); i++) { if (virHashUpdateEntry(hash, uuids_new[i], (void *) 1) < 0) { - if (virTestGetVerbose()) { - fprintf(stderr, "\nnew entry \"%s\" could not be updated\n", - uuids_new[i]); - } + VIR_TEST_VERBOSE("\nnew entry \"%s\" could not be updated\n", + uuids_new[i]); goto cleanup; } } @@ -182,10 +163,8 @@ testHashRemove(const void *data ATTRIBUTE_UNUSED) for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) { if (virHashRemoveEntry(hash, uuids_subset[i]) < 0) { - if (virTestGetVerbose()) { - fprintf(stderr, "\nentry \"%s\" could not be removed\n", - uuids_subset[i]); - } + VIR_TEST_VERBOSE("\nentry \"%s\" could not be removed\n", + uuids_subset[i]); goto cleanup; } } @@ -214,8 +193,8 @@ testHashRemoveForEachSome(void *payload ATTRIBUTE_UNUSED, for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) { if (STREQ(uuids_subset[i], name)) { - if (virHashRemoveEntry(hash, name) < 0 && virTestGetVerbose()) { - fprintf(stderr, "\nentry \"%s\" could not be removed", + if (virHashRemoveEntry(hash, name) < 0) { + VIR_TEST_VERBOSE("\nentry \"%s\" could not be removed", uuids_subset[i]); } break; @@ -251,9 +230,8 @@ testHashRemoveForEachForbidden(void *payload ATTRIBUTE_UNUSED, if (STREQ(uuids_subset[i], name)) { int next = (i + 1) % ARRAY_CARDINALITY(uuids_subset); - if (virHashRemoveEntry(hash, uuids_subset[next]) == 0 && - virTestGetVerbose()) { - fprintf(stderr, + if (virHashRemoveEntry(hash, uuids_subset[next]) == 0) { + VIR_TEST_VERBOSE( "\nentry \"%s\" should not be allowed to be removed", uuids_subset[next]); } @@ -277,11 +255,9 @@ testHashRemoveForEach(const void *data) count = virHashForEach(hash, (virHashIterator) info->data, hash); if (count != ARRAY_CARDINALITY(uuids)) { - if (virTestGetVerbose()) { - testError("\nvirHashForEach didn't go through all entries," - " %d != %zu\n", - count, ARRAY_CARDINALITY(uuids)); - } + VIR_TEST_VERBOSE("\nvirHashForEach didn't go through all entries," + " %d != %zu\n", + count, ARRAY_CARDINALITY(uuids)); goto cleanup; } @@ -309,10 +285,8 @@ testHashSteal(const void *data ATTRIBUTE_UNUSED) for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) { if (!virHashSteal(hash, uuids_subset[i])) { - if (virTestGetVerbose()) { - fprintf(stderr, "\nentry \"%s\" could not be stolen\n", - uuids_subset[i]); - } + VIR_TEST_VERBOSE("\nentry \"%s\" could not be stolen\n", + uuids_subset[i]); goto cleanup; } } @@ -343,31 +317,21 @@ testHashForEachIter(void *payload ATTRIBUTE_UNUSED, { virHashTablePtr hash = data; - if (virHashAddEntry(hash, uuids_new[0], NULL) == 0 && - virTestGetVerbose()) { - fprintf(stderr, "\nadding entries in ForEach should be forbidden"); - } + if (virHashAddEntry(hash, uuids_new[0], NULL) == 0) + VIR_TEST_VERBOSE("\nadding entries in ForEach should be forbidden"); - if (virHashUpdateEntry(hash, uuids_new[0], NULL) == 0 && - virTestGetVerbose()) { - fprintf(stderr, "\nupdating entries in ForEach should be forbidden"); - } + if (virHashUpdateEntry(hash, uuids_new[0], NULL) == 0) + VIR_TEST_VERBOSE("\nupdating entries in ForEach should be forbidden"); - if (virHashSteal(hash, uuids_new[0]) != NULL && - virTestGetVerbose()) { - fprintf(stderr, "\nstealing entries in ForEach should be forbidden"); - } + if (virHashSteal(hash, uuids_new[0]) != NULL) + VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden"); - if (virHashSteal(hash, uuids_new[0]) != NULL && - virTestGetVerbose()) { - fprintf(stderr, "\nstealing entries in ForEach should be forbidden"); - } + if (virHashSteal(hash, uuids_new[0]) != NULL) + VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden"); - if (virHashForEach(hash, testHashIter, NULL) >= 0 && - virTestGetVerbose()) { - fprintf(stderr, "\niterating through hash in ForEach" + if (virHashForEach(hash, testHashIter, NULL) >= 0) + VIR_TEST_VERBOSE("\niterating through hash in ForEach" " should be forbidden"); - } } static int @@ -383,11 +347,9 @@ testHashForEach(const void *data ATTRIBUTE_UNUSED) count = virHashForEach(hash, testHashForEachIter, hash); if (count != ARRAY_CARDINALITY(uuids)) { - if (virTestGetVerbose()) { - testError("\nvirHashForEach didn't go through all entries," - " %d != %zu\n", - count, ARRAY_CARDINALITY(uuids)); - } + VIR_TEST_VERBOSE("\nvirHashForEach didn't go through all entries," + " %d != %zu\n", + count, ARRAY_CARDINALITY(uuids)); goto cleanup; } @@ -440,11 +402,9 @@ testHashRemoveSet(const void *data ATTRIBUTE_UNUSED) rcount = virHashRemoveSet(hash, testHashRemoveSetIter, &count); if (count != rcount) { - if (virTestGetVerbose()) { - testError("\nvirHashRemoveSet didn't remove expected number of" - " entries, %d != %u\n", - rcount, count); - } + VIR_TEST_VERBOSE("\nvirHashRemoveSet didn't remove expected number of" + " entries, %d != %u\n", + rcount, count); goto cleanup; } @@ -482,10 +442,8 @@ testHashSearch(const void *data ATTRIBUTE_UNUSED) entry = virHashSearch(hash, testHashSearchIter, NULL); if (!entry || STRNEQ(uuids_subset[testSearchIndex], entry)) { - if (virTestGetVerbose()) { - testError("\nvirHashSearch didn't find entry '%s'\n", - uuids_subset[testSearchIndex]); - } + VIR_TEST_VERBOSE("\nvirHashSearch didn't find entry '%s'\n", + uuids_subset[testSearchIndex]); goto cleanup; } @@ -531,15 +489,13 @@ testHashGetItems(const void *data ATTRIBUTE_UNUSED) virHashAddEntry(hash, keya, value3) < 0 || virHashAddEntry(hash, keyc, value1) < 0 || virHashAddEntry(hash, keyb, value2) < 0) { - if (virTestGetVerbose()) - testError("\nfailed to create hash"); + VIR_TEST_VERBOSE("\nfailed to create hash"); goto cleanup; } if (!(array = virHashGetItems(hash, NULL)) || array[3].key || array[3].value) { - if (virTestGetVerbose()) - testError("\nfailed to get items with NULL sort"); + VIR_TEST_VERBOSE("\nfailed to get items with NULL sort"); goto cleanup; } VIR_FREE(array); @@ -552,8 +508,7 @@ testHashGetItems(const void *data ATTRIBUTE_UNUSED) STRNEQ(array[2].key, "c") || STRNEQ(array[2].value, "1") || array[3].key || array[3].value) { - if (virTestGetVerbose()) - testError("\nfailed to get items with key sort"); + VIR_TEST_VERBOSE("\nfailed to get items with key sort"); goto cleanup; } VIR_FREE(array); @@ -566,8 +521,7 @@ testHashGetItems(const void *data ATTRIBUTE_UNUSED) STRNEQ(array[2].key, "a") || STRNEQ(array[2].value, "3") || array[3].key || array[3].value) { - if (virTestGetVerbose()) - testError("\nfailed to get items with value sort"); + VIR_TEST_VERBOSE("\nfailed to get items with value sort"); goto cleanup; } @@ -608,38 +562,32 @@ testHashEqual(const void *data ATTRIBUTE_UNUSED) virHashAddEntry(hash1, keyc, value3_l) < 0 || virHashAddEntry(hash2, keya, value1_u) < 0 || virHashAddEntry(hash2, keyb, value2_u) < 0) { - if (virTestGetVerbose()) - testError("\nfailed to create hashes"); + VIR_TEST_VERBOSE("\nfailed to create hashes"); goto cleanup; } if (virHashEqual(hash1, hash2, testHashEqualCompValue)) { - if (virTestGetVerbose()) - testError("\nfailed equal test for different number of elements"); + VIR_TEST_VERBOSE("\nfailed equal test for different number of elements"); goto cleanup; } if (virHashAddEntry(hash2, keyc, value4_u) < 0) { - if (virTestGetVerbose()) - testError("\nfailed to add element to hash2"); + VIR_TEST_VERBOSE("\nfailed to add element to hash2"); goto cleanup; } if (virHashEqual(hash1, hash2, testHashEqualCompValue)) { - if (virTestGetVerbose()) - testError("\nfailed equal test for same number of elements"); + VIR_TEST_VERBOSE("\nfailed equal test for same number of elements"); goto cleanup; } if (virHashUpdateEntry(hash2, keyc, value3_u) < 0) { - if (virTestGetVerbose()) - testError("\nfailed to update element in hash2"); + VIR_TEST_VERBOSE("\nfailed to update element in hash2"); goto cleanup; } if (!virHashEqual(hash1, hash2, testHashEqualCompValue)) { - if (virTestGetVerbose()) - testError("\nfailed equal test for equal hash tables"); + VIR_TEST_VERBOSE("\nfailed equal test for equal hash tables"); goto cleanup; } diff --git a/tests/virpcitest.c b/tests/virpcitest.c index 4a8e4a9..d4d3253 100644 --- a/tests/virpcitest.c +++ b/tests/virpcitest.c @@ -359,12 +359,12 @@ mymain(void) char *fakesysfsdir; if (VIR_STRDUP_QUIET(fakesysfsdir, FAKESYSFSDIRTEMPLATE) < 0) { - fprintf(stderr, "Out of memory\n"); + VIR_TEST_DEBUG("Out of memory\n"); abort(); } if (!mkdtemp(fakesysfsdir)) { - fprintf(stderr, "Cannot create fakesysfsdir"); + VIR_TEST_DEBUG("Cannot create fakesysfsdir"); abort(); } diff --git a/tests/virportallocatortest.c b/tests/virportallocatortest.c index ef503ce..1125b0a 100644 --- a/tests/virportallocatortest.c +++ b/tests/virportallocatortest.c @@ -49,7 +49,7 @@ static void init_syms(void) realsocket = dlsym(RTLD_NEXT, "socket"); if (!realsocket) { - fprintf(stderr, "Unable to find 'socket' symbol\n"); + VIR_TEST_DEBUG("Unable to find 'socket' symbol\n"); abort(); } @@ -132,54 +132,47 @@ static int testAllocAll(const void *args ATTRIBUTE_UNUSED) if (virPortAllocatorAcquire(alloc, &p1) < 0) goto cleanup; if (p1 != 5901) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5901, got %d", p1); + VIR_TEST_DEBUG("Expected 5901, got %d", p1); goto cleanup; } if (virPortAllocatorAcquire(alloc, &p2) < 0) goto cleanup; if (p2 != 5902) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5902, got %d", p2); + VIR_TEST_DEBUG("Expected 5902, got %d", p2); goto cleanup; } if (virPortAllocatorAcquire(alloc, &p3) < 0) goto cleanup; if (p3 != 5903) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5903, got %d", p3); + VIR_TEST_DEBUG("Expected 5903, got %d", p3); goto cleanup; } if (virPortAllocatorAcquire(alloc, &p4) < 0) goto cleanup; if (p4 != 5907) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5907, got %d", p4); + VIR_TEST_DEBUG("Expected 5907, got %d", p4); goto cleanup; } if (virPortAllocatorAcquire(alloc, &p5) < 0) goto cleanup; if (p5 != 5908) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5908, got %d", p5); + VIR_TEST_DEBUG("Expected 5908, got %d", p5); goto cleanup; } if (virPortAllocatorAcquire(alloc, &p6) < 0) goto cleanup; if (p6 != 5909) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5909, got %d", p6); + VIR_TEST_DEBUG("Expected 5909, got %d", p6); goto cleanup; } if (virPortAllocatorAcquire(alloc, &p7) == 0) { - if (virTestGetDebug()) - fprintf(stderr, "Expected error, got %d", p7); + VIR_TEST_DEBUG("Expected error, got %d", p7); goto cleanup; } @@ -203,24 +196,21 @@ static int testAllocReuse(const void *args ATTRIBUTE_UNUSED) if (virPortAllocatorAcquire(alloc, &p1) < 0) goto cleanup; if (p1 != 5901) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5901, got %d", p1); + VIR_TEST_DEBUG("Expected 5901, got %d", p1); goto cleanup; } if (virPortAllocatorAcquire(alloc, &p2) < 0) goto cleanup; if (p2 != 5902) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5902, got %d", p2); + VIR_TEST_DEBUG("Expected 5902, got %d", p2); goto cleanup; } if (virPortAllocatorAcquire(alloc, &p3) < 0) goto cleanup; if (p3 != 5903) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5903, got %d", p3); + VIR_TEST_DEBUG("Expected 5903, got %d", p3); goto cleanup; } @@ -231,8 +221,7 @@ static int testAllocReuse(const void *args ATTRIBUTE_UNUSED) if (virPortAllocatorAcquire(alloc, &p4) < 0) goto cleanup; if (p4 != 5902) { - if (virTestGetDebug()) - fprintf(stderr, "Expected 5902, got %d", p4); + VIR_TEST_DEBUG("Expected 5902, got %d", p4); goto cleanup; } -- 2.3.5

On 04/23/2015 02:19 PM, Cole Robinson wrote:
To remove a bunch of TestGetDebug()/TestGetVerbose() checks --- tests/cputest.c | 34 ++++----- tests/jsontest.c | 48 +++++-------- tests/nodeinfotest.c | 2 +- tests/qemuargv2xmltest.c | 15 ++-- tests/qemuhelptest.c | 6 +- tests/qemuhotplugtest.c | 19 +++-- tests/qemumonitortest.c | 24 +++---- tests/qemuxml2argvtest.c | 14 ++-- tests/qemuxmlnstest.c | 6 +- tests/securityselinuxlabeltest.c | 6 +- tests/statstest.c | 3 +- tests/testutils.c | 2 +- tests/testutils.h | 12 ++++ tests/testutilslxc.c | 2 +- tests/testutilsqemu.c | 2 +- tests/utiltest.c | 30 +++----- tests/virbuftest.c | 60 +++++++--------- tests/virhashtest.c | 152 +++++++++++++-------------------------- tests/virpcitest.c | 4 +- tests/virportallocatortest.c | 35 ++++----- 20 files changed, 188 insertions(+), 288 deletions(-)
rather than make you crawl through all of this to find the comment - testutils.h fails make syntax-check when cppi is installed, due to improper indentation (you need to put a space between # and define). It's also missing an update to the copyright date. Other than that looks fine. Nice cleanup! ACK.

--- tests/bhyvexml2xmltest.c | 9 ++------- tests/domainconftest.c | 7 +------ tests/lxcxml2xmltest.c | 8 ++------ tests/networkxml2conftest.c | 7 +------ tests/networkxml2xmltest.c | 6 +----- tests/networkxml2xmlupdatetest.c | 6 +----- tests/nwfilterxml2xmltest.c | 6 +----- tests/qemuagenttest.c | 9 ++------- tests/secretxml2xmltest.c | 6 +----- tests/securityselinuxlabeltest.c | 7 +------ tests/storagebackendsheepdogtest.c | 20 +++----------------- tests/storagepoolxml2xmltest.c | 6 +----- tests/storagevolxml2argvtest.c | 26 ++++---------------------- tests/storagevolxml2xmltest.c | 12 ++---------- tests/xlconfigtest.c | 9 ++------- tests/xmconfigtest.c | 9 ++------- tests/xml2sexprtest.c | 9 ++------- tests/xml2vmxtest.c | 9 ++------- 18 files changed, 31 insertions(+), 140 deletions(-) diff --git a/tests/bhyvexml2xmltest.c b/tests/bhyvexml2xmltest.c index 2e742cf..740c957 100644 --- a/tests/bhyvexml2xmltest.c +++ b/tests/bhyvexml2xmltest.c @@ -14,20 +14,16 @@ static bhyveConn driver; static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { - char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; virDomainDefPtr def = NULL; int ret = -1; - if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; - if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail; - if (!(def = virDomainDefParseString(inXmlData, driver.caps, driver.xmlopt, - VIR_DOMAIN_DEF_PARSE_INACTIVE))) + if (!(def = virDomainDefParseFile(inxml, driver.caps, driver.xmlopt, + VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; if (!(actual = virDomainDefFormat(def, VIR_DOMAIN_DEF_FORMAT_INACTIVE))) @@ -41,7 +37,6 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml) ret = 0; fail: - VIR_FREE(inXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); virDomainDefFree(def); diff --git a/tests/domainconftest.c b/tests/domainconftest.c index b27dd14..53c40c1 100644 --- a/tests/domainconftest.c +++ b/tests/domainconftest.c @@ -43,7 +43,6 @@ struct testGetFilesystemData { static int testGetFilesystem(const void *opaque) { int ret = -1; - char *xmlData = NULL; virDomainDefPtr def = NULL; char *filename = NULL; const struct testGetFilesystemData *data = opaque; @@ -53,10 +52,7 @@ static int testGetFilesystem(const void *opaque) abs_srcdir, data->filename) < 0) goto cleanup; - if (virtTestLoadFile(filename, &xmlData) < 0) - goto cleanup; - - if (!(def = virDomainDefParseString(xmlData, caps, xmlopt, 0))) + if (!(def = virDomainDefParseFile(filename, caps, xmlopt, 0))) goto cleanup; fsdef = virDomainGetFilesystemForTarget(def, @@ -79,7 +75,6 @@ static int testGetFilesystem(const void *opaque) cleanup: virDomainDefFree(def); - VIR_FREE(xmlData); VIR_FREE(filename); return ret; } diff --git a/tests/lxcxml2xmltest.c b/tests/lxcxml2xmltest.c index 37997f5..a9b5419 100644 --- a/tests/lxcxml2xmltest.c +++ b/tests/lxcxml2xmltest.c @@ -25,19 +25,16 @@ static virDomainXMLOptionPtr xmlopt; static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml, bool live) { - char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; int ret = -1; virDomainDefPtr def = NULL; - if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail; - if (!(def = virDomainDefParseString(inXmlData, caps, xmlopt, - live ? 0 : VIR_DOMAIN_DEF_PARSE_INACTIVE))) + if (!(def = virDomainDefParseFile(inxml, caps, xmlopt, + live ? 0 : VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; if (!virDomainDefCheckABIStability(def, def)) { @@ -55,7 +52,6 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, bool live) ret = 0; fail: - VIR_FREE(inXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); virDomainDefFree(def); diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index 6df161f..1d9a772 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -21,7 +21,6 @@ static int testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps) { - char *inXmlData = NULL; char *outConfData = NULL; char *actual = NULL; int ret = -1; @@ -31,13 +30,10 @@ testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr char *pidfile = NULL; dnsmasqContext *dctx = NULL; - if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; - if (virtTestLoadFile(outconf, &outConfData) < 0) goto fail; - if (!(dev = virNetworkDefParseString(inXmlData))) + if (!(dev = virNetworkDefParseFile(inxml))) goto fail; if (!(obj = virNetworkObjNew())) @@ -61,7 +57,6 @@ testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr ret = 0; fail: - VIR_FREE(inXmlData); VIR_FREE(outConfData); VIR_FREE(actual); VIR_FREE(pidfile); diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c index 34a5211..467be2a 100644 --- a/tests/networkxml2xmltest.c +++ b/tests/networkxml2xmltest.c @@ -20,18 +20,15 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml, unsigned int flags) { - char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; int ret = -1; virNetworkDefPtr dev = NULL; - if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail; - if (!(dev = virNetworkDefParseString(inXmlData))) + if (!(dev = virNetworkDefParseFile(inxml))) goto fail; if (!(actual = virNetworkDefFormat(dev, flags))) @@ -45,7 +42,6 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, ret = 0; fail: - VIR_FREE(inXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); virNetworkDefFree(dev); diff --git a/tests/networkxml2xmlupdatetest.c b/tests/networkxml2xmlupdatetest.c index f34638a..2051a01 100644 --- a/tests/networkxml2xmlupdatetest.c +++ b/tests/networkxml2xmlupdatetest.c @@ -22,19 +22,16 @@ testCompareXMLToXMLFiles(const char *netxml, const char *updatexml, unsigned int command, unsigned int section, int parentIndex, bool expectFailure) { - char *netXmlData = NULL; char *updateXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; int ret = -1; virNetworkDefPtr def = NULL; - if (virtTestLoadFile(netxml, &netXmlData) < 0) - goto error; if (virtTestLoadFile(updatexml, &updateXmlData) < 0) goto error; - if (!(def = virNetworkDefParseString(netXmlData))) + if (!(def = virNetworkDefParseFile(netxml))) goto fail; if (virNetworkDefUpdateSection(def, command, section, parentIndex, @@ -67,7 +64,6 @@ testCompareXMLToXMLFiles(const char *netxml, const char *updatexml, } } error: - VIR_FREE(netXmlData); VIR_FREE(updateXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); diff --git a/tests/nwfilterxml2xmltest.c b/tests/nwfilterxml2xmltest.c index ba1ead1..7640af0 100644 --- a/tests/nwfilterxml2xmltest.c +++ b/tests/nwfilterxml2xmltest.c @@ -23,20 +23,17 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml, bool expect_error) { - char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; int ret = -1; virNWFilterDefPtr dev = NULL; - if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail; virResetLastError(); - if (!(dev = virNWFilterDefParseString(inXmlData))) { + if (!(dev = virNWFilterDefParseFile(inxml))) { if (expect_error) { virResetLastError(); goto done; @@ -56,7 +53,6 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, ret = 0; fail: - VIR_FREE(inXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); virNWFilterDefFree(dev); diff --git a/tests/qemuagenttest.c b/tests/qemuagenttest.c index 4410ffe..52cc834 100644 --- a/tests/qemuagenttest.c +++ b/tests/qemuagenttest.c @@ -170,7 +170,6 @@ testQemuAgentGetFSInfo(const void *data) virCapsPtr caps = testQemuCapsInit(); qemuMonitorTestPtr test = qemuMonitorTestNewAgent(xmlopt); char *domain_filename = NULL; - char *domain_xml = NULL; virDomainDefPtr def = NULL; virDomainFSInfoPtr *info = NULL; int ret = -1, ninfo = 0, i; @@ -182,11 +181,8 @@ testQemuAgentGetFSInfo(const void *data) abs_srcdir) < 0) goto cleanup; - if (virtTestLoadFile(domain_filename, &domain_xml) < 0) - goto cleanup; - - if (!(def = virDomainDefParseString(domain_xml, caps, xmlopt, - VIR_DOMAIN_DEF_PARSE_INACTIVE))) + if (!(def = virDomainDefParseFile(domain_filename, caps, xmlopt, + VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto cleanup; if (qemuMonitorTestAddAgentSyncResponse(test) < 0) @@ -297,7 +293,6 @@ testQemuAgentGetFSInfo(const void *data) virDomainFSInfoFree(info[i]); VIR_FREE(info); VIR_FREE(domain_filename); - VIR_FREE(domain_xml); virObjectUnref(caps); virDomainDefFree(def); qemuMonitorTestFree(test); diff --git a/tests/secretxml2xmltest.c b/tests/secretxml2xmltest.c index c9d879e..cf5f00f 100644 --- a/tests/secretxml2xmltest.c +++ b/tests/secretxml2xmltest.c @@ -11,18 +11,15 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { - char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; int ret = -1; virSecretDefPtr secret = NULL; - if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail; - if (!(secret = virSecretDefParseString(inXmlData))) + if (!(secret = virSecretDefParseFile(inxml))) goto fail; if (!(actual = virSecretDefFormat(secret))) @@ -36,7 +33,6 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml) ret = 0; fail: - VIR_FREE(inXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); virSecretDefFree(secret); diff --git a/tests/securityselinuxlabeltest.c b/tests/securityselinuxlabeltest.c index 730f7be..85fad37 100644 --- a/tests/securityselinuxlabeltest.c +++ b/tests/securityselinuxlabeltest.c @@ -182,7 +182,6 @@ static virDomainDefPtr testSELinuxLoadDef(const char *testname) { char *xmlfile = NULL; - char *xmlstr = NULL; virDomainDefPtr def = NULL; size_t i; @@ -190,10 +189,7 @@ testSELinuxLoadDef(const char *testname) abs_srcdir, testname) < 0) goto cleanup; - if (virFileReadAll(xmlfile, 1024*1024, &xmlstr) < 0) - goto cleanup; - - if (!(def = virDomainDefParseString(xmlstr, caps, xmlopt, 0))) + if (!(def = virDomainDefParseFile(xmlfile, caps, xmlopt, 0))) goto cleanup; for (i = 0; i < def->ndisks; i++) { @@ -230,7 +226,6 @@ testSELinuxLoadDef(const char *testname) cleanup: VIR_FREE(xmlfile); - VIR_FREE(xmlstr); return def; } diff --git a/tests/storagebackendsheepdogtest.c b/tests/storagebackendsheepdogtest.c index c85a9b4..7744119 100644 --- a/tests/storagebackendsheepdogtest.c +++ b/tests/storagebackendsheepdogtest.c @@ -50,13 +50,9 @@ test_node_info_parser(collie_test test, char *poolxml) { int ret = -1; char *output = NULL; - char *poolXmlData = NULL; virStoragePoolDefPtr pool = NULL; - if (virtTestLoadFile(poolxml, &poolXmlData) < 0) - goto cleanup; - - if (!(pool = virStoragePoolDefParseString(poolXmlData))) + if (!(pool = virStoragePoolDefParseFile(poolxml))) goto cleanup; if (VIR_STRDUP(output, test.output) < 0) @@ -77,7 +73,6 @@ test_node_info_parser(collie_test test, char *poolxml) cleanup: VIR_FREE(output); - VIR_FREE(poolXmlData); virStoragePoolDefFree(pool); return ret; } @@ -86,21 +81,14 @@ static int test_vdi_list_parser(collie_test test, char *poolxml, char *volxml) { int ret = -1; - char *poolXmlData = NULL; - char *volXmlData = NULL; char *output = NULL; virStoragePoolDefPtr pool = NULL; virStorageVolDefPtr vol = NULL; - if (virtTestLoadFile(poolxml, &poolXmlData) < 0) - goto cleanup; - if (virtTestLoadFile(volxml, &volXmlData) < 0) - goto cleanup; - - if (!(pool = virStoragePoolDefParseString(poolXmlData))) + if (!(pool = virStoragePoolDefParseFile(poolxml))) goto cleanup; - if (!(vol = virStorageVolDefParseString(pool, volXmlData, 0))) + if (!(vol = virStorageVolDefParseFile(pool, volxml, 0))) goto cleanup; if (VIR_STRDUP(output, test.output) < 0) @@ -121,8 +109,6 @@ test_vdi_list_parser(collie_test test, char *poolxml, char *volxml) cleanup: VIR_FREE(output); - VIR_FREE(poolXmlData); - VIR_FREE(volXmlData); virStoragePoolDefFree(pool); virStorageVolDefFree(vol); return ret; diff --git a/tests/storagepoolxml2xmltest.c b/tests/storagepoolxml2xmltest.c index 270f75d..54b2a00 100644 --- a/tests/storagepoolxml2xmltest.c +++ b/tests/storagepoolxml2xmltest.c @@ -19,18 +19,15 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { - char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; int ret = -1; virStoragePoolDefPtr dev = NULL; - if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail; - if (!(dev = virStoragePoolDefParseString(inXmlData))) + if (!(dev = virStoragePoolDefParseFile(inxml))) goto fail; if (!(actual = virStoragePoolDefFormat(dev))) @@ -44,7 +41,6 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml) ret = 0; fail: - VIR_FREE(inXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); virStoragePoolDefFree(dev); diff --git a/tests/storagevolxml2argvtest.c b/tests/storagevolxml2argvtest.c index f5f86c8..d957f63 100644 --- a/tests/storagevolxml2argvtest.c +++ b/tests/storagevolxml2argvtest.c @@ -43,10 +43,6 @@ testCompareXMLToArgvFiles(bool shouldFail, int imgformat, unsigned long parse_flags) { - char *volXmlData = NULL; - char *poolXmlData = NULL; - char *inputpoolXmlData = NULL; - char *inputvolXmlData = NULL; char *expectedCmdline = NULL; char *actualCmdline = NULL; int ret = -1; @@ -65,34 +61,24 @@ testCompareXMLToArgvFiles(bool shouldFail, if (!(conn = virGetConnect())) goto cleanup; - if (virtTestLoadFile(poolxml, &poolXmlData) < 0) - goto cleanup; - if (virtTestLoadFile(volxml, &volXmlData) < 0) - goto cleanup; - if (inputvolxml && - virtTestLoadFile(inputvolxml, &inputvolXmlData) < 0) - goto cleanup; - - if (!(pool = virStoragePoolDefParseString(poolXmlData))) + if (!(pool = virStoragePoolDefParseFile(poolxml))) goto cleanup; poolobj.def = pool; if (inputpoolxml) { - if (virtTestLoadFile(inputpoolxml, &inputpoolXmlData) < 0) - goto cleanup; - if (!(inputpool = virStoragePoolDefParseString(inputpoolXmlData))) + if (!(inputpool = virStoragePoolDefParseFile(inputpoolxml))) goto cleanup; } if (inputvolxml) parse_flags |= VIR_VOL_XML_PARSE_NO_CAPACITY; - if (!(vol = virStorageVolDefParseString(pool, volXmlData, parse_flags))) + if (!(vol = virStorageVolDefParseFile(pool, volxml, parse_flags))) goto cleanup; if (inputvolxml && - !(inputvol = virStorageVolDefParseString(inputpool, inputvolXmlData, 0))) + !(inputvol = virStorageVolDefParseFile(inputpool, inputvolxml, 0))) goto cleanup; testSetVolumeType(vol, pool); @@ -133,10 +119,6 @@ testCompareXMLToArgvFiles(bool shouldFail, virCommandFree(cmd); VIR_FREE(actualCmdline); VIR_FREE(expectedCmdline); - VIR_FREE(inputpoolXmlData); - VIR_FREE(poolXmlData); - VIR_FREE(volXmlData); - VIR_FREE(inputvolXmlData); virObjectUnref(conn); return ret; } diff --git a/tests/storagevolxml2xmltest.c b/tests/storagevolxml2xmltest.c index 0d6c818..64b6673 100644 --- a/tests/storagevolxml2xmltest.c +++ b/tests/storagevolxml2xmltest.c @@ -20,25 +20,19 @@ static int testCompareXMLToXMLFiles(const char *poolxml, const char *inxml, const char *outxml, unsigned int flags) { - char *poolXmlData = NULL; - char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; int ret = -1; virStoragePoolDefPtr pool = NULL; virStorageVolDefPtr dev = NULL; - if (virtTestLoadFile(poolxml, &poolXmlData) < 0) - goto fail; - if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail; - if (!(pool = virStoragePoolDefParseString(poolXmlData))) + if (!(pool = virStoragePoolDefParseFile(poolxml))) goto fail; - if (!(dev = virStorageVolDefParseString(pool, inXmlData, flags))) + if (!(dev = virStorageVolDefParseFile(pool, inxml, flags))) goto fail; if (!(actual = virStorageVolDefFormat(pool, dev))) @@ -52,8 +46,6 @@ testCompareXMLToXMLFiles(const char *poolxml, const char *inxml, ret = 0; fail: - VIR_FREE(poolXmlData); - VIR_FREE(inXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); virStoragePoolDefFree(pool); diff --git a/tests/xlconfigtest.c b/tests/xlconfigtest.c index 8806566..899cb1b 100644 --- a/tests/xlconfigtest.c +++ b/tests/xlconfigtest.c @@ -48,7 +48,6 @@ static virDomainXMLOptionPtr xmlopt; static int testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) { - char *xmlData = NULL; char *xmcfgData = NULL; char *gotxmcfgData = NULL; virConfPtr conf = NULL; @@ -63,14 +62,11 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) conn = virGetConnect(); if (!conn) goto fail; - if (virtTestLoadFile(xml, &xmlData) < 0) - goto fail; - if (virtTestLoadFile(xmcfg, &xmcfgData) < 0) goto fail; - if (!(def = virDomainDefParseString(xmlData, caps, xmlopt, - VIR_DOMAIN_XML_INACTIVE))) + if (!(def = virDomainDefParseFile(xml, caps, xmlopt, + VIR_DOMAIN_XML_INACTIVE))) goto fail; if (!virDomainDefCheckABIStability(def, def)) { @@ -93,7 +89,6 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) ret = 0; fail: - VIR_FREE(xmlData); VIR_FREE(xmcfgData); VIR_FREE(gotxmcfgData); if (conf) diff --git a/tests/xmconfigtest.c b/tests/xmconfigtest.c index 2db012e..1d67c5b 100644 --- a/tests/xmconfigtest.c +++ b/tests/xmconfigtest.c @@ -45,7 +45,6 @@ static virDomainXMLOptionPtr xmlopt; static int testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) { - char *xmlData = NULL; char *xmcfgData = NULL; char *gotxmcfgData = NULL; virConfPtr conf = NULL; @@ -61,9 +60,6 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) conn = virGetConnect(); if (!conn) goto fail; - if (virtTestLoadFile(xml, &xmlData) < 0) - goto fail; - if (virtTestLoadFile(xmcfg, &xmcfgData) < 0) goto fail; @@ -72,8 +68,8 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) priv.caps = caps; conn->privateData = &priv; - if (!(def = virDomainDefParseString(xmlData, caps, xmlopt, - VIR_DOMAIN_DEF_PARSE_INACTIVE))) + if (!(def = virDomainDefParseFile(xml, caps, xmlopt, + VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; if (!virDomainDefCheckABIStability(def, def)) { @@ -96,7 +92,6 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) ret = 0; fail: - VIR_FREE(xmlData); VIR_FREE(xmcfgData); VIR_FREE(gotxmcfgData); if (conf) diff --git a/tests/xml2sexprtest.c b/tests/xml2sexprtest.c index c706914..32c8a32 100644 --- a/tests/xml2sexprtest.c +++ b/tests/xml2sexprtest.c @@ -23,20 +23,16 @@ static virDomainXMLOptionPtr xmlopt; static int testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion) { - char *xmlData = NULL; char *sexprData = NULL; char *gotsexpr = NULL; int ret = -1; virDomainDefPtr def = NULL; - if (virtTestLoadFile(xml, &xmlData) < 0) - goto fail; - if (virtTestLoadFile(sexpr, &sexprData) < 0) goto fail; - if (!(def = virDomainDefParseString(xmlData, caps, xmlopt, - VIR_DOMAIN_DEF_PARSE_INACTIVE))) + if (!(def = virDomainDefParseFile(xml, caps, xmlopt, + VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; if (!virDomainDefCheckABIStability(def, def)) { @@ -55,7 +51,6 @@ testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion) ret = 0; fail: - VIR_FREE(xmlData); VIR_FREE(sexprData); VIR_FREE(gotsexpr); virDomainDefFree(def); diff --git a/tests/xml2vmxtest.c b/tests/xml2vmxtest.c index b576f83..6d7d4c2 100644 --- a/tests/xml2vmxtest.c +++ b/tests/xml2vmxtest.c @@ -73,19 +73,15 @@ static int testCompareFiles(const char *xml, const char *vmx, int virtualHW_version) { int result = -1; - char *xmlData = NULL; char *vmxData = NULL; char *formatted = NULL; virDomainDefPtr def = NULL; - if (virtTestLoadFile(xml, &xmlData) < 0) - goto failure; - if (virtTestLoadFile(vmx, &vmxData) < 0) goto failure; - def = virDomainDefParseString(xmlData, caps, xmlopt, - VIR_DOMAIN_DEF_PARSE_INACTIVE); + def = virDomainDefParseFile(xml, caps, xmlopt, + VIR_DOMAIN_DEF_PARSE_INACTIVE); if (def == NULL) goto failure; @@ -108,7 +104,6 @@ testCompareFiles(const char *xml, const char *vmx, int virtualHW_version) result = 0; failure: - VIR_FREE(xmlData); VIR_FREE(vmxData); VIR_FREE(formatted); virDomainDefFree(def); -- 2.3.5

On 04/23/2015 02:19 PM, Cole Robinson wrote:
--- tests/bhyvexml2xmltest.c | 9 ++------- tests/domainconftest.c | 7 +------ tests/lxcxml2xmltest.c | 8 ++------ tests/networkxml2conftest.c | 7 +------ tests/networkxml2xmltest.c | 6 +----- tests/networkxml2xmlupdatetest.c | 6 +----- tests/nwfilterxml2xmltest.c | 6 +----- tests/qemuagenttest.c | 9 ++------- tests/secretxml2xmltest.c | 6 +----- tests/securityselinuxlabeltest.c | 7 +------ tests/storagebackendsheepdogtest.c | 20 +++----------------- tests/storagepoolxml2xmltest.c | 6 +----- tests/storagevolxml2argvtest.c | 26 ++++---------------------- tests/storagevolxml2xmltest.c | 12 ++---------- tests/xlconfigtest.c | 9 ++------- tests/xmconfigtest.c | 9 ++------- tests/xml2sexprtest.c | 9 ++------- tests/xml2vmxtest.c | 9 ++------- 18 files changed, 31 insertions(+), 140 deletions(-)
diff --git a/tests/bhyvexml2xmltest.c b/tests/bhyvexml2xmltest.c index 2e742cf..740c957 100644 --- a/tests/bhyvexml2xmltest.c +++ b/tests/bhyvexml2xmltest.c @@ -14,20 +14,16 @@ static bhyveConn driver; static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { - char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; virDomainDefPtr def = NULL; int ret = -1;
- if (virtTestLoadFile(inxml, &inXmlData) < 0) - goto fail; - if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail;
- if (!(def = virDomainDefParseString(inXmlData, driver.caps, driver.xmlopt, - VIR_DOMAIN_DEF_PARSE_INACTIVE))) + if (!(def = virDomainDefParseFile(inxml, driver.caps, driver.xmlopt, + VIR_DOMAIN_DEF_PARSE_INACTIVE)))
You'll see a minor conflict here due to a recent change pushed upstream. ACK.

Replaces a common pattern used in many test files --- tests/bhyvexml2argvtest.c | 41 ++++++---------------------------------- tests/bhyvexml2xmltest.c | 9 +-------- tests/cputest.c | 11 +---------- tests/domaincapstest.c | 9 +-------- tests/lxcxml2xmltest.c | 9 +-------- tests/networkxml2conftest.c | 9 +-------- tests/networkxml2firewalltest.c | 9 +-------- tests/networkxml2xmltest.c | 9 +-------- tests/networkxml2xmlupdatetest.c | 9 +-------- tests/nodeinfotest.c | 18 ++---------------- tests/nwfilterxml2firewalltest.c | 11 +---------- tests/nwfilterxml2xmltest.c | 9 +-------- tests/qemucaps2xmltest.c | 27 ++++---------------------- tests/qemumonitorjsontest.c | 9 ++------- tests/qemuxml2argvtest.c | 13 +------------ tests/qemuxmlnstest.c | 13 +------------ tests/secretxml2xmltest.c | 9 +-------- tests/sexpr2xmltest.c | 9 +-------- tests/storagepoolxml2xmltest.c | 9 +-------- tests/storagevolxml2argvtest.c | 14 +------------- tests/storagevolxml2xmltest.c | 9 +-------- tests/sysinfotest.c | 10 ++-------- tests/testutils.c | 33 ++++++++++++++++++++++++++++++++ tests/testutils.h | 2 ++ tests/vircaps2xmltest.c | 10 +--------- tests/vircgrouptest.c | 9 +-------- tests/vmx2xmltest.c | 9 +-------- tests/xencapstest.c | 9 +-------- tests/xlconfigtest.c | 18 ++---------------- tests/xmconfigtest.c | 18 ++---------------- tests/xml2sexprtest.c | 9 +-------- tests/xml2vmxtest.c | 10 +--------- 32 files changed, 78 insertions(+), 324 deletions(-) diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c index 26d0075..1cce2aa 100644 --- a/tests/bhyvexml2argvtest.c +++ b/tests/bhyvexml2argvtest.c @@ -19,7 +19,6 @@ static int testCompareXMLToArgvFiles(const char *xml, const char *ldcmdline, const char *dmcmdline) { - char *expectargv = NULL, *expectld = NULL, *expectdm = NULL; int len; char *actualargv = NULL, *actualld = NULL, *actualdm = NULL; virDomainDefPtr vmdef = NULL; @@ -54,51 +53,23 @@ static int testCompareXMLToArgvFiles(const char *xml, if (!(actualld = virCommandToString(ldcmd))) goto out; - len = virtTestLoadFile(cmdline, &expectargv); - if (len < 0) + if (virtTestCompareToFile(actualargv, cmdline) < 0) goto out; - if (len && expectargv[len - 1] == '\n') - expectargv[len - 1] = '\0'; - - len = virtTestLoadFile(ldcmdline, &expectld); - if (len < 0) + if (virtTestCompareToFile(actualld, ldcmdline) < 0) goto out; - if (len && expectld[len - 1] == '\n') - expectld[len - 1] = '\0'; - - len = virFileReadAllQuiet(dmcmdline, 1000, &expectdm); - if (len < 0) { - if (actualdm != NULL) { - virtTestDifference(stderr, "", actualdm); - goto out; - } - } else if (len && expectdm[len - 1] == '\n') { - expectdm[len - 1] = '\0'; - } - - if (STRNEQ(expectargv, actualargv)) { - virtTestDifference(stderr, expectargv, actualargv); + if (virtTestCompareToFile(formatted, xml) < 0) goto out; - } - if (STRNEQ(expectld, actualld)) { - virtTestDifference(stderr, expectld, actualld); - goto out; - } - - if (expectdm && STRNEQ(expectdm, actualdm)) { - virtTestDifference(stderr, expectdm, actualdm); - goto out; + if (virFileExists(dmcmdline) || actualdm) { + if (virtTestCompareToFile(actualdm, dmcmdline) < 0) + goto out; } ret = 0; out: - VIR_FREE(expectargv); - VIR_FREE(expectld); - VIR_FREE(expectdm); VIR_FREE(actualargv); VIR_FREE(actualld); VIR_FREE(actualdm); diff --git a/tests/bhyvexml2xmltest.c b/tests/bhyvexml2xmltest.c index 740c957..826baea 100644 --- a/tests/bhyvexml2xmltest.c +++ b/tests/bhyvexml2xmltest.c @@ -14,14 +14,10 @@ static bhyveConn driver; static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { - char *outXmlData = NULL; char *actual = NULL; virDomainDefPtr def = NULL; int ret = -1; - if (virtTestLoadFile(outxml, &outXmlData) < 0) - goto fail; - if (!(def = virDomainDefParseFile(inxml, driver.caps, driver.xmlopt, VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; @@ -29,15 +25,12 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml) if (!(actual = virDomainDefFormat(def, VIR_DOMAIN_DEF_FORMAT_INACTIVE))) goto fail; - if (STRNEQ(outXmlData, actual)) { - virtTestDifference(stderr, outXmlData, actual); + if (virtTestCompareToFile(actual, outxml) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(outXmlData); VIR_FREE(actual); virDomainDefFree(def); return ret; diff --git a/tests/cputest.c b/tests/cputest.c index 56bcc90..06b3f12 100644 --- a/tests/cputest.c +++ b/tests/cputest.c @@ -158,7 +158,6 @@ cpuTestCompareXML(const char *arch, bool updateCPU) { char *xml = NULL; - char *expected = NULL; char *actual = NULL; int ret = -1; @@ -166,24 +165,16 @@ cpuTestCompareXML(const char *arch, abs_srcdir, arch, name) < 0) goto cleanup; - if (virtTestLoadFile(xml, &expected) < 0) - goto cleanup; - if (!(actual = virCPUDefFormat(cpu, NULL, updateCPU))) goto cleanup; - if (STRNEQ(expected, actual)) { - if (virTestGetVerbose()) - fprintf(stderr, "\nCompared to %s-%s.xml", arch, name); - virtTestDifference(stderr, expected, actual); + if (virtTestCompareToFile(actual, xml) < 0) goto cleanup; - } ret = 0; cleanup: VIR_FREE(xml); - VIR_FREE(expected); VIR_FREE(actual); return ret; } diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c index 7a7fa64..f6a060e 100644 --- a/tests/domaincapstest.c +++ b/tests/domaincapstest.c @@ -178,16 +178,12 @@ test_virDomainCapsFormat(const void *opaque) virDomainCapsPtr domCaps = NULL; char *path = NULL; char *domCapsXML = NULL; - char *domCapsFromFile = NULL; int ret = -1; if (virAsprintf(&path, "%s/domaincapsschemadata/domaincaps-%s.xml", abs_srcdir, data->filename) < 0) goto cleanup; - if (virFileReadAll(path, 8192, &domCapsFromFile) < 0) - goto cleanup; - if (!(domCaps = buildVirDomainCaps(data->emulatorbin, data->machine, data->arch, data->type, data->fillFunc, data->opaque))) @@ -196,14 +192,11 @@ test_virDomainCapsFormat(const void *opaque) if (!(domCapsXML = virDomainCapsFormat(domCaps))) goto cleanup; - if (STRNEQ(domCapsFromFile, domCapsXML)) { - virtTestDifference(stderr, domCapsFromFile, domCapsXML); + if (virtTestCompareToFile(domCapsXML, path) < 0) goto cleanup; - } ret = 0; cleanup: - VIR_FREE(domCapsFromFile); VIR_FREE(domCapsXML); VIR_FREE(path); virObjectUnref(domCaps); diff --git a/tests/lxcxml2xmltest.c b/tests/lxcxml2xmltest.c index a9b5419..3e00347 100644 --- a/tests/lxcxml2xmltest.c +++ b/tests/lxcxml2xmltest.c @@ -25,14 +25,10 @@ static virDomainXMLOptionPtr xmlopt; static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml, bool live) { - char *outXmlData = NULL; char *actual = NULL; int ret = -1; virDomainDefPtr def = NULL; - if (virtTestLoadFile(outxml, &outXmlData) < 0) - goto fail; - if (!(def = virDomainDefParseFile(inxml, caps, xmlopt, live ? 0 : VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; @@ -45,14 +41,11 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, bool live) if (!(actual = virDomainDefFormat(def, VIR_DOMAIN_DEF_FORMAT_SECURE))) goto fail; - if (STRNEQ(outXmlData, actual)) { - virtTestDifference(stderr, outXmlData, actual); + if (virtTestCompareToFile(actual, outxml) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(outXmlData); VIR_FREE(actual); virDomainDefFree(def); return ret; diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index 1d9a772..fe95e8c 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -21,7 +21,6 @@ static int testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps) { - char *outConfData = NULL; char *actual = NULL; int ret = -1; virNetworkDefPtr dev = NULL; @@ -30,9 +29,6 @@ testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr char *pidfile = NULL; dnsmasqContext *dctx = NULL; - if (virtTestLoadFile(outconf, &outConfData) < 0) - goto fail; - if (!(dev = virNetworkDefParseFile(inxml))) goto fail; @@ -49,15 +45,12 @@ testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr dctx, caps) < 0) goto fail; - if (STRNEQ(outConfData, actual)) { - virtTestDifference(stderr, outConfData, actual); + if (virtTestCompareToFile(actual, outconf) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(outConfData); VIR_FREE(actual); VIR_FREE(pidfile); virCommandFree(cmd); diff --git a/tests/networkxml2firewalltest.c b/tests/networkxml2firewalltest.c index c69ab54..0f38180 100644 --- a/tests/networkxml2firewalltest.c +++ b/tests/networkxml2firewalltest.c @@ -48,7 +48,6 @@ static int testCompareXMLToArgvFiles(const char *xml, const char *cmdline) { char *expectargv = NULL; - int len; char *actualargv = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; virNetworkDefPtr def = NULL; @@ -69,15 +68,9 @@ static int testCompareXMLToArgvFiles(const char *xml, virtTestClearCommandPath(actualargv); virCommandSetDryRun(NULL, NULL, NULL); - len = virtTestLoadFile(cmdline, &expectargv); - if (len < 0) + if (virtTestCompareToFile(actualargv, cmdline) < 0) goto cleanup; - if (STRNEQ(expectargv, actualargv)) { - virtTestDifference(stderr, expectargv, actualargv); - goto cleanup; - } - ret = 0; cleanup: diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c index 467be2a..290336e 100644 --- a/tests/networkxml2xmltest.c +++ b/tests/networkxml2xmltest.c @@ -20,29 +20,22 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml, unsigned int flags) { - char *outXmlData = NULL; char *actual = NULL; int ret = -1; virNetworkDefPtr dev = NULL; - if (virtTestLoadFile(outxml, &outXmlData) < 0) - goto fail; - if (!(dev = virNetworkDefParseFile(inxml))) goto fail; if (!(actual = virNetworkDefFormat(dev, flags))) goto fail; - if (STRNEQ(outXmlData, actual)) { - virtTestDifference(stderr, outXmlData, actual); + if (virtTestCompareToFile(actual, outxml) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(outXmlData); VIR_FREE(actual); virNetworkDefFree(dev); return ret; diff --git a/tests/networkxml2xmlupdatetest.c b/tests/networkxml2xmlupdatetest.c index 2051a01..af697bb 100644 --- a/tests/networkxml2xmlupdatetest.c +++ b/tests/networkxml2xmlupdatetest.c @@ -23,7 +23,6 @@ testCompareXMLToXMLFiles(const char *netxml, const char *updatexml, int parentIndex, bool expectFailure) { char *updateXmlData = NULL; - char *outXmlData = NULL; char *actual = NULL; int ret = -1; virNetworkDefPtr def = NULL; @@ -42,13 +41,8 @@ testCompareXMLToXMLFiles(const char *netxml, const char *updatexml, goto fail; if (!expectFailure) { - if (virtTestLoadFile(outxml, &outXmlData) < 0) + if (virtTestCompareToFile(actual, outxml) < 0) goto error; - - if (STRNEQ(outXmlData, actual)) { - virtTestDifference(stderr, outXmlData, actual); - goto fail; - } } ret = 0; @@ -65,7 +59,6 @@ testCompareXMLToXMLFiles(const char *netxml, const char *updatexml, } error: VIR_FREE(updateXmlData); - VIR_FREE(outXmlData); VIR_FREE(actual); virNetworkDefFree(def); return ret; diff --git a/tests/nodeinfotest.c b/tests/nodeinfotest.c index e53a604..8d0a06f 100644 --- a/tests/nodeinfotest.c +++ b/tests/nodeinfotest.c @@ -31,13 +31,9 @@ linuxTestCompareFiles(const char *cpuinfofile, { int ret = -1; char *actualData = NULL; - char *expectData = NULL; virNodeInfo nodeinfo; FILE *cpuinfo; - if (virtTestLoadFile(outputfile, &expectData) < 0) - goto fail; - cpuinfo = fopen(cpuinfofile, "r"); if (!cpuinfo) { fprintf(stderr, "unable to open: %s : %s\n", @@ -66,15 +62,12 @@ linuxTestCompareFiles(const char *cpuinfofile, nodeinfo.cores, nodeinfo.threads) < 0) goto fail; - if (STRNEQ(actualData, expectData)) { - virtTestDifference(stderr, expectData, actualData); + if (virtTestCompareToFile(actualData, outputfile) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(expectData); VIR_FREE(actualData); return ret; } @@ -109,16 +102,12 @@ linuxCPUStatsCompareFiles(const char *cpustatfile, { int ret = -1; char *actualData = NULL; - char *expectData = NULL; FILE *cpustat = NULL; virNodeCPUStatsPtr params = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; size_t i; int nparams = 0; - if (virtTestLoadFile(outfile, &expectData) < 0) - goto fail; - if (!(cpustat = fopen(cpustatfile, "r"))) { virReportSystemError(errno, "failed to open '%s': ", cpustatfile); goto fail; @@ -150,17 +139,14 @@ linuxCPUStatsCompareFiles(const char *cpustatfile, goto fail; } - if (STRNEQ(actualData, expectData)) { - virtTestDifference(stderr, expectData, actualData); + if (virtTestCompareToFile(actualData, outfile) < 0) goto fail; - } ret = 0; fail: virBufferFreeAndReset(&buf); VIR_FORCE_FCLOSE(cpustat); - VIR_FREE(expectData); VIR_FREE(actualData); VIR_FREE(params); return ret; diff --git a/tests/nwfilterxml2firewalltest.c b/tests/nwfilterxml2firewalltest.c index 167ad42..3398989 100644 --- a/tests/nwfilterxml2firewalltest.c +++ b/tests/nwfilterxml2firewalltest.c @@ -372,8 +372,6 @@ static int testSetDefaultParameters(virNWFilterHashTablePtr vars) static int testCompareXMLToArgvFiles(const char *xml, const char *cmdline) { - char *expectargv = NULL; - int len; char *actualargv = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; virNWFilterHashTablePtr vars = virNWFilterHashTableCreate(0); @@ -407,20 +405,13 @@ static int testCompareXMLToArgvFiles(const char *xml, testRemoveCommonRules(actualargv); - len = virtTestLoadFile(cmdline, &expectargv); - if (len < 0) + if (virtTestCompareToFile(actualargv, cmdline) < 0) goto cleanup; - if (STRNEQ(expectargv, actualargv)) { - virtTestDifference(stderr, expectargv, actualargv); - goto cleanup; - } - ret = 0; cleanup: virBufferFreeAndReset(&buf); - VIR_FREE(expectargv); VIR_FREE(actualargv); virNWFilterInstReset(&inst); virNWFilterHashTableFree(vars); diff --git a/tests/nwfilterxml2xmltest.c b/tests/nwfilterxml2xmltest.c index 7640af0..361a9a2 100644 --- a/tests/nwfilterxml2xmltest.c +++ b/tests/nwfilterxml2xmltest.c @@ -23,14 +23,10 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml, bool expect_error) { - char *outXmlData = NULL; char *actual = NULL; int ret = -1; virNWFilterDefPtr dev = NULL; - if (virtTestLoadFile(outxml, &outXmlData) < 0) - goto fail; - virResetLastError(); if (!(dev = virNWFilterDefParseFile(inxml))) { @@ -44,16 +40,13 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, if (!(actual = virNWFilterDefFormat(dev))) goto fail; - if (STRNEQ(outXmlData, actual)) { - virtTestDifference(stderr, outXmlData, actual); + if (virtTestCompareToFile(actual, outxml) < 0) goto fail; - } done: ret = 0; fail: - VIR_FREE(outXmlData); VIR_FREE(actual); virNWFilterDefFree(dev); return ret; diff --git a/tests/qemucaps2xmltest.c b/tests/qemucaps2xmltest.c index fd2056d..6a5811c 100644 --- a/tests/qemucaps2xmltest.c +++ b/tests/qemucaps2xmltest.c @@ -28,23 +28,6 @@ #define VIR_FROM_THIS VIR_FROM_NONE -static int -testCompareXMLToXML(const char *inxmldata, const char *outxmldata) -{ - int ret = -1; - - if (STRNEQ(outxmldata, inxmldata)) { - virtTestDifference(stderr, outxmldata, inxmldata); - goto cleanup; - } - - ret = 0; - cleanup: - - return ret; -} - - typedef struct _testQemuData testQemuData; typedef testQemuData *testQemuDataPtr; struct _testQemuData { @@ -139,7 +122,7 @@ testQemuCapsXML(const void *opaque) int ret = -1; const testQemuData *data = opaque; char *capsFile = NULL, *xmlFile = NULL; - char *capsData = NULL, *xmlData = NULL; + char *capsData = NULL; char *capsXml = NULL; virCapsPtr capsProvided = NULL; @@ -151,9 +134,6 @@ testQemuCapsXML(const void *opaque) abs_srcdir, data->base) < 0) goto cleanup; - if (virtTestLoadFile(xmlFile, &xmlData) < 0) - goto cleanup; - if (virtTestLoadFile(capsFile, &capsData) < 0) goto cleanup; @@ -164,14 +144,15 @@ testQemuCapsXML(const void *opaque) if (!capsXml) goto cleanup; - ret = testCompareXMLToXML(capsXml, xmlData); + if (virtTestCompareToFile(capsXml, xmlFile) < 0) + goto cleanup; + ret = 0; cleanup: VIR_FREE(xmlFile); VIR_FREE(capsFile); VIR_FREE(capsXml); VIR_FREE(capsData); - VIR_FREE(xmlData); virObjectUnref(capsProvided); return ret; } diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index 170d690..f729c7c 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -2129,7 +2129,6 @@ testQemuMonitorJSONGetCPUData(const void *opaque) char *jsonFile = NULL; char *dataFile = NULL; char *jsonStr = NULL; - char *expected = NULL; char *actual = NULL; int ret = -1; @@ -2144,8 +2143,7 @@ testQemuMonitorJSONGetCPUData(const void *opaque) abs_srcdir, data->name) < 0) goto cleanup; - if (virtTestLoadFile(jsonFile, &jsonStr) < 0 || - virtTestLoadFile(dataFile, &expected) < 0) + if (virtTestLoadFile(jsonFile, &jsonStr) < 0) goto cleanup; if (qemuMonitorTestAddItem(test, "qom-list", @@ -2175,17 +2173,14 @@ testQemuMonitorJSONGetCPUData(const void *opaque) if (!(actual = cpuDataFormat(cpuData))) goto cleanup; - if (STRNEQ(expected, actual)) { - virtTestDifference(stderr, expected, actual); + if (virtTestCompareToFile(actual, dataFile) < 0) goto cleanup; - } ret = 0; cleanup: VIR_FREE(jsonFile); VIR_FREE(dataFile); VIR_FREE(jsonStr); - VIR_FREE(expected); VIR_FREE(actual); cpuDataFree(cpuData); qemuMonitorTestFree(test); diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index e489a13..1fe445a 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -253,8 +253,6 @@ static int testCompareXMLToArgvFiles(const char *xml, int migrateFd, virQemuXML2ArgvTestFlags flags) { - char *expectargv = NULL; - int len; char *actualargv = NULL; int ret = -1; virDomainDefPtr vmdef = NULL; @@ -373,16 +371,8 @@ static int testCompareXMLToArgvFiles(const char *xml, if (!(actualargv = virCommandToString(cmd))) goto out; - len = virtTestLoadFile(cmdline, &expectargv); - if (len < 0) + if (virtTestCompareToFile(actualargv, cmdline) < 0) goto out; - if (len && expectargv[len - 1] == '\n') - expectargv[len - 1] = '\0'; - - if (STRNEQ(expectargv, actualargv)) { - virtTestDifference(stderr, expectargv, actualargv); - goto out; - } ok: if (!virtTestOOMActive() && @@ -395,7 +385,6 @@ static int testCompareXMLToArgvFiles(const char *xml, out: VIR_FREE(log); - VIR_FREE(expectargv); VIR_FREE(actualargv); virCommandFree(cmd); virDomainDefFree(vmdef); diff --git a/tests/qemuxmlnstest.c b/tests/qemuxmlnstest.c index 4d7a142..8eaab8a 100644 --- a/tests/qemuxmlnstest.c +++ b/tests/qemuxmlnstest.c @@ -34,8 +34,6 @@ static int testCompareXMLToArgvFiles(const char *xml, bool json, bool expectError) { - char *expectargv = NULL; - int len; char *actualargv = NULL; int ret = -1; virDomainDefPtr vmdef = NULL; @@ -48,12 +46,6 @@ static int testCompareXMLToArgvFiles(const char *xml, if (!(conn = virGetConnect())) goto fail; - len = virtTestLoadFile(cmdline, &expectargv); - if (len < 0) - goto fail; - if (len && expectargv[len - 1] == '\n') - expectargv[len - 1] = '\0'; - if (!(vmdef = virDomainDefParseFile(xml, driver.caps, driver.xmlopt, VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; @@ -144,17 +136,14 @@ static int testCompareXMLToArgvFiles(const char *xml, memmove(start_skip, end_skip, strlen(end_skip) + 1); } - if (STRNEQ(expectargv, actualargv)) { - virtTestDifference(stderr, expectargv, actualargv); + if (virtTestCompareToFile(actualargv, cmdline) < 0) goto fail; - } ret = 0; fail: VIR_FREE(log); VIR_FREE(emulator); - VIR_FREE(expectargv); VIR_FREE(actualargv); virCommandFree(cmd); virDomainDefFree(vmdef); diff --git a/tests/secretxml2xmltest.c b/tests/secretxml2xmltest.c index cf5f00f..b4c9386 100644 --- a/tests/secretxml2xmltest.c +++ b/tests/secretxml2xmltest.c @@ -11,29 +11,22 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { - char *outXmlData = NULL; char *actual = NULL; int ret = -1; virSecretDefPtr secret = NULL; - if (virtTestLoadFile(outxml, &outXmlData) < 0) - goto fail; - if (!(secret = virSecretDefParseFile(inxml))) goto fail; if (!(actual = virSecretDefFormat(secret))) goto fail; - if (STRNEQ(outXmlData, actual)) { - virtTestDifference(stderr, outXmlData, actual); + if (virtTestCompareToFile(actual, outxml) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(outXmlData); VIR_FREE(actual); virSecretDefFree(secret); return ret; diff --git a/tests/sexpr2xmltest.c b/tests/sexpr2xmltest.c index 7247414..99999f1 100644 --- a/tests/sexpr2xmltest.c +++ b/tests/sexpr2xmltest.c @@ -21,7 +21,6 @@ static virCapsPtr caps; static int testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion) { - char *xmlData = NULL; char *sexprData = NULL; char *gotxml = NULL; int id; @@ -36,9 +35,6 @@ testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion) conn = virGetConnect(); if (!conn) goto fail; - if (virtTestLoadFile(xml, &xmlData) < 0) - goto fail; - if (virtTestLoadFile(sexpr, &sexprData) < 0) goto fail; @@ -68,15 +64,12 @@ testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion) if (!(gotxml = virDomainDefFormat(def, 0))) goto fail; - if (STRNEQ(xmlData, gotxml)) { - virtTestDifference(stderr, xmlData, gotxml); + if (virtTestCompareToFile(gotxml, xml) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(xmlData); VIR_FREE(sexprData); VIR_FREE(gotxml); virDomainDefFree(def); diff --git a/tests/storagepoolxml2xmltest.c b/tests/storagepoolxml2xmltest.c index 54b2a00..bec1b8f 100644 --- a/tests/storagepoolxml2xmltest.c +++ b/tests/storagepoolxml2xmltest.c @@ -19,29 +19,22 @@ static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { - char *outXmlData = NULL; char *actual = NULL; int ret = -1; virStoragePoolDefPtr dev = NULL; - if (virtTestLoadFile(outxml, &outXmlData) < 0) - goto fail; - if (!(dev = virStoragePoolDefParseFile(inxml))) goto fail; if (!(actual = virStoragePoolDefFormat(dev))) goto fail; - if (STRNEQ(outXmlData, actual)) { - virtTestDifference(stderr, outXmlData, actual); + if (virtTestCompareToFile(actual, outxml) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(outXmlData); VIR_FREE(actual); virStoragePoolDefFree(dev); return ret; diff --git a/tests/storagevolxml2argvtest.c b/tests/storagevolxml2argvtest.c index d957f63..ed002ce 100644 --- a/tests/storagevolxml2argvtest.c +++ b/tests/storagevolxml2argvtest.c @@ -43,12 +43,9 @@ testCompareXMLToArgvFiles(bool shouldFail, int imgformat, unsigned long parse_flags) { - char *expectedCmdline = NULL; char *actualCmdline = NULL; int ret = -1; - int len; - virCommandPtr cmd = NULL; virConnectPtr conn; @@ -98,16 +95,8 @@ testCompareXMLToArgvFiles(bool shouldFail, if (!(actualCmdline = virCommandToString(cmd))) goto cleanup; - len = virtTestLoadFile(cmdline, &expectedCmdline); - if (len < 0) - goto cleanup; - if (len && expectedCmdline[len-1] == '\n') - expectedCmdline[len-1] = '\0'; - - if (STRNEQ_NULLABLE(expectedCmdline, actualCmdline)) { - virtTestDifference(stderr, expectedCmdline, actualCmdline); + if (virtTestCompareToFile(actualCmdline, cmdline) < 0) goto cleanup; - } ret = 0; @@ -118,7 +107,6 @@ testCompareXMLToArgvFiles(bool shouldFail, virStorageVolDefFree(inputvol); virCommandFree(cmd); VIR_FREE(actualCmdline); - VIR_FREE(expectedCmdline); virObjectUnref(conn); return ret; } diff --git a/tests/storagevolxml2xmltest.c b/tests/storagevolxml2xmltest.c index 64b6673..148d1e6 100644 --- a/tests/storagevolxml2xmltest.c +++ b/tests/storagevolxml2xmltest.c @@ -20,15 +20,11 @@ static int testCompareXMLToXMLFiles(const char *poolxml, const char *inxml, const char *outxml, unsigned int flags) { - char *outXmlData = NULL; char *actual = NULL; int ret = -1; virStoragePoolDefPtr pool = NULL; virStorageVolDefPtr dev = NULL; - if (virtTestLoadFile(outxml, &outXmlData) < 0) - goto fail; - if (!(pool = virStoragePoolDefParseFile(poolxml))) goto fail; @@ -38,15 +34,12 @@ testCompareXMLToXMLFiles(const char *poolxml, const char *inxml, if (!(actual = virStorageVolDefFormat(pool, dev))) goto fail; - if (STRNEQ(outXmlData, actual)) { - virtTestDifference(stderr, outXmlData, actual); + if (virtTestCompareToFile(actual, outxml) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(outXmlData); VIR_FREE(actual); virStoragePoolDefFree(pool); virStorageVolDefFree(dev); diff --git a/tests/sysinfotest.c b/tests/sysinfotest.c index 5ba3298..d8266a7 100644 --- a/tests/sysinfotest.c +++ b/tests/sysinfotest.c @@ -62,7 +62,6 @@ static int testSysinfo(const void *data) { int result = -1; - char *sysfsExpectData = NULL; const char *sysfsActualData; virSysinfoDefPtr ret = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; @@ -71,10 +70,8 @@ testSysinfo(const void *data) virSysinfoSetup(testdata->decoder, testdata->sysinfo, testdata->cpuinfo); if (!testdata->expected || - virtTestLoadFile(testdata->expected, &sysfsExpectData) < 0 || - !(ret = virSysinfoRead())) { + !(ret = virSysinfoRead())) goto cleanup; - } if (virSysinfoFormat(&buf, ret) < 0) goto cleanup; @@ -82,15 +79,12 @@ testSysinfo(const void *data) if (!(sysfsActualData = virBufferCurrentContent(&buf))) goto cleanup; - if (STRNEQ(sysfsActualData, sysfsExpectData)) { - virtTestDifference(stderr, sysfsExpectData, sysfsActualData); + if (virtTestCompareToFile(sysfsActualData, testdata->expected) < 0) goto cleanup; - } result = 0; cleanup: - VIR_FREE(sysfsExpectData); virSysinfoDefFree(ret); virBufferFreeAndReset(&buf); diff --git a/tests/testutils.c b/tests/testutils.c index 3274e40..8367b4b 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -596,6 +596,39 @@ int virtTestDifferenceBin(FILE *stream, return 0; } +/* + * @param strcontent: String input content + * @param filename: File to compare strcontent against + */ +int +virtTestCompareToFile(const char *strcontent, + const char *filename) +{ + int ret = -1; + char *filecontent = NULL; + char *fixedcontent = NULL; + + if (virtTestLoadFile(filename, &filecontent) < 0) + goto failure; + + if (filecontent[strlen(filecontent) - 1] == '\n' && + strcontent[strlen(strcontent) - 1] != '\n') { + if (virAsprintf(&fixedcontent, "%s\n", strcontent) < 0) + goto failure; + } + + if (STRNEQ(fixedcontent ? fixedcontent : strcontent, filecontent)) { + virtTestDifference(stderr, strcontent, filecontent); + goto failure; + } + + ret = 0; + failure: + VIR_FREE(fixedcontent); + VIR_FREE(filecontent); + return ret; +} + static void virtTestErrorFuncQuiet(void *data ATTRIBUTE_UNUSED, virErrorPtr err ATTRIBUTE_UNUSED) diff --git a/tests/testutils.h b/tests/testutils.h index 8c05d85..a6c88f2 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -73,6 +73,8 @@ int virtTestDifferenceBin(FILE *stream, const char *expect, const char *actual, size_t length); +int virtTestCompareToFile(const char *strcontent, + const char *filename); unsigned int virTestGetDebug(void); unsigned int virTestGetVerbose(void); diff --git a/tests/vircaps2xmltest.c b/tests/vircaps2xmltest.c index b5b4f48..be2bad5 100644 --- a/tests/vircaps2xmltest.c +++ b/tests/vircaps2xmltest.c @@ -106,7 +106,6 @@ test_virCapabilitiesFormat(const void *opaque) struct virCapabilitiesFormatData *data = (struct virCapabilitiesFormatData *) opaque; virCapsPtr caps = NULL; char *capsXML = NULL; - char *capsFromFile = NULL; char *path = NULL; int ret = -1; @@ -121,20 +120,13 @@ test_virCapabilitiesFormat(const void *opaque) abs_srcdir, data->filename) < 0) goto cleanup; - if (virFileReadAll(path, 8192, &capsFromFile) < 0) + if (virtTestCompareToFile(capsXML, path) < 0) goto cleanup; - - if (STRNEQ(capsXML, capsFromFile)) { - virtTestDifference(stderr, capsFromFile, capsXML); - goto cleanup; - } - ret = 0; cleanup: VIR_FREE(path); - VIR_FREE(capsFromFile); VIR_FREE(capsXML); virObjectUnref(caps); return ret; diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c index ad49425..dddf33a 100644 --- a/tests/vircgrouptest.c +++ b/tests/vircgrouptest.c @@ -166,7 +166,6 @@ testCgroupDetectMounts(const void *args) const char *file = args; char *mounts = NULL; char *parsed = NULL; - char *expected = NULL; const char *actual; virCgroupPtr group = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; @@ -182,9 +181,6 @@ testCgroupDetectMounts(const void *args) if (virCgroupDetectMountsFromFile(group, mounts, false) < 0) goto cleanup; - if (virtTestLoadFile(parsed, &expected) < 0) - goto cleanup; - for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { virBufferAsprintf(&buf, "%-12s %s\n", virCgroupControllerTypeToString(i), @@ -194,17 +190,14 @@ testCgroupDetectMounts(const void *args) goto cleanup; actual = virBufferCurrentContent(&buf); - if (STRNEQ(expected, actual)) { - virtTestDifference(stderr, expected, actual); + if (virtTestCompareToFile(actual, parsed) < 0) goto cleanup; - } result = 0; cleanup: VIR_FREE(mounts); VIR_FREE(parsed); - VIR_FREE(expected); virCgroupFree(&group); virBufferFreeAndReset(&buf); return result; diff --git a/tests/vmx2xmltest.c b/tests/vmx2xmltest.c index 69d2b43..d3e33e6 100644 --- a/tests/vmx2xmltest.c +++ b/tests/vmx2xmltest.c @@ -72,16 +72,12 @@ testCompareFiles(const char *vmx, const char *xml) { int ret = -1; char *vmxData = NULL; - char *xmlData = NULL; char *formatted = NULL; virDomainDefPtr def = NULL; if (virtTestLoadFile(vmx, &vmxData) < 0) goto cleanup; - if (virtTestLoadFile(xml, &xmlData) < 0) - goto cleanup; - if (!(def = virVMXParseConfig(&ctx, xmlopt, vmxData))) goto cleanup; @@ -93,16 +89,13 @@ testCompareFiles(const char *vmx, const char *xml) if (!(formatted = virDomainDefFormat(def, VIR_DOMAIN_DEF_FORMAT_SECURE))) goto cleanup; - if (STRNEQ(xmlData, formatted)) { - virtTestDifference(stderr, xmlData, formatted); + if (virtTestCompareToFile(formatted, xml) < 0) goto cleanup; - } ret = 0; cleanup: VIR_FREE(vmxData); - VIR_FREE(xmlData); VIR_FREE(formatted); virDomainDefFree(def); diff --git a/tests/xencapstest.c b/tests/xencapstest.c index 176850b..6439254 100644 --- a/tests/xencapstest.c +++ b/tests/xencapstest.c @@ -18,7 +18,6 @@ static int testCompareFiles(virArch hostmachine, const char *xml_rel, const char *cpuinfo_rel, const char *capabilities_rel) { - char *expectxml = NULL; char *actualxml = NULL; FILE *fp1 = NULL, *fp2 = NULL; virCapsPtr caps = NULL; @@ -34,9 +33,6 @@ testCompareFiles(virArch hostmachine, const char *xml_rel, virAsprintf(&capabilities, "%s/%s", abs_srcdir, capabilities_rel) < 0) goto fail; - if (virtTestLoadFile(xml, &expectxml) < 0) - goto fail; - if (!(fp1 = fopen(cpuinfo, "r"))) goto fail; @@ -49,15 +45,12 @@ testCompareFiles(virArch hostmachine, const char *xml_rel, if (!(actualxml = virCapabilitiesFormatXML(caps))) goto fail; - if (STRNEQ(expectxml, actualxml)) { - virtTestDifference(stderr, expectxml, actualxml); + if (virtTestCompareToFile(actualxml, xml) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(expectxml); VIR_FREE(actualxml); VIR_FREE(xml); VIR_FREE(cpuinfo); diff --git a/tests/xlconfigtest.c b/tests/xlconfigtest.c index 899cb1b..0b47fbb 100644 --- a/tests/xlconfigtest.c +++ b/tests/xlconfigtest.c @@ -48,7 +48,6 @@ static virDomainXMLOptionPtr xmlopt; static int testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) { - char *xmcfgData = NULL; char *gotxmcfgData = NULL; virConfPtr conf = NULL; virConnectPtr conn = NULL; @@ -62,9 +61,6 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) conn = virGetConnect(); if (!conn) goto fail; - if (virtTestLoadFile(xmcfg, &xmcfgData) < 0) - goto fail; - if (!(def = virDomainDefParseFile(xml, caps, xmlopt, VIR_DOMAIN_XML_INACTIVE))) goto fail; @@ -81,15 +77,12 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) goto fail; gotxmcfgData[wrote] = '\0'; - if (STRNEQ(xmcfgData, gotxmcfgData)) { - virtTestDifference(stderr, xmcfgData, gotxmcfgData); + if (virtTestCompareToFile(gotxmcfgData, xmcfg) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(xmcfgData); VIR_FREE(gotxmcfgData); if (conf) virConfFree(conf); @@ -104,7 +97,6 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) static int testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion) { - char *xmlData = NULL; char *xmcfgData = NULL; char *gotxml = NULL; virConfPtr conf = NULL; @@ -115,9 +107,6 @@ testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion) conn = virGetConnect(); if (!conn) goto fail; - if (virtTestLoadFile(xml, &xmlData) < 0) - goto fail; - if (virtTestLoadFile(xmcfg, &xmcfgData) < 0) goto fail; @@ -131,17 +120,14 @@ testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion) VIR_DOMAIN_XML_SECURE))) goto fail; - if (STRNEQ(xmlData, gotxml)) { - virtTestDifference(stderr, xmlData, gotxml); + if (virtTestCompareToFile(gotxml, xml) < 0) goto fail; - } ret = 0; fail: if (conf) virConfFree(conf); - VIR_FREE(xmlData); VIR_FREE(xmcfgData); VIR_FREE(gotxml); virDomainDefFree(def); diff --git a/tests/xmconfigtest.c b/tests/xmconfigtest.c index 1d67c5b..79b09ca 100644 --- a/tests/xmconfigtest.c +++ b/tests/xmconfigtest.c @@ -45,7 +45,6 @@ static virDomainXMLOptionPtr xmlopt; static int testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) { - char *xmcfgData = NULL; char *gotxmcfgData = NULL; virConfPtr conf = NULL; int ret = -1; @@ -60,9 +59,6 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) conn = virGetConnect(); if (!conn) goto fail; - if (virtTestLoadFile(xmcfg, &xmcfgData) < 0) - goto fail; - /* Many puppies died to bring you this code. */ priv.xendConfigVersion = xendConfigVersion; priv.caps = caps; @@ -84,15 +80,12 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) goto fail; gotxmcfgData[wrote] = '\0'; - if (STRNEQ(xmcfgData, gotxmcfgData)) { - virtTestDifference(stderr, xmcfgData, gotxmcfgData); + if (virtTestCompareToFile(gotxmcfgData, xmcfg) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(xmcfgData); VIR_FREE(gotxmcfgData); if (conf) virConfFree(conf); @@ -105,7 +98,6 @@ testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) static int testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion) { - char *xmlData = NULL; char *xmcfgData = NULL; char *gotxml = NULL; virConfPtr conf = NULL; @@ -117,9 +109,6 @@ testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion) conn = virGetConnect(); if (!conn) goto fail; - if (virtTestLoadFile(xml, &xmlData) < 0) - goto fail; - if (virtTestLoadFile(xmcfg, &xmcfgData) < 0) goto fail; @@ -137,17 +126,14 @@ testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion) if (!(gotxml = virDomainDefFormat(def, VIR_DOMAIN_DEF_FORMAT_SECURE))) goto fail; - if (STRNEQ(xmlData, gotxml)) { - virtTestDifference(stderr, xmlData, gotxml); + if (virtTestCompareToFile(gotxml, xml) < 0) goto fail; - } ret = 0; fail: if (conf) virConfFree(conf); - VIR_FREE(xmlData); VIR_FREE(xmcfgData); VIR_FREE(gotxml); virDomainDefFree(def); diff --git a/tests/xml2sexprtest.c b/tests/xml2sexprtest.c index 32c8a32..47c9db8 100644 --- a/tests/xml2sexprtest.c +++ b/tests/xml2sexprtest.c @@ -23,14 +23,10 @@ static virDomainXMLOptionPtr xmlopt; static int testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion) { - char *sexprData = NULL; char *gotsexpr = NULL; int ret = -1; virDomainDefPtr def = NULL; - if (virtTestLoadFile(sexpr, &sexprData) < 0) - goto fail; - if (!(def = virDomainDefParseFile(xml, caps, xmlopt, VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; @@ -43,15 +39,12 @@ testCompareFiles(const char *xml, const char *sexpr, int xendConfigVersion) if (!(gotsexpr = xenFormatSxpr(NULL, def, xendConfigVersion))) goto fail; - if (STRNEQ(sexprData, gotsexpr)) { - virtTestDifference(stderr, sexprData, gotsexpr); + if (virtTestCompareToFile(gotsexpr, sexpr) < 0) goto fail; - } ret = 0; fail: - VIR_FREE(sexprData); VIR_FREE(gotsexpr); virDomainDefFree(def); diff --git a/tests/xml2vmxtest.c b/tests/xml2vmxtest.c index 6d7d4c2..357f1e6 100644 --- a/tests/xml2vmxtest.c +++ b/tests/xml2vmxtest.c @@ -73,13 +73,9 @@ static int testCompareFiles(const char *xml, const char *vmx, int virtualHW_version) { int result = -1; - char *vmxData = NULL; char *formatted = NULL; virDomainDefPtr def = NULL; - if (virtTestLoadFile(vmx, &vmxData) < 0) - goto failure; - def = virDomainDefParseFile(xml, caps, xmlopt, VIR_DOMAIN_DEF_PARSE_INACTIVE); @@ -92,19 +88,15 @@ testCompareFiles(const char *xml, const char *vmx, int virtualHW_version) } formatted = virVMXFormatConfig(&ctx, xmlopt, def, virtualHW_version); - if (formatted == NULL) goto failure; - if (STRNEQ(vmxData, formatted)) { - virtTestDifference(stderr, vmxData, formatted); + if (virtTestCompareToFile(formatted, vmx) < 0) goto failure; - } result = 0; failure: - VIR_FREE(vmxData); VIR_FREE(formatted); virDomainDefFree(def); -- 2.3.5

On 04/23/2015 02:19 PM, Cole Robinson wrote:
Replaces a common pattern used in many test files --- tests/bhyvexml2argvtest.c | 41 ++++++---------------------------------- tests/bhyvexml2xmltest.c | 9 +-------- tests/cputest.c | 11 +---------- tests/domaincapstest.c | 9 +-------- tests/lxcxml2xmltest.c | 9 +-------- tests/networkxml2conftest.c | 9 +-------- tests/networkxml2firewalltest.c | 9 +-------- tests/networkxml2xmltest.c | 9 +-------- tests/networkxml2xmlupdatetest.c | 9 +-------- tests/nodeinfotest.c | 18 ++---------------- tests/nwfilterxml2firewalltest.c | 11 +---------- tests/nwfilterxml2xmltest.c | 9 +-------- tests/qemucaps2xmltest.c | 27 ++++---------------------- tests/qemumonitorjsontest.c | 9 ++------- tests/qemuxml2argvtest.c | 13 +------------ tests/qemuxmlnstest.c | 13 +------------ tests/secretxml2xmltest.c | 9 +-------- tests/sexpr2xmltest.c | 9 +-------- tests/storagepoolxml2xmltest.c | 9 +-------- tests/storagevolxml2argvtest.c | 14 +------------- tests/storagevolxml2xmltest.c | 9 +-------- tests/sysinfotest.c | 10 ++-------- tests/testutils.c | 33 ++++++++++++++++++++++++++++++++ tests/testutils.h | 2 ++ tests/vircaps2xmltest.c | 10 +--------- tests/vircgrouptest.c | 9 +-------- tests/vmx2xmltest.c | 9 +-------- tests/xencapstest.c | 9 +-------- tests/xlconfigtest.c | 18 ++---------------- tests/xmconfigtest.c | 18 ++---------------- tests/xml2sexprtest.c | 9 +-------- tests/xml2vmxtest.c | 10 +--------- 32 files changed, 78 insertions(+), 324 deletions(-)
Nice! ACK.

If this enviroment variable is set, the virTestCompareToFile helper will overwrite the file content we are comparing against, if the file doesn't exist or it doesn't match the expected input. This is useful when adding new test cases, or making changes that generate a lot of output churn. --- HACKING | 7 +++++++ docs/hacking.html.in | 12 ++++++++++++ tests/testutils.c | 45 +++++++++++++++++++++++++++------------------ 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/HACKING b/HACKING index 94d9d2c..cfc507a 100644 --- a/HACKING +++ b/HACKING @@ -136,6 +136,13 @@ Also, individual tests can be run from inside the "tests/" directory, like: ./qemuxml2xmltest +If you are adding new test cases, or making changes that alter existing test +output, you can use the environment variable VIR_TEST_REGENERATE_OUTPUT to +quickly update the saved test data. Of course you still need to review the +changes to ensure they are correct. + + VIR_TEST_REGENERATE_OUTPUT=1 ./qemuxml2argvtest + There is also a "./run" script at the top level, to make it easier to run programs that have not yet been installed, as well as to wrap invocations of various tests under gdb or Valgrind. diff --git a/docs/hacking.html.in b/docs/hacking.html.in index 53786b7..8f25bd4 100644 --- a/docs/hacking.html.in +++ b/docs/hacking.html.in @@ -164,6 +164,18 @@ <pre> ./qemuxml2xmltest </pre> + + <p> + If you are adding new test cases, or making changes that alter + existing test output, you can use the environment variable + VIR_TEST_REGENERATE_OUTPUT to quickly update the saved test data. + Of course you still need to review the changes to ensure they are + correct. + </p> +<pre> + VIR_TEST_REGENERATE_OUTPUT=1 ./qemuxml2argvtest +</pre> + <p>There is also a <code>./run</code> script at the top level, to make it easier to run programs that have not yet been installed, as well as to wrap invocations of various tests diff --git a/tests/testutils.c b/tests/testutils.c index 8367b4b..acb2ef1 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -91,6 +91,21 @@ bool virtTestOOMActive(void) return testOOMActive; } +static unsigned int +virTestGetFlag(const char *name) +{ + char *flagStr; + unsigned int flag; + + if ((flagStr = getenv(name)) == NULL) + return 0; + + if (virStrToLong_ui(flagStr, NULL, 10, &flag) < 0) + return 0; + + return flag; +} + #ifdef TEST_OOM_TRACE static void virTestAllocHook(int nalloc ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED) @@ -607,21 +622,30 @@ virtTestCompareToFile(const char *strcontent, int ret = -1; char *filecontent = NULL; char *fixedcontent = NULL; + bool regenerate = !!virTestGetFlag("VIR_TEST_REGENERATE_OUTPUT"); - if (virtTestLoadFile(filename, &filecontent) < 0) + if (virtTestLoadFile(filename, &filecontent) < 0 && !regenerate) goto failure; - if (filecontent[strlen(filecontent) - 1] == '\n' && + if (filecontent && + filecontent[strlen(filecontent) - 1] == '\n' && strcontent[strlen(strcontent) - 1] != '\n') { if (virAsprintf(&fixedcontent, "%s\n", strcontent) < 0) goto failure; } - if (STRNEQ(fixedcontent ? fixedcontent : strcontent, filecontent)) { + if (STRNEQ_NULLABLE(fixedcontent ? fixedcontent : strcontent, + filecontent)) { + if (regenerate) { + if (virFileWriteStr(filename, strcontent, 0666) < 0) + goto failure; + goto out; + } virtTestDifference(stderr, strcontent, filecontent); goto failure; } + out: ret = 0; failure: VIR_FREE(fixedcontent); @@ -692,21 +716,6 @@ virtTestLogContentAndReset(void) } -static unsigned int -virTestGetFlag(const char *name) -{ - char *flagStr; - unsigned int flag; - - if ((flagStr = getenv(name)) == NULL) - return 0; - - if (virStrToLong_ui(flagStr, NULL, 10, &flag) < 0) - return 0; - - return flag; -} - unsigned int virTestGetDebug(void) { -- 2.3.5

On 04/23/2015 02:20 PM, Cole Robinson wrote:
If this enviroment variable is set, the virTestCompareToFile helper will overwrite the file content we are comparing against, if the file doesn't exist or it doesn't match the expected input.
This is useful when adding new test cases, or making changes that generate a lot of output churn. --- HACKING | 7 +++++++ docs/hacking.html.in | 12 ++++++++++++ tests/testutils.c | 45 +++++++++++++++++++++++++++------------------ 3 files changed, 46 insertions(+), 18 deletions(-)
diff --git a/HACKING b/HACKING index 94d9d2c..cfc507a 100644 --- a/HACKING +++ b/HACKING @@ -136,6 +136,13 @@ Also, individual tests can be run from inside the "tests/" directory, like:
./qemuxml2xmltest
+If you are adding new test cases, or making changes that alter existing test +output, you can use the environment variable VIR_TEST_REGENERATE_OUTPUT to +quickly update the saved test data. Of course you still need to review the +changes to ensure they are correct.
Maybe say "review the changes *VERY CAREFULLY*" or something like that. This is incredibly convenient, but could make it easier for someone to gloss over a regression that happens to be introduced with the same patch where they are adding some new functionality.
[...]
- if (STRNEQ(fixedcontent ? fixedcontent : strcontent, filecontent)) { + if (STRNEQ_NULLABLE(fixedcontent ? fixedcontent : strcontent, + filecontent)) { + if (regenerate) { + if (virFileWriteStr(filename, strcontent, 0666) < 0) + goto failure; + goto out; + } virtTestDifference(stderr, strcontent, filecontent); goto failure;
Okay, so you still report a failure for this test, but in the process you update the test file. Makes sense - that way you can see which tests are getting updated. This is *really* cool! ACK++

On 04/23/2015 03:46 PM, Laine Stump wrote:
On 04/23/2015 02:20 PM, Cole Robinson wrote:
If this enviroment variable is set, the virTestCompareToFile helper will overwrite the file content we are comparing against, if the file doesn't exist or it doesn't match the expected input.
This is useful when adding new test cases, or making changes that generate a lot of output churn. --- HACKING | 7 +++++++ docs/hacking.html.in | 12 ++++++++++++ tests/testutils.c | 45 +++++++++++++++++++++++++++------------------ 3 files changed, 46 insertions(+), 18 deletions(-)
diff --git a/HACKING b/HACKING index 94d9d2c..cfc507a 100644 --- a/HACKING +++ b/HACKING @@ -136,6 +136,13 @@ Also, individual tests can be run from inside the "tests/" directory, like:
./qemuxml2xmltest
+If you are adding new test cases, or making changes that alter existing test +output, you can use the environment variable VIR_TEST_REGENERATE_OUTPUT to +quickly update the saved test data. Of course you still need to review the +changes to ensure they are correct.
Maybe say "review the changes *VERY CAREFULLY*" or something like that. This is incredibly convenient, but could make it easier for someone to gloss over a regression that happens to be introduced with the same patch where they are adding some new functionality.
[...]
- if (STRNEQ(fixedcontent ? fixedcontent : strcontent, filecontent)) { + if (STRNEQ_NULLABLE(fixedcontent ? fixedcontent : strcontent, + filecontent)) { + if (regenerate) { + if (virFileWriteStr(filename, strcontent, 0666) < 0) + goto failure; + goto out; + } virtTestDifference(stderr, strcontent, filecontent); goto failure;
Okay, so you still report a failure for this test, but in the process you update the test file. Makes sense - that way you can see which tests are getting updated.
Actually no failure is reported, see goto out; vs goto failure; However git diff/git status still makes it very obvious what files have been added or changed, so I think that's okay.
This is *really* cool! ACK++
Thanks for the review. I pushed the the docs suggestion, and the cppi fix for patch #1 - Cole
participants (2)
-
Cole Robinson
-
Laine Stump