qemuhotplugtest.c 20.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) 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>

22
#include "qemu/qemu_alias.h"
23 24
#include "qemu/qemu_conf.h"
#include "qemu/qemu_hotplug.h"
25
#include "qemu/qemu_hotplugpriv.h"
26 27 28 29 30 31 32 33 34 35 36 37
#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;

38 39 40 41 42 43
enum {
    ATTACH,
    DETACH,
    UPDATE
};

44 45
#define QEMU_HOTPLUG_TEST_DOMAIN_ID 7

46 47 48 49 50
struct qemuHotplugTestData {
    const char *domain_filename;
    const char *device_filename;
    bool fail;
    const char *const *mon;
51 52 53
    int action;
    bool keep;
    virDomainObjPtr vm;
54
    bool deviceDeletedEvent;
55 56 57 58 59
};

static int
qemuHotplugCreateObjects(virDomainXMLOptionPtr xmlopt,
                         virDomainObjPtr *vm,
60
                         const char *domxml,
P
Pavel Fedin 已提交
61
                         bool event, const char *testname)
62 63
{
    int ret = -1;
64
    qemuDomainObjPrivatePtr priv = NULL;
65 66 67 68

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

69 70 71 72 73
    priv = (*vm)->privateData;

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

74
    virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_VIRTIO_SCSI);
75
    virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_DEVICE_USB_STORAGE);
76
    virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_VIRTIO_CCW);
77 78
    if (event)
        virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_DEVICE_DEL_EVENT);
79

80 81
    if (qemuTestCapsCacheInsert(driver.qemuCapsCache, testname,
                                priv->qemuCaps) < 0)
P
Pavel Fedin 已提交
82 83 84 85 86 87 88 89
        goto cleanup;

    if (!((*vm)->def = virDomainDefParseString(domxml,
                                               driver.caps,
                                               driver.xmlopt,
                                               VIR_DOMAIN_DEF_PARSE_INACTIVE)))
        goto cleanup;

90
    if (qemuDomainAssignAddresses((*vm)->def, priv->qemuCaps, *vm, true) < 0)
91 92 93 94
        goto cleanup;

    if (qemuAssignDeviceAliases((*vm)->def, priv->qemuCaps) < 0)
        goto cleanup;
95

96 97
    (*vm)->def->id = QEMU_HOTPLUG_TEST_DOMAIN_ID;

98 99 100
    if (qemuDomainSetPrivatePaths(&driver, *vm) < 0)
        goto cleanup;

101
    ret = 0;
102
 cleanup:
103 104 105
    return ret;
}

106 107 108 109 110 111 112
static int
testQemuHotplugAttach(virDomainObjPtr vm,
                      virDomainDeviceDefPtr dev)
{
    int ret = -1;

    switch (dev->type) {
113 114 115 116 117 118
    case VIR_DOMAIN_DEVICE_DISK:
        /* conn in only used for storage pool and secrets lookup so as long
         * as we don't use any of them, passing NULL should be safe
         */
        ret = qemuDomainAttachDeviceDiskLive(NULL, &driver, vm, dev);
        break;
119 120 121 122
    case VIR_DOMAIN_DEVICE_CHR:
        ret = qemuDomainAttachChrDevice(&driver, vm, dev->data.chr);
        break;
    default:
123 124
        VIR_TEST_VERBOSE("device type '%s' cannot be attached\n",
                virDomainDeviceTypeToString(dev->type));
125 126 127 128 129 130 131 132 133 134 135 136 137
        break;
    }

    return ret;
}

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

    switch (dev->type) {
138 139 140
    case VIR_DOMAIN_DEVICE_DISK:
        ret = qemuDomainDetachDeviceDiskLive(&driver, vm, dev);
        break;
141 142 143 144
    case VIR_DOMAIN_DEVICE_CHR:
        ret = qemuDomainDetachChrDevice(&driver, vm, dev->data.chr);
        break;
    default:
145 146
        VIR_TEST_VERBOSE("device type '%s' cannot be detached\n",
                virDomainDeviceTypeToString(dev->type));
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        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:
169 170
        VIR_TEST_VERBOSE("device type '%s' cannot be updated\n",
                virDomainDeviceTypeToString(dev->type));
171 172 173 174 175 176
        break;
    }

    return ret;
}

177 178 179
static int
testQemuHotplugCheckResult(virDomainObjPtr vm,
                           const char *expected,
180
                           const char *expectedFile,
181 182 183 184 185
                           bool fail)
{
    char *actual;
    int ret;

186 187
    actual = virDomainDefFormat(vm->def, driver.caps,
                                VIR_DOMAIN_DEF_FORMAT_SECURE);
188 189
    if (!actual)
        return -1;
190
    vm->def->id = QEMU_HOTPLUG_TEST_DOMAIN_ID;
191 192

    if (STREQ(expected, actual)) {
193 194
        if (fail)
            VIR_TEST_VERBOSE("domain XML should not match the expected result\n");
195 196 197
        ret = 0;
    } else {
        if (!fail)
198 199 200
            virTestDifferenceFull(stderr,
                                  expected, expectedFile,
                                  actual, NULL);
201 202 203 204 205 206 207
        ret = -1;
    }

    VIR_FREE(actual);
    return ret;
}

208 209 210 211 212 213 214
static int
testQemuHotplug(const void *data)
{
    int ret = -1;
    struct qemuHotplugTestData *test = (struct qemuHotplugTestData *) data;
    char *domain_filename = NULL;
    char *device_filename = NULL;
215 216
    char *result_filename = NULL;
    char *domain_xml = NULL;
217
    char *device_xml = NULL;
218
    char *result_xml = NULL;
219 220
    const char *const *tmp;
    bool fail = test->fail;
221
    bool keep = test->keep;
222
    unsigned int device_parse_flags = 0;
223 224 225 226 227 228
    virDomainObjPtr vm = NULL;
    virDomainDeviceDefPtr dev = NULL;
    virCapsPtr caps = NULL;
    qemuMonitorTestPtr test_mon = NULL;
    qemuDomainObjPrivatePtr priv = NULL;

229
    if (virAsprintf(&domain_filename, "%s/qemuhotplugtestdomains/qemuhotplug-%s.xml",
230
                    abs_srcdir, test->domain_filename) < 0 ||
231
        virAsprintf(&device_filename, "%s/qemuhotplugtestdevices/qemuhotplug-%s.xml",
232 233
                    abs_srcdir, test->device_filename) < 0 ||
        virAsprintf(&result_filename,
234
                    "%s/qemuhotplugtestdomains/qemuhotplug-%s+%s.xml",
235 236 237 238
                    abs_srcdir, test->domain_filename,
                    test->device_filename) < 0)
        goto cleanup;

239 240
    if (virTestLoadFile(domain_filename, &domain_xml) < 0 ||
        virTestLoadFile(device_filename, &device_xml) < 0)
241 242
        goto cleanup;

243
    if (test->action == ATTACH &&
244
        virTestLoadFile(result_filename, &result_xml) < 0)
245 246 247 248 249
        goto cleanup;

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

250 251 252
    if (test->vm) {
        vm = test->vm;
    } else {
253
        if (qemuHotplugCreateObjects(driver.xmlopt, &vm, domain_xml,
P
Pavel Fedin 已提交
254 255
                                     test->deviceDeletedEvent,
                                     test->domain_filename) < 0)
256 257
            goto cleanup;
    }
258

259
    if (test->action == ATTACH)
260
        device_parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
261

262
    if (!(dev = virDomainDeviceDefParse(device_xml, vm->def,
263 264
                                        caps, driver.xmlopt,
                                        device_parse_flags)))
265 266 267 268
        goto cleanup;

    /* Now is the best time to feed the spoofed monitor with predefined
     * replies. */
269
    if (!(test_mon = qemuMonitorTestNew(true, driver.xmlopt, vm, &driver, NULL)))
270 271 272 273 274 275 276 277 278 279 280 281 282 283
        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;
    }

284
    priv = vm->privateData;
285 286 287 288 289 290 291 292
    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);

293 294 295
    switch (test->action) {
    case ATTACH:
        ret = testQemuHotplugAttach(vm, dev);
296 297 298 299 300
        if (ret == 0) {
            /* vm->def stolen dev->data.* so we just need to free the dev
             * envelope */
            VIR_FREE(dev);
        }
301
        if (ret == 0 || fail)
302 303
            ret = testQemuHotplugCheckResult(vm, result_xml,
                                             result_filename, fail);
304
        break;
305 306 307

    case DETACH:
        ret = testQemuHotplugDetach(vm, dev);
308
        if (ret == 0 || fail)
309 310
            ret = testQemuHotplugCheckResult(vm, domain_xml,
                                             domain_filename, fail);
311
        break;
312 313 314

    case UPDATE:
        ret = testQemuHotplugUpdate(vm, dev);
315 316
    }

317
 cleanup:
318 319
    VIR_FREE(domain_filename);
    VIR_FREE(device_filename);
320 321
    VIR_FREE(result_filename);
    VIR_FREE(domain_xml);
322
    VIR_FREE(device_xml);
323
    VIR_FREE(result_xml);
324 325 326
    /* don't dispose test monitor with VM */
    if (priv)
        priv->mon = NULL;
327 328 329 330 331 332
    if (keep) {
        test->vm = vm;
    } else {
        virObjectUnref(vm);
        test->vm = NULL;
    }
333 334 335 336 337 338 339 340 341 342
    virDomainDeviceDefFree(dev);
    virObjectUnref(caps);
    qemuMonitorTestFree(test_mon);
    return ((ret < 0 && fail) || (!ret && !fail)) ? 0 : -1;
}

static int
mymain(void)
{
    int ret = 0;
343
    struct qemuHotplugTestData data = {0};
344 345 346 347 348 349 350

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

    if (virThreadInitialize() < 0 ||
351
        qemuTestDriverInit(&driver) < 0)
352 353 354 355 356 357
        return EXIT_FAILURE;

    virEventRegisterDefaultImpl();

    VIR_FREE(driver.config->spiceListen);
    VIR_FREE(driver.config->vncListen);
358 359 360
    /* some dummy values from 'config file' */
    if (VIR_STRDUP_QUIET(driver.config->spicePassword, "123456") < 0)
        return EXIT_FAILURE;
361

362
    if (!(driver.domainEventState = virObjectEventStateNew()))
363 364
        return EXIT_FAILURE;

365 366 367 368 369 370
    driver.lockManager = virLockManagerPluginNew("nop", "qemu",
                                                 driver.config->configBaseDir,
                                                 0);
    if (!driver.lockManager)
        return EXIT_FAILURE;

371 372 373 374
    /* wait only 100ms for DEVICE_DELETED event */
    qemuDomainRemoveDeviceWaitTime = 100;

#define DO_TEST(file, ACTION, dev, event, fial, kep, ...)                   \
375
    do {                                                                    \
376
        const char *my_mon[] = { __VA_ARGS__, NULL};                        \
377 378
        const char *name = file " " #ACTION " " dev;                        \
        data.action = ACTION;                                               \
379 380 381 382 383
        data.domain_filename = file;                                        \
        data.device_filename = dev;                                         \
        data.fail = fial;                                                   \
        data.mon = my_mon;                                                  \
        data.keep = kep;                                                    \
384
        data.deviceDeletedEvent = event;                                    \
385
        if (virTestRun(name, testQemuHotplug, &data) < 0)                   \
386
            ret = -1;                                                       \
387
    } while (0)
388 389

#define DO_TEST_ATTACH(file, dev, fial, kep, ...)                           \
390
    DO_TEST(file, ATTACH, dev, false, fial, kep, __VA_ARGS__)
391 392

#define DO_TEST_DETACH(file, dev, fial, kep, ...)                           \
393 394 395 396 397 398 399
    DO_TEST(file, DETACH, dev, false, fial, kep, __VA_ARGS__)

#define DO_TEST_ATTACH_EVENT(file, dev, fial, kep, ...)                     \
    DO_TEST(file, ATTACH, dev, true, fial, kep, __VA_ARGS__)

#define DO_TEST_DETACH_EVENT(file, dev, fial, kep, ...)                     \
    DO_TEST(file, DETACH, dev, true, fial, kep, __VA_ARGS__)
400 401

#define DO_TEST_UPDATE(file, dev, fial, kep, ...)                           \
402
    DO_TEST(file, UPDATE, dev, false, fial, kep, __VA_ARGS__)
403

404 405

#define QMP_OK      "{\"return\": {}}"
406
#define HMP(msg)    "{\"return\": \"" msg "\"}"
407

408 409 410 411 412 413 414 415 416 417 418 419 420
#define QMP_DEVICE_DELETED(dev) \
    "{"                                                     \
    "    \"timestamp\": {"                                  \
    "        \"seconds\": 1374137171,"                      \
    "        \"microseconds\": 2659"                        \
    "    },"                                                \
    "    \"event\": \"DEVICE_DELETED\","                    \
    "    \"data\": {"                                       \
    "        \"device\": \"" dev "\","                      \
    "        \"path\": \"/machine/peripheral/" dev "\""     \
    "    }"                                                 \
    "}\r\n"

421 422
    DO_TEST_UPDATE("graphics-spice", "graphics-spice-nochange", false, false, NULL);
    DO_TEST_UPDATE("graphics-spice-timeout", "graphics-spice-timeout-nochange", false, false,
423
                   "set_password", QMP_OK, "expire_password", QMP_OK);
424
    DO_TEST_UPDATE("graphics-spice-timeout", "graphics-spice-timeout-password", false, false,
425
                   "set_password", QMP_OK, "expire_password", QMP_OK);
426
    DO_TEST_UPDATE("graphics-spice", "graphics-spice-listen", true, false, NULL);
427
    DO_TEST_UPDATE("graphics-spice-listen-network", "graphics-spice-listen-network-password", false, false,
428
                   "set_password", QMP_OK, "expire_password", QMP_OK);
429 430 431
    /* Strange huh? Currently, only graphics can be updated :-P */
    DO_TEST_UPDATE("disk-cdrom", "disk-cdrom-nochange", true, false, NULL);

432
    DO_TEST_ATTACH("console-compat-2-live", "console-virtio", false, true,
433
                   "chardev-add", "{\"return\": {\"pty\": \"/dev/pts/26\"}}",
434
                   "device_add", QMP_OK);
435

436
    DO_TEST_DETACH("console-compat-2-live", "console-virtio", false, false,
437 438
                   "device_del", QMP_OK,
                   "chardev-remove", QMP_OK);
439

440
    DO_TEST_ATTACH("base-live", "disk-virtio", false, true,
441 442
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);
443
    DO_TEST_DETACH("base-live", "disk-virtio", false, false,
444 445 446
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

447
    DO_TEST_ATTACH_EVENT("base-live", "disk-virtio", false, true,
448 449
                         "human-monitor-command", HMP("OK\\r\\n"),
                         "device_add", QMP_OK);
450
    DO_TEST_DETACH("base-live", "disk-virtio", true, true,
451 452
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));
453
    DO_TEST_DETACH("base-live", "disk-virtio", false, false,
454 455 456
                   "device_del", QMP_DEVICE_DELETED("virtio-disk4") QMP_OK,
                   "human-monitor-command", HMP(""));

457
    DO_TEST_ATTACH("base-live", "disk-usb", false, true,
458 459
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);
460
    DO_TEST_DETACH("base-live", "disk-usb", false, false,
461 462 463
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

464
    DO_TEST_ATTACH_EVENT("base-live", "disk-usb", false, true,
465 466
                         "human-monitor-command", HMP("OK\\r\\n"),
                         "device_add", QMP_OK);
467
    DO_TEST_DETACH("base-live", "disk-usb", true, true,
468 469
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));
470
    DO_TEST_DETACH("base-live", "disk-usb", false, false,
471 472 473
                   "device_del", QMP_DEVICE_DELETED("usb-disk16") QMP_OK,
                   "human-monitor-command", HMP(""));

474
    DO_TEST_ATTACH("base-live", "disk-scsi", false, true,
475 476
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);
477
    DO_TEST_DETACH("base-live", "disk-scsi", false, false,
478 479 480
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

481
    DO_TEST_ATTACH_EVENT("base-live", "disk-scsi", false, true,
482 483
                         "human-monitor-command", HMP("OK\\r\\n"),
                         "device_add", QMP_OK);
484
    DO_TEST_DETACH("base-live", "disk-scsi", true, true,
485 486
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));
487
    DO_TEST_DETACH("base-live", "disk-scsi", false, false,
488 489 490
                   "device_del", QMP_DEVICE_DELETED("scsi0-0-0-5") QMP_OK,
                   "human-monitor-command", HMP(""));

491
    DO_TEST_ATTACH("base-without-scsi-controller-live", "disk-scsi-2", false, true,
492 493 494 495 496 497 498 499
                   /* Four controllers added */
                   "device_add", QMP_OK,
                   "device_add", QMP_OK,
                   "device_add", QMP_OK,
                   "device_add", QMP_OK,
                   "human-monitor-command", HMP("OK\\r\\n"),
                   /* Disk added */
                   "device_add", QMP_OK);
500
    DO_TEST_DETACH("base-with-scsi-controller-live", "disk-scsi-2", false, false,
501 502 503
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

504
    DO_TEST_ATTACH_EVENT("base-without-scsi-controller-live", "disk-scsi-2", false, true,
505 506 507 508 509 510 511 512
                         /* Four controllers added */
                         "device_add", QMP_OK,
                         "device_add", QMP_OK,
                         "device_add", QMP_OK,
                         "device_add", QMP_OK,
                         "human-monitor-command", HMP("OK\\r\\n"),
                         /* Disk added */
                         "device_add", QMP_OK);
513
    DO_TEST_DETACH("base-with-scsi-controller-live", "disk-scsi-2", true, true,
514 515
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));
516
    DO_TEST_DETACH("base-with-scsi-controller-live", "disk-scsi-2", false, false,
517 518 519
                   "device_del", QMP_DEVICE_DELETED("scsi3-0-5-7") QMP_OK,
                   "human-monitor-command", HMP(""));

520
    DO_TEST_ATTACH("base-live", "qemu-agent", false, true,
521 522
                   "chardev-add", QMP_OK,
                   "device_add", QMP_OK);
523
    DO_TEST_DETACH("base-live", "qemu-agent-detach", false, false,
524 525 526
                   "device_del", QMP_OK,
                   "chardev-remove", QMP_OK);

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    DO_TEST_ATTACH("base-ccw-live", "ccw-virtio", false, true,
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);
    DO_TEST_DETACH("base-ccw-live", "ccw-virtio", false, false,
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

    DO_TEST_ATTACH("base-ccw-live-with-ccw-virtio", "ccw-virtio-2", false, true,
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);

    DO_TEST_DETACH("base-ccw-live-with-ccw-virtio", "ccw-virtio-2", false, false,
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

    DO_TEST_ATTACH("base-ccw-live-with-ccw-virtio", "ccw-virtio-2-explicit", false, true,
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);

    DO_TEST_DETACH("base-ccw-live-with-ccw-virtio", "ccw-virtio-2-explicit", false, false,
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

    /* Attach a second device, then detach the first one. Then attach the first one again. */
    DO_TEST_ATTACH("base-ccw-live-with-ccw-virtio", "ccw-virtio-2-explicit", false, true,
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);

    DO_TEST_DETACH("base-ccw-live-with-2-ccw-virtio", "ccw-virtio-1-explicit", false, true,
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

    DO_TEST_ATTACH("base-ccw-live-with-2-ccw-virtio", "ccw-virtio-1-reverse", false, false,
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);

563
    qemuTestDriverFree(&driver);
564 565 566 567
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)