domaincapstest.c 10.2 KB
Newer Older
M
Michal Privoznik 已提交
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
/*
 * Copyright (C) Red Hat, Inc. 2014
 *
 * 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/>.
 *
 * Authors:
 *      Michal Privoznik <mprivozn@redhat.com>
 */

#include <config.h>
#include <stdlib.h>

#include "testutils.h"
#include "domain_capabilities.h"


#define VIR_FROM_THIS VIR_FROM_NONE

31 32
typedef int (*virDomainCapsFill)(virDomainCapsPtr domCaps,
                                 void *opaque);
M
Michal Privoznik 已提交
33 34 35 36

#define SET_ALL_BITS(x) \
    memset(&(x.values), 0xff, sizeof(x.values))

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static int ATTRIBUTE_SENTINEL
fillStringValues(virDomainCapsStringValuesPtr values, ...)
{
    int ret = 0;
    va_list list;
    const char *str;

    va_start(list, values);
    while ((str = va_arg(list, const char *))) {
        if (VIR_REALLOC_N(values->values, values->nvalues + 1) < 0 ||
            VIR_STRDUP(values->values[values->nvalues], str) < 0) {
            ret = -1;
            break;
        }
        values->nvalues++;
    }
    va_end(list);

    return ret;
}

58
static int
J
Jiri Denemark 已提交
59
fillAllCaps(virDomainCapsPtr domCaps)
M
Michal Privoznik 已提交
60
{
61 62
    virDomainCapsOSPtr os = &domCaps->os;
    virDomainCapsLoaderPtr loader = &os->loader;
M
Michal Privoznik 已提交
63
    virDomainCapsDeviceDiskPtr disk = &domCaps->disk;
64
    virDomainCapsDeviceGraphicsPtr graphics = &domCaps->graphics;
65
    virDomainCapsDeviceVideoPtr video = &domCaps->video;
M
Michal Privoznik 已提交
66 67 68
    virDomainCapsDeviceHostdevPtr hostdev = &domCaps->hostdev;
    domCaps->maxvcpus = 255;

69
    os->supported = true;
70

71
    loader->supported = true;
72 73
    SET_ALL_BITS(loader->type);
    SET_ALL_BITS(loader->readonly);
74 75 76 77 78
    if (fillStringValues(&loader->values,
                         "/foo/bar",
                         "/tmp/my_path",
                         NULL) < 0)
        return -1;
79

80
    disk->supported = true;
M
Michal Privoznik 已提交
81 82 83
    SET_ALL_BITS(disk->diskDevice);
    SET_ALL_BITS(disk->bus);

84 85 86
    graphics->supported = true;
    SET_ALL_BITS(graphics->type);

87 88 89
    video->supported = true;
    SET_ALL_BITS(video->modelType);

90
    hostdev->supported = true;
M
Michal Privoznik 已提交
91 92 93 94 95
    SET_ALL_BITS(hostdev->mode);
    SET_ALL_BITS(hostdev->startupPolicy);
    SET_ALL_BITS(hostdev->subsysType);
    SET_ALL_BITS(hostdev->capsType);
    SET_ALL_BITS(hostdev->pciBackend);
96
    return 0;
M
Michal Privoznik 已提交
97 98
}

99

J
Jiri Denemark 已提交
100
#if WITH_QEMU
101
# include "testutilsqemu.h"
102

103
static int
104
fillQemuCaps(virDomainCapsPtr domCaps,
J
Jiri Denemark 已提交
105
             const char *name,
J
Jiri Denemark 已提交
106
             const char *arch,
107
             const char *machine,
J
Jiri Denemark 已提交
108
             virQEMUDriverConfigPtr cfg)
109
{
J
Jiri Denemark 已提交
110 111 112
    int ret = -1;
    char *path = NULL;
    virQEMUCapsPtr qemuCaps = NULL;
113
    virDomainCapsLoaderPtr loader = &domCaps->os.loader;
114

115
    if (virAsprintf(&path, "%s/qemucapabilitiesdata/%s.%s.xml",
J
Jiri Denemark 已提交
116
                    abs_srcdir, name, arch) < 0 ||
J
Jiri Denemark 已提交
117 118 119
        !(qemuCaps = qemuTestParseCapabilities(path)))
        goto cleanup;

120 121 122 123 124 125 126 127 128 129
    if (machine &&
        VIR_STRDUP(domCaps->machine,
                   virQEMUCapsGetCanonicalMachine(qemuCaps, machine)) < 0)
        goto cleanup;

    if (!domCaps->machine &&
        VIR_STRDUP(domCaps->machine,
                   virQEMUCapsGetDefaultMachine(qemuCaps)) < 0)
        goto cleanup;

130 131
    if (virQEMUCapsFillDomainCaps(domCaps, qemuCaps,
                                  cfg->loader, cfg->nloader) < 0)
J
Jiri Denemark 已提交
132
        goto cleanup;
133 134 135 136 137 138 139 140 141 142

    /* The function above tries to query host's KVM & VFIO capabilities by
     * calling qemuHostdevHostSupportsPassthroughLegacy() and
     * qemuHostdevHostSupportsPassthroughVFIO() which, however, can't be
     * successfully mocked as they are not exposed as internal APIs. Therefore,
     * instead of mocking set the expected values here by hand. */
    VIR_DOMAIN_CAPS_ENUM_SET(domCaps->hostdev.pciBackend,
                             VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT,
                             VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM,
                             VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO);
143

J
Jiri Denemark 已提交
144 145
    /* As of f05b6a918e28 we are expecting to see OVMF_CODE.fd file which
     * may not exists everywhere. */
146 147 148 149
    while (loader->values.nvalues)
        VIR_FREE(loader->values.values[--loader->values.nvalues]);

    if (fillStringValues(&loader->values,
150
                         "/usr/share/AAVMF/AAVMF_CODE.fd",
151 152
                         "/usr/share/OVMF/OVMF_CODE.fd",
                         NULL) < 0)
M
Michal Privoznik 已提交
153 154
        goto cleanup;

J
Jiri Denemark 已提交
155
    ret = 0;
M
Michal Privoznik 已提交
156
 cleanup:
J
Jiri Denemark 已提交
157 158
    virObjectUnref(qemuCaps);
    VIR_FREE(path);
159
    return ret;
M
Michal Privoznik 已提交
160
}
J
Jiri Denemark 已提交
161 162 163 164 165 166 167 168
#endif /* WITH_QEMU */


enum testCapsType {
    CAPS_NONE,
    CAPS_ALL,
    CAPS_QEMU,
};
M
Michal Privoznik 已提交
169

J
Jiri Denemark 已提交
170 171 172
struct testData {
    const char *name;
    const char *emulator;
M
Michal Privoznik 已提交
173
    const char *machine;
J
Jiri Denemark 已提交
174
    const char *arch;
M
Michal Privoznik 已提交
175
    virDomainVirtType type;
J
Jiri Denemark 已提交
176 177 178
    enum testCapsType capsType;
    const char *capsName;
    void *capsOpaque;
M
Michal Privoznik 已提交
179 180 181 182 183
};

static int
test_virDomainCapsFormat(const void *opaque)
{
J
Jiri Denemark 已提交
184
    const struct testData *data = opaque;
M
Michal Privoznik 已提交
185 186 187 188 189
    virDomainCapsPtr domCaps = NULL;
    char *path = NULL;
    char *domCapsXML = NULL;
    int ret = -1;

190
    if (virAsprintf(&path, "%s/domaincapsschemadata/%s.xml",
J
Jiri Denemark 已提交
191
                    abs_srcdir, data->name) < 0)
M
Michal Privoznik 已提交
192 193
        goto cleanup;

J
Jiri Denemark 已提交
194 195
    if (!(domCaps = virDomainCapsNew(data->emulator, data->machine,
                                     virArchFromString(data->arch),
J
Jiri Denemark 已提交
196
                                     data->type)))
M
Michal Privoznik 已提交
197 198
        goto cleanup;

J
Jiri Denemark 已提交
199 200 201 202 203 204 205 206 207 208 209
    switch (data->capsType) {
    case CAPS_NONE:
        break;

    case CAPS_ALL:
        if (fillAllCaps(domCaps) < 0)
            goto cleanup;
        break;

    case CAPS_QEMU:
#if WITH_QEMU
210
        if (fillQemuCaps(domCaps, data->capsName, data->arch, data->machine,
211
                         data->capsOpaque) < 0)
J
Jiri Denemark 已提交
212 213 214 215 216
            goto cleanup;
#endif
        break;
    }

M
Michal Privoznik 已提交
217 218 219
    if (!(domCapsXML = virDomainCapsFormat(domCaps)))
        goto cleanup;

220
    if (virTestCompareToFile(domCapsXML, path) < 0)
M
Michal Privoznik 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_FREE(domCapsXML);
    VIR_FREE(path);
    virObjectUnref(domCaps);
    return ret;
}

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

J
Jiri Denemark 已提交
236
#if WITH_QEMU
237 238
    virQEMUDriverConfigPtr cfg = virQEMUDriverConfigNew(false);

239 240
    if (!cfg)
        return EXIT_FAILURE;
J
Jiri Denemark 已提交
241 242 243 244 245 246 247 248 249 250 251 252
#endif

#define DO_TEST(Name, Emulator, Machine, Arch, Type, CapsType)          \
    do {                                                                \
        struct testData data = {                                        \
            .name = Name,                                               \
            .emulator = Emulator,                                       \
            .machine = Machine,                                         \
            .arch = Arch,                                               \
            .type = Type,                                               \
            .capsType = CapsType,                                       \
        };                                                              \
253
        if (virTestRun(Name, test_virDomainCapsFormat, &data) < 0)      \
J
Jiri Denemark 已提交
254 255
            ret = -1;                                                   \
    } while (0)
256

J
Jiri Denemark 已提交
257 258
#define DO_TEST_QEMU(Name, CapsName, Emulator, Machine, Arch, Type)     \
    do {                                                                \
259 260 261 262 263 264 265 266
        char *name = NULL;                                              \
        if (virAsprintf(&name, "qemu_%s%s%s.%s",                        \
                        Name,                                           \
                        Machine ? "-" : "", Machine ? Machine : "",     \
                        Arch) < 0) {                                    \
            ret = -1;                                                   \
            break;                                                      \
        }                                                               \
J
Jiri Denemark 已提交
267
        struct testData data = {                                        \
268
            .name = name,                                               \
J
Jiri Denemark 已提交
269 270 271 272 273 274 275 276
            .emulator = Emulator,                                       \
            .machine = Machine,                                         \
            .arch = Arch,                                               \
            .type = Type,                                               \
            .capsType = CAPS_QEMU,                                      \
            .capsName = CapsName,                                       \
            .capsOpaque = cfg,                                          \
        };                                                              \
277
        if (virTestRun(name, test_virDomainCapsFormat, &data) < 0)      \
J
Jiri Denemark 已提交
278
            ret = -1;                                                   \
279
        VIR_FREE(name);                                                 \
280 281
    } while (0)

J
Jiri Denemark 已提交
282
    DO_TEST("basic", "/bin/emulatorbin", "my-machine-type",
J
Jiri Denemark 已提交
283
            "x86_64", VIR_DOMAIN_VIRT_UML, CAPS_NONE);
J
Jiri Denemark 已提交
284
    DO_TEST("full", "/bin/emulatorbin", "my-machine-type",
J
Jiri Denemark 已提交
285
            "x86_64", VIR_DOMAIN_VIRT_KVM, CAPS_ALL);
J
Jiri Denemark 已提交
286 287 288

#if WITH_QEMU

289
    DO_TEST_QEMU("1.7.0", "caps_1.7.0",
290
                 "/usr/bin/qemu-system-x86_64", NULL,
J
Jiri Denemark 已提交
291
                 "x86_64", VIR_DOMAIN_VIRT_KVM);
292

293
    DO_TEST_QEMU("2.6.0", "caps_2.6.0",
294
                 "/usr/bin/qemu-system-x86_64", NULL,
J
Jiri Denemark 已提交
295
                 "x86_64", VIR_DOMAIN_VIRT_KVM);
296

297
    DO_TEST_QEMU("2.6.0", "caps_2.6.0-gicv2",
298
                 "/usr/bin/qemu-system-aarch64", NULL,
J
Jiri Denemark 已提交
299
                 "aarch64", VIR_DOMAIN_VIRT_KVM);
300

301
    DO_TEST_QEMU("2.6.0-gicv2", "caps_2.6.0-gicv2",
302
                 "/usr/bin/qemu-system-aarch64", "virt",
J
Jiri Denemark 已提交
303
                 "aarch64", VIR_DOMAIN_VIRT_KVM);
304

305
    DO_TEST_QEMU("2.6.0-gicv3", "caps_2.6.0-gicv3",
306
                 "/usr/bin/qemu-system-aarch64", "virt",
J
Jiri Denemark 已提交
307
                 "aarch64", VIR_DOMAIN_VIRT_KVM);
308

309
    DO_TEST_QEMU("2.6.0", "caps_2.6.0",
310
                 "/usr/bin/qemu-system-ppc64", NULL,
J
Jiri Denemark 已提交
311
                 "ppc64le", VIR_DOMAIN_VIRT_KVM);
312

313 314
#endif /* WITH_QEMU */

M
Michal Privoznik 已提交
315 316 317 318
    return ret;
}

VIRT_TEST_MAIN(mymain)