File: domaincapstest.c

package info (click to toggle)
libvirt 1.2.9-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 173,536 kB
  • sloc: ansic: 463,129; xml: 68,283; sh: 16,393; makefile: 4,588; python: 3,705; perl: 3,675; ml: 470; sed: 16
file content (258 lines) | stat: -rw-r--r-- 8,490 bytes parent folder | download | duplicates (3)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * 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

typedef int (*virDomainCapsFill)(virDomainCapsPtr domCaps,
                                 void *opaque);

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

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

static int
fillAll(virDomainCapsPtr domCaps,
        void *opaque ATTRIBUTE_UNUSED)
{
    virDomainCapsOSPtr os = &domCaps->os;
    virDomainCapsLoaderPtr loader = &os->loader;
    virDomainCapsDeviceDiskPtr disk = &domCaps->disk;
    virDomainCapsDeviceHostdevPtr hostdev = &domCaps->hostdev;
    domCaps->maxvcpus = 255;

    os->device.supported = true;

    loader->device.supported = true;
    SET_ALL_BITS(loader->type);
    SET_ALL_BITS(loader->readonly);
    if (fillStringValues(&loader->values,
                         "/foo/bar",
                         "/tmp/my_path",
                         NULL) < 0)
        return -1;

    disk->device.supported = true;
    SET_ALL_BITS(disk->diskDevice);
    SET_ALL_BITS(disk->bus);

    hostdev->device.supported = true;
    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);
    return 0;
}


#ifdef WITH_QEMU
# include "testutilsqemu.h"

struct fillQemuCapsData {
    virQEMUCapsPtr qemuCaps;
    virQEMUDriverConfigPtr cfg;
};

static int
fillQemuCaps(virDomainCapsPtr domCaps,
             void *opaque)
{
    struct fillQemuCapsData *data = (struct fillQemuCapsData *) opaque;
    virQEMUCapsPtr qemuCaps = data->qemuCaps;
    virQEMUDriverConfigPtr cfg = data->cfg;

    if (virQEMUCapsFillDomainCaps(domCaps, qemuCaps,
                                  cfg->loader, cfg->nloader) < 0)
        return -1;

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

    /* Moreover, as of f05b6a918e28 we are expecting to see
     * OVMF_CODE.fd file which may not exists everywhere. */
    if (!domCaps->os.loader.values.nvalues) {
        virDomainCapsLoaderPtr loader = &domCaps->os.loader;

        if (fillStringValues(&loader->values,
                             "/usr/share/OVMF/OVMF_CODE.fd",
                             NULL) < 0)
            return -1;
    }
    return 0;
}
#endif /* WITH_QEMU */


static virDomainCapsPtr
buildVirDomainCaps(const char *emulatorbin,
                   const char *machine,
                   virArch arch,
                   virDomainVirtType type,
                   virDomainCapsFill fillFunc,
                   void *opaque)
{
    virDomainCapsPtr domCaps, ret = NULL;

    if (!(domCaps = virDomainCapsNew(emulatorbin, machine, arch, type)))
        goto cleanup;

    if (fillFunc && fillFunc(domCaps, opaque) < 0) {
        virObjectUnref(domCaps);
        domCaps = NULL;
    }

    ret = domCaps;
 cleanup:
    return ret;
}

struct test_virDomainCapsFormatData {
    const char *filename;
    const char *emulatorbin;
    const char *machine;
    virArch arch;
    virDomainVirtType type;
    virDomainCapsFill fillFunc;
    void *opaque;
};

static int
test_virDomainCapsFormat(const void *opaque)
{
    struct test_virDomainCapsFormatData *data =
        (struct test_virDomainCapsFormatData *) opaque;
    virDomainCapsPtr domCaps = NULL;
    char *path = NULL;
    char *domCapsXML = NULL;
    char *domCapsFromFile = NULL;
    int ret = -1;

    if (virAsprintf(&path, "%s/domaincapsschemadata/domaincaps-%s.xml",
                    abs_srcdir, data->filename) < 0)
        goto cleanup;

    if (virFileReadAll(path, 8192, &domCapsFromFile) < 0)
        goto cleanup;

    if (!(domCaps = buildVirDomainCaps(data->emulatorbin, data->machine,
                                       data->arch, data->type,
                                       data->fillFunc, data->opaque)))
        goto cleanup;

    if (!(domCapsXML = virDomainCapsFormat(domCaps)))
        goto cleanup;

    if (STRNEQ(domCapsFromFile, domCapsXML)) {
        virtTestDifference(stderr, domCapsFromFile, domCapsXML);
        goto cleanup;
    }

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

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

#define DO_TEST(Filename, Emulatorbin, Machine, Arch, Type, ...)                    \
    do {                                                                            \
        struct test_virDomainCapsFormatData data = {.filename = Filename,           \
            .emulatorbin = Emulatorbin, .machine = Machine, .arch = Arch,           \
            .type = Type, __VA_ARGS__};                                             \
        if (virtTestRun(Filename, test_virDomainCapsFormat, &data) < 0)             \
            ret = -1;                                                               \
    } while (0)

    DO_TEST("basic", "/bin/emulatorbin", "my-machine-type",
            VIR_ARCH_X86_64, VIR_DOMAIN_VIRT_UML);
    DO_TEST("full", "/bin/emulatorbin", "my-machine-type",
            VIR_ARCH_X86_64, VIR_DOMAIN_VIRT_KVM, .fillFunc = fillAll);

#ifdef WITH_QEMU

    virQEMUDriverConfigPtr cfg = virQEMUDriverConfigNew(false);

# define DO_TEST_QEMU(Filename, QemuCapsFile, Emulatorbin, Machine, Arch, Type, ...)    \
    do {                                                                                \
        const char *capsPath = abs_srcdir "/qemucapabilitiesdata/" QemuCapsFile ".caps";    \
        virQEMUCapsPtr qemuCaps = qemuTestParseCapabilities(capsPath);                  \
        struct fillQemuCapsData fillData = {.qemuCaps = qemuCaps, .cfg = cfg};          \
        struct test_virDomainCapsFormatData data = {.filename = Filename,               \
            .emulatorbin = Emulatorbin, .machine = Machine, .arch = Arch,               \
            .type = Type, .fillFunc = fillQemuCaps, .opaque = &fillData};               \
        if (!qemuCaps) {                                                                \
            fprintf(stderr, "Unable to build qemu caps from %s\n", capsPath);           \
            ret = -1;                                                                   \
        } else if (virtTestRun(Filename, test_virDomainCapsFormat, &data) < 0)          \
            ret = -1;                                                                   \
    } while (0)

    DO_TEST_QEMU("qemu_1.6.50-1", "caps_1.6.50-1", "/usr/bin/qemu-system-x86_64",
                 "pc-1.2",  VIR_ARCH_X86_64, VIR_DOMAIN_VIRT_KVM);

    virObjectUnref(cfg);
#endif /* WITH_QEMU */

    return ret;
}

VIRT_TEST_MAIN(mymain)