domainsnapshotxml2xmltest.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <fcntl.h>

11 12
#include "testutils.h"

13 14 15 16 17 18
#ifdef WITH_QEMU

# include "internal.h"
# include "qemu/qemu_conf.h"
# include "qemu/qemu_domain.h"
# include "testutilsqemu.h"
19
# include "virstring.h"
20

21 22
# define VIR_FROM_THIS VIR_FROM_NONE

23
static virQEMUDriver driver;
24 25

static int
26
testCompareXMLToXMLFiles(const char *inxml, const char *uuid, bool internal)
27 28 29 30 31 32 33 34 35
{
    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)
36
        goto cleanup;
37 38 39 40

    if (internal)
        flags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
    if (!(def = virDomainSnapshotDefParseString(inXmlData, driver.caps,
41
                                                driver.xmlopt,
42 43
                                                QEMU_EXPECTED_VIRT_TYPES,
                                                flags)))
44
        goto cleanup;
45 46 47 48

    if (!(actual = virDomainSnapshotDefFormat(uuid, def,
                                              VIR_DOMAIN_XML_SECURE,
                                              internal)))
49
        goto cleanup;
50 51 52 53


    if (STRNEQ(inXmlData, actual)) {
        virtTestDifference(stderr, inXmlData, actual);
54
        goto cleanup;
55 56 57
    }

    ret = 0;
58 59

cleanup:
60 61
    VIR_FREE(inXmlData);
    VIR_FREE(actual);
62 63 64 65 66 67 68
    virDomainSnapshotDefFree(def);
    return ret;
}

struct testInfo {
    const char *name;
    const char *uuid;
69
    bool internal;
70 71
};

72

73 74 75 76 77 78 79 80 81 82 83 84 85 86
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:
87
    VIR_FREE(xml_in);
88 89 90 91 92 93 94 95 96 97
    return ret;
}


static int
mymain(void)
{
    int ret = 0;

    if ((driver.caps = testQemuCapsInit()) == NULL)
98
        return EXIT_FAILURE;
99

100 101
    if (!(driver.xmlopt = virQEMUDriverCreateXMLConf(&driver))) {
        virObjectUnref(driver.caps);
102
        return EXIT_FAILURE;
103
    }
104

105 106 107 108
# define DO_TEST(name, uuid, internal)                                  \
    do {                                                                \
        const struct testInfo info = {name, uuid, internal};            \
        if (virtTestRun("SNAPSHOT XML-2-XML " name,                     \
109
                        testCompareXMLToXMLHelper, &info) < 0)          \
110 111 112 113 114 115 116 117
            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);

118 119 120 121 122 123 124 125
    DO_TEST("all_parameters", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", true);
    DO_TEST("disk_snapshot", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
    DO_TEST("full_domain", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
    DO_TEST("noparent_nodescription_noactive", NULL, false);
    DO_TEST("noparent_nodescription", NULL, true);
    DO_TEST("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false);
    DO_TEST("metadata", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
    DO_TEST("external_vm", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
126

127
    virObjectUnref(driver.caps);
128
    virObjectUnref(driver.xmlopt);
129

130
    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
131 132 133 134 135 136 137 138 139 140 141 142 143
}

VIRT_TEST_MAIN(mymain)

#else

int
main(void)
{
    return EXIT_AM_SKIP;
}

#endif /* WITH_QEMU */