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 | 14 ++-
tests/domainsnapshotxml2xmlout/all_parameters.xml | 2 +-
tests/domainsnapshotxml2xmlout/disk_snapshot.xml | 102 ++++++++--------
tests/domainsnapshotxml2xmlout/full_domain.xml | 52 ++++----
tests/domainsnapshotxml2xmltest.c | 128 +++++++++++++++++++++
8 files changed, 224 insertions(+), 87 deletions(-)
create mode 100644 tests/domainsnapshotxml2xmltest.c
diff --git a/.gitignore b/.gitignore
index 41fa50f..bfe8e87 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 33482c0..0b82c3d 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -11942,7 +11942,7 @@ cleanup:
return ret;
}
-char *virDomainSnapshotDefFormat(char *domain_uuid,
+char *virDomainSnapshotDefFormat(const char *domain_uuid,
virDomainSnapshotDefPtr def,
unsigned int flags,
int internal)
@@ -11983,12 +11983,12 @@ char *virDomainSnapshotDefFormat(char *domain_uuid,
virDomainDiskSnapshotTypeToString(disk->snapshot));
if (disk->file || disk->driverType) {
virBufferAddLit(&buf, ">\n");
- if (disk->file)
- virBufferEscapeString(&buf, " <source
file='%s'/>\n",
- disk->file);
if (disk->driverType)
virBufferEscapeString(&buf, " <driver
type='%s'/>\n",
disk->driverType);
+ if (disk->file)
+ virBufferEscapeString(&buf, " <source
file='%s'/>\n",
+ disk->file);
virBufferAddLit(&buf, " </disk>\n");
} else {
virBufferAddLit(&buf, "/>\n");
@@ -12003,7 +12003,7 @@ char *virDomainSnapshotDefFormat(char *domain_uuid,
return NULL;
}
virBufferAdjustIndent(&buf, -2);
- } else {
+ } else if (domain_uuid) {
virBufferAddLit(&buf, " <domain>\n");
virBufferAsprintf(&buf, " <uuid>%s</uuid>\n",
domain_uuid);
virBufferAddLit(&buf, " </domain>\n");
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index effc630..4126d58 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1472,7 +1472,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 cbbbc6f..4769fa5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -102,7 +102,8 @@ check_PROGRAMS += xml2sexprtest sexpr2xmltest \
xmconfigtest xencapstest statstest reconnect
endif
if WITH_QEMU
-check_PROGRAMS += qemuxml2argvtest qemuxml2xmltest qemuargv2xmltest qemuhelptest
+check_PROGRAMS += qemuxml2argvtest qemuxml2xmltest qemuargv2xmltest \
+ qemuhelptest domainsnapshotxml2xmltest
endif
if WITH_OPENVZ
@@ -228,7 +229,7 @@ endif
if WITH_QEMU
TESTS += qemuxml2argvtest qemuxml2xmltest qemuargv2xmltest qemuhelptest
-TESTS += nwfilterxml2xmltest
+TESTS += domainsnapshotxml2xmltest nwfilterxml2xmltest
endif
if WITH_OPENVZ
@@ -350,8 +351,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 qemuargv2xmltest.c qemuhelptest.c
testutilsqemu.c testutilsqemu.h
+EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuargv2xmltest.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 ed4a600..eb2ee85 100644
--- a/tests/domainsnapshotxml2xmlout/all_parameters.xml
+++ b/tests/domainsnapshotxml2xmlout/all_parameters.xml
@@ -1,10 +1,10 @@
<domainsnapshot>
<name>my snap name</name>
<description>!@#$%^</description>
+ <state>running</state>
<parent>
<name>earlier_snap</name>
</parent>
- <state>running</state>
<creationTime>1272917631</creationTime>
<domain>
<uuid>9d37b878-a7cc-9f9a-b78f-49b3abad25a8</uuid>
diff --git a/tests/domainsnapshotxml2xmlout/disk_snapshot.xml
b/tests/domainsnapshotxml2xmlout/disk_snapshot.xml
index e0414a1..a0a0965 100644
--- a/tests/domainsnapshotxml2xmlout/disk_snapshot.xml
+++ b/tests/domainsnapshotxml2xmlout/disk_snapshot.xml
@@ -1,10 +1,10 @@
<domainsnapshot>
<name>my snap name</name>
<description>!@#$%^</description>
+ <state>disk-snapshot</state>
<parent>
<name>earlier_snap</name>
</parent>
- <state>disk-snapshot</state>
<creationTime>1272917631</creationTime>
<disks>
<disk name='hda' snapshot='no'/>
@@ -23,55 +23,55 @@
<source file='/path/to/generated5'/>
</disk>
</disks>
-<domain type='qemu'>
- <name>QEMUGuest1</name>
- <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
- <memory>219100</memory>
- <currentMemory>219100</currentMemory>
- <vcpu cpuset='1-4,8-20,525'>1</vcpu>
- <os>
- <type arch='i686' machine='pc'>hvm</type>
- <boot dev='hd'/>
- </os>
- <clock offset='utc'/>
- <on_poweroff>destroy</on_poweroff>
- <on_reboot>restart</on_reboot>
- <on_crash>destroy</on_crash>
- <devices>
- <emulator>/usr/bin/qemu</emulator>
- <disk type='block' device='disk'>
- <source dev='/dev/HostVG/QEMUGuest1'/>
- <target dev='hda' bus='ide'/>
- <address type='drive' controller='0' bus='0'
unit='0'/>
- </disk>
- <disk type='block' device='disk'>
- <source dev='/dev/HostVG/QEMUGuest2'/>
- <target dev='hdb' bus='ide'/>
- <address type='drive' controller='0' bus='1'
unit='0'/>
- </disk>
- <disk type='block' device='disk'>
- <source dev='/dev/HostVG/QEMUGuest3'/>
- <target dev='hdc' bus='ide'/>
- <address type='drive' controller='0' bus='2'
unit='0'/>
- </disk>
- <disk type='block' device='disk'>
- <source dev='/dev/HostVG/QEMUGuest4'/>
- <target dev='hdd' bus='ide'/>
- <address type='drive' controller='0' bus='3'
unit='0'/>
- </disk>
- <disk type='block' device='disk'>
- <source dev='/dev/HostVG/QEMUGuest5'/>
- <target dev='hde' bus='ide'/>
- <address type='drive' controller='0' bus='4'
unit='0'/>
- </disk>
- <disk type='block' device='disk'>
- <source dev='/dev/HostVG/QEMUGuest6'/>
- <target dev='hdf' bus='ide'/>
- <address type='drive' controller='0' bus='5'
unit='0'/>
- </disk>
- <controller type='ide' index='0'/>
- <memballoon model='virtio'/>
- </devices>
-</domain>
+ <domain type='qemu'>
+ <name>QEMUGuest1</name>
+ <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+ <memory>219100</memory>
+ <currentMemory>219100</currentMemory>
+ <vcpu cpuset='1-4,8-20,525'>1</vcpu>
+ <os>
+ <type arch='i686' machine='pc'>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>destroy</on_crash>
+ <devices>
+ <emulator>/usr/bin/qemu</emulator>
+ <disk type='block' device='disk'>
+ <source dev='/dev/HostVG/QEMUGuest1'/>
+ <target dev='hda' bus='ide'/>
+ <address type='drive' controller='0' bus='0'
unit='0'/>
+ </disk>
+ <disk type='block' device='disk'>
+ <source dev='/dev/HostVG/QEMUGuest2'/>
+ <target dev='hdb' bus='ide'/>
+ <address type='drive' controller='0' bus='1'
unit='0'/>
+ </disk>
+ <disk type='block' device='disk'>
+ <source dev='/dev/HostVG/QEMUGuest3'/>
+ <target dev='hdc' bus='ide'/>
+ <address type='drive' controller='0' bus='2'
unit='0'/>
+ </disk>
+ <disk type='block' device='disk'>
+ <source dev='/dev/HostVG/QEMUGuest4'/>
+ <target dev='hdd' bus='ide'/>
+ <address type='drive' controller='0' bus='3'
unit='0'/>
+ </disk>
+ <disk type='block' device='disk'>
+ <source dev='/dev/HostVG/QEMUGuest5'/>
+ <target dev='hde' bus='ide'/>
+ <address type='drive' controller='0' bus='4'
unit='0'/>
+ </disk>
+ <disk type='block' device='disk'>
+ <source dev='/dev/HostVG/QEMUGuest6'/>
+ <target dev='hdf' bus='ide'/>
+ <address type='drive' controller='0' bus='5'
unit='0'/>
+ </disk>
+ <controller type='ide' index='0'/>
+ <memballoon model='virtio'/>
+ </devices>
+ </domain>
<active>1</active>
</domainsnapshot>
diff --git a/tests/domainsnapshotxml2xmlout/full_domain.xml
b/tests/domainsnapshotxml2xmlout/full_domain.xml
index 942bd7f..76f17e1 100644
--- a/tests/domainsnapshotxml2xmlout/full_domain.xml
+++ b/tests/domainsnapshotxml2xmlout/full_domain.xml
@@ -1,35 +1,35 @@
<domainsnapshot>
<name>my snap name</name>
<description>!@#$%^</description>
+ <state>running</state>
<parent>
<name>earlier_snap</name>
</parent>
- <state>running</state>
<creationTime>1272917631</creationTime>
-<domain type='qemu'>
- <name>QEMUGuest1</name>
- <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
- <memory>219100</memory>
- <currentMemory>219100</currentMemory>
- <vcpu cpuset='1-4,8-20,525'>1</vcpu>
- <os>
- <type arch='i686' machine='pc'>hvm</type>
- <boot dev='hd'/>
- </os>
- <clock offset='utc'/>
- <on_poweroff>destroy</on_poweroff>
- <on_reboot>restart</on_reboot>
- <on_crash>destroy</on_crash>
- <devices>
- <emulator>/usr/bin/qemu</emulator>
- <disk type='block' device='disk'>
- <source dev='/dev/HostVG/QEMUGuest1'/>
- <target dev='hda' bus='ide'/>
- <address type='drive' controller='0' bus='0'
unit='0'/>
- </disk>
- <controller type='ide' index='0'/>
- <memballoon model='virtio'/>
- </devices>
-</domain>
+ <domain type='qemu'>
+ <name>QEMUGuest1</name>
+ <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+ <memory>219100</memory>
+ <currentMemory>219100</currentMemory>
+ <vcpu cpuset='1-4,8-20,525'>1</vcpu>
+ <os>
+ <type arch='i686' machine='pc'>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>destroy</on_crash>
+ <devices>
+ <emulator>/usr/bin/qemu</emulator>
+ <disk type='block' device='disk'>
+ <source dev='/dev/HostVG/QEMUGuest1'/>
+ <target dev='hda' bus='ide'/>
+ <address type='drive' controller='0' bus='0'
unit='0'/>
+ </disk>
+ <controller type='ide' index='0'/>
+ <memballoon model='virtio'/>
+ </devices>
+ </domain>
<active>1</active>
</domainsnapshot>
diff --git a/tests/domainsnapshotxml2xmltest.c b/tests/domainsnapshotxml2xmltest.c
new file mode 100644
index 0000000..c2266f4
--- /dev/null
+++ b/tests/domainsnapshotxml2xmltest.c
@@ -0,0 +1,128 @@
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <fcntl.h>
+
+#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 */
--
1.7.4.4