2011-09-22 20:29:00 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2013-12-04 13:53:24 +00:00
|
|
|
#include <regex.h>
|
|
|
|
|
2013-04-16 13:41:44 +00:00
|
|
|
#include "testutils.h"
|
|
|
|
|
2011-09-22 20:29:00 +00:00
|
|
|
#ifdef WITH_QEMU
|
|
|
|
|
|
|
|
# include "internal.h"
|
|
|
|
# include "qemu/qemu_conf.h"
|
|
|
|
# include "qemu/qemu_domain.h"
|
|
|
|
# include "testutilsqemu.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
# include "virstring.h"
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2013-06-07 15:10:28 +00:00
|
|
|
# define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2012-11-28 16:43:10 +00:00
|
|
|
static virQEMUDriver driver;
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2013-12-04 13:53:24 +00:00
|
|
|
/* This regex will skip the following XML constructs in test files
|
|
|
|
* that are dynamically generated and thus problematic to test:
|
|
|
|
* <name>1234352345</name> if the snapshot has no name,
|
|
|
|
* <creationTime>23523452345</creationTime>,
|
|
|
|
* <state>nostate</state> as the backend code doesn't fill this
|
|
|
|
*/
|
|
|
|
static const char *testSnapshotXMLVariableLineRegexStr =
|
|
|
|
"(<(name|creationTime)>[0-9]+</(name|creationTime)>|"
|
|
|
|
"<state>nostate</state>)";
|
|
|
|
|
|
|
|
regex_t *testSnapshotXMLVariableLineRegex = NULL;
|
|
|
|
|
|
|
|
static char *
|
|
|
|
testFilterXML(char *xml)
|
|
|
|
{
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
char **xmlLines = NULL;
|
|
|
|
char **xmlLine;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if (!(xmlLines = virStringSplit(xml, "\n", 0))) {
|
|
|
|
VIR_FREE(xml);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
VIR_FREE(xml);
|
|
|
|
|
|
|
|
for (xmlLine = xmlLines; *xmlLine; xmlLine++) {
|
|
|
|
if (regexec(testSnapshotXMLVariableLineRegex,
|
|
|
|
*xmlLine, 0, NULL, 0) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
virBufferStrcat(&buf, *xmlLine, "\n", NULL);
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:40:15 +00:00
|
|
|
if (virBufferCheckError(&buf) < 0)
|
2013-12-04 13:53:24 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = virBufferContentAndReset(&buf);
|
|
|
|
|
2014-03-25 06:53:44 +00:00
|
|
|
cleanup:
|
2013-12-04 13:53:24 +00:00
|
|
|
virBufferFreeAndReset(&buf);
|
2016-11-25 08:18:35 +00:00
|
|
|
virStringListFree(xmlLines);
|
2013-12-04 13:53:24 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-22 20:29:00 +00:00
|
|
|
static int
|
2013-12-04 13:53:24 +00:00
|
|
|
testCompareXMLToXMLFiles(const char *inxml,
|
|
|
|
const char *outxml,
|
|
|
|
const char *uuid,
|
|
|
|
bool internal,
|
|
|
|
bool redefine)
|
2011-09-22 20:29:00 +00:00
|
|
|
{
|
|
|
|
char *inXmlData = NULL;
|
2013-12-04 13:53:24 +00:00
|
|
|
char *outXmlData = NULL;
|
2011-09-22 20:29:00 +00:00
|
|
|
char *actual = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
virDomainSnapshotDefPtr def = NULL;
|
2013-12-04 13:53:24 +00:00
|
|
|
unsigned int flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
|
|
|
|
|
|
|
|
if (internal)
|
|
|
|
flags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
|
|
|
|
|
|
|
|
if (redefine)
|
|
|
|
flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2016-05-26 15:01:52 +00:00
|
|
|
if (virTestLoadFile(inxml, &inXmlData) < 0)
|
2013-12-04 12:49:40 +00:00
|
|
|
goto cleanup;
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2016-05-26 15:01:52 +00:00
|
|
|
if (virTestLoadFile(outxml, &outXmlData) < 0)
|
2013-12-04 13:53:24 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2011-09-22 20:29:00 +00:00
|
|
|
if (!(def = virDomainSnapshotDefParseString(inXmlData, driver.caps,
|
2013-03-31 18:03:42 +00:00
|
|
|
driver.xmlopt,
|
2011-09-22 20:29:00 +00:00
|
|
|
flags)))
|
2013-12-04 12:49:40 +00:00
|
|
|
goto cleanup;
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2016-02-04 22:55:05 +00:00
|
|
|
if (!(actual = virDomainSnapshotDefFormat(uuid, def, driver.caps,
|
2017-06-01 22:27:33 +00:00
|
|
|
driver.xmlopt,
|
2014-11-18 16:44:00 +00:00
|
|
|
VIR_DOMAIN_DEF_FORMAT_SECURE,
|
2011-09-22 20:29:00 +00:00
|
|
|
internal)))
|
2013-12-04 12:49:40 +00:00
|
|
|
goto cleanup;
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2013-12-04 13:53:24 +00:00
|
|
|
if (!redefine) {
|
|
|
|
if (!(actual = testFilterXML(actual)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(outXmlData = testFilterXML(outXmlData)))
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2013-12-04 13:53:24 +00:00
|
|
|
if (STRNEQ(outXmlData, actual)) {
|
2016-05-26 15:01:54 +00:00
|
|
|
virTestDifferenceFull(stderr, outXmlData, outxml, actual, inxml);
|
2013-12-04 12:49:40 +00:00
|
|
|
goto cleanup;
|
2011-09-22 20:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2013-12-04 12:49:40 +00:00
|
|
|
|
2014-03-25 06:53:44 +00:00
|
|
|
cleanup:
|
2012-02-02 23:16:43 +00:00
|
|
|
VIR_FREE(inXmlData);
|
2013-12-04 13:53:24 +00:00
|
|
|
VIR_FREE(outXmlData);
|
2012-02-02 23:16:43 +00:00
|
|
|
VIR_FREE(actual);
|
2011-09-22 20:29:00 +00:00
|
|
|
virDomainSnapshotDefFree(def);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct testInfo {
|
2013-12-04 13:53:24 +00:00
|
|
|
const char *inxml;
|
|
|
|
const char *outxml;
|
2011-09-22 20:29:00 +00:00
|
|
|
const char *uuid;
|
2013-12-04 12:49:40 +00:00
|
|
|
bool internal;
|
2013-12-04 13:53:24 +00:00
|
|
|
bool redefine;
|
2011-09-22 20:29:00 +00:00
|
|
|
};
|
|
|
|
|
2013-12-04 12:49:40 +00:00
|
|
|
|
2011-09-22 20:29:00 +00:00
|
|
|
static int
|
|
|
|
testCompareXMLToXMLHelper(const void *data)
|
|
|
|
{
|
|
|
|
const struct testInfo *info = data;
|
|
|
|
|
2013-12-04 13:53:24 +00:00
|
|
|
return testCompareXMLToXMLFiles(info->inxml, info->outxml, info->uuid,
|
|
|
|
info->internal, info->redefine);
|
2011-09-22 20:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2015-09-15 06:16:02 +00:00
|
|
|
if (qemuTestDriverInit(&driver) < 0)
|
2012-03-22 11:33:35 +00:00
|
|
|
return EXIT_FAILURE;
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2015-09-15 06:16:02 +00:00
|
|
|
/* TODO: test with format probing disabled too */
|
|
|
|
driver.config->allowDiskFormatProbing = true;
|
2013-03-05 15:17:24 +00:00
|
|
|
|
2013-12-04 13:53:24 +00:00
|
|
|
if (VIR_ALLOC(testSnapshotXMLVariableLineRegex) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (regcomp(testSnapshotXMLVariableLineRegex,
|
|
|
|
testSnapshotXMLVariableLineRegexStr,
|
|
|
|
REG_EXTENDED | REG_NOSUB) != 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
"failed to compile test regex");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# define DO_TEST(prefix, name, inpath, outpath, uuid, internal, redefine) \
|
|
|
|
do { \
|
|
|
|
const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml", \
|
|
|
|
abs_srcdir "/" outpath "/" name ".xml", \
|
|
|
|
uuid, internal, redefine}; \
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("SNAPSHOT XML-2-XML " prefix " " name, \
|
|
|
|
testCompareXMLToXMLHelper, &info) < 0) \
|
2013-12-04 13:53:24 +00:00
|
|
|
ret = -1; \
|
2011-09-22 20:29:00 +00:00
|
|
|
} while (0)
|
|
|
|
|
2013-12-04 13:53:24 +00:00
|
|
|
# define DO_TEST_IN(name, uuid) DO_TEST("in->in", name,\
|
|
|
|
"domainsnapshotxml2xmlin",\
|
|
|
|
"domainsnapshotxml2xmlin",\
|
|
|
|
uuid, false, false)
|
|
|
|
|
|
|
|
# define DO_TEST_OUT(name, uuid, internal) DO_TEST("out->out", name,\
|
|
|
|
"domainsnapshotxml2xmlout",\
|
|
|
|
"domainsnapshotxml2xmlout",\
|
|
|
|
uuid, internal, true)
|
|
|
|
|
|
|
|
# define DO_TEST_INOUT(name, uuid, internal, redefine) \
|
|
|
|
DO_TEST("in->out", name,\
|
|
|
|
"domainsnapshotxml2xmlin",\
|
|
|
|
"domainsnapshotxml2xmlout",\
|
|
|
|
uuid, internal, redefine)
|
|
|
|
|
2011-09-22 20:29:00 +00:00
|
|
|
/* 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);
|
|
|
|
|
2013-12-04 13:53:24 +00:00
|
|
|
DO_TEST_OUT("all_parameters", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", true);
|
2013-12-04 17:25:56 +00:00
|
|
|
DO_TEST_OUT("disk_snapshot_redefine", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
|
2013-12-04 13:53:24 +00:00
|
|
|
DO_TEST_OUT("full_domain", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
|
|
|
|
DO_TEST_OUT("noparent_nodescription_noactive", NULL, false);
|
|
|
|
DO_TEST_OUT("noparent_nodescription", NULL, true);
|
|
|
|
DO_TEST_OUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false);
|
|
|
|
DO_TEST_OUT("metadata", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
|
2013-12-04 17:25:56 +00:00
|
|
|
DO_TEST_OUT("external_vm_redefine", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2013-12-04 17:31:39 +00:00
|
|
|
DO_TEST_INOUT("empty", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
|
|
|
|
DO_TEST_INOUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
|
|
|
|
DO_TEST_INOUT("external_vm", NULL, false, false);
|
|
|
|
DO_TEST_INOUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
|
|
|
|
DO_TEST_INOUT("disk_snapshot", NULL, false, false);
|
2013-12-03 11:04:43 +00:00
|
|
|
DO_TEST_INOUT("disk_driver_name_null", NULL, false, false);
|
2013-12-04 17:31:39 +00:00
|
|
|
|
|
|
|
DO_TEST_IN("name_and_description", NULL);
|
|
|
|
DO_TEST_IN("description_only", NULL);
|
|
|
|
DO_TEST_IN("name_only", NULL);
|
|
|
|
|
2014-03-25 06:53:44 +00:00
|
|
|
cleanup:
|
2013-12-04 13:53:24 +00:00
|
|
|
if (testSnapshotXMLVariableLineRegex)
|
|
|
|
regfree(testSnapshotXMLVariableLineRegex);
|
|
|
|
VIR_FREE(testSnapshotXMLVariableLineRegex);
|
2015-09-15 06:16:02 +00:00
|
|
|
qemuTestDriverFree(&driver);
|
2011-09-22 20:29:00 +00:00
|
|
|
|
2014-03-17 09:38:38 +00:00
|
|
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2011-09-22 20:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|
2011-09-22 20:29:00 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
return EXIT_AM_SKIP;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WITH_QEMU */
|