qemuhotplugtest.c 17.3 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 77
    if (event)
        virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_DEVICE_DEL_EVENT);
78

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

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

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

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

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

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

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

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

    switch (dev->type) {
112 113 114 115 116 117
    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;
118 119 120 121
    case VIR_DOMAIN_DEVICE_CHR:
        ret = qemuDomainAttachChrDevice(&driver, vm, dev->data.chr);
        break;
    default:
122 123
        VIR_TEST_VERBOSE("device type '%s' cannot be attached\n",
                virDomainDeviceTypeToString(dev->type));
124 125 126 127 128 129 130 131 132 133 134 135 136
        break;
    }

    return ret;
}

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

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

    return ret;
}

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

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

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

    VIR_FREE(actual);
    return ret;
}

207 208 209 210 211 212 213
static int
testQemuHotplug(const void *data)
{
    int ret = -1;
    struct qemuHotplugTestData *test = (struct qemuHotplugTestData *) data;
    char *domain_filename = NULL;
    char *device_filename = NULL;
214 215
    char *result_filename = NULL;
    char *domain_xml = NULL;
216
    char *device_xml = NULL;
217
    char *result_xml = NULL;
218 219
    const char *const *tmp;
    bool fail = test->fail;
220
    bool keep = test->keep;
221
    unsigned int device_parse_flags = 0;
222 223 224 225 226 227 228 229 230
    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",
231 232 233 234 235 236 237
                    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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    virEventRegisterDefaultImpl();

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

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

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

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

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

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

#define DO_TEST_DETACH(file, dev, fial, kep, ...)                           \
392 393 394 395 396 397 398
    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__)
399 400

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

403 404

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

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

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

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

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

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

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

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

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

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

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

490 491 492 493 494 495 496
    DO_TEST_ATTACH("hotplug-base-live", "qemu-agent", false, true,
                   "chardev-add", QMP_OK,
                   "device_add", QMP_OK);
    DO_TEST_DETACH("hotplug-base-live", "qemu-agent-detach", false, false,
                   "device_del", QMP_OK,
                   "chardev-remove", QMP_OK);

497
    qemuTestDriverFree(&driver);
498 499 500 501
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)