qemuhotplugtest.c 16.8 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) < 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
    ret = 0;
98
 cleanup:
99 100 101
    return ret;
}

102 103 104 105 106 107 108
static int
testQemuHotplugAttach(virDomainObjPtr vm,
                      virDomainDeviceDefPtr dev)
{
    int ret = -1;

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

    return ret;
}

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

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

    return ret;
}

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

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

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

    VIR_FREE(actual);
    return ret;
}

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

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

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

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

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

256
    if (test->action == ATTACH)
257
        device_parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
258

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

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

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

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

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

    case UPDATE:
        ret = testQemuHotplugUpdate(vm, dev);
312 313
    }

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

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

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

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

    virEventRegisterDefaultImpl();

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

359
    if (!(driver.domainEventState = virObjectEventStateNew()))
360 361
        return EXIT_FAILURE;

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

368 369 370 371
    /* wait only 100ms for DEVICE_DELETED event */
    qemuDomainRemoveDeviceWaitTime = 100;

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

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

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

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

401 402

#define QMP_OK      "{\"return\": {}}"
403
#define HMP(msg)    "{\"return\": \"" msg "\"}"
404

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

418 419
    DO_TEST_UPDATE("graphics-spice", "graphics-spice-nochange", false, false, NULL);
    DO_TEST_UPDATE("graphics-spice-timeout", "graphics-spice-timeout-nochange", false, false,
420
                   "set_password", QMP_OK, "expire_password", QMP_OK);
421
    DO_TEST_UPDATE("graphics-spice-timeout", "graphics-spice-timeout-password", false, false,
422
                   "set_password", QMP_OK, "expire_password", QMP_OK);
423 424
    DO_TEST_UPDATE("graphics-spice", "graphics-spice-listen", true, false, NULL);
    DO_TEST_UPDATE("graphics-spice-listen-network", "graphics-spice-listen-network", false, false,
425
                   "set_password", QMP_OK, "expire_password", QMP_OK);
426 427 428 429 430
    /* 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\"}}",
431
                   "device_add", QMP_OK);
432 433

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

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

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

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    DO_TEST_ATTACH("hotplug-base", "disk-usb", false, true,
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);
    DO_TEST_DETACH("hotplug-base", "disk-usb", false, false,
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

    DO_TEST_ATTACH_EVENT("hotplug-base", "disk-usb", false, true,
                         "human-monitor-command", HMP("OK\\r\\n"),
                         "device_add", QMP_OK);
    DO_TEST_DETACH("hotplug-base", "disk-usb", true, true,
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));
    DO_TEST_DETACH("hotplug-base", "disk-usb", false, false,
                   "device_del", QMP_DEVICE_DELETED("usb-disk16") QMP_OK,
                   "human-monitor-command", HMP(""));

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    DO_TEST_ATTACH("hotplug-base", "disk-scsi", false, true,
                   "human-monitor-command", HMP("OK\\r\\n"),
                   "device_add", QMP_OK);
    DO_TEST_DETACH("hotplug-base", "disk-scsi", false, false,
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));

    DO_TEST_ATTACH_EVENT("hotplug-base", "disk-scsi", false, true,
                         "human-monitor-command", HMP("OK\\r\\n"),
                         "device_add", QMP_OK);
    DO_TEST_DETACH("hotplug-base", "disk-scsi", true, true,
                   "device_del", QMP_OK,
                   "human-monitor-command", HMP(""));
    DO_TEST_DETACH("hotplug-base", "disk-scsi", false, false,
                   "device_del", QMP_DEVICE_DELETED("scsi0-0-0-5") QMP_OK,
                   "human-monitor-command", HMP(""));

488
    qemuTestDriverFree(&driver);
489 490 491 492
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)