xmconfigtest.c 7.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * xmconfigtest.c: Test backend for xm_internal config file handling
 *
 * Copyright (C) 2007 Red Hat
 *
 * 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
#ifdef WITH_XEN
31 32

#include "internal.h"
33
#include "datatypes.h"
34
#include "xen_unified.h"
35 36
#include "xm_internal.h"
#include "testutils.h"
37 38
#include "testutilsxen.h"
#include "memory.h"
39 40

static char *progname;
41
static char *abs_srcdir;
42
static virCapsPtr caps;
43 44 45

#define MAX_FILE 4096

46
static int testCompareParseXML(const char *xmcfg, const char *xml,
47
                               int xendConfigVersion) {
48 49 50 51 52 53 54 55 56 57
    char xmlData[MAX_FILE];
    char xmcfgData[MAX_FILE];
    char gotxmcfgData[MAX_FILE];
    char *xmlPtr = &(xmlData[0]);
    char *xmcfgPtr = &(xmcfgData[0]);
    char *gotxmcfgPtr = &(gotxmcfgData[0]);
    virConfPtr conf = NULL;
    int ret = -1;
    virConnectPtr conn;
    int wrote = MAX_FILE;
58
    void *old_priv = NULL;
59
    struct _xenUnifiedPrivate priv;
60
    virDomainDefPtr def = NULL;
61

62
    conn = virConnectOpenReadOnly("test:///default");
63 64
    if (!conn) goto fail;
    old_priv = conn->privateData;
65 66 67 68 69 70 71

    if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
        goto fail;

    if (virtTestLoadFile(xmcfg, &xmcfgPtr, MAX_FILE) < 0)
        goto fail;

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

77 78 79 80
    if (!(def = virDomainDefParseString(NULL, caps, xmlPtr)))
        goto fail;

    if (!(conf = xenXMDomainConfigFormat(conn, def)))
81 82 83 84 85 86
        goto fail;

    if (virConfWriteMem(gotxmcfgPtr, &wrote, conf) < 0)
        goto fail;
    gotxmcfgPtr[wrote] = '\0';

87 88
    if (STRNEQ(xmcfgData, gotxmcfgData)) {
        virtTestDifference(stderr, xmcfgData, gotxmcfgData);
89
        goto fail;
90
    }
91 92 93 94 95 96

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);
97
    virDomainDefFree(def);
98 99 100 101 102
    if (conn) {
        conn->privateData = old_priv;
        virConnectClose(conn);
    }

103 104 105
    return ret;
}

106
static int testCompareFormatXML(const char *xmcfg, const char *xml,
107
                                int xendConfigVersion) {
108 109 110 111 112 113 114 115
    char xmlData[MAX_FILE];
    char xmcfgData[MAX_FILE];
    char *xmlPtr = &(xmlData[0]);
    char *xmcfgPtr = &(xmcfgData[0]);
    char *gotxml = NULL;
    virConfPtr conf = NULL;
    int ret = -1;
    virConnectPtr conn;
116 117
    void *old_priv;
    struct _xenUnifiedPrivate priv;
118
    virDomainDefPtr def = NULL;
119

120
    conn = virConnectOpenReadOnly("test:///default");
121 122
    if (!conn) goto fail;
    old_priv = conn->privateData;
123 124 125 126 127 128 129

    if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
        goto fail;

    if (virtTestLoadFile(xmcfg, &xmcfgPtr, MAX_FILE) < 0)
        goto fail;

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

    if (!(conf = virConfReadMem(xmcfgPtr, strlen(xmcfgPtr))))
        goto fail;

138 139 140 141
    if (!(def = xenXMDomainConfigParse(conn, conf)))
        goto fail;

    if (!(gotxml = virDomainDefFormat(conn, def, VIR_DOMAIN_XML_SECURE)))
142 143
        goto fail;

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

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);
154 155
    VIR_FREE(gotxml);
    virDomainDefFree(def);
156 157 158 159 160
    if (conn) {
        conn->privateData = old_priv;
        virConnectClose(conn);
    }

161 162 163
    return ret;
}

164

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

171 172 173 174 175 176 177 178 179 180 181 182
static int testCompareHelper(const void *data) {
    const struct testInfo *info = data;
    char xml[PATH_MAX];
    char cfg[PATH_MAX];
    snprintf(xml, PATH_MAX, "%s/xmconfigdata/test-%s.xml",
             abs_srcdir, info->name);
    snprintf(cfg, PATH_MAX, "%s/xmconfigdata/test-%s.cfg",
             abs_srcdir, info->name);
    if (info->mode == 0)
        return testCompareParseXML(cfg, xml, info->version);
    else
        return testCompareFormatXML(cfg, xml, info->version);
183 184
}

185

186 187
static int
mymain(int argc, char **argv)
188 189
{
    int ret = 0;
190
    char cwd[PATH_MAX];
191 192 193 194 195

    progname = argv[0];

    if (argc > 1) {
        fprintf(stderr, "Usage: %s\n", progname);
196
        return(EXIT_FAILURE);
197 198
    }

199 200 201 202
    abs_srcdir = getenv("abs_srcdir");
    if (!abs_srcdir)
        abs_srcdir = getcwd(cwd, sizeof(cwd));

203 204
    if (!(caps = testXenCapsInit()))
        return(EXIT_FAILURE);
205 206 207 208 209 210 211 212 213 214 215 216 217 218

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

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

    DO_TEST("fullvirt-parallel-tcp", 2);
240

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

243
    DO_TEST("escape-paths", 2);
244 245 246

    virCapabilitiesFree(caps);

247
    return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
248
}
249 250 251

VIRT_TEST_MAIN(mymain)

252 253 254 255 256
#else /* WITHOUT_XEN */
int
main(void)
{
    fprintf(stderr, "libvirt compiled without Xen support\n");
257
    return(0);
258 259
}
#endif /* WITH_XEN */