qemucaps2xmltest.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2014 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"
22
#include "testutilsqemu.h"
23 24 25 26 27 28 29 30 31
#include "qemu/qemu_capabilities.h"


#define VIR_FROM_THIS VIR_FROM_NONE


typedef struct _testQemuData testQemuData;
typedef testQemuData *testQemuDataPtr;
struct _testQemuData {
32 33
    const char *inputDir;
    const char *outputDir;
34
    const char *base;
35
    const char *archName;
36
    int ret;
37 38
};

39
static int
40
testQemuDataInit(testQemuDataPtr data)
41
{
42
    data->inputDir = TEST_QEMU_CAPS_PATH;
43 44
    data->outputDir = abs_srcdir "/qemucaps2xmloutdata";

45 46
    data->ret = 0;

47 48 49
    return 0;
}

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
static virQEMUCapsPtr
testQemuGetCaps(char *caps)
{
    virQEMUCapsPtr qemuCaps = NULL;
    xmlDocPtr xml;
    xmlXPathContextPtr ctxt = NULL;
    ssize_t i, n;
    xmlNodePtr *nodes = NULL;

    if (!(xml = virXMLParseStringCtxt(caps, "(test caps)", &ctxt)))
        goto error;

    if ((n = virXPathNodeSet("/qemuCaps/flag", ctxt, &nodes)) < 0) {
        fprintf(stderr, "failed to parse qemu capabilities flags");
        goto error;
    }

    if (!(qemuCaps = virQEMUCapsNew()))
        goto error;

    for (i = 0; i < n; i++) {
        char *str = virXMLPropString(nodes[i], "name");
        if (str) {
            int flag = virQEMUCapsTypeFromString(str);
            if (flag < 0) {
                fprintf(stderr, "Unknown qemu capabilities flag %s", str);
                VIR_FREE(str);
                goto error;
            }
            VIR_FREE(str);
            virQEMUCapsSet(qemuCaps, flag);
        }
    }

    VIR_FREE(nodes);
    xmlFreeDoc(xml);
    xmlXPathFreeContext(ctxt);
    return qemuCaps;

 error:
    VIR_FREE(nodes);
    virObjectUnref(qemuCaps);
    xmlFreeDoc(xml);
    xmlXPathFreeContext(ctxt);
    return NULL;
}

static virCapsPtr
testGetCaps(char *capsData, const testQemuData *data)
{
    virQEMUCapsPtr qemuCaps = NULL;
    virCapsPtr caps = NULL;
102
    virArch arch = virArchFromString(data->archName);
103
    VIR_AUTOFREE(char *) binary = NULL;
104 105 106

    if (virAsprintf(&binary, "/usr/bin/qemu-system-%s", data->archName) < 0)
        goto error;
107 108 109 110 111 112

    if ((qemuCaps = testQemuGetCaps(capsData)) == NULL) {
        fprintf(stderr, "failed to parse qemu capabilities flags");
        goto error;
    }

113
    if ((caps = virCapabilitiesNew(arch, false, false)) == NULL) {
114 115 116 117 118
        fprintf(stderr, "failed to create the fake capabilities");
        goto error;
    }

    if (virQEMUCapsInitGuestFromBinary(caps,
119
                                       binary,
120
                                       qemuCaps,
121
                                       arch) < 0) {
122 123 124 125
        fprintf(stderr, "failed to create the capabilities from qemu");
        goto error;
    }

N
Nehal J Wani 已提交
126
    virObjectUnref(qemuCaps);
127 128 129 130
    return caps;

 error:
    virObjectUnref(qemuCaps);
131
    virObjectUnref(caps);
132 133 134 135 136 137 138 139 140
    return NULL;
}

static int
testQemuCapsXML(const void *opaque)
{
    int ret = -1;
    const testQemuData *data = opaque;
    char *capsFile = NULL, *xmlFile = NULL;
C
Cole Robinson 已提交
141
    char *capsData = NULL;
142 143 144
    char *capsXml = NULL;
    virCapsPtr capsProvided = NULL;

145 146
    if (virAsprintf(&xmlFile, "%s/caps.%s.xml",
                    data->outputDir, data->archName) < 0)
147 148
        goto cleanup;

149 150
    if (virAsprintf(&capsFile, "%s/%s.%s.xml",
                    data->inputDir, data->base, data->archName) < 0)
151 152
        goto cleanup;

153
    if (virTestLoadFile(capsFile, &capsData) < 0)
154 155 156 157 158 159 160 161 162
        goto cleanup;

    if (!(capsProvided = testGetCaps(capsData, data)))
        goto cleanup;

    capsXml = virCapabilitiesFormatXML(capsProvided);
    if (!capsXml)
        goto cleanup;

163
    if (virTestCompareToFile(capsXml, xmlFile) < 0)
C
Cole Robinson 已提交
164
        goto cleanup;
165

C
Cole Robinson 已提交
166
    ret = 0;
167 168 169 170 171 172 173 174 175
 cleanup:
    VIR_FREE(xmlFile);
    VIR_FREE(capsFile);
    VIR_FREE(capsXml);
    VIR_FREE(capsData);
    virObjectUnref(capsProvided);
    return ret;
}

176 177 178
static int
doCapsTest(const char *base,
           const char *archName,
179
           void *opaque)
180
{
181
    testQemuDataPtr data = (testQemuDataPtr) opaque;
182 183 184 185 186 187 188 189 190 191 192 193 194 195
    VIR_AUTOFREE(char *) title = NULL;

    if (virAsprintf(&title, "%s (%s)", base, archName) < 0)
        return -1;

    data->base = base;
    data->archName = archName;

    if (virTestRun(title, testQemuCapsXML, data) < 0)
        data->ret = -1;

    return 0;
}

196 197 198 199 200
static int
mymain(void)
{
    testQemuData data;

201
#if !WITH_YAJL
J
Ján Tomko 已提交
202
    fputs("libvirt not compiled with JSON support, skipping this test\n", stderr);
203 204 205 206 207 208 209 210
    return EXIT_AM_SKIP;
#endif

    if (virThreadInitialize() < 0)
        return EXIT_FAILURE;

    virEventRegisterDefaultImpl();

211 212 213
    if (testQemuDataInit(&data) < 0)
        return EXIT_FAILURE;

214
    if (testQemuCapsIterate(".xml", doCapsTest, &data) < 0)
215
        return EXIT_FAILURE;
216

217
    return (data.ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
218 219
}

220
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("qemucaps2xml"))