xlconfigtest.c 8.0 KB
Newer Older
1
/*
2
 * xlconfigtest.c: Test xl.cfg(5) <-> domXML config conversions
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * Copyright (C) 2007, 2010-2011, 2014 Red Hat, Inc.
 * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany.
 *
 * 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, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 *
 */

#include <config.h>

#include <unistd.h>

#include "internal.h"
#include "datatypes.h"
#include "xenconfig/xen_xl.h"
#include "viralloc.h"
#include "virstring.h"
#include "testutils.h"
#include "testutilsxen.h"
36
#include "libxl/libxl_conf.h"
37 38 39 40 41

#define VIR_FROM_THIS VIR_FROM_NONE

static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt;
42

J
Jim Fehlig 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

/*
 * This function provides a mechanism to replace variables in test
 * data files whose values are discovered at built time.
 */
static char *
testReplaceVarsXML(const char *xml)
{
    char *xmlcfgData;
    char *replacedXML;

    if (virTestLoadFile(xml, &xmlcfgData) < 0)
        return NULL;

    replacedXML = virStringReplace(xmlcfgData, "/LIBXL_FIRMWARE_DIR",
                                   LIBXL_FIRMWARE_DIR);

    VIR_FREE(xmlcfgData);
    return replacedXML;
}

64
/*
65 66
 * Parses domXML to virDomainDef object, which is then converted to xl.cfg(5)
 * config and compared with expected config.
67 68
 */
static int
J
Jim Fehlig 已提交
69
testCompareParseXML(const char *xlcfg, const char *xml, bool replaceVars)
70
{
71
    char *gotxlcfgData = NULL;
72 73 74 75 76
    virConfPtr conf = NULL;
    virConnectPtr conn = NULL;
    int wrote = 4096;
    int ret = -1;
    virDomainDefPtr def = NULL;
J
Jim Fehlig 已提交
77
    char *replacedXML = NULL;
78

79
    if (VIR_ALLOC_N(gotxlcfgData, wrote) < 0)
80 81 82 83 84
        goto fail;

    conn = virGetConnect();
    if (!conn) goto fail;

J
Jim Fehlig 已提交
85 86 87 88
    if (replaceVars) {
        if (!(replacedXML = testReplaceVarsXML(xml)))
            goto fail;
        if (!(def = virDomainDefParseString(replacedXML, caps, xmlopt,
89
                                            NULL, VIR_DOMAIN_XML_INACTIVE)))
J
Jim Fehlig 已提交
90 91 92
            goto fail;
    } else {
        if (!(def = virDomainDefParseFile(xml, caps, xmlopt,
93
                                          NULL, VIR_DOMAIN_XML_INACTIVE)))
J
Jim Fehlig 已提交
94 95
            goto fail;
    }
96

97
    if (!virDomainDefCheckABIStability(def, def, xmlopt)) {
98 99 100 101
        fprintf(stderr, "ABI stability check failed on %s", xml);
        goto fail;
    }

102
    if (!(conf = xenFormatXL(def, conn)))
103 104
        goto fail;

105
    if (virConfWriteMem(gotxlcfgData, &wrote, conf) < 0)
106
        goto fail;
107
    gotxlcfgData[wrote] = '\0';
108

109
    if (virTestCompareToFile(gotxlcfgData, xlcfg) < 0)
110 111 112 113 114
        goto fail;

    ret = 0;

 fail:
J
Jim Fehlig 已提交
115
    VIR_FREE(replacedXML);
116
    VIR_FREE(gotxlcfgData);
117 118 119 120 121 122 123
    if (conf)
        virConfFree(conf);
    virDomainDefFree(def);
    virObjectUnref(conn);

    return ret;
}
124

125
/*
126 127
 * Parses xl.cfg(5) config to virDomainDef object, which is then converted to
 * domXML and compared to expected XML.
128 129
 */
static int
J
Jim Fehlig 已提交
130
testCompareFormatXML(const char *xlcfg, const char *xml, bool replaceVars)
131
{
132
    char *xlcfgData = NULL;
133 134 135 136 137
    char *gotxml = NULL;
    virConfPtr conf = NULL;
    int ret = -1;
    virConnectPtr conn;
    virDomainDefPtr def = NULL;
J
Jim Fehlig 已提交
138
    char *replacedXML = NULL;
139 140 141 142

    conn = virGetConnect();
    if (!conn) goto fail;

143
    if (virTestLoadFile(xlcfg, &xlcfgData) < 0)
144 145
        goto fail;

J
Ján Tomko 已提交
146
    if (!(conf = virConfReadString(xlcfgData, 0)))
147 148
        goto fail;

149
    if (!(def = xenParseXL(conf, caps, xmlopt)))
150 151
        goto fail;

152
    if (!(gotxml = virDomainDefFormat(def, caps, VIR_DOMAIN_XML_INACTIVE |
153 154 155
                                      VIR_DOMAIN_XML_SECURE)))
        goto fail;

J
Jim Fehlig 已提交
156 157 158 159 160 161 162 163 164
    if (replaceVars) {
        if (!(replacedXML = testReplaceVarsXML(xml)))
            goto fail;
        if (virTestCompareToString(gotxml, replacedXML) < 0)
            goto fail;
    } else {
        if (virTestCompareToFile(gotxml, xml) < 0)
            goto fail;
    }
165 166 167 168 169 170

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);
J
Jim Fehlig 已提交
171
    VIR_FREE(replacedXML);
172
    VIR_FREE(xlcfgData);
173 174 175 176 177 178 179 180 181 182 183
    VIR_FREE(gotxml);
    virDomainDefFree(def);
    virObjectUnref(conn);

    return ret;
}


struct testInfo {
    const char *name;
    int mode;
J
Jim Fehlig 已提交
184
    bool replaceVars;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
};

static int
testCompareHelper(const void *data)
{
    int result = -1;
    const struct testInfo *info = data;
    char *xml = NULL;
    char *cfg = NULL;

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

    if (info->mode == 0)
J
Jim Fehlig 已提交
202
        result = testCompareParseXML(cfg, xml, info->replaceVars);
203
    else
J
Jim Fehlig 已提交
204
        result = testCompareFormatXML(cfg, xml, info->replaceVars);
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

 cleanup:
    VIR_FREE(xml);
    VIR_FREE(cfg);

    return result;
}


static int
mymain(void)
{
    int ret = 0;

    if (!(caps = testXLInitCaps()))
        return EXIT_FAILURE;

222
    if (!(xmlopt = libxlCreateXMLConf()))
223 224
        return EXIT_FAILURE;

225 226 227 228 229 230
#define DO_TEST_PARSE(name, replace) \
    do { \
        struct testInfo info0 = { name, 0, replace }; \
        if (virTestRun("Xen XL-2-XML Parse  " name, \
                       testCompareHelper, &info0) < 0) \
            ret = -1; \
231 232
    } while (0)

233 234 235 236 237 238
#define DO_TEST_FORMAT(name, replace) \
    do { \
        struct testInfo info1 = { name, 1, replace }; \
        if (virTestRun("Xen XL-2-XML Format " name, \
                       testCompareHelper, &info1) < 0) \
            ret = -1; \
239 240
    } while (0)

241 242 243 244
#define DO_TEST(name) \
    do { \
        DO_TEST_PARSE(name, false); \
        DO_TEST_FORMAT(name, false); \
J
Jim Fehlig 已提交
245 246
    } while (0)

247 248 249 250
#define DO_TEST_REPLACE_VARS(name) \
    do { \
        DO_TEST_PARSE(name, true); \
        DO_TEST_FORMAT(name, true); \
251 252
    } while (0)

J
Jim Fehlig 已提交
253
    DO_TEST_REPLACE_VARS("fullvirt-ovmf");
254
    DO_TEST("paravirt-maxvcpus");
255
    DO_TEST("new-disk");
J
Jim Fehlig 已提交
256 257
    DO_TEST_FORMAT("disk-positional-parms-full", false);
    DO_TEST_FORMAT("disk-positional-parms-partial", false);
C
Cédric Bosdonnat 已提交
258 259 260
#ifdef LIBXL_HAVE_QED
    DO_TEST_FORMAT("disk-qed", false);
#endif
261 262
    DO_TEST("spice");
    DO_TEST("spice-features");
263
    DO_TEST("vif-rate");
264
    DO_TEST("fullvirt-nohap");
265 266 267
    DO_TEST("fullvirt-hpet-timer");
    DO_TEST("fullvirt-tsc-timer");
    DO_TEST("fullvirt-multi-timer");
268 269
    DO_TEST("fullvirt-nestedhvm");
    DO_TEST("fullvirt-nestedhvm-disabled");
270
    DO_TEST("fullvirt-cpuid");
271 272 273 274 275 276
#ifdef LIBXL_HAVE_VNUMA
    DO_TEST("fullvirt-vnuma");
    DO_TEST_PARSE("fullvirt-vnuma-autocomplete", false);
    DO_TEST_PARSE("fullvirt-vnuma-nodistances", false);
    DO_TEST_PARSE("fullvirt-vnuma-partialdist", false);
#endif
J
Ján Tomko 已提交
277

278
    DO_TEST("paravirt-cmdline");
J
Jim Fehlig 已提交
279 280
    DO_TEST_FORMAT("paravirt-cmdline-extra-root", false);
    DO_TEST_FORMAT("paravirt-cmdline-bogus-extra-root", false);
281
    DO_TEST("rbd-multihost-noauth");
282 283
    DO_TEST_FORMAT("paravirt-type", false);
    DO_TEST_FORMAT("fullvirt-type", false);
284
    DO_TEST("pvh-type");
285

286 287 288 289
#ifdef LIBXL_HAVE_DEVICE_CHANNEL
    DO_TEST("channel-pty");
    DO_TEST("channel-unix");
#endif
290 291 292
#ifdef LIBXL_HAVE_BUILDINFO_SERIAL_LIST
    DO_TEST("fullvirt-multiserial");
#endif
J
Ján Tomko 已提交
293
#ifdef LIBXL_HAVE_BUILDINFO_USBDEVICE_LIST
294
    DO_TEST("fullvirt-multiusb");
J
Ján Tomko 已提交
295
#endif
296
#ifdef LIBXL_HAVE_BUILDINFO_KERNEL
297
    DO_TEST("fullvirt-direct-kernel-boot");
J
Jim Fehlig 已提交
298 299
    DO_TEST_FORMAT("fullvirt-direct-kernel-boot-extra", false);
    DO_TEST_FORMAT("fullvirt-direct-kernel-boot-bogus-extra", false);
300
#endif
301
    DO_TEST("vif-typename");
302
    DO_TEST("vif-multi-ip");
303
    DO_TEST("usb");
304
    DO_TEST("usbctrl");
305 306 307 308 309 310 311

    virObjectUnref(caps);
    virObjectUnref(xmlopt);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

312
VIR_TEST_MAIN(mymain)