xmconfigtest.c 6.9 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
#include "virstring.h"
39

40
static virCapsPtr caps;
41
static virDomainXMLOptionPtr xmlopt;
42

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

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

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

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

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

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

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

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

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

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

    ret = 0;

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

101 102 103
    return ret;
}

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

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

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

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

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

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

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

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

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

    ret = 0;

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

155 156 157
    return ret;
}

158

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

165 166 167 168
static int
testCompareHelper(const void *data)
{
    int result = -1;
169
    const struct testInfo *info = data;
170 171 172 173 174 175 176 177 178
    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;

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

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

    return result;
189 190
}

191

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

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

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

203 204 205 206 207 208 209 210 211 212 213 214
#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)

215 216
    DO_TEST("paravirt-old-pvfb", 1);
    DO_TEST("paravirt-old-pvfb-vncdisplay", 1);
217
    DO_TEST("paravirt-new-pvfb", 3);
218
    DO_TEST("paravirt-new-pvfb-vncdisplay", 3);
219
    DO_TEST("paravirt-net-e1000", 3);
220
    DO_TEST("paravirt-net-vifname", 3);
221
    DO_TEST("paravirt-vcpu", 2);
222 223 224 225 226 227
    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);
228
    DO_TEST("fullvirt-serial-file", 2);
229 230
    DO_TEST("fullvirt-serial-dev-2-ports", 2);
    DO_TEST("fullvirt-serial-dev-2nd-port", 2);
231 232 233 234 235 236 237 238 239
    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);

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

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

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

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

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

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

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

VIRT_TEST_MAIN(mymain)