qemucapabilitiestest.c 5.9 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 __QEMU_CAPSPRIV_H_ALLOW__
26
#include "qemu/qemu_capspriv.h"
27 28
#define __QEMU_MONITOR_PRIV_H_ALLOW__
#include "qemu/qemu_monitor_priv.h"
29 30 31 32

#define VIR_FROM_THIS VIR_FROM_NONE

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


static int
testQemuCaps(const void *opaque)
{
    int ret = -1;
45
    testQemuData *data = (void *) opaque;
46 47
    char *repliesFile = NULL;
    char *capsFile = NULL;
48
    qemuMonitorTestPtr mon = NULL;
49
    virQEMUCapsPtr capsActual = NULL;
50
    char *actual = NULL;
51

52 53
    if (virAsprintf(&repliesFile, "%s/qemucapabilitiesdata/%s.%s.replies",
                    abs_srcdir, data->base, data->archName) < 0 ||
54
        virAsprintf(&capsFile, "%s/qemucapabilitiesdata/%s.%s.xml",
55
                    abs_srcdir, data->base, data->archName) < 0)
56 57
        goto cleanup;

58
    if (!(mon = qemuMonitorTestNewFromFileFull(repliesFile, &data->driver, NULL)))
59 60
        goto cleanup;

61 62 63
    if (!(capsActual = virQEMUCapsNew()) ||
        virQEMUCapsInitQMPMonitor(capsActual,
                                  qemuMonitorTestGetMonitor(mon)) < 0)
64 65
        goto cleanup;

66
    if (virQEMUCapsGet(capsActual, QEMU_CAPS_KVM)) {
67
        qemuMonitorResetCommandID(qemuMonitorTestGetMonitor(mon));
68 69 70 71 72 73 74 75 76
        if (virQEMUCapsInitQMPMonitorTCG(capsActual,
                                         qemuMonitorTestGetMonitor(mon)) < 0)
            goto cleanup;

        /* Fill microcodeVersion with a "random" value which is the file
         * length to provide a reproducible number for testing.
         */
        virQEMUCapsSetMicrocodeVersion(capsActual, virFileLength(repliesFile, -1));
    }
77

78
    if (!(actual = virQEMUCapsFormatCache(capsActual)))
79 80
        goto cleanup;

81
    if (virTestCompareToFile(actual, capsFile) < 0)
82 83 84
        goto cleanup;

    ret = 0;
85
 cleanup:
86 87
    VIR_FREE(repliesFile);
    VIR_FREE(capsFile);
88
    VIR_FREE(actual);
89
    qemuMonitorTestFree(mon);
90
    virObjectUnref(capsActual);
91 92 93
    return ret;
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

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;

114
    if (!(orig = qemuTestParseCapabilities(caps, capsFile)))
115 116 117 118 119
        goto cleanup;

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

120
    if (!(actual = virQEMUCapsFormatCache(copy)))
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        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;
}


138 139 140 141 142 143 144
static int
mymain(void)
{
    int ret = 0;
    testQemuData data;

#if !WITH_YAJL
J
Ján Tomko 已提交
145
    fputs("libvirt not compiled with JSON support, skipping this test\n", stderr);
146 147 148 149
    return EXIT_AM_SKIP;
#endif

    if (virThreadInitialize() < 0 ||
150
        qemuTestDriverInit(&data.driver) < 0)
151 152 153 154
        return EXIT_FAILURE;

    virEventRegisterDefaultImpl();

155 156 157 158 159 160 161 162 163
#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; \
164
    } while (0)
165

166 167
    DO_TEST("x86_64", "caps_1.5.3");
    DO_TEST("x86_64", "caps_1.6.0");
168
    DO_TEST("x86_64", "caps_1.7.0");
169 170 171 172
    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");
173
    DO_TEST("x86_64", "caps_2.7.0");
174
    DO_TEST("x86_64", "caps_2.8.0");
P
Pavel Hrdina 已提交
175
    DO_TEST("x86_64", "caps_2.9.0");
176
    DO_TEST("x86_64", "caps_2.10.0");
177
    DO_TEST("x86_64", "caps_2.12.0");
178
    DO_TEST("x86_64", "caps_3.0.0");
179 180 181
    DO_TEST("aarch64", "caps_2.6.0");
    DO_TEST("aarch64", "caps_2.10.0");
    DO_TEST("aarch64", "caps_2.12.0");
182 183
    DO_TEST("ppc64", "caps_2.6.0");
    DO_TEST("ppc64", "caps_2.9.0");
184
    DO_TEST("ppc64", "caps_2.10.0");
185
    DO_TEST("ppc64", "caps_2.12.0");
186
    DO_TEST("ppc64", "caps_3.0.0");
187 188
    DO_TEST("s390x", "caps_2.7.0");
    DO_TEST("s390x", "caps_2.8.0");
189
    DO_TEST("s390x", "caps_2.9.0");
190
    DO_TEST("s390x", "caps_2.10.0");
191
    DO_TEST("s390x", "caps_2.11.0");
192
    DO_TEST("s390x", "caps_2.12.0");
193

J
Jiri Denemark 已提交
194 195 196
    /*
     * Run "tests/qemucapsprobe /path/to/qemu/binary >foo.replies"
     * to generate updated or new *.replies data files.
197 198 199
     *
     * If you manually edit replies files you can run
     * "tests/qemucapsfixreplies foo.replies" to fix the replies ids.
J
Jiri Denemark 已提交
200 201
     */

202
    qemuTestDriverFree(&data.driver);
203

204 205 206
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

207
VIR_TEST_MAIN(mymain)