qemuhotplugtest.c 11.5 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 25 26 27 28 29 30 31 32 33 34 35
/*
 * 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 "qemu/qemu_conf.h"
#include "qemu/qemu_hotplug.h"
#include "qemumonitortestutils.h"
#include "testutils.h"
#include "testutilsqemu.h"
#include "virerror.h"
#include "virstring.h"
#include "virthread.h"
#include "virfile.h"

#define VIR_FROM_THIS VIR_FROM_NONE

static virQEMUDriver driver;

36 37 38 39 40 41
enum {
    ATTACH,
    DETACH,
    UPDATE
};

42 43 44 45 46
struct qemuHotplugTestData {
    const char *domain_filename;
    const char *device_filename;
    bool fail;
    const char *const *mon;
47 48 49
    int action;
    bool keep;
    virDomainObjPtr vm;
50 51 52 53 54
};

static int
qemuHotplugCreateObjects(virDomainXMLOptionPtr xmlopt,
                         virDomainObjPtr *vm,
55
                         const char *domxml)
56 57
{
    int ret = -1;
58
    qemuDomainObjPrivatePtr priv = NULL;
59 60 61 62

    if (!(*vm = virDomainObjNew(xmlopt)))
        goto cleanup;

63 64 65 66 67
    if (!((*vm)->def = virDomainDefParseString(domxml,
                                               driver.caps,
                                               driver.xmlopt,
                                               QEMU_EXPECTED_VIRT_TYPES,
                                               0)))
68 69
        goto cleanup;

70 71 72 73 74 75 76 77
    priv = (*vm)->privateData;

    if (!(priv->qemuCaps = virQEMUCapsNew()))
        goto cleanup;

    /* for attach & detach qemu must support -device */
    virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_DEVICE);

78 79 80 81 82
    ret = 0;
cleanup:
    return ret;
}

83 84 85 86 87 88 89 90 91
static int
testQemuHotplugAttach(virDomainObjPtr vm,
                      virDomainDeviceDefPtr dev)
{
    int ret = -1;

    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_CHR:
        ret = qemuDomainAttachChrDevice(&driver, vm, dev->data.chr);
92 93 94 95
        if (!ret) {
            /* vm->def stolen dev->data.chr so we ought to avoid freeing it */
            dev->data.chr = NULL;
        }
96 97 98
        break;
    default:
        if (virTestGetVerbose())
99
            fprintf(stderr, "device type '%s' cannot be attached\n",
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
                    virDomainDeviceTypeToString(dev->type));
        break;
    }

    return ret;
}

static int
testQemuHotplugDetach(virDomainObjPtr vm,
                      virDomainDeviceDefPtr dev)
{
    int ret = -1;

    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_CHR:
        ret = qemuDomainDetachChrDevice(&driver, vm, dev->data.chr);
        break;
    default:
        if (virTestGetVerbose())
119
            fprintf(stderr, "device type '%s' cannot be detached\n",
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                    virDomainDeviceTypeToString(dev->type));
        break;
    }

    return ret;
}

static int
testQemuHotplugUpdate(virDomainObjPtr vm,
                      virDomainDeviceDefPtr dev)
{
    int ret = -1;

    /* XXX Ideally, we would call qemuDomainUpdateDeviceLive here.  But that
     * would require us to provide virConnectPtr and virDomainPtr (they're used
     * in case of updating a disk device. So for now, we will proceed with
     * breaking the function into pieces. If we ever learn how to fake those
     * required object, we can replace this code then. */
    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_GRAPHICS:
        ret = qemuDomainChangeGraphics(&driver, vm, dev->data.graphics);
        break;
    default:
        if (virTestGetVerbose())
144
            fprintf(stderr, "device type '%s' cannot be updated\n",
145 146 147 148 149 150 151
                    virDomainDeviceTypeToString(dev->type));
        break;
    }

    return ret;
}

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
static int
testQemuHotplugCheckResult(virDomainObjPtr vm,
                           const char *expected,
                           bool fail)
{
    char *actual;
    int ret;

    actual = virDomainDefFormat(vm->def, VIR_DOMAIN_XML_SECURE);
    if (!actual)
        return -1;

    if (STREQ(expected, actual)) {
        if (fail && virTestGetVerbose())
            fprintf(stderr, "domain XML should not match the expected result\n");
        ret = 0;
    } else {
        if (!fail)
            virtTestDifference(stderr, expected, actual);
        ret = -1;
    }

    VIR_FREE(actual);
    return ret;
}

178 179 180 181 182 183 184
static int
testQemuHotplug(const void *data)
{
    int ret = -1;
    struct qemuHotplugTestData *test = (struct qemuHotplugTestData *) data;
    char *domain_filename = NULL;
    char *device_filename = NULL;
185 186
    char *result_filename = NULL;
    char *domain_xml = NULL;
187
    char *device_xml = NULL;
188
    char *result_xml = NULL;
189 190
    const char *const *tmp;
    bool fail = test->fail;
191
    bool keep = test->keep;
192 193 194 195 196 197 198 199 200
    virDomainObjPtr vm = NULL;
    virDomainDeviceDefPtr dev = NULL;
    virCapsPtr caps = NULL;
    qemuMonitorTestPtr test_mon = NULL;
    qemuDomainObjPrivatePtr priv = NULL;

    if (virAsprintf(&domain_filename, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
                    abs_srcdir, test->domain_filename) < 0 ||
        virAsprintf(&device_filename, "%s/qemuhotplugtestdata/qemuhotplug-%s.xml",
201 202 203 204 205 206 207 208 209 210 211 212 213
                    abs_srcdir, test->device_filename) < 0 ||
        virAsprintf(&result_filename,
                    "%s/qemuhotplugtestdata/qemuhotplug-%s+%s.xml",
                    abs_srcdir, test->domain_filename,
                    test->device_filename) < 0)
        goto cleanup;

    if (virtTestLoadFile(domain_filename, &domain_xml) < 0 ||
        virtTestLoadFile(device_filename, &device_xml) < 0)
        goto cleanup;

    if (test->action != UPDATE &&
        virtTestLoadFile(result_filename, &result_xml) < 0)
214 215 216 217 218
        goto cleanup;

    if (!(caps = virQEMUDriverGetCapabilities(&driver, false)))
        goto cleanup;

219 220 221
    if (test->vm) {
        vm = test->vm;
    } else {
222
        if (qemuHotplugCreateObjects(driver.xmlopt, &vm, domain_xml) < 0)
223 224
            goto cleanup;
    }
225 226

    if (!(dev = virDomainDeviceDefParse(device_xml, vm->def,
227
                                        caps, driver.xmlopt, 0)))
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        goto cleanup;

    /* Now is the best time to feed the spoofed monitor with predefined
     * replies. */
    if (!(test_mon = qemuMonitorTestNew(true, driver.xmlopt)))
        goto cleanup;

    tmp = test->mon;
    while (tmp && *tmp) {
        const char *command_name;
        const char *response;

        if (!(command_name = *tmp++) ||
            !(response = *tmp++))
            break;
        if (qemuMonitorTestAddItem(test_mon, command_name, response) < 0)
            goto cleanup;
    }

247
    priv = vm->privateData;
248 249 250 251 252 253 254 255
    priv->mon = qemuMonitorTestGetMonitor(test_mon);
    priv->monJSON = true;

    /* XXX We need to unlock the monitor here, as
     * qemuDomainObjEnterMonitorInternal (called from qemuDomainChangeGraphics)
     * tries to lock it again */
    virObjectUnlock(priv->mon);

256 257 258
    switch (test->action) {
    case ATTACH:
        ret = testQemuHotplugAttach(vm, dev);
259 260
        if (ret == 0 || fail)
            ret = testQemuHotplugCheckResult(vm, result_xml, fail);
261
        break;
262 263 264

    case DETACH:
        ret = testQemuHotplugDetach(vm, dev);
265 266
        if (ret == 0 || fail)
            ret = testQemuHotplugCheckResult(vm, domain_xml, fail);
267
        break;
268 269 270

    case UPDATE:
        ret = testQemuHotplugUpdate(vm, dev);
271 272 273 274 275
    }

cleanup:
    VIR_FREE(domain_filename);
    VIR_FREE(device_filename);
276 277
    VIR_FREE(result_filename);
    VIR_FREE(domain_xml);
278
    VIR_FREE(device_xml);
279
    VIR_FREE(result_xml);
280 281 282
    /* don't dispose test monitor with VM */
    if (priv)
        priv->mon = NULL;
283 284 285 286 287 288
    if (keep) {
        test->vm = vm;
    } else {
        virObjectUnref(vm);
        test->vm = NULL;
    }
289 290 291 292 293 294 295 296 297 298
    virDomainDeviceDefFree(dev);
    virObjectUnref(caps);
    qemuMonitorTestFree(test_mon);
    return ((ret < 0 && fail) || (!ret && !fail)) ? 0 : -1;
}

static int
mymain(void)
{
    int ret = 0;
299
    struct qemuHotplugTestData data = {0};
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

#if !WITH_YAJL
    fputs("libvirt not compiled with yajl, skipping this test\n", stderr);
    return EXIT_AM_SKIP;
#endif

    if (virThreadInitialize() < 0 ||
        !(driver.caps = testQemuCapsInit()) ||
        !(driver.xmlopt = virQEMUDriverCreateXMLConf(&driver)))
        return EXIT_FAILURE;

    virEventRegisterDefaultImpl();

    driver.config = virQEMUDriverConfigNew(false);
    VIR_FREE(driver.config->spiceListen);
    VIR_FREE(driver.config->vncListen);

317 318 319
    if (!(driver.domainEventState = virDomainEventStateNew()))
        return EXIT_FAILURE;

320 321 322 323
    /* some dummy values from 'config file' */
    if (VIR_STRDUP_QUIET(driver.config->spicePassword, "123456") < 0)
        return EXIT_FAILURE;

324 325
#define DO_TEST(file, ACTION, dev, fial, kep, ...)                          \
    do {                                                                    \
326
        const char *my_mon[] = { __VA_ARGS__, NULL};                        \
327 328
        const char *name = file " " #ACTION " " dev;                        \
        data.action = ACTION;                                               \
329 330 331 332 333
        data.domain_filename = file;                                        \
        data.device_filename = dev;                                         \
        data.fail = fial;                                                   \
        data.mon = my_mon;                                                  \
        data.keep = kep;                                                    \
334
        if (virtTestRun(name, 1, testQemuHotplug, &data) < 0)               \
335
            ret = -1;                                                       \
336
    } while (0)
337 338

#define DO_TEST_ATTACH(file, dev, fial, kep, ...)                           \
339
    DO_TEST(file, ATTACH, dev, fial, kep, __VA_ARGS__)
340 341

#define DO_TEST_DETACH(file, dev, fial, kep, ...)                           \
342
    DO_TEST(file, DETACH, dev, fial, kep, __VA_ARGS__)
343 344

#define DO_TEST_UPDATE(file, dev, fial, kep, ...)                           \
345
    DO_TEST(file, UPDATE, dev, fial, kep, __VA_ARGS__)
346

347 348 349

#define QMP_OK      "{\"return\": {}}"

350 351
    DO_TEST_UPDATE("graphics-spice", "graphics-spice-nochange", false, false, NULL);
    DO_TEST_UPDATE("graphics-spice-timeout", "graphics-spice-timeout-nochange", false, false,
352
                   "set_password", QMP_OK, "expire_password", QMP_OK);
353
    DO_TEST_UPDATE("graphics-spice-timeout", "graphics-spice-timeout-password", false, false,
354
                   "set_password", QMP_OK, "expire_password", QMP_OK);
355 356
    DO_TEST_UPDATE("graphics-spice", "graphics-spice-listen", true, false, NULL);
    DO_TEST_UPDATE("graphics-spice-listen-network", "graphics-spice-listen-network", false, false,
357
                   "set_password", QMP_OK, "expire_password", QMP_OK);
358 359 360 361 362
    /* Strange huh? Currently, only graphics can be updated :-P */
    DO_TEST_UPDATE("disk-cdrom", "disk-cdrom-nochange", true, false, NULL);

    DO_TEST_ATTACH("console-compat-2", "console-virtio", false, true,
                   "chardev-add", "{\"return\": {\"pty\": \"/dev/pts/26\"}}",
363
                   "device_add", QMP_OK);
364 365

    DO_TEST_DETACH("console-compat-2", "console-virtio", false, false,
366 367
                   "device_del", QMP_OK,
                   "chardev-remove", QMP_OK);
368 369 370

    virObjectUnref(driver.caps);
    virObjectUnref(driver.xmlopt);
371
    virObjectUnref(driver.config);
372 373 374 375
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)