lxcconf2xmltest.c 3.1 KB
Newer Older
1 2 3 4 5 6 7
#include <config.h>

#include "testutils.h"

#ifdef WITH_LXC

# include "lxc/lxc_native.h"
8 9
# include "lxc/lxc_conf.h"
# include "testutilslxc.h"
10 11 12

# define VIR_FROM_THIS VIR_FROM_NONE

13 14 15
static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt;

16
static int testSanitizeDef(virDomainDefPtr vmdef)
17
{
18 19
    /* Remove UUID randomness */
    if (virUUIDParse("c7a5fdbd-edaf-9455-926a-d65c16db1809", vmdef->uuid) < 0)
20 21 22 23 24
        return -1;
    return 0;
}

static int
25
testCompareXMLToConfigFiles(const char *xmlfile,
26 27
                            const char *configfile,
                            bool expectError)
28 29 30 31 32 33 34 35 36
{
    int ret = -1;
    char *config = NULL;
    char *actualxml = NULL;
    virDomainDefPtr vmdef = NULL;

    if (virtTestLoadFile(configfile, &config) < 0)
        goto fail;

37
    vmdef = lxcParseConfigString(config, caps, xmlopt);
38
    if ((vmdef && expectError) || (!vmdef && !expectError))
39 40
        goto fail;

41
    if (vmdef) {
42
        if (testSanitizeDef(vmdef) < 0)
43
            goto fail;
44

45
        if (!(actualxml = virDomainDefFormat(vmdef, caps, 0)))
46 47
            goto fail;

48
        if (virtTestCompareToFile(actualxml, xmlfile) < 0)
49
            goto fail;
50 51 52 53
    }

    ret = 0;

54
 fail:
55 56 57 58 59 60
    VIR_FREE(actualxml);
    VIR_FREE(config);
    virDomainDefFree(vmdef);
    return ret;
}

61 62 63 64 65
struct testInfo {
    const char *name;
    bool expectError;
};

66 67 68 69
static int
testCompareXMLToConfigHelper(const void *data)
{
    int result = -1;
70
    const struct testInfo *info = data;
71 72 73 74
    char *xml = NULL;
    char *config = NULL;

    if (virAsprintf(&xml, "%s/lxcconf2xmldata/lxcconf2xml-%s.xml",
75
                    abs_srcdir, info->name) < 0 ||
76
        virAsprintf(&config, "%s/lxcconf2xmldata/lxcconf2xml-%s.config",
77
                    abs_srcdir, info->name) < 0)
78 79
        goto cleanup;

80
    result = testCompareXMLToConfigFiles(xml, config, info->expectError);
81

82
 cleanup:
83 84 85 86 87 88 89 90 91 92
    VIR_FREE(xml);
    VIR_FREE(config);
    return result;
}

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

93 94 95 96 97 98 99 100
    if (!(caps = testLXCCapsInit()))
        return EXIT_FAILURE;

    if (!(xmlopt = lxcDomainXMLConfInit())) {
        virObjectUnref(caps);
        return EXIT_FAILURE;
    }

101 102 103 104 105 106 107 108 109 110 111
# define DO_TEST(name, expectError)                         \
    do {                                                    \
        const struct testInfo info = { name, expectError }; \
        if (virtTestRun("LXC Native-2-XML " name,           \
                        testCompareXMLToConfigHelper,       \
                        &info) < 0)                         \
            ret = EXIT_FAILURE;                             \
    } while (0)

    DO_TEST("simple", false);
    DO_TEST("fstab", true);
112 113
    DO_TEST("nonetwork", false);
    DO_TEST("nonenetwork", false);
114
    DO_TEST("physnetwork", false);
115
    DO_TEST("macvlannetwork", false);
116
    DO_TEST("vlannetwork", false);
117
    DO_TEST("idmap", false);
118
    DO_TEST("memtune", false);
119
    DO_TEST("cputune", false);
120
    DO_TEST("cpusettune", false);
121
    DO_TEST("blkiotune", false);
122
    DO_TEST("ethernet", false);
123

124 125 126
    virObjectUnref(xmlopt);
    virObjectUnref(caps);

127 128 129 130 131 132 133 134 135 136 137 138 139 140
    return ret;
}

VIRT_TEST_MAIN(mymain)

#else

int
main(void)
{
    return EXIT_AM_SKIP;
}

#endif /* WITH_LXC */