xmconfigtest.c 6.8 KB
Newer Older
1 2 3
/*
 * xmconfigtest.c: Test backend for xm_internal config file handling
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2007, 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 *
 */

24
#include <config.h>
25

26 27
#include <stdio.h>
#include <string.h>
28
#include <unistd.h>
29

30
#include "internal.h"
31
#include "datatypes.h"
32 33
#include "xen/xen_driver.h"
#include "xen/xm_internal.h"
34
#include "xenxs/xen_xm.h"
35
#include "testutils.h"
36
#include "testutilsxen.h"
37
#include "viralloc.h"
38

39
static virCapsPtr caps;
40
static virDomainXMLOptionPtr xmlopt;
41

42 43 44 45 46 47
static int
testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion)
{
    char *xmlData = NULL;
    char *xmcfgData = NULL;
    char *gotxmcfgData = NULL;
48 49
    virConfPtr conf = NULL;
    int ret = -1;
E
Eric Blake 已提交
50
    virConnectPtr conn = NULL;
51
    int wrote = 4096;
52
    struct _xenUnifiedPrivate priv;
53
    virDomainDefPtr def = NULL;
54

55 56 57
    if (VIR_ALLOC_N(gotxmcfgData, wrote) < 0)
        goto fail;

58
    conn = virGetConnect();
59
    if (!conn) goto fail;
60

61
    if (virtTestLoadFile(xml, &xmlData) < 0)
62 63
        goto fail;

64
    if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
65 66
        goto fail;

67 68
    /* Many puppies died to bring you this code. */
    priv.xendConfigVersion = xendConfigVersion;
69
    priv.caps = caps;
70
    conn->privateData = &priv;
71

72
    if (!(def = virDomainDefParseString(xmlData, caps, xmlopt,
73
                                        1 << VIR_DOMAIN_VIRT_XEN,
G
Guido Günther 已提交
74
                                        VIR_DOMAIN_XML_INACTIVE)))
75 76
        goto fail;

M
Markus Groß 已提交
77
    if (!(conf = xenFormatXM(conn, def, xendConfigVersion)))
78 79
        goto fail;

80
    if (virConfWriteMem(gotxmcfgData, &wrote, conf) < 0)
81
        goto fail;
82
    gotxmcfgData[wrote] = '\0';
83

84 85
    if (STRNEQ(xmcfgData, gotxmcfgData)) {
        virtTestDifference(stderr, xmcfgData, gotxmcfgData);
86
        goto fail;
87
    }
88 89 90 91

    ret = 0;

 fail:
92 93 94
    VIR_FREE(xmlData);
    VIR_FREE(xmcfgData);
    VIR_FREE(gotxmcfgData);
95 96
    if (conf)
        virConfFree(conf);
97
    virDomainDefFree(def);
98
    virObjectUnref(conn);
99

100 101 102
    return ret;
}

103 104 105 106 107
static int
testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion)
{
    char *xmlData = NULL;
    char *xmcfgData = NULL;
108 109 110 111
    char *gotxml = NULL;
    virConfPtr conf = NULL;
    int ret = -1;
    virConnectPtr conn;
112
    struct _xenUnifiedPrivate priv;
113
    virDomainDefPtr def = NULL;
114

115
    conn = virGetConnect();
116
    if (!conn) goto fail;
117

118
    if (virtTestLoadFile(xml, &xmlData) < 0)
119 120
        goto fail;

121
    if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
122 123
        goto fail;

124 125
    /* Many puppies died to bring you this code. */
    priv.xendConfigVersion = xendConfigVersion;
126
    priv.caps = caps;
127
    conn->privateData = &priv;
128

129
    if (!(conf = virConfReadMem(xmcfgData, strlen(xmcfgData), 0)))
130 131
        goto fail;

M
Markus Groß 已提交
132
    if (!(def = xenParseXM(conf, priv.xendConfigVersion, priv.caps)))
133 134
        goto fail;

135
    if (!(gotxml = virDomainDefFormat(def, VIR_DOMAIN_XML_SECURE)))
136 137
        goto fail;

138 139
    if (STRNEQ(xmlData, gotxml)) {
        virtTestDifference(stderr, xmlData, gotxml);
140
        goto fail;
141
    }
142 143 144 145 146 147

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);
148 149
    VIR_FREE(xmlData);
    VIR_FREE(xmcfgData);
150 151
    VIR_FREE(gotxml);
    virDomainDefFree(def);
152
    virObjectUnref(conn);
153

154 155 156
    return ret;
}

157

158 159 160 161 162
struct testInfo {
    const char *name;
    int version;
    int mode;
};
163

164 165 166 167
static int
testCompareHelper(const void *data)
{
    int result = -1;
168
    const struct testInfo *info = data;
169 170 171 172 173 174 175 176 177
    char *xml = NULL;
    char *cfg = NULL;

    if (virAsprintf(&xml, "%s/xmconfigdata/test-%s.xml",
                    abs_srcdir, info->name) < 0 ||
        virAsprintf(&cfg, "%s/xmconfigdata/test-%s.cfg",
                    abs_srcdir, info->name) < 0)
        goto cleanup;

178
    if (info->mode == 0)
179
        result = testCompareParseXML(cfg, xml, info->version);
180
    else
181 182 183
        result = testCompareFormatXML(cfg, xml, info->version);

cleanup:
184 185
    VIR_FREE(xml);
    VIR_FREE(cfg);
186 187

    return result;
188 189
}

190

191
static int
E
Eric Blake 已提交
192
mymain(void)
193 194
{
    int ret = 0;
195

196
    if (!(caps = testXenCapsInit()))
197
        return EXIT_FAILURE;
198

199
    if (!(xmlopt = xenDomainXMLConfInit()))
200 201
        return EXIT_FAILURE;

202 203 204 205 206 207 208 209 210 211 212 213
#define DO_TEST(name, version)                                          \
    do {                                                                \
        struct testInfo info0 = { name, version, 0 };                   \
        struct testInfo info1 = { name, version, 1 };                   \
        if (virtTestRun("Xen XM-2-XML Parse  " name,                    \
                        1, testCompareHelper, &info0) < 0)              \
            ret = -1;                                                   \
        if (virtTestRun("Xen XM-2-XML Format " name,                    \
                        1, testCompareHelper, &info1) < 0)              \
            ret = -1;                                                   \
    } while (0)

214 215
    DO_TEST("paravirt-old-pvfb", 1);
    DO_TEST("paravirt-old-pvfb-vncdisplay", 1);
216
    DO_TEST("paravirt-new-pvfb", 3);
217
    DO_TEST("paravirt-new-pvfb-vncdisplay", 3);
218
    DO_TEST("paravirt-net-e1000", 3);
219
    DO_TEST("paravirt-net-vifname", 3);
220
    DO_TEST("paravirt-vcpu", 2);
221 222 223 224 225 226
    DO_TEST("fullvirt-old-cdrom", 1);
    DO_TEST("fullvirt-new-cdrom", 2);
    DO_TEST("fullvirt-utc", 2);
    DO_TEST("fullvirt-localtime", 2);
    DO_TEST("fullvirt-usbtablet", 2);
    DO_TEST("fullvirt-usbmouse", 2);
227
    DO_TEST("fullvirt-serial-file", 2);
228 229
    DO_TEST("fullvirt-serial-dev-2-ports", 2);
    DO_TEST("fullvirt-serial-dev-2nd-port", 2);
230 231 232 233 234 235 236 237 238
    DO_TEST("fullvirt-serial-null", 2);
    DO_TEST("fullvirt-serial-pipe", 2);
    DO_TEST("fullvirt-serial-pty", 2);
    DO_TEST("fullvirt-serial-stdio", 2);
    DO_TEST("fullvirt-serial-tcp", 2);
    DO_TEST("fullvirt-serial-tcp-telnet", 2);
    DO_TEST("fullvirt-serial-udp", 2);
    DO_TEST("fullvirt-serial-unix", 2);

239 240 241
    DO_TEST("fullvirt-force-hpet", 2);
    DO_TEST("fullvirt-force-nohpet", 2);

242
    DO_TEST("fullvirt-parallel-tcp", 2);
243

D
Daniel Veillard 已提交
244 245
    DO_TEST("fullvirt-sound", 2);

246 247 248
    DO_TEST("fullvirt-net-ioemu", 2);
    DO_TEST("fullvirt-net-netfront", 2);

249
    DO_TEST("escape-paths", 2);
250
    DO_TEST("no-source-cdrom", 2);
251
    DO_TEST("pci-devs", 2);
252

253
    virObjectUnref(caps);
254
    virObjectUnref(xmlopt);
255

256
    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
257
}
258 259

VIRT_TEST_MAIN(mymain)