xlconfigtest.c 8.2 KB
Newer Older
1
/*
2
 * xlconfigtest.c: Test xl.cfg(5) <-> domXML config conversions
3 4 5
 *
 * Copyright (C) 2007, 2010-2011, 2014 Red Hat, Inc.
 * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany.
6
 * Copyright (C) 2014 David Kiarie Kahurani
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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/>.
 *
 */

#include <config.h>

#include <unistd.h>

#include "internal.h"
#include "datatypes.h"
30
#include "libxl/xen_xl.h"
31 32 33 34
#include "viralloc.h"
#include "virstring.h"
#include "testutils.h"
#include "testutilsxen.h"
35
#include "libxl/libxl_conf.h"
36 37 38 39 40

#define VIR_FROM_THIS VIR_FROM_NONE

static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt;
41

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

/*
 * 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;
}

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

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

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

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

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

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

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

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

    ret = 0;

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

    return ret;
}
123

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

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

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

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

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

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

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

    ret = 0;

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

    return ret;
}


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

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 已提交
201
        result = testCompareParseXML(cfg, xml, info->replaceVars);
202
    else
J
Jim Fehlig 已提交
203
        result = testCompareFormatXML(cfg, xml, info->replaceVars);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

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

    return result;
}


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

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

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

224 225 226 227 228 229
#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; \
230 231
    } while (0)

232 233 234 235 236 237
#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; \
238 239
    } while (0)

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

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

252 253
    DO_TEST("fullvirt-ovswitch-tagged");
    DO_TEST("fullvirt-ovswitch-trunked");
J
Jim Fehlig 已提交
254
    DO_TEST_REPLACE_VARS("fullvirt-ovmf");
255
    DO_TEST("paravirt-maxvcpus");
256
    DO_TEST("new-disk");
J
Jim Fehlig 已提交
257 258
    DO_TEST_FORMAT("disk-positional-parms-full", false);
    DO_TEST_FORMAT("disk-positional-parms-partial", false);
C
Cédric Bosdonnat 已提交
259 260 261
#ifdef LIBXL_HAVE_QED
    DO_TEST_FORMAT("disk-qed", false);
#endif
262
    DO_TEST("net-fakemodel");
263 264
    DO_TEST("spice");
    DO_TEST("spice-features");
265
    DO_TEST("vif-rate");
266
    DO_TEST("fullvirt-nohap");
267 268 269
    DO_TEST("fullvirt-hpet-timer");
    DO_TEST("fullvirt-tsc-timer");
    DO_TEST("fullvirt-multi-timer");
270 271
    DO_TEST("fullvirt-nestedhvm");
    DO_TEST("fullvirt-nestedhvm-disabled");
272
    DO_TEST("fullvirt-cpuid");
273 274 275 276 277 278
#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 已提交
279

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

288 289 290 291
#ifdef LIBXL_HAVE_DEVICE_CHANNEL
    DO_TEST("channel-pty");
    DO_TEST("channel-unix");
#endif
292 293 294
#ifdef LIBXL_HAVE_BUILDINFO_SERIAL_LIST
    DO_TEST("fullvirt-multiserial");
#endif
J
Ján Tomko 已提交
295
#ifdef LIBXL_HAVE_BUILDINFO_USBDEVICE_LIST
296
    DO_TEST("fullvirt-multiusb");
J
Ján Tomko 已提交
297
#endif
298
#ifdef LIBXL_HAVE_BUILDINFO_KERNEL
299
    DO_TEST("fullvirt-direct-kernel-boot");
J
Jim Fehlig 已提交
300 301
    DO_TEST_FORMAT("fullvirt-direct-kernel-boot-extra", false);
    DO_TEST_FORMAT("fullvirt-direct-kernel-boot-bogus-extra", false);
302
#endif
303 304 305 306
#ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
    DO_TEST("max-gntframes");
#endif

307
    DO_TEST("vif-typename");
308
    DO_TEST("vif-multi-ip");
309
    DO_TEST("usb");
310
    DO_TEST("usbctrl");
311 312 313 314 315 316 317

    virObjectUnref(caps);
    virObjectUnref(xmlopt);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

318
VIR_TEST_MAIN(mymain)