qemucapabilitiestest.c 7.0 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
/*
 * Copyright (C) 2011-2013 Red Hat, Inc.
 *
 * 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 "testutils.h"
#include "testutilsqemu.h"
#include "qemumonitortestutils.h"
25
#define LIBVIRT_QEMU_CAPSPRIV_H_ALLOW
26
#include "qemu/qemu_capspriv.h"
27
#define LIBVIRT_QEMU_MONITOR_PRIV_H_ALLOW
28
#include "qemu/qemu_monitor_priv.h"
29 30
#define LIBVIRT_QEMU_PROCESSPRIV_H_ALLOW
#include "qemu/qemu_processpriv.h"
31 32 33 34

#define VIR_FROM_THIS VIR_FROM_NONE

typedef struct _testQemuData testQemuData;
35
typedef testQemuData *testQemuDataPtr;
36
struct _testQemuData {
37
    virQEMUDriver driver;
38
    const char *archName;
39 40 41 42
    const char *base;
};


43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static int
testQemuDataInit(testQemuDataPtr data)
{
    if (qemuTestDriverInit(&data->driver) < 0)
        return -1;

    return 0;
}


static void
testQemuDataReset(testQemuDataPtr data)
{
    qemuTestDriverFree(&data->driver);
}


60 61 62 63
static int
testQemuCaps(const void *opaque)
{
    int ret = -1;
64
    testQemuData *data = (void *) opaque;
65 66
    char *repliesFile = NULL;
    char *capsFile = NULL;
67
    qemuMonitorTestPtr mon = NULL;
68
    virQEMUCapsPtr capsActual = NULL;
69
    char *actual = NULL;
70 71
    unsigned int fakeMicrocodeVersion = 0;
    const char *p;
72

73 74
    if (virAsprintf(&repliesFile, "%s/qemucapabilitiesdata/%s.%s.replies",
                    abs_srcdir, data->base, data->archName) < 0 ||
75
        virAsprintf(&capsFile, "%s/qemucapabilitiesdata/%s.%s.xml",
76
                    abs_srcdir, data->base, data->archName) < 0)
77 78
        goto cleanup;

79
    if (!(mon = qemuMonitorTestNewFromFileFull(repliesFile, &data->driver, NULL)))
80 81
        goto cleanup;

82 83 84
    if (qemuProcessQMPInitMonitor(qemuMonitorTestGetMonitor(mon)) < 0)
        goto cleanup;

85 86 87
    if (!(capsActual = virQEMUCapsNew()) ||
        virQEMUCapsInitQMPMonitor(capsActual,
                                  qemuMonitorTestGetMonitor(mon)) < 0)
88 89
        goto cleanup;

90
    if (virQEMUCapsGet(capsActual, QEMU_CAPS_KVM)) {
91
        qemuMonitorResetCommandID(qemuMonitorTestGetMonitor(mon));
92 93 94 95

        if (qemuProcessQMPInitMonitor(qemuMonitorTestGetMonitor(mon)) < 0)
            goto cleanup;

96 97 98 99
        if (virQEMUCapsInitQMPMonitorTCG(capsActual,
                                         qemuMonitorTestGetMonitor(mon)) < 0)
            goto cleanup;

100 101 102 103 104 105 106 107 108 109 110
        /* calculate fake microcode version based on filename for a reproducible
         * number for testing which does not change with the contents */
        for (p = data->archName; *p; p++)
            fakeMicrocodeVersion += *p;

        fakeMicrocodeVersion *= 100000;

        for (p = data->base; *p; p++)
            fakeMicrocodeVersion += *p;

        virQEMUCapsSetMicrocodeVersion(capsActual, fakeMicrocodeVersion);
111
    }
112

113
    if (!(actual = virQEMUCapsFormatCache(capsActual)))
114 115
        goto cleanup;

116
    if (virTestCompareToFile(actual, capsFile) < 0)
117 118 119
        goto cleanup;

    ret = 0;
120
 cleanup:
121 122
    VIR_FREE(repliesFile);
    VIR_FREE(capsFile);
123
    VIR_FREE(actual);
124
    qemuMonitorTestFree(mon);
125
    virObjectUnref(capsActual);
126 127 128
    return ret;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

static int
testQemuCapsCopy(const void *opaque)
{
    int ret = -1;
    const testQemuData *data = opaque;
    char *capsFile = NULL;
    virCapsPtr caps = NULL;
    virQEMUCapsPtr orig = NULL;
    virQEMUCapsPtr copy = NULL;
    char *actual = NULL;

    if (virAsprintf(&capsFile, "%s/qemucapabilitiesdata/%s.%s.xml",
                    abs_srcdir, data->base, data->archName) < 0)
        goto cleanup;

    if (!(caps = virCapabilitiesNew(virArchFromString(data->archName),
                                    false, false)))
        goto cleanup;

149
    if (!(orig = qemuTestParseCapabilities(caps, capsFile)))
150 151 152 153 154
        goto cleanup;

    if (!(copy = virQEMUCapsNewCopy(orig)))
        goto cleanup;

155
    if (!(actual = virQEMUCapsFormatCache(copy)))
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        goto cleanup;

    if (virTestCompareToFile(actual, capsFile) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    VIR_FREE(capsFile);
    virObjectUnref(caps);
    virObjectUnref(orig);
    virObjectUnref(copy);
    VIR_FREE(actual);
    return ret;
}


173 174 175 176 177 178
static int
mymain(void)
{
    int ret = 0;
    testQemuData data;

179
#if !WITH_YAJL
J
Ján Tomko 已提交
180
    fputs("libvirt not compiled with JSON support, skipping this test\n", stderr);
181 182 183
    return EXIT_AM_SKIP;
#endif

184
    if (virThreadInitialize() < 0)
185 186 187 188
        return EXIT_FAILURE;

    virEventRegisterDefaultImpl();

189 190 191
    if (testQemuDataInit(&data) < 0)
        return EXIT_FAILURE;

192 193 194 195 196 197 198 199 200
#define DO_TEST(arch, name) \
    do { \
        data.archName = arch; \
        data.base = name; \
        if (virTestRun(name "(" arch ")", testQemuCaps, &data) < 0) \
            ret = -1; \
        if (virTestRun("copy " name "(" arch ")", \
                       testQemuCapsCopy, &data) < 0) \
            ret = -1; \
201
    } while (0)
202

203
    /* Keep this in sync with qemucaps2xmltest */
204 205
    DO_TEST("x86_64", "caps_1.5.3");
    DO_TEST("x86_64", "caps_1.6.0");
206
    DO_TEST("x86_64", "caps_1.7.0");
207 208 209 210
    DO_TEST("x86_64", "caps_2.1.1");
    DO_TEST("x86_64", "caps_2.4.0");
    DO_TEST("x86_64", "caps_2.5.0");
    DO_TEST("x86_64", "caps_2.6.0");
211
    DO_TEST("x86_64", "caps_2.7.0");
212
    DO_TEST("x86_64", "caps_2.8.0");
P
Pavel Hrdina 已提交
213
    DO_TEST("x86_64", "caps_2.9.0");
214
    DO_TEST("x86_64", "caps_2.10.0");
215
    DO_TEST("x86_64", "caps_2.11.0");
216
    DO_TEST("x86_64", "caps_2.12.0");
217
    DO_TEST("x86_64", "caps_3.0.0");
218
    DO_TEST("x86_64", "caps_3.1.0");
219
    DO_TEST("x86_64", "caps_4.0.0");
220 221 222
    DO_TEST("aarch64", "caps_2.6.0");
    DO_TEST("aarch64", "caps_2.10.0");
    DO_TEST("aarch64", "caps_2.12.0");
223 224
    DO_TEST("ppc64", "caps_2.6.0");
    DO_TEST("ppc64", "caps_2.9.0");
225
    DO_TEST("ppc64", "caps_2.10.0");
226
    DO_TEST("ppc64", "caps_2.12.0");
227
    DO_TEST("ppc64", "caps_3.0.0");
228
    DO_TEST("ppc64", "caps_3.1.0");
229 230
    DO_TEST("s390x", "caps_2.7.0");
    DO_TEST("s390x", "caps_2.8.0");
231
    DO_TEST("s390x", "caps_2.9.0");
232
    DO_TEST("s390x", "caps_2.10.0");
233
    DO_TEST("s390x", "caps_2.11.0");
234
    DO_TEST("s390x", "caps_2.12.0");
235
    DO_TEST("s390x", "caps_3.0.0");
L
Lubomir Rintel 已提交
236
    DO_TEST("riscv32", "caps_3.0.0");
237
    DO_TEST("riscv32", "caps_4.0.0");
L
Lubomir Rintel 已提交
238
    DO_TEST("riscv64", "caps_3.0.0");
239
    DO_TEST("riscv64", "caps_4.0.0");
240

J
Jiri Denemark 已提交
241 242 243
    /*
     * Run "tests/qemucapsprobe /path/to/qemu/binary >foo.replies"
     * to generate updated or new *.replies data files.
244 245 246
     *
     * If you manually edit replies files you can run
     * "tests/qemucapsfixreplies foo.replies" to fix the replies ids.
J
Jiri Denemark 已提交
247 248
     */

249
    testQemuDataReset(&data);
250

251 252 253
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

254
VIR_TEST_MAIN(mymain)