From 27b3b303d966a7dff3bf7219e253dbefd814f97d Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 22 Sep 2011 14:29:00 -0600 Subject: [PATCH] snapshot: test domainsnapshot indentation Add a test for the simple parts of my indentation changes, and fix the fallout. * tests/domainsnapshotxml2xmltest.c: New test. * tests/Makefile.am (domainsnapshotxml2xmltest_SOURCES): Build it. * src/conf/domain_conf.c (virDomainSnapshotDefFormat): Avoid NULL deref, match documented order. * src/conf/domain_conf.h (virDomainSnapshotDefFormat): Add const. * tests/domainsnapshotxml2xmlout/all_parameters.xml: Tweak output. * tests/domainsnapshotxml2xmlout/disk_snapshot.xml: Likewise. * tests/domainsnapshotxml2xmlout/full_domain.xml: Likewise. * .gitignore: Exempt new binary. --- .gitignore | 1 + src/conf/domain_conf.c | 10 +- src/conf/domain_conf.h | 2 +- tests/Makefile.am | 16 ++- .../all_parameters.xml | 2 +- .../disk_snapshot.xml | 102 +++++++------- .../domainsnapshotxml2xmlout/full_domain.xml | 52 +++---- tests/domainsnapshotxml2xmltest.c | 128 ++++++++++++++++++ 8 files changed, 225 insertions(+), 88 deletions(-) create mode 100644 tests/domainsnapshotxml2xmltest.c diff --git a/.gitignore b/.gitignore index b04db588f9..0552b5245d 100644 --- a/.gitignore +++ b/.gitignore @@ -69,6 +69,7 @@ /src/util/virkeymaps.h /tests/*.log /tests/cputest +/tests/domainsnapshotxml2xmltest /tests/hashtest /tests/jsontest /tests/networkxml2argvtest diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index adb596edd5..ae3bce73bd 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -12020,7 +12020,7 @@ cleanup: return ret; } -char *virDomainSnapshotDefFormat(char *domain_uuid, +char *virDomainSnapshotDefFormat(const char *domain_uuid, virDomainSnapshotDefPtr def, unsigned int flags, int internal) @@ -12061,12 +12061,12 @@ char *virDomainSnapshotDefFormat(char *domain_uuid, virDomainDiskSnapshotTypeToString(disk->snapshot)); if (disk->file || disk->driverType) { virBufferAddLit(&buf, ">\n"); - if (disk->file) - virBufferEscapeString(&buf, " \n", - disk->file); if (disk->driverType) virBufferEscapeString(&buf, " \n", disk->driverType); + if (disk->file) + virBufferEscapeString(&buf, " \n", + disk->file); virBufferAddLit(&buf, " \n"); } else { virBufferAddLit(&buf, "/>\n"); @@ -12081,7 +12081,7 @@ char *virDomainSnapshotDefFormat(char *domain_uuid, return NULL; } virBufferAdjustIndent(&buf, -2); - } else { + } else if (domain_uuid) { virBufferAddLit(&buf, " \n"); virBufferAsprintf(&buf, " %s\n", domain_uuid); virBufferAddLit(&buf, " \n"); diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 8ef9d46b35..0a409186a9 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1498,7 +1498,7 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseString(const char *xmlStr, unsigned int expectedVirtTypes, unsigned int flags); void virDomainSnapshotDefFree(virDomainSnapshotDefPtr def); -char *virDomainSnapshotDefFormat(char *domain_uuid, +char *virDomainSnapshotDefFormat(const char *domain_uuid, virDomainSnapshotDefPtr def, unsigned int flags, int internal); diff --git a/tests/Makefile.am b/tests/Makefile.am index b522118659..6bff670a9f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -109,7 +109,8 @@ check_PROGRAMS += xml2sexprtest sexpr2xmltest \ xmconfigtest xencapstest statstest reconnect endif if WITH_QEMU -check_PROGRAMS += qemuxml2argvtest qemuxml2xmltest qemuxmlnstest qemuargv2xmltest qemuhelptest +check_PROGRAMS += qemuxml2argvtest qemuxml2xmltest qemuxmlnstest \ + qemuargv2xmltest qemuhelptest domainsnapshotxml2xmltest endif if WITH_OPENVZ @@ -234,8 +235,8 @@ TESTS += xml2sexprtest \ endif if WITH_QEMU -TESTS += qemuxml2argvtest qemuxml2xmltest qemuxmlnstest qemuargv2xmltest qemuhelptest -TESTS += nwfilterxml2xmltest +TESTS += qemuxml2argvtest qemuxml2xmltest qemuxmlnstest qemuargv2xmltest \ + qemuhelptest domainsnapshotxml2xmltest nwfilterxml2xmltest endif if WITH_OPENVZ @@ -362,8 +363,15 @@ qemuargv2xmltest_LDADD = $(qemu_LDADDS) $(LDADDS) qemuhelptest_SOURCES = qemuhelptest.c testutils.c testutils.h qemuhelptest_LDADD = $(qemu_LDADDS) $(LDADDS) + +domainsnapshotxml2xmltest_SOURCES = \ + domainsnapshotxml2xmltest.c testutilsqemu.c testutilsqemu.h \ + testutils.c testutils.h +domainsnapshotxml2xmltest_LDADD = $(qemu_LDADDS) $(LDADDS) else -EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuxmlnstest.c qemuargv2xmltest.c qemuhelptest.c testutilsqemu.c testutilsqemu.h +EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuargv2xmltest.c \ + qemuxmlnstest.c qemuhelptest.c domainsnapshotxml2xmltest.c \ + testutilsqemu.c testutilsqemu.h endif if WITH_OPENVZ diff --git a/tests/domainsnapshotxml2xmlout/all_parameters.xml b/tests/domainsnapshotxml2xmlout/all_parameters.xml index ed4a60006c..eb2ee85f38 100644 --- a/tests/domainsnapshotxml2xmlout/all_parameters.xml +++ b/tests/domainsnapshotxml2xmlout/all_parameters.xml @@ -1,10 +1,10 @@ my snap name !@#$%^ + running earlier_snap - running 1272917631 9d37b878-a7cc-9f9a-b78f-49b3abad25a8 diff --git a/tests/domainsnapshotxml2xmlout/disk_snapshot.xml b/tests/domainsnapshotxml2xmlout/disk_snapshot.xml index e0414a1b9c..a0a0965479 100644 --- a/tests/domainsnapshotxml2xmlout/disk_snapshot.xml +++ b/tests/domainsnapshotxml2xmlout/disk_snapshot.xml @@ -1,10 +1,10 @@ my snap name !@#$%^ + disk-snapshot earlier_snap - disk-snapshot 1272917631 @@ -23,55 +23,55 @@ - - QEMUGuest1 - c7a5fdbd-edaf-9455-926a-d65c16db1809 - 219100 - 219100 - 1 - - hvm - - - - destroy - restart - destroy - - /usr/bin/qemu - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - - + + QEMUGuest1 + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 219100 + 219100 + 1 + + hvm + + + + destroy + restart + destroy + + /usr/bin/qemu + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + + 1 diff --git a/tests/domainsnapshotxml2xmlout/full_domain.xml b/tests/domainsnapshotxml2xmlout/full_domain.xml index 942bd7faa5..76f17e11fc 100644 --- a/tests/domainsnapshotxml2xmlout/full_domain.xml +++ b/tests/domainsnapshotxml2xmlout/full_domain.xml @@ -1,35 +1,35 @@ my snap name !@#$%^ + running earlier_snap - running 1272917631 - - QEMUGuest1 - c7a5fdbd-edaf-9455-926a-d65c16db1809 - 219100 - 219100 - 1 - - hvm - - - - destroy - restart - destroy - - /usr/bin/qemu - - - -
- - - - - + + QEMUGuest1 + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 219100 + 219100 + 1 + + hvm + + + + destroy + restart + destroy + + /usr/bin/qemu + + + +
+ + + + + 1 diff --git a/tests/domainsnapshotxml2xmltest.c b/tests/domainsnapshotxml2xmltest.c new file mode 100644 index 0000000000..c2266f4eb1 --- /dev/null +++ b/tests/domainsnapshotxml2xmltest.c @@ -0,0 +1,128 @@ +#include + +#include +#include +#include +#include + +#include +#include + +#ifdef WITH_QEMU + +# include "internal.h" +# include "testutils.h" +# include "qemu/qemu_conf.h" +# include "qemu/qemu_domain.h" +# include "testutilsqemu.h" + +static struct qemud_driver driver; + +static int +testCompareXMLToXMLFiles(const char *inxml, const char *uuid, int internal) +{ + char *inXmlData = NULL; + char *actual = NULL; + int ret = -1; + virDomainSnapshotDefPtr def = NULL; + unsigned int flags = (VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE | + VIR_DOMAIN_SNAPSHOT_PARSE_DISKS); + + if (virtTestLoadFile(inxml, &inXmlData) < 0) + goto fail; + + if (internal) + flags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL; + if (!(def = virDomainSnapshotDefParseString(inXmlData, driver.caps, + QEMU_EXPECTED_VIRT_TYPES, + flags))) + goto fail; + + if (!(actual = virDomainSnapshotDefFormat(uuid, def, + VIR_DOMAIN_XML_SECURE, + internal))) + goto fail; + + + if (STRNEQ(inXmlData, actual)) { + virtTestDifference(stderr, inXmlData, actual); + goto fail; + } + + ret = 0; + fail: + free(inXmlData); + free(actual); + virDomainSnapshotDefFree(def); + return ret; +} + +struct testInfo { + const char *name; + const char *uuid; + int internal; +}; + +static int +testCompareXMLToXMLHelper(const void *data) +{ + const struct testInfo *info = data; + char *xml_in = NULL; + int ret = -1; + + if (virAsprintf(&xml_in, "%s/domainsnapshotxml2xmlout/%s.xml", + abs_srcdir, info->name) < 0) + goto cleanup; + + ret = testCompareXMLToXMLFiles(xml_in, info->uuid, info->internal); + +cleanup: + free(xml_in); + return ret; +} + + +static int +mymain(void) +{ + int ret = 0; + + if ((driver.caps = testQemuCapsInit()) == NULL) + return (EXIT_FAILURE); + +# define DO_TEST(name, uuid, internal) \ + do { \ + const struct testInfo info = {name, uuid, internal}; \ + if (virtTestRun("SNAPSHOT XML-2-XML " name, \ + 1, testCompareXMLToXMLHelper, &info) < 0) \ + ret = -1; \ + } while (0) + + /* Unset or set all envvars here that are copied in qemudBuildCommandLine + * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected + * values for these envvars */ + setenv("PATH", "/bin", 1); + + DO_TEST("all_parameters", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", 1); + DO_TEST("disk_snapshot", "c7a5fdbd-edaf-9455-926a-d65c16db1809", 1); + DO_TEST("full_domain", "c7a5fdbd-edaf-9455-926a-d65c16db1809", 1); + DO_TEST("noparent_nodescription_noactive", NULL, 0); + DO_TEST("noparent_nodescription", NULL, 1); + DO_TEST("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", 0); + + virCapabilitiesFree(driver.caps); + + return (ret==0 ? EXIT_SUCCESS : EXIT_FAILURE); +} + +VIRT_TEST_MAIN(mymain) + +#else + +int +main(void) +{ + return EXIT_AM_SKIP; +} + +#endif /* WITH_QEMU */ -- GitLab