xmconfigtest.c 7.1 KB
Newer Older
1 2 3
/*
 * xmconfigtest.c: Test backend for xm_internal config file handling
 *
4
 * Copyright (C) 2007, 2010-2011, 2014 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 41
#define VIR_FROM_THIS VIR_FROM_NONE

42
static virCapsPtr caps;
43
static virDomainXMLOptionPtr xmlopt;
44

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

58 59 60
    if (VIR_ALLOC_N(gotxmcfgData, wrote) < 0)
        goto fail;

61
    conn = virGetConnect();
62
    if (!conn) goto fail;
63

64
    if (virtTestLoadFile(xml, &xmlData) < 0)
65 66
        goto fail;

67
    if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
68 69
        goto fail;

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

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

80 81 82 83 84
    if (!virDomainDefCheckABIStability(def, def)) {
        fprintf(stderr, "ABI stability check failed on %s", xml);
        goto fail;
    }

M
Markus Groß 已提交
85
    if (!(conf = xenFormatXM(conn, def, xendConfigVersion)))
86 87
        goto fail;

88
    if (virConfWriteMem(gotxmcfgData, &wrote, conf) < 0)
89
        goto fail;
90
    gotxmcfgData[wrote] = '\0';
91

92 93
    if (STRNEQ(xmcfgData, gotxmcfgData)) {
        virtTestDifference(stderr, xmcfgData, gotxmcfgData);
94
        goto fail;
95
    }
96 97 98 99

    ret = 0;

 fail:
100 101 102
    VIR_FREE(xmlData);
    VIR_FREE(xmcfgData);
    VIR_FREE(gotxmcfgData);
103 104
    if (conf)
        virConfFree(conf);
105
    virDomainDefFree(def);
106
    virObjectUnref(conn);
107

108 109 110
    return ret;
}

111 112 113 114 115
static int
testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion)
{
    char *xmlData = NULL;
    char *xmcfgData = NULL;
116 117 118 119
    char *gotxml = NULL;
    virConfPtr conf = NULL;
    int ret = -1;
    virConnectPtr conn;
120
    struct _xenUnifiedPrivate priv;
121
    virDomainDefPtr def = NULL;
122

123
    conn = virGetConnect();
124
    if (!conn) goto fail;
125

126
    if (virtTestLoadFile(xml, &xmlData) < 0)
127 128
        goto fail;

129
    if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
130 131
        goto fail;

132 133
    /* Many puppies died to bring you this code. */
    priv.xendConfigVersion = xendConfigVersion;
134
    priv.caps = caps;
135
    conn->privateData = &priv;
136

137
    if (!(conf = virConfReadMem(xmcfgData, strlen(xmcfgData), 0)))
138 139
        goto fail;

M
Markus Groß 已提交
140
    if (!(def = xenParseXM(conf, priv.xendConfigVersion, priv.caps)))
141 142
        goto fail;

143
    if (!(gotxml = virDomainDefFormat(def, VIR_DOMAIN_XML_SECURE)))
144 145
        goto fail;

146 147
    if (STRNEQ(xmlData, gotxml)) {
        virtTestDifference(stderr, xmlData, gotxml);
148
        goto fail;
149
    }
150 151 152 153 154 155

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);
156 157
    VIR_FREE(xmlData);
    VIR_FREE(xmcfgData);
158 159
    VIR_FREE(gotxml);
    virDomainDefFree(def);
160
    virObjectUnref(conn);
161

162 163 164
    return ret;
}

165

166 167 168 169 170
struct testInfo {
    const char *name;
    int version;
    int mode;
};
171

172 173 174 175
static int
testCompareHelper(const void *data)
{
    int result = -1;
176
    const struct testInfo *info = data;
177 178 179 180 181 182 183 184 185
    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;

186
    if (info->mode == 0)
187
        result = testCompareParseXML(cfg, xml, info->version);
188
    else
189 190 191
        result = testCompareFormatXML(cfg, xml, info->version);

cleanup:
192 193
    VIR_FREE(xml);
    VIR_FREE(cfg);
194 195

    return result;
196 197
}

198

199
static int
E
Eric Blake 已提交
200
mymain(void)
201 202
{
    int ret = 0;
203

204
    if (!(caps = testXenCapsInit()))
205
        return EXIT_FAILURE;
206

207
    if (!(xmlopt = xenDomainXMLConfInit()))
208 209
        return EXIT_FAILURE;

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,                    \
215
                        testCompareHelper, &info0) < 0)                 \
216 217
            ret = -1;                                                   \
        if (virtTestRun("Xen XM-2-XML Format " name,                    \
218
                        testCompareHelper, &info1) < 0)                 \
219 220 221
            ret = -1;                                                   \
    } while (0)

222 223
    DO_TEST("paravirt-old-pvfb", 1);
    DO_TEST("paravirt-old-pvfb-vncdisplay", 1);
224
    DO_TEST("paravirt-new-pvfb", 3);
225
    DO_TEST("paravirt-new-pvfb-vncdisplay", 3);
226
    DO_TEST("paravirt-net-e1000", 3);
227
    DO_TEST("paravirt-net-vifname", 3);
228
    DO_TEST("paravirt-vcpu", 2);
229 230 231 232 233 234
    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);
235
    DO_TEST("fullvirt-serial-file", 2);
236 237
    DO_TEST("fullvirt-serial-dev-2-ports", 2);
    DO_TEST("fullvirt-serial-dev-2nd-port", 2);
238 239 240 241 242 243 244 245 246
    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);

247 248 249
    DO_TEST("fullvirt-force-hpet", 2);
    DO_TEST("fullvirt-force-nohpet", 2);

250
    DO_TEST("fullvirt-parallel-tcp", 2);
251

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

254 255 256
    DO_TEST("fullvirt-net-ioemu", 2);
    DO_TEST("fullvirt-net-netfront", 2);

257
    DO_TEST("escape-paths", 2);
258
    DO_TEST("no-source-cdrom", 2);
259
    DO_TEST("pci-devs", 2);
260

261
    virObjectUnref(caps);
262
    virObjectUnref(xmlopt);
263

264
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
265
}
266 267

VIRT_TEST_MAIN(mymain)