xmconfigtest.c 6.7 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 17 18 19 20 21 22 23
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * 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 37
#include "testutilsxen.h"
#include "memory.h"
38

39
static virCapsPtr caps;
40

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

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

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

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

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

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

M
Matthias Bolte 已提交
71
    if (!(def = virDomainDefParseString(caps, xmlData, 1 << VIR_DOMAIN_VIRT_XEN,
G
Guido Günther 已提交
72
                                        VIR_DOMAIN_XML_INACTIVE)))
73 74
        goto fail;

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

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

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

    ret = 0;

 fail:
90 91 92
    free(xmlData);
    free(xmcfgData);
    free(gotxmcfgData);
93 94
    if (conf)
        virConfFree(conf);
95
    virDomainDefFree(def);
96
    virUnrefConnect(conn);
97

98 99 100
    return ret;
}

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

113
    conn = virGetConnect();
114
    if (!conn) goto fail;
115

116
    if (virtTestLoadFile(xml, &xmlData) < 0)
117 118
        goto fail;

119
    if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
120 121
        goto fail;

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

127
    if (!(conf = virConfReadMem(xmcfgData, strlen(xmcfgData), 0)))
128 129
        goto fail;

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

133
    if (!(gotxml = virDomainDefFormat(def, VIR_DOMAIN_XML_SECURE)))
134 135
        goto fail;

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

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);
146 147
    VIR_FREE(xmlData);
    VIR_FREE(xmcfgData);
148 149
    VIR_FREE(gotxml);
    virDomainDefFree(def);
150
    virUnrefConnect(conn);
151

152 153 154
    return ret;
}

155

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

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

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

cleanup:
    free(xml);
    free(cfg);

    return result;
186 187
}

188

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

194 195
    if (!(caps = testXenCapsInit()))
        return(EXIT_FAILURE);
196 197 198 199 200 201 202 203 204 205 206 207 208

#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)

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

234 235 236
    DO_TEST("fullvirt-force-hpet", 2);
    DO_TEST("fullvirt-force-nohpet", 2);

237
    DO_TEST("fullvirt-parallel-tcp", 2);
238

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

241 242 243
    DO_TEST("fullvirt-net-ioemu", 2);
    DO_TEST("fullvirt-net-netfront", 2);

244
    DO_TEST("escape-paths", 2);
245
    DO_TEST("no-source-cdrom", 2);
246
    DO_TEST("pci-devs", 2);
247 248 249

    virCapabilitiesFree(caps);

250
    return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
251
}
252 253

VIRT_TEST_MAIN(mymain)