libxlxml2domconfigtest.c 6.1 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 24 25 26 27 28 29 30
/*
 * libxlxml2domconfigtest.c: test conversion of domXML to
 * libxl_domain_config structure.
 *
 * Copyright (C) 2017 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/>.
 */

#include <config.h>

#include <unistd.h>

#include <sys/types.h>
#include <fcntl.h>

#include "testutils.h"

31
#if defined(WITH_LIBXL) && defined(WITH_YAJL)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

# include "internal.h"
# include "viralloc.h"
# include "libxl/libxl_conf.h"
# include "datatypes.h"
# include "virstring.h"
# include "virmock.h"
# include "virjson.h"
# include "testutilsxen.h"

# define VIR_FROM_THIS VIR_FROM_LIBXL

static virCapsPtr caps;

static int
testCompareXMLToDomConfig(const char *xmlfile,
                          const char *jsonfile)
{
    int ret = -1;
    libxl_domain_config actualconfig;
    libxl_domain_config expectconfig;
53
    libxlDriverConfigPtr cfg;
54
    xentoollog_logger *log = NULL;
55
    virPortAllocatorRangePtr gports = NULL;
56 57 58 59 60 61
    virDomainXMLOptionPtr xmlopt = NULL;
    virDomainDefPtr vmdef = NULL;
    char *actualjson = NULL;
    char *tempjson = NULL;
    char *expectjson = NULL;

62
    if (!(cfg = libxlDriverConfigNew()))
63
        return -1;
64 65 66

    cfg->caps = caps;

67 68 69
    libxl_domain_config_init(&actualconfig);
    libxl_domain_config_init(&expectconfig);

70 71 72
    if (!(log = (xentoollog_logger *)xtl_createlogger_stdiostream(stderr, XTL_DEBUG, 0)))
        goto cleanup;

73 74 75
    /* for testing nested HVM */
    cfg->nested_hvm = true;

76 77 78 79
    /* replace logger with stderr one */
    libxl_ctx_free(cfg->ctx);

    if (libxl_ctx_alloc(&cfg->ctx, LIBXL_VERSION, 0, log) < 0)
80 81
        goto cleanup;

82
    if (!(gports = virPortAllocatorRangeNew("vnc", 5900, 6000)))
83 84 85 86 87 88 89 90 91
        goto cleanup;

    if (!(xmlopt = libxlCreateXMLConf()))
        goto cleanup;

    if (!(vmdef = virDomainDefParseFile(xmlfile, caps, xmlopt,
                                        NULL, VIR_DOMAIN_XML_INACTIVE)))
        goto cleanup;

92
    if (libxlBuildDomainConfig(gports, vmdef, cfg, &actualconfig) < 0)
93 94
        goto cleanup;

95
    if (!(actualjson = libxl_domain_config_to_json(cfg->ctx, &actualconfig))) {
96 97 98 99 100
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Failed to retrieve JSON doc for libxl_domain_config");
        goto cleanup;
    }

101 102 103
    if (virTestLoadFile(jsonfile, &tempjson) < 0)
        goto cleanup;

104
    if (libxl_domain_config_from_json(cfg->ctx, &expectconfig, tempjson) != 0) {
105 106 107 108
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Failed to create libxl_domain_config from JSON doc");
        goto cleanup;
    }
109
    if (!(expectjson = libxl_domain_config_to_json(cfg->ctx, &expectconfig))) {
110 111 112 113 114 115 116 117 118 119 120
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Failed to retrieve JSON doc for libxl_domain_config");
        goto cleanup;
    }

    if (virTestCompareToString(expectjson, actualjson) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
121 122
    if (vmdef &&
        vmdef->ngraphics == 1 &&
123 124
        vmdef->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC)
        virPortAllocatorRelease(vmdef->graphics[0]->data.vnc.port);
125

126 127 128 129
    VIR_FREE(expectjson);
    VIR_FREE(actualjson);
    VIR_FREE(tempjson);
    virDomainDefFree(vmdef);
130
    virPortAllocatorRangeFree(gports);
131 132 133 134
    virObjectUnref(xmlopt);
    libxl_domain_config_dispose(&actualconfig);
    libxl_domain_config_dispose(&expectconfig);
    xtl_logger_destroy(log);
135
    cfg->caps = NULL;
136
    virObjectUnref(cfg);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    return ret;
}


struct testInfo {
    const char *name;
};


static int
testCompareXMLToDomConfigHelper(const void *data)
{
    int ret = -1;
    const struct testInfo *info = data;
    char *xmlfile = NULL;
    char *jsonfile = NULL;

    if (virAsprintf(&xmlfile, "%s/libxlxml2domconfigdata/%s.xml",
                    abs_srcdir, info->name) < 0 ||
        virAsprintf(&jsonfile, "%s/libxlxml2domconfigdata/%s.json",
                    abs_srcdir, info->name) < 0)
        goto cleanup;

    ret = testCompareXMLToDomConfig(xmlfile, jsonfile);

 cleanup:
    VIR_FREE(xmlfile);
    VIR_FREE(jsonfile);
    return ret;
}


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

    /* Set the timezone because we are mocking the time() function.
     * If we don't do that, then localtime() may return unpredictable
     * results. In order to detect things that just work by a blind
     * chance, we need to set an virtual timezone that no libvirt
     * developer resides in. */
    if (setenv("TZ", "VIR00:30", 1) < 0) {
        perror("setenv");
        return EXIT_FAILURE;
    }

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

187 188 189 190 191 192 193 194
# define DO_TEST(name) \
    do { \
        static struct testInfo info = { \
            name, \
        }; \
        if (virTestRun("LibXL XML-2-JSON " name, \
                        testCompareXMLToDomConfigHelper, &info) < 0) \
            ret = -1; \
195 196 197 198
    } while (0)

    DO_TEST("basic-pv");
    DO_TEST("basic-hvm");
199 200 201
# ifdef HAVE_XEN_PVH
    DO_TEST("basic-pvh");
# endif
202
    DO_TEST("cpu-shares-hvm");
203
    DO_TEST("variable-clock-hvm");
204
    DO_TEST("moredevs-hvm");
205
    DO_TEST("multiple-ip");
206 207 208

# ifdef LIBXL_HAVE_BUILDINFO_NESTED_HVM
    DO_TEST("vnuma-hvm");
209
    DO_TEST("fullvirt-cpuid");
210 211 212
# else
    DO_TEST("vnuma-hvm-legacy-nest");
    DO_TEST("fullvirt-cpuid-legacy-nest");
213 214
# endif

215 216 217
# ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
    DO_TEST("max-gntframes-hvm");
# endif
218

219 220
    unlink("libxl-driver.log");

221 222 223
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

224
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("xl"))
225 226 227 228 229 230 231 232

#else

int main(void)
{
    return EXIT_AM_SKIP;
}

233
#endif /* WITH_LIBXL && WITH_YAJL */