qemumonitorjsontest.c 111.1 KB
Newer Older
1
/*
E
Eric Blake 已提交
2
 * Copyright (C) 2011-2014 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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
15
 * License along with this library.  If not, see
16 17 18 19 20 21 22 23
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "testutils.h"
#include "testutilsqemu.h"
24
#include "testutilsqemuschema.h"
25
#include "qemumonitortestutils.h"
26
#include "qemu/qemu_domain.h"
27
#include "qemu/qemu_block.h"
28
#include "qemu/qemu_monitor_json.h"
29
#include "qemu/qemu_qapi.h"
30
#include "virthread.h"
31
#include "virerror.h"
32
#include "virstring.h"
33
#include "cpu/cpu.h"
34
#include "qemu/qemu_monitor.h"
35
#include "qemu/qemu_migration_params.h"
36
#define LIBVIRT_QEMU_MIGRATION_PARAMSPRIV_H_ALLOW
37
#include "qemu/qemu_migration_paramspriv.h"
38 39 40

#define VIR_FROM_THIS VIR_FROM_NONE

41 42 43 44 45 46 47
typedef struct _testQemuMonitorJSONSimpleFuncData testQemuMonitorJSONSimpleFuncData;
typedef testQemuMonitorJSONSimpleFuncData *testQemuMonitorJSONSimpleFuncDataPtr;
struct _testQemuMonitorJSONSimpleFuncData {
    const char *cmd;
    int (* func) (qemuMonitorPtr mon);
    virDomainXMLOptionPtr xmlopt;
    const char *reply;
48
    virHashTablePtr schema;
49 50
};

51 52 53 54 55 56
typedef struct _testGenericData testGenericData;
struct _testGenericData {
    virDomainXMLOptionPtr xmlopt;
    virHashTablePtr schema;
};

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
const char *queryBlockReply =
"{"
"    \"return\": ["
"        {"
"            \"io-status\": \"ok\","
"            \"device\": \"drive-virtio-disk0\","
"            \"locked\": false,"
"            \"removable\": false,"
"            \"inserted\": {"
"                \"iops_rd\": 5,"
"                \"iops_wr\": 6,"
"                \"ro\": false,"
"                \"backing_file_depth\": 0,"
"                \"drv\": \"qcow2\","
"                \"iops\": 4,"
"                \"bps_wr\": 3,"
"                \"encrypted\": false,"
"                \"bps\": 1,"
"                \"bps_rd\": 2,"
76 77 78 79 80 81 82
"                \"bps_max\": 7,"
"                \"iops_max\": 10,"
"                \"bps_rd_max\": 8,"
"                \"bps_wr_max\": 9,"
"                \"iops_rd_max\": 11,"
"                \"iops_wr_max\": 12,"
"                \"iops_size\": 13,"
83 84 85 86 87 88 89
"                \"group\": \"group14\","
"                \"bps_max_length\": 15,"
"                \"bps_rd_max_length\": 16,"
"                \"bps_wr_max_length\": 17,"
"                \"iops_max_length\": 18,"
"                \"iops_rd_max_length\": 19,"
"                \"iops_wr_max_length\": 20,"
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
"                \"file\": \"/home/zippy/work/tmp/gentoo.qcow2\","
"                \"encryption_key_missing\": false"
"            },"
"            \"type\": \"unknown\""
"        },"
"        {"
"            \"io-status\": \"ok\","
"            \"device\": \"drive-virtio-disk1\","
"            \"locked\": false,"
"            \"removable\": false,"
"            \"inserted\": {"
"                \"iops_rd\": 0,"
"                \"iops_wr\": 0,"
"                \"ro\": false,"
"                \"backing_file_depth\": 0,"
"                \"drv\": \"raw\","
"                \"iops\": 0,"
"                \"bps_wr\": 0,"
"                \"encrypted\": false,"
"                \"bps\": 0,"
"                \"bps_rd\": 0,"
"                \"file\": \"/home/zippy/test.bin\","
"                \"encryption_key_missing\": false"
"            },"
"            \"type\": \"unknown\""
"        },"
"        {"
"            \"io-status\": \"ok\","
"            \"device\": \"drive-ide0-1-0\","
"            \"locked\": true,"
"            \"removable\": true,"
"            \"inserted\": {"
"                \"iops_rd\": 0,"
"                \"iops_wr\": 0,"
"                \"ro\": true,"
"                \"backing_file_depth\": 0,"
"                \"drv\": \"raw\","
"                \"iops\": 0,"
"                \"bps_wr\": 0,"
"                \"encrypted\": false,"
"                \"bps\": 0,"
"                \"bps_rd\": 0,"
"                \"file\": \"/home/zippy/tmp/install-amd64-minimal-20121210.iso\","
"                \"encryption_key_missing\": false"
"            },"
"            \"tray_open\": false,"
"            \"type\": \"unknown\""
137 138 139 140 141 142 143 144
"        },"
"        {"
"            \"io-status\": \"ok\","
"            \"device\": \"drive-ide0-1-1\","
"            \"locked\": false,"
"            \"removable\": true,"
"            \"tray_open\": false,"
"            \"type\": \"unknown\""
145 146 147 148 149
"        }"
"    ],"
"    \"id\": \"libvirt-10\""
"}";

150
static int
151
testQemuMonitorJSONGetStatus(const void *opaque)
152
{
153 154
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
155 156
    bool running = false;
    virDomainPausedReason reason = 0;
157
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
158

159
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
160 161 162 163 164 165 166 167 168 169
        return -1;

    if (qemuMonitorTestAddItem(test, "query-status",
                               "{ "
                               "    \"return\": { "
                               "        \"status\": \"running\", "
                               "        \"singlestep\": false, "
                               "        \"running\": true "
                               "    } "
                               "}") < 0)
170
        return -1;
171 172 173 174 175 176 177
    if (qemuMonitorTestAddItem(test, "query-status",
                               "{ "
                               "    \"return\": { "
                               "        \"singlestep\": false, "
                               "        \"running\": false "
                               "    } "
                               "}") < 0)
178
        return -1;
179 180 181 182 183 184 185 186
    if (qemuMonitorTestAddItem(test, "query-status",
                               "{ "
                               "    \"return\": { "
                               "        \"status\": \"inmigrate\", "
                               "        \"singlestep\": false, "
                               "        \"running\": false "
                               "    } "
                               "}") < 0)
187
        return -1;
188 189 190

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
191
        return -1;
192 193 194 195

    if (!running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not true");
196
        return -1;
197 198 199 200 201
    }

    if (reason != VIR_DOMAIN_PAUSED_UNKNOWN) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Reason was unexpectedly set to %d", reason);
202
        return -1;
203 204 205 206
    }

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
207
        return -1;
208 209 210 211

    if (running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not false");
212
        return -1;
213 214 215 216 217
    }

    if (reason != VIR_DOMAIN_PAUSED_UNKNOWN) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Reason was unexpectedly set to %d", reason);
218
        return -1;
219 220 221 222
    }

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
223
        return -1;
224 225 226 227

    if (running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not false");
228
        return -1;
229 230 231 232 233
    }

    if (reason != VIR_DOMAIN_PAUSED_MIGRATION) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Reason was unexpectedly set to %d", reason);
234
        return -1;
235 236
    }

237
    return 0;
238 239
}

240
static int
241
testQemuMonitorJSONGetVersion(const void *opaque)
242
{
243 244
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
245 246 247 248
    int ret = -1;
    int major;
    int minor;
    int micro;
249
    char *package = NULL;
250
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
251

252
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        return -1;

    if (qemuMonitorTestAddItem(test, "query-version",
                               "{ "
                               "  \"return\":{ "
                               "     \"qemu\":{ "
                               "        \"major\":1, "
                               "        \"minor\":2, "
                               "        \"micro\":3 "
                               "      },"
                               "     \"package\":\"\""
                               "  }"
                               "}") < 0)
        goto cleanup;

    if (qemuMonitorTestAddItem(test, "query-version",
                               "{ "
                               "  \"return\":{ "
                               "     \"qemu\":{ "
                               "        \"major\":0, "
                               "        \"minor\":11, "
                               "        \"micro\":6 "
                               "      },"
                               "     \"package\":\"2.283.el6\""
                               "  }"
                               "}") < 0)
        goto cleanup;

    if (qemuMonitorGetVersion(qemuMonitorTestGetMonitor(test),
                              &major, &minor, &micro,
                              &package) < 0)
        goto cleanup;

    if (major != 1) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Major %d was not 1", major);
        goto cleanup;
    }
    if (minor != 2) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Minor %d was not 2", major);
        goto cleanup;
    }
    if (micro != 3) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Micro %d was not 3", major);
        goto cleanup;
    }

    if (STRNEQ(package, "")) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Package %s was not ''", package);
        goto cleanup;
    }
307
    VIR_FREE(package);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

    if (qemuMonitorGetVersion(qemuMonitorTestGetMonitor(test),
                              &major, &minor, &micro,
                              &package) < 0)
        goto cleanup;

    if (major != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Major %d was not 0", major);
        goto cleanup;
    }
    if (minor != 11) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Minor %d was not 11", major);
        goto cleanup;
    }
    if (micro != 6) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Micro %d was not 6", major);
        goto cleanup;
    }

    if (STRNEQ(package, "2.283.el6")) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Package %s was not '2.283.el6'", package);
        goto cleanup;
    }

    ret = 0;

338
 cleanup:
339
    VIR_FREE(package);
340 341 342
    return ret;
}

343
static int
344
testQemuMonitorJSONGetMachines(const void *opaque)
345
{
346 347
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
348 349
    int ret = -1;
    qemuMonitorMachineInfoPtr *info;
350
    int ninfo = 0;
351
    const char *null = NULL;
352
    size_t i;
353

354 355 356
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;

    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        return -1;

    if (qemuMonitorTestAddItem(test, "query-machines",
                               "{ "
                               "  \"return\": [ "
                               "   { "
                               "     \"name\": \"pc-1.0\" "
                               "   }, "
                               "   { "
                               "     \"name\": \"pc-1.1\" "
                               "   }, "
                               "   { "
                               "     \"name\": \"pc-1.2\", "
                               "     \"is-default\": true, "
                               "     \"alias\": \"pc\" "
                               "   } "
                               "  ]"
                               "}") < 0)
        goto cleanup;

    if ((ninfo = qemuMonitorGetMachines(qemuMonitorTestGetMonitor(test),
                                        &info)) < 0)
        goto cleanup;

    if (ninfo != 3) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "ninfo %d is not 3", ninfo);
        goto cleanup;
    }

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
#define CHECK(i, wantname, wantisDefault, wantalias) \
    do { \
        if (STRNEQ(info[i]->name, (wantname))) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "name %s is not %s", \
                           info[i]->name, (wantname)); \
            goto cleanup; \
        } \
        if (info[i]->isDefault != (wantisDefault)) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "isDefault %d is not %d", \
                           info[i]->isDefault, (wantisDefault)); \
            goto cleanup; \
        } \
        if (STRNEQ_NULLABLE(info[i]->alias, (wantalias))) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "alias %s is not %s", \
                           info[i]->alias, NULLSTR(wantalias)); \
            goto cleanup; \
        } \
407 408 409 410 411 412 413 414 415 416
    } while (0)

    CHECK(0, "pc-1.0", false, null);
    CHECK(1, "pc-1.1", false, null);
    CHECK(2, "pc-1.2", true, "pc");

#undef CHECK

    ret = 0;

417
 cleanup:
418 419 420 421
    for (i = 0; i < ninfo; i++)
        qemuMonitorMachineInfoFree(info[i]);
    VIR_FREE(info);

422 423 424 425
    return ret;
}


426
static int
427
testQemuMonitorJSONGetCPUDefinitions(const void *opaque)
428
{
429 430
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
431
    int ret = -1;
432
    qemuMonitorCPUDefInfoPtr *cpus = NULL;
433
    int ncpus = 0;
434
    size_t i;
435
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
436

437
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
438 439 440 441 442 443 444 445 446
        return -1;

    if (qemuMonitorTestAddItem(test, "query-cpu-definitions",
                               "{ "
                               "  \"return\": [ "
                               "   { "
                               "     \"name\": \"qemu64\" "
                               "   }, "
                               "   { "
447 448
                               "     \"name\": \"Opteron_G4\", "
                               "     \"unavailable-features\": [\"vme\"]"
449 450
                               "   }, "
                               "   { "
451 452
                               "     \"name\": \"Westmere\", "
                               "     \"unavailable-features\": []"
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
                               "   } "
                               "  ]"
                               "}") < 0)
        goto cleanup;

    if ((ncpus = qemuMonitorGetCPUDefinitions(qemuMonitorTestGetMonitor(test),
                                              &cpus)) < 0)
        goto cleanup;

    if (ncpus != 3) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "ncpus %d is not 3", ncpus);
        goto cleanup;
    }

468 469 470 471 472 473 474 475 476 477 478 479 480 481
#define CHECK_FULL(i, wantname, Usable) \
    do { \
        if (STRNEQ(cpus[i]->name, (wantname))) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "name %s is not %s", \
                           cpus[i]->name, (wantname)); \
            goto cleanup; \
        } \
        if (cpus[i]->usable != (Usable)) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "%s: expecting usable flag %d, got %d", \
                           cpus[i]->name, Usable, cpus[i]->usable); \
            goto cleanup; \
        } \
482 483
    } while (0)

484
#define CHECK(i, wantname) \
485 486
    CHECK_FULL(i, wantname, VIR_TRISTATE_BOOL_ABSENT)

487 488
#define CHECK_USABLE(i, wantname, usable) \
    CHECK_FULL(i, wantname, \
489 490
               usable ? VIR_TRISTATE_BOOL_YES : VIR_TRISTATE_BOOL_NO)

491
    CHECK(0, "qemu64");
492 493
    CHECK_USABLE(1, "Opteron_G4", false);
    CHECK_USABLE(2, "Westmere", true);
494 495

#undef CHECK
496 497
#undef CHECK_USABLE
#undef CHECK_FULL
498 499 500

    ret = 0;

501
 cleanup:
502
    for (i = 0; i < ncpus; i++)
503
        qemuMonitorCPUDefInfoFree(cpus[i]);
504
    VIR_FREE(cpus);
505 506 507 508
    return ret;
}


509
static int
510
testQemuMonitorJSONGetCommands(const void *opaque)
511
{
512 513
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
514 515
    int ret = -1;
    char **commands = NULL;
516
    int ncommands = 0;
517
    size_t i;
518
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
519

520
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
        return -1;

    if (qemuMonitorTestAddItem(test, "query-commands",
                               "{ "
                               "  \"return\": [ "
                               "   { "
                               "     \"name\": \"system_wakeup\" "
                               "   }, "
                               "   { "
                               "     \"name\": \"cont\" "
                               "   }, "
                               "   { "
                               "     \"name\": \"quit\" "
                               "   } "
                               "  ]"
                               "}") < 0)
        goto cleanup;

    if ((ncommands = qemuMonitorGetCommands(qemuMonitorTestGetMonitor(test),
                                        &commands)) < 0)
        goto cleanup;

    if (ncommands != 3) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "ncommands %d is not 3", ncommands);
        goto cleanup;
    }

549 550 551 552 553 554 555 556
#define CHECK(i, wantname) \
    do { \
        if (STRNEQ(commands[i], (wantname))) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "name %s is not %s", \
                           commands[i], (wantname)); \
            goto cleanup; \
        } \
557 558 559 560 561 562 563 564 565
    } while (0)

    CHECK(0, "system_wakeup");
    CHECK(1, "cont");
    CHECK(2, "quit");

#undef CHECK
    ret = 0;

566
 cleanup:
567 568 569
    for (i = 0; i < ncommands; i++)
        VIR_FREE(commands[i]);
    VIR_FREE(commands);
570 571 572 573
    return ret;
}


574
static int
575
testQemuMonitorJSONGetTPMModels(const void *opaque)
576
{
577 578
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
579 580 581
    int ret = -1;
    char **tpmmodels = NULL;
    int ntpmmodels = 0;
582
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
583

584
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
        return -1;

    if (qemuMonitorTestAddItem(test, "query-tpm-models",
                               "{ "
                               "  \"return\": [ "
                               "  \"passthrough\""
                               "  ]"
                               "}") < 0)
        goto cleanup;

    if ((ntpmmodels = qemuMonitorGetTPMModels(qemuMonitorTestGetMonitor(test),
                                              &tpmmodels)) < 0)
        goto cleanup;

    if (ntpmmodels != 1) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "ntpmmodels %d is not 1", ntpmmodels);
        goto cleanup;
    }

605 606 607 608 609 610 611 612
#define CHECK(i, wantname) \
    do { \
        if (STRNEQ(tpmmodels[i], (wantname))) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "name %s is not %s", \
                           tpmmodels[i], (wantname)); \
            goto cleanup; \
        } \
613 614 615 616 617 618 619 620
    } while (0)

    CHECK(0, "passthrough");

#undef CHECK

    ret = 0;

621
 cleanup:
622
    virStringListFree(tpmmodels);
623 624 625 626
    return ret;
}


627
static int
628
testQemuMonitorJSONGetCommandLineOptionParameters(const void *opaque)
629
{
630 631
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
632 633 634
    int ret = -1;
    char **params = NULL;
    int nparams = 0;
635
    bool found = false;
636
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
637

638
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
        return -1;

    if (qemuMonitorTestAddItem(test, "query-command-line-options",
                               "{ "
                               "  \"return\": [ "
                               "  {\"parameters\": [], \"option\": \"acpi\" },"
                               "  {\"parameters\": ["
                               "    {\"name\": \"romfile\", "
                               "     \"type\": \"string\"}, "
                               "    {\"name\": \"bootindex\", "
                               "     \"type\": \"number\"}], "
                               "   \"option\": \"option-rom\"}"
                               "  ]"
                               "}") < 0)
        goto cleanup;

    /* present with params */
    if ((nparams = qemuMonitorGetCommandLineOptionParameters(qemuMonitorTestGetMonitor(test),
                                                             "option-rom",
658 659
                                                             &params,
                                                             NULL)) < 0)
660 661 662 663 664 665 666 667
        goto cleanup;

    if (nparams != 2) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "nparams was %d, expected 2", nparams);
        goto cleanup;
    }

668 669 670 671 672 673 674 675
#define CHECK(i, wantname) \
    do { \
        if (STRNEQ(params[i], (wantname))) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "name was %s, expected %s", \
                           params[i], (wantname)); \
            goto cleanup; \
        } \
676 677 678 679 680 681 682
    } while (0)

    CHECK(0, "romfile");
    CHECK(1, "bootindex");

#undef CHECK

683
    virStringListFree(params);
684 685 686 687 688
    params = NULL;

    /* present but empty */
    if ((nparams = qemuMonitorGetCommandLineOptionParameters(qemuMonitorTestGetMonitor(test),
                                                             "acpi",
689 690
                                                             &params,
                                                             &found)) < 0)
691 692 693 694 695 696 697
        goto cleanup;

    if (nparams != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "nparams was %d, expected 0", nparams);
        goto cleanup;
    }
698 699 700 701 702
    if (!found) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "found was false, expected true");
        goto cleanup;
    }
703 704 705 706 707 708
    if (params && params[0]) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "unexpected array contents");
        goto cleanup;
    }

709
    virStringListFree(params);
710 711 712 713 714
    params = NULL;

    /* no such option */
    if ((nparams = qemuMonitorGetCommandLineOptionParameters(qemuMonitorTestGetMonitor(test),
                                                             "foobar",
715 716
                                                             &params,
                                                             &found)) < 0)
717 718 719 720 721 722 723
        goto cleanup;

    if (nparams != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "nparams was %d, expected 0", nparams);
        goto cleanup;
    }
724 725 726 727 728
    if (found) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "found was true, expected false");
        goto cleanup;
    }
729 730 731 732 733 734 735 736
    if (params && params[0]) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "unexpected array contents");
        goto cleanup;
    }

    ret = 0;

737
 cleanup:
738
    virStringListFree(params);
739 740 741
    return ret;
}

742 743 744 745 746 747 748 749

struct qemuMonitorJSONTestAttachChardevData {
    qemuMonitorTestPtr test;
    virDomainChrSourceDefPtr chr;
    const char *expectPty;
    bool fail;
};

750
static int
751
testQemuMonitorJSONAttachChardev(const void *opaque)
752
{
753 754
    const struct qemuMonitorJSONTestAttachChardevData *data = opaque;
    int rc;
755

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
    if ((rc = qemuMonitorAttachCharDev(qemuMonitorTestGetMonitor(data->test),
                                       "alias", data->chr)) < 0)
        goto cleanup;

    if (data->chr->type == VIR_DOMAIN_CHR_TYPE_PTY) {
        if (STRNEQ_NULLABLE(data->expectPty, data->chr->data.file.path)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "expected PTY path: %s got: %s",
                           NULLSTR(data->expectPty),
                           NULLSTR(data->chr->data.file.path));
            rc = -1;
        }

        VIR_FREE(data->chr->data.file.path);
    }

 cleanup:
    if ((rc != 0) != data->fail)
774
        return -1;
775 776 777
    else
        return 0;
}
778 779


780 781
static int
qemuMonitorJSONTestAttachOneChardev(virDomainXMLOptionPtr xmlopt,
782
                                    virHashTablePtr schema,
783 784
                                    const char *label,
                                    virDomainChrSourceDefPtr chr,
785
                                    const char *expectargs,
786 787 788
                                    const char *reply,
                                    const char *expectPty,
                                    bool fail)
789

790
{
J
John Ferlan 已提交
791
    struct qemuMonitorJSONTestAttachChardevData data = {0};
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
    char *jsonreply = NULL;
    char *fulllabel = NULL;
    int ret = -1;

    if (!reply)
        reply = "";

    if (virAsprintf(&jsonreply, "{\"return\": {%s}}", reply) < 0)
        goto cleanup;

    if (virAsprintf(&fulllabel, "qemuMonitorJSONTestAttachChardev(%s)", label) < 0)
        goto cleanup;

    data.chr = chr;
    data.fail = fail;
    data.expectPty = expectPty;
808
    if (!(data.test = qemuMonitorTestNewSchema(xmlopt, schema)))
809 810
        goto cleanup;

811 812
    if (qemuMonitorTestAddItemExpect(data.test, "chardev-add",
                                     expectargs, true, jsonreply) < 0)
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        goto cleanup;

    if (virTestRun(fulllabel, &testQemuMonitorJSONAttachChardev, &data) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    qemuMonitorTestFree(data.test);
    VIR_FREE(jsonreply);
    VIR_FREE(fulllabel);
    return ret;
}

static int
828 829
qemuMonitorJSONTestAttachChardev(virDomainXMLOptionPtr xmlopt,
                                 virHashTablePtr schema)
830 831 832 833
{
    virDomainChrSourceDef chr;
    int ret = 0;

834
#define CHECK(label, fail, expectargs) \
835 836
    if (qemuMonitorJSONTestAttachOneChardev(xmlopt, schema, label, &chr, \
                                            expectargs, NULL, NULL, fail) < 0) \
837
        ret = -1
838 839

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_NULL };
840 841
    CHECK("null", false,
          "{'id':'alias','backend':{'type':'null','data':{}}}");
842

843
    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_VC };
844 845
    CHECK("vc", false,
          "{'id':'alias','backend':{'type':'null','data':{}}}");
846 847

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_PTY };
848
    if (qemuMonitorJSONTestAttachOneChardev(xmlopt, schema, "pty", &chr,
849 850 851
                                            "{'id':'alias',"
                                             "'backend':{'type':'pty',"
                                                        "'data':{}}}",
852 853
                                            "\"pty\" : \"/dev/pts/0\"",
                                            "/dev/pts/0", false) < 0)
854 855 856
        ret = -1;

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_PTY };
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
    CHECK("pty missing path", true,
          "{'id':'alias','backend':{'type':'pty','data':{}}}");

    memset(&chr, 0, sizeof(chr));
    chr.type = VIR_DOMAIN_CHR_TYPE_FILE;
    chr.data.file.path = (char *) "/test/path";
    CHECK("file", false,
          "{'id':'alias','backend':{'type':'file','data':{'out':'/test/path'}}}");

    memset(&chr, 0, sizeof(chr));
    chr.type = VIR_DOMAIN_CHR_TYPE_DEV;
    chr.data.file.path = (char *) "/test/path";
    CHECK("device", false,
          "{'id':'alias','backend':{'type':'serial','data':{'device':'/test/path'}}}");

    memset(&chr, 0, sizeof(chr));
    chr.type = VIR_DOMAIN_CHR_TYPE_TCP;
    chr.data.tcp.host = (char *) "example.com";
    chr.data.tcp.service = (char *) "1234";
    CHECK("tcp", false,
          "{'id':'alias',"
           "'backend':{'type':'socket',"
                      "'data':{'addr':{'type':'inet',"
                                      "'data':{'host':'example.com',"
                                              "'port':'1234'}},"
                              "'telnet':false,"
                              "'server':false}}}");

    memset(&chr, 0, sizeof(chr));
    chr.type = VIR_DOMAIN_CHR_TYPE_UDP;
    chr.data.udp.connectHost = (char *) "example.com";
    chr.data.udp.connectService = (char *) "1234";
    CHECK("udp", false,
          "{'id':'alias',"
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
           "'backend':{'type':'udp',"
                      "'data':{'remote':{'type':'inet',"
                                        "'data':{'host':'example.com',"
                                                "'port':'1234'}}}}}");

    chr.data.udp.bindHost = (char *) "localhost";
    chr.data.udp.bindService = (char *) "4321";
    CHECK("udp", false,
          "{'id':'alias',"
           "'backend':{'type':'udp',"
                      "'data':{'remote':{'type':'inet',"
                                        "'data':{'host':'example.com',"
                                                "'port':'1234'}},"
                              "'local':{'type':'inet',"
                                       "'data':{'host':'localhost',"
                                               "'port':'4321'}}}}}");
907

908 909 910 911 912 913 914 915 916 917 918
    chr.data.udp.bindHost = NULL;
    chr.data.udp.bindService = (char *) "4321";
    CHECK("udp", false,
          "{'id':'alias',"
           "'backend':{'type':'udp',"
                      "'data':{'remote':{'type':'inet',"
                                        "'data':{'host':'example.com',"
                                                "'port':'1234'}},"
                              "'local':{'type':'inet',"
                                       "'data':{'host':'',"
                                               "'port':'4321'}}}}}");
919 920 921 922 923 924 925 926 927
    memset(&chr, 0, sizeof(chr));
    chr.type = VIR_DOMAIN_CHR_TYPE_UNIX;
    chr.data.nix.path = (char *) "/path/to/socket";
    CHECK("unix", false,
          "{'id':'alias',"
           "'backend':{'type':'socket',"
                      "'data':{'addr':{'type':'unix',"
                                      "'data':{'path':'/path/to/socket'}},"
                              "'server':false}}}");
928 929

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_SPICEVMC };
930 931 932
    CHECK("spicevmc", false,
          "{'id':'alias','backend':{'type':'spicevmc','"
                                    "data':{'type':'vdagent'}}}");
933 934

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_PIPE };
935
    CHECK("pipe", true, "");
936 937

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_STDIO };
938
    CHECK("stdio", true, "");
939 940 941 942
#undef CHECK

    return ret;
}
943

944

945
static int
946
testQemuMonitorJSONDetachChardev(const void *opaque)
947
{
948 949 950
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
951

952
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
953
        return -1;
954 955

    if (qemuMonitorTestAddItem(test, "chardev-remove", "{\"return\": {}}") < 0)
956
        return -1;
957 958 959

    if (qemuMonitorDetachCharDev(qemuMonitorTestGetMonitor(test),
                                 "dummy_chrID") < 0)
960
        return -1;
961

962
    return 0;
963 964
}

965 966 967 968 969 970 971 972 973 974
/*
 * This test will request to return a list of paths for "/". It should be
 * a simple list of 1 real element that being the "machine". The following
 * is the execution and expected return:
 *
 *  {"execute":"qom-list", "arguments": { "path": "/"}}"
 *  {"return": [{"name": "machine", "type": "child<container>"}, \
 *              {"name": "type", "type": "string"}]}
 */
static int
975
testQemuMonitorJSONGetListPaths(const void *opaque)
976
{
977 978
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
979 980 981 982
    int ret = -1;
    qemuMonitorJSONListPathPtr *paths;
    int npaths = 0;
    size_t i;
983
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
984

985
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
        return -1;

    if (qemuMonitorTestAddItem(test, "qom-list",
                               "{ "
                               "  \"return\": [ "
                               "  {\"name\": \"machine\", "
                               "   \"type\": \"child<container>\"}, "
                               "  {\"name\": \"type\", "
                               "   \"type\": \"string\"} "
                               " ]"
                               "}") < 0)
        goto cleanup;

    /* present with path */
    if ((npaths = qemuMonitorJSONGetObjectListPaths(
                                                qemuMonitorTestGetMonitor(test),
                                                "/",
                                                &paths)) < 0)
        goto cleanup;

    if (npaths != 2) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "npaths was %d, expected 1", npaths);
        goto cleanup;
    }

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
#define CHECK(i, wantname, wanttype) \
    do { \
        if (STRNEQ(paths[i]->name, (wantname))) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "name was %s, expected %s", \
                           paths[i]->name, (wantname)); \
            goto cleanup; \
        } \
        if (STRNEQ_NULLABLE(paths[i]->type, (wanttype))) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "type was %s, expected %s", \
                           NULLSTR(paths[i]->type), (wanttype)); \
            goto cleanup; \
        } \
1026 1027 1028 1029 1030 1031 1032 1033
    } while (0)

    CHECK(0, "machine", "child<container>");

#undef CHECK

    ret = 0;

1034
 cleanup:
1035 1036 1037 1038 1039 1040 1041
    for (i = 0; i < npaths; i++)
        qemuMonitorJSONListPathFree(paths[i]);
    VIR_FREE(paths);
    return ret;
}


1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
/*
 * This test will use a path to /machine/i440fx which should exist in order
 * to ensure that the qom-get property fetch works properly. The following
 * is the execution and expected return:
 *
 *
 *  { "execute": "qom-get","arguments": \
 *      { "path": "/machine/i440fx","property": "realized"}}
 *   {"return": true}
 */
static int
1053
testQemuMonitorJSONGetObjectProperty(const void *opaque)
1054
{
1055 1056
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1057
    qemuMonitorJSONObjectProperty prop;
1058
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1059

1060
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1061 1062 1063 1064
        return -1;

    if (qemuMonitorTestAddItem(test, "qom-get",
                               "{ \"return\": true }") < 0)
1065
        return -1;
1066 1067 1068 1069 1070 1071 1072 1073

    /* Present with path and property */
    memset(&prop, 0, sizeof(qemuMonitorJSONObjectProperty));
    prop.type = QEMU_MONITOR_OBJECT_PROPERTY_BOOLEAN;
    if (qemuMonitorJSONGetObjectProperty(qemuMonitorTestGetMonitor(test),
                                         "/machine/i440fx",
                                         "realized",
                                         &prop) < 0)
1074
        return -1;
1075 1076 1077 1078

    if (!prop.val.b) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "expected true, but false returned");
1079
        return -1;
1080 1081
    }

1082
    return 0;
1083 1084 1085
}


1086 1087 1088 1089 1090 1091 1092
/*
 * This test will use a path to /machine/i440fx which should exist in order
 * to ensure that the qom-set property set works properly. The test will
 * set a true property to true just as a proof of concept.  Setting it to
 * false is not a good idea...
 */
static int
1093
testQemuMonitorJSONSetObjectProperty(const void *opaque)
1094
{
1095 1096
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1097
    qemuMonitorJSONObjectProperty prop;
1098
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1099

1100
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1101 1102 1103 1104
        return -1;

    if (qemuMonitorTestAddItem(test, "qom-set",
                               "{ \"return\": {} }") < 0)
1105
        return -1;
1106 1107
    if (qemuMonitorTestAddItem(test, "qom-get",
                               "{ \"return\": true }") < 0)
1108
        return -1;
1109 1110 1111 1112 1113 1114 1115 1116 1117

    /* Let's attempt the setting */
    memset(&prop, 0, sizeof(qemuMonitorJSONObjectProperty));
    prop.type = QEMU_MONITOR_OBJECT_PROPERTY_BOOLEAN;
    prop.val.b = true;
    if (qemuMonitorJSONSetObjectProperty(qemuMonitorTestGetMonitor(test),
                                         "/machine/i440fx",
                                         "realized",
                                         &prop) < 0)
1118
        return -1;
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

    /* To make sure it worked, fetch the property - if this succeeds then
     * we didn't hose things
     */
    memset(&prop, 0, sizeof(qemuMonitorJSONObjectProperty));
    prop.type = QEMU_MONITOR_OBJECT_PROPERTY_BOOLEAN;
    if (qemuMonitorJSONGetObjectProperty(qemuMonitorTestGetMonitor(test),
                                         "/machine/i440fx",
                                         "realized",
                                         &prop) < 0)
1129
        return -1;
1130 1131 1132 1133

    if (!prop.val.b) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "expected true, but false returned");
1134
        return -1;
1135 1136
    }

1137
    return 0;
1138 1139 1140
}


1141
static int
1142
testQemuMonitorJSONGetDeviceAliases(const void *opaque)
1143
{
1144 1145
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1146 1147
    int ret = -1;
    char **aliases = NULL;
1148
    const char **alias;
1149 1150
    const char *expected[] = {
        "virtio-disk25", "video0", "serial0", "ide0-0-0", "usb", NULL };
1151
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1152

1153
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
        return -1;

    if (qemuMonitorTestAddItem(test,
                               "qom-list",
                               "{\"return\": ["
                               " {\"name\": \"virtio-disk25\","
                               "  \"type\": \"child<virtio-blk-pci>\"},"
                               " {\"name\": \"video0\","
                               "  \"type\": \"child<VGA>\"},"
                               " {\"name\": \"serial0\","
                               "  \"type\": \"child<isa-serial>\"},"
                               " {\"name\": \"ide0-0-0\","
                               "  \"type\": \"child<ide-cd>\"},"
                               " {\"name\": \"usb\","
                               "  \"type\": \"child<piix3-usb-uhci>\"},"
                               " {\"name\": \"type\", \"type\": \"string\"}"
                               "]}") < 0)
        goto cleanup;

    if (qemuMonitorJSONGetDeviceAliases(qemuMonitorTestGetMonitor(test),
                                        &aliases) < 0)
        goto cleanup;

    if (!aliases) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", "no aliases returned");
        goto cleanup;
    }

    ret = 0;
1183
    for (alias = (const char **) aliases; *alias; alias++) {
1184
        if (!virStringListHasString(expected, *alias)) {
1185 1186 1187 1188
            fprintf(stderr, "got unexpected device alias '%s'\n", *alias);
            ret = -1;
        }
    }
1189
    for (alias = expected; *alias; alias++) {
1190
        if (!virStringListHasString((const char **) aliases, *alias)) {
1191 1192 1193 1194 1195
            fprintf(stderr, "missing expected alias '%s'\n", *alias);
            ret = -1;
        }
    }

1196
 cleanup:
1197
    virStringListFree(aliases);
1198 1199 1200
    return ret;
}

1201
static int
1202
testQemuMonitorJSONCPU(const void *opaque)
1203
{
1204 1205
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1206 1207
    bool running = false;
    virDomainPausedReason reason = 0;
1208
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1209

1210
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1211 1212
        return -1;

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    if (qemuMonitorTestAddItem(test, "stop", "{\"return\": {}}") < 0 ||
        qemuMonitorTestAddItem(test, "query-status",
                               "{\"return\": {"
                               "   \"status\": \"paused\","
                               "   \"singlestep\": false,"
                               "   \"running\": false}}") < 0 ||
        qemuMonitorTestAddItem(test, "cont", "{\"return\": {}}") < 0 ||
        qemuMonitorTestAddItem(test, "query-status",
                               "{\"return\": {"
                               "    \"status\": \"running\","
                               "    \"singlestep\": false,"
                               "    \"running\": true}}") < 0)
1225
        return -1;
1226 1227

    if (qemuMonitorJSONStopCPUs(qemuMonitorTestGetMonitor(test)) < 0)
1228
        return -1;
1229 1230 1231

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
1232
        return -1;
1233 1234 1235 1236

    if (running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not false");
1237
        return -1;
1238 1239
    }

1240
    if (qemuMonitorJSONStartCPUs(qemuMonitorTestGetMonitor(test)) < 0)
1241
        return -1;
1242 1243 1244

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
1245
        return -1;
1246 1247 1248 1249

    if (!running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not true");
1250
        return -1;
1251 1252
    }

1253
    return 0;
1254
}
1255

1256 1257 1258
static int
testQemuMonitorJSONSimpleFunc(const void *opaque)
{
E
Eric Blake 已提交
1259 1260
    testQemuMonitorJSONSimpleFuncDataPtr data =
        (testQemuMonitorJSONSimpleFuncDataPtr) opaque;
1261 1262
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
    const char *reply = data->reply;
1263
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1264

1265
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1266 1267
        return -1;

1268 1269 1270 1271
    if (!reply)
        reply = "{\"return\":{}}";

    if (qemuMonitorTestAddItem(test, data->cmd, reply) < 0)
1272
        return -1;
1273 1274

    if (data->func(qemuMonitorTestGetMonitor(test)) < 0)
1275
        return -1;
1276

1277
    return 0;
1278 1279
}

1280 1281 1282 1283 1284 1285 1286
#define GEN_TEST_FUNC(funcName, ...) \
static int \
testQemuMonitorJSON ## funcName(const void *opaque) \
{ \
    const testQemuMonitorJSONSimpleFuncData *data = opaque; \
    virDomainXMLOptionPtr xmlopt = data->xmlopt; \
    const char *reply = data->reply; \
1287
    VIR_AUTOPTR(qemuMonitorTest) test = NULL; \
1288
 \
1289
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema))) \
1290 1291 1292 1293 1294 1295
        return -1; \
 \
    if (!reply) \
        reply = "{\"return\":{}}"; \
 \
    if (qemuMonitorTestAddItem(test, data->cmd, reply) < 0) \
1296
        return -1; \
1297 1298
 \
    if (funcName(qemuMonitorTestGetMonitor(test), __VA_ARGS__) < 0) \
1299
        return -1; \
1300
 \
1301
    return 0; \
1302 1303 1304
}

GEN_TEST_FUNC(qemuMonitorJSONSetLink, "vnet0", VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN)
1305
GEN_TEST_FUNC(qemuMonitorJSONBlockResize, "vda", "asdf", 123456)
1306 1307 1308 1309 1310
GEN_TEST_FUNC(qemuMonitorJSONSetPassword, "spice", "secret_password", "disconnect")
GEN_TEST_FUNC(qemuMonitorJSONExpirePassword, "spice", "123456")
GEN_TEST_FUNC(qemuMonitorJSONSetBalloon, 1024)
GEN_TEST_FUNC(qemuMonitorJSONSetCPU, 1, true)
GEN_TEST_FUNC(qemuMonitorJSONEjectMedia, "hdc", true)
1311
GEN_TEST_FUNC(qemuMonitorJSONChangeMedia, "hdc", "/foo/bar", "formatstr")
1312 1313 1314 1315 1316 1317 1318
GEN_TEST_FUNC(qemuMonitorJSONSaveVirtualMemory, 0, 1024, "/foo/bar")
GEN_TEST_FUNC(qemuMonitorJSONSavePhysicalMemory, 0, 1024, "/foo/bar")
GEN_TEST_FUNC(qemuMonitorJSONSetMigrationSpeed, 1024)
GEN_TEST_FUNC(qemuMonitorJSONSetMigrationDowntime, 1)
GEN_TEST_FUNC(qemuMonitorJSONMigrate, QEMU_MONITOR_MIGRATE_BACKGROUND |
              QEMU_MONITOR_MIGRATE_NON_SHARED_DISK |
              QEMU_MONITOR_MIGRATE_NON_SHARED_INC, "tcp:localhost:12345")
1319
GEN_TEST_FUNC(qemuMonitorJSONDump, "dummy_protocol", "elf",
1320
              true)
1321
GEN_TEST_FUNC(qemuMonitorJSONGraphicsRelocate, VIR_DOMAIN_GRAPHICS_TYPE_SPICE,
1322
              "localhost", 12345, 12346, "certsubjectval")
1323
GEN_TEST_FUNC(qemuMonitorJSONAddNetdev, "id=net0,type=test")
1324 1325 1326
GEN_TEST_FUNC(qemuMonitorJSONRemoveNetdev, "net0")
GEN_TEST_FUNC(qemuMonitorJSONDelDevice, "ide0")
GEN_TEST_FUNC(qemuMonitorJSONAddDevice, "some_dummy_devicestr")
1327
GEN_TEST_FUNC(qemuMonitorJSONDriveMirror, "vdb", "/foo/bar", "formatstr", 1024, 1234, 31234, true, true)
1328
GEN_TEST_FUNC(qemuMonitorJSONBlockdevMirror, "jobname", true, "vdb", "targetnode", 1024, 1234, 31234, true)
1329
GEN_TEST_FUNC(qemuMonitorJSONBlockStream, "vdb", "jobname", true, "/foo/bar1", "backingnode", "backingfilename", 1024)
1330
GEN_TEST_FUNC(qemuMonitorJSONBlockCommit, "vdb", "jobname", true, "/foo/bar1", "topnode", "/foo/bar2", "basenode", "backingfilename", 1024)
1331
GEN_TEST_FUNC(qemuMonitorJSONDrivePivot, "vdb")
1332
GEN_TEST_FUNC(qemuMonitorJSONScreendump, "devicename", 1, "/foo/bar")
1333
GEN_TEST_FUNC(qemuMonitorJSONOpenGraphics, "spice", "spicefd", false)
1334
GEN_TEST_FUNC(qemuMonitorJSONNBDServerAdd, "vda", "export", true, "bitmap")
1335
GEN_TEST_FUNC(qemuMonitorJSONDetachCharDev, "serial1")
1336 1337 1338 1339
GEN_TEST_FUNC(qemuMonitorJSONBlockdevTrayOpen, "foodev", true)
GEN_TEST_FUNC(qemuMonitorJSONBlockdevTrayClose, "foodev")
GEN_TEST_FUNC(qemuMonitorJSONBlockdevMediumRemove, "foodev")
GEN_TEST_FUNC(qemuMonitorJSONBlockdevMediumInsert, "foodev", "newnode")
E
Eric Blake 已提交
1340 1341 1342
GEN_TEST_FUNC(qemuMonitorJSONAddBitmap, "node", "bitmap", true)
GEN_TEST_FUNC(qemuMonitorJSONEnableBitmap, "node", "bitmap")
GEN_TEST_FUNC(qemuMonitorJSONDeleteBitmap, "node", "bitmap")
1343
GEN_TEST_FUNC(qemuMonitorJSONJobDismiss, "jobname")
1344
GEN_TEST_FUNC(qemuMonitorJSONJobCancel, "jobname", false)
1345
GEN_TEST_FUNC(qemuMonitorJSONJobComplete, "jobname")
1346

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
static int
testQemuMonitorJSONqemuMonitorJSONNBDServerStart(const void *opaque)
{
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
    virStorageNetHostDef server_tcp = {
        .name = (char *)"localhost",
        .port = 12345,
        .transport = VIR_STORAGE_NET_HOST_TRANS_TCP,
    };
    virStorageNetHostDef server_unix = {
        .socket = (char *)"/tmp/sock",
        .transport = VIR_STORAGE_NET_HOST_TRANS_UNIX,
    };
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;

    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
        return -1;

    if (qemuMonitorTestAddItem(test, "nbd-server-start",
                               "{\"return\":{}}") < 0)
        return -1;

    if (qemuMonitorTestAddItem(test, "nbd-server-start",
                               "{\"return\":{}}") < 0)
        return -1;

    if (qemuMonitorJSONNBDServerStart(qemuMonitorTestGetMonitor(test),
                                      &server_tcp, "test-alias") < 0)
        return -1;

    if (qemuMonitorJSONNBDServerStart(qemuMonitorTestGetMonitor(test),
                                      &server_unix, "test-alias") < 0)
        return -1;

    return 0;
}

E
Eric Blake 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
static int
testQemuMonitorJSONqemuMonitorJSONMergeBitmaps(const void *opaque)
{
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
    VIR_AUTOPTR(virJSONValue) arr = NULL;

    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
        return -1;

    if (!(arr = virJSONValueNewArray()))
        return -1;

    if (virJSONValueArrayAppendString(arr, "b1") < 0 ||
        virJSONValueArrayAppendString(arr, "b2") < 0)
        return -1;

    if (qemuMonitorTestAddItem(test, "block-dirty-bitmap-merge",
                               "{\"return\":{}}") < 0)
        return -1;

    if (qemuMonitorJSONMergeBitmaps(qemuMonitorTestGetMonitor(test),
                                    "node", "dst", &arr) < 0)
        return -1;

    if (arr) {
1412
        VIR_TEST_VERBOSE("arr should have been cleared");
E
Eric Blake 已提交
1413 1414 1415 1416 1417 1418
        return -1;
    }

    return 0;
}

1419 1420 1421 1422
static bool
testQemuMonitorJSONqemuMonitorJSONQueryCPUsEqual(struct qemuMonitorQueryCpusEntry *a,
                                                 struct qemuMonitorQueryCpusEntry *b)
{
1423 1424
    if (a->tid != b->tid ||
        STRNEQ_NULLABLE(a->qom_path, b->qom_path))
1425 1426 1427 1428 1429
        return false;

    return true;
}

1430

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
static int
testQEMUMonitorJSONqemuMonitorJSONQueryCPUsHelper(qemuMonitorTestPtr test,
                                                  struct qemuMonitorQueryCpusEntry *expect,
                                                  bool fast,
                                                  size_t num)
{
    struct qemuMonitorQueryCpusEntry *cpudata = NULL;
    size_t ncpudata = 0;
    size_t i;
    int ret = -1;

    if (qemuMonitorJSONQueryCPUs(qemuMonitorTestGetMonitor(test),
                                 &cpudata, &ncpudata, true, fast) < 0)
        goto cleanup;

    if (ncpudata != num) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Expecting ncpupids = %zu but got %zu", num, ncpudata);
        goto cleanup;
    }

    for (i = 0; i < ncpudata; i++) {
        if (!testQemuMonitorJSONqemuMonitorJSONQueryCPUsEqual(cpudata + i,
                                                              expect + i)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "vcpu entry %zu does not match expected data", i);
            goto cleanup;
        }
    }

    ret = 0;

 cleanup:
    qemuMonitorQueryCpusFree(cpudata, ncpudata);
    return ret;
}


1469
static int
1470
testQemuMonitorJSONqemuMonitorJSONQueryCPUs(const void *opaque)
1471
{
1472 1473
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1474 1475 1476 1477 1478 1479 1480 1481 1482
    struct qemuMonitorQueryCpusEntry expect_slow[] = {
            {0, 17622, (char *) "/machine/unattached/device[0]", true},
            {1, 17624, (char *) "/machine/unattached/device[1]", true},
            {2, 17626, (char *) "/machine/unattached/device[2]", true},
            {3, 17628, NULL, true},
    };
    struct qemuMonitorQueryCpusEntry expect_fast[] = {
            {0, 17629, (char *) "/machine/unattached/device[0]", false},
            {1, 17630, (char *) "/machine/unattached/device[1]", false},
1483
    };
1484
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1485

1486
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1487 1488 1489 1490 1491 1492 1493 1494
        return -1;

    if (qemuMonitorTestAddItem(test, "query-cpus",
                               "{"
                               "    \"return\": ["
                               "        {"
                               "            \"current\": true,"
                               "            \"CPU\": 0,"
1495
                               "            \"qom_path\": \"/machine/unattached/device[0]\","
1496 1497 1498 1499 1500 1501 1502
                               "            \"pc\": -2130530478,"
                               "            \"halted\": true,"
                               "            \"thread_id\": 17622"
                               "        },"
                               "        {"
                               "            \"current\": false,"
                               "            \"CPU\": 1,"
1503
                               "            \"qom_path\": \"/machine/unattached/device[1]\","
1504 1505 1506 1507 1508 1509 1510
                               "            \"pc\": -2130530478,"
                               "            \"halted\": true,"
                               "            \"thread_id\": 17624"
                               "        },"
                               "        {"
                               "            \"current\": false,"
                               "            \"CPU\": 2,"
1511
                               "            \"qom_path\": \"/machine/unattached/device[2]\","
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
                               "            \"pc\": -2130530478,"
                               "            \"halted\": true,"
                               "            \"thread_id\": 17626"
                               "        },"
                               "        {"
                               "            \"current\": false,"
                               "            \"CPU\": 3,"
                               "            \"pc\": -2130530478,"
                               "            \"halted\": true,"
                               "            \"thread_id\": 17628"
                               "        }"
                               "    ],"
                               "    \"id\": \"libvirt-7\""
                               "}") < 0)
1526
        return -1;
1527

1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
    if (qemuMonitorTestAddItem(test, "query-cpus-fast",
                               "{"
                               "    \"return\": ["
                               "        {"
                               "            \"cpu-index\": 0,"
                               "            \"qom-path\": \"/machine/unattached/device[0]\","
                               "            \"thread-id\": 17629"
                               "        },"
                               "        {"
                               "            \"cpu-index\": 1,"
                               "            \"qom-path\": \"/machine/unattached/device[1]\","
                               "            \"thread-id\": 17630"
                               "        }"
                               "    ],"
                               "    \"id\": \"libvirt-8\""
                               "}") < 0)
1544
        return -1;
1545

1546 1547 1548
    /* query-cpus */
    if (testQEMUMonitorJSONqemuMonitorJSONQueryCPUsHelper(test, expect_slow,
                                                          false, 4))
1549
        return -1;
1550

1551 1552 1553
    /* query-cpus-fast */
    if (testQEMUMonitorJSONqemuMonitorJSONQueryCPUsHelper(test, expect_fast,
                                                          true, 2))
1554
        return -1;
1555

1556
    return 0;
1557 1558
}

1559
static int
1560
testQemuMonitorJSONqemuMonitorJSONGetBalloonInfo(const void *opaque)
1561
{
1562 1563
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1564
    unsigned long long currmem;
1565
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1566

1567
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1568 1569 1570 1571 1572
        return -1;

    if (qemuMonitorTestAddItem(test, "query-balloon",
                               "{"
                               "    \"return\": {"
1573
                               "        \"actual\": 18446744073709551615"
1574 1575 1576
                               "    },"
                               "    \"id\": \"libvirt-9\""
                               "}") < 0)
1577
        return -1;
1578 1579

    if (qemuMonitorJSONGetBalloonInfo(qemuMonitorTestGetMonitor(test), &currmem) < 0)
1580
        return -1;
1581

1582
    if (currmem != (18446744073709551615ULL/1024)) {
1583 1584
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected currmem value: %llu", currmem);
1585
        return -1;
1586 1587
    }

1588
    return 0;
1589 1590
}

1591
static int
1592
testQemuMonitorJSONqemuMonitorJSONGetVirtType(const void *opaque)
1593
{
1594 1595
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1596
    virDomainVirtType virtType;
1597
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1598

1599
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
        return -1;

    if (qemuMonitorTestAddItem(test, "query-kvm",
                               "{"
                               "    \"return\": {"
                               "        \"enabled\": true,"
                               "        \"present\": true"
                               "    },"
                               "    \"id\": \"libvirt-8\""
                               "}") < 0 ||
        qemuMonitorTestAddItem(test, "query-kvm",
                               "{"
                               "    \"return\": {"
                               "        \"enabled\": false,"
                               "        \"present\": true"
                               "    },"
                               "    \"id\": \"libvirt-7\""
                               "}") < 0)
1618
        return -1;
1619 1620

    if (qemuMonitorJSONGetVirtType(qemuMonitorTestGetMonitor(test), &virtType) < 0)
1621
        return -1;
1622 1623 1624 1625

    if (virtType != VIR_DOMAIN_VIRT_KVM) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected virt type: %d, expecting %d", virtType, VIR_DOMAIN_VIRT_KVM);
1626
        return -1;
1627 1628 1629
    }

    if (qemuMonitorJSONGetVirtType(qemuMonitorTestGetMonitor(test), &virtType) < 0)
1630
        return -1;
1631 1632 1633 1634

    if (virtType != VIR_DOMAIN_VIRT_QEMU) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected virt type: %d, expecting %d", virtType, VIR_DOMAIN_VIRT_QEMU);
1635
        return -1;
1636 1637
    }

1638
    return 0;
1639 1640
}

1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651

static void
testQemuMonitorJSONGetBlockInfoPrint(const struct qemuDomainDiskInfo *d)
{
    VIR_TEST_VERBOSE("removable: %d, tray: %d, tray_open: %d, empty: %d, "
                     "io_status: %d, nodename: '%s'\n",
                     d->removable, d->tray, d->tray_open, d->empty,
                     d->io_status, NULLSTR(d->nodename));
}


1652 1653 1654 1655
static int
testHashEqualQemuDomainDiskInfo(const void *value1, const void *value2)
{
    const struct qemuDomainDiskInfo *info1 = value1, *info2 = value2;
1656 1657 1658 1659 1660 1661
    int ret;

    if ((ret = memcmp(info1, info2, sizeof(*info1))) != 0) {
        testQemuMonitorJSONGetBlockInfoPrint(info1);
        testQemuMonitorJSONGetBlockInfoPrint(info2);
    }
1662

1663
    return ret;
1664 1665 1666
}

static int
1667
testQemuMonitorJSONqemuMonitorJSONGetBlockInfo(const void *opaque)
1668
{
1669 1670
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1671 1672 1673
    int ret = -1;
    virHashTablePtr blockDevices = NULL, expectedBlockDevices = NULL;
    struct qemuDomainDiskInfo *info;
1674
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1675

1676
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1677 1678
        return -1;

E
Eric Blake 已提交
1679 1680
    if (!(blockDevices = virHashCreate(32, virHashValueFree)) ||
        !(expectedBlockDevices = virHashCreate(32, virHashValueFree)))
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
        goto cleanup;

    if (VIR_ALLOC(info) < 0)
        goto cleanup;

    if (virHashAddEntry(expectedBlockDevices, "virtio-disk0", info) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Unable to create expectedBlockDevices hash table");
        goto cleanup;
    }

    if (VIR_ALLOC(info) < 0)
        goto cleanup;

    if (virHashAddEntry(expectedBlockDevices, "virtio-disk1", info) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Unable to create expectedBlockDevices hash table");
        goto cleanup;
    }

    if (VIR_ALLOC(info) < 0)
        goto cleanup;

    info->removable = true;
1705 1706
    info->tray = true;

1707 1708 1709 1710 1711 1712
    if (virHashAddEntry(expectedBlockDevices, "ide0-1-0", info) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Unable to create expectedBlockDevices hash table");
        goto cleanup;
    }

1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    if (VIR_ALLOC(info) < 0)
        goto cleanup;

    info->removable = true;
    info->tray = true;
    info->empty = true;

    if (virHashAddEntry(expectedBlockDevices, "ide0-1-1", info) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Unable to create expectedBlockDevices hash table");
        goto cleanup;
    }

1726
    if (qemuMonitorTestAddItem(test, "query-block", queryBlockReply) < 0)
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
        goto cleanup;

    if (qemuMonitorJSONGetBlockInfo(qemuMonitorTestGetMonitor(test), blockDevices) < 0)
        goto cleanup;

    if (!virHashEqual(blockDevices, expectedBlockDevices, testHashEqualQemuDomainDiskInfo)) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Hashtable is different to the expected one");
        goto cleanup;
    }

    ret = 0;
1739
 cleanup:
1740 1741 1742 1743 1744
    virHashFree(blockDevices);
    virHashFree(expectedBlockDevices);
    return ret;
}

1745
static int
1746
testQemuMonitorJSONqemuMonitorJSONGetAllBlockStatsInfo(const void *opaque)
1747
{
1748 1749
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1750 1751
    virHashTablePtr blockstats = NULL;
    qemuBlockStatsPtr stats;
1752
    int ret = -1;
1753
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841

    const char *reply =
        "{"
        "    \"return\": ["
        "        {"
        "            \"device\": \"drive-virtio-disk0\","
        "            \"parent\": {"
        "                \"stats\": {"
        "                    \"flush_total_time_ns\": 0,"
        "                    \"wr_highest_offset\": 5256018944,"
        "                    \"wr_total_time_ns\": 0,"
        "                    \"wr_bytes\": 0,"
        "                    \"rd_total_time_ns\": 0,"
        "                    \"flush_operations\": 0,"
        "                    \"wr_operations\": 0,"
        "                    \"rd_bytes\": 0,"
        "                    \"rd_operations\": 0"
        "                }"
        "            },"
        "            \"stats\": {"
        "                \"flush_total_time_ns\": 0,"
        "                \"wr_highest_offset\": 10406001664,"
        "                \"wr_total_time_ns\": 530699221,"
        "                \"wr_bytes\": 2845696,"
        "                \"rd_total_time_ns\": 640616474,"
        "                \"flush_operations\": 0,"
        "                \"wr_operations\": 174,"
        "                \"rd_bytes\": 28505088,"
        "                \"rd_operations\": 1279"
        "            }"
        "        },"
        "        {"
        "            \"device\": \"drive-virtio-disk1\","
        "            \"parent\": {"
        "                \"stats\": {"
        "                    \"flush_total_time_ns\": 0,"
        "                    \"wr_highest_offset\": 0,"
        "                    \"wr_total_time_ns\": 0,"
        "                    \"wr_bytes\": 0,"
        "                    \"rd_total_time_ns\": 0,"
        "                    \"flush_operations\": 0,"
        "                    \"wr_operations\": 0,"
        "                    \"rd_bytes\": 0,"
        "                    \"rd_operations\": 0"
        "                }"
        "            },"
        "            \"stats\": {"
        "                \"flush_total_time_ns\": 0,"
        "                \"wr_highest_offset\": 0,"
        "                \"wr_total_time_ns\": 0,"
        "                \"wr_bytes\": 0,"
        "                \"rd_total_time_ns\": 8232156,"
        "                \"flush_operations\": 0,"
        "                \"wr_operations\": 0,"
        "                \"rd_bytes\": 348160,"
        "                \"rd_operations\": 85"
        "            }"
        "        },"
        "        {"
        "            \"device\": \"drive-ide0-1-0\","
        "            \"parent\": {"
        "                \"stats\": {"
        "                    \"flush_total_time_ns\": 0,"
        "                    \"wr_total_time_ns\": 0,"
        "                    \"wr_bytes\": 0,"
        "                    \"rd_total_time_ns\": 0,"
        "                    \"flush_operations\": 0,"
        "                    \"wr_operations\": 0,"
        "                    \"rd_bytes\": 0,"
        "                    \"rd_operations\": 0"
        "                }"
        "            },"
        "            \"stats\": {"
        "                \"flush_total_time_ns\": 0,"
        "                \"wr_highest_offset\": 0,"
        "                \"wr_total_time_ns\": 0,"
        "                \"wr_bytes\": 0,"
        "                \"rd_total_time_ns\": 1004952,"
        "                \"flush_operations\": 0,"
        "                \"wr_operations\": 0,"
        "                \"rd_bytes\": 49250,"
        "                \"rd_operations\": 16"
        "            }"
        "        }"
        "    ],"
        "    \"id\": \"libvirt-11\""
        "}";

1842
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1843 1844
        return -1;

1845 1846 1847
    if (!(blockstats = virHashCreate(10, virHashValueFree)))
        goto cleanup;

1848
    if (qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0)
1849 1850
        goto cleanup;

1851
#define CHECK0FULL(var, value, varformat, valformat) \
1852
    if (stats->var != value) { \
1853
        virReportError(VIR_ERR_INTERNAL_ERROR, \
1854 1855
                       "Invalid " #var " value: " varformat \
                       ", expected " valformat, \
1856
                       stats->var, value); \
1857 1858 1859
        goto cleanup; \
    }

1860 1861
#define CHECK0(var, value) CHECK0FULL(var, value, "%lld", "%d")

1862 1863 1864 1865 1866 1867 1868 1869
#define CHECK(NAME, RD_REQ, RD_BYTES, RD_TOTAL_TIMES, WR_REQ, WR_BYTES, \
              WR_TOTAL_TIMES, FLUSH_REQ, FLUSH_TOTAL_TIMES, \
              WR_HIGHEST_OFFSET, WR_HIGHEST_OFFSET_VALID) \
    if (!(stats = virHashLookup(blockstats, NAME))) { \
        virReportError(VIR_ERR_INTERNAL_ERROR, \
                       "block stats for device '%s' is missing", NAME); \
        goto cleanup; \
    } \
1870 1871 1872 1873 1874 1875 1876
    CHECK0(rd_req, RD_REQ) \
    CHECK0(rd_bytes, RD_BYTES) \
    CHECK0(rd_total_times, RD_TOTAL_TIMES) \
    CHECK0(wr_req, WR_REQ) \
    CHECK0(wr_bytes, WR_BYTES) \
    CHECK0(wr_total_times, WR_TOTAL_TIMES) \
    CHECK0(flush_req, FLUSH_REQ) \
1877 1878 1879
    CHECK0(flush_total_times, FLUSH_TOTAL_TIMES) \
    CHECK0FULL(wr_highest_offset, WR_HIGHEST_OFFSET, "%llu", "%llu") \
    CHECK0FULL(wr_highest_offset_valid, WR_HIGHEST_OFFSET_VALID, "%d", "%d")
1880

1881 1882
    if (qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorTestGetMonitor(test),
                                            blockstats, false) < 0)
1883 1884
        goto cleanup;

1885 1886
    if (!blockstats) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1887
                       "qemuMonitorJSONGetAllBlockStatsInfo didn't return stats");
1888 1889 1890
        goto cleanup;
    }

1891 1892
    CHECK("virtio-disk0", 1279, 28505088, 640616474, 174, 2845696, 530699221, 0, 0, 5256018944ULL, true)
    CHECK("virtio-disk1", 85, 348160, 8232156, 0, 0, 0, 0, 0, 0ULL, true)
1893
    CHECK("ide0-1-0", 16, 49250, 1004952, 0, 0, 0, 0, 0, 0ULL, false)
1894

1895 1896 1897 1898
    ret = 0;

#undef CHECK
#undef CHECK0
1899
#undef CHECK0FULL
1900

1901
 cleanup:
1902
    virHashFree(blockstats);
1903 1904 1905
    return ret;
}

1906

1907
static int
1908
testQemuMonitorJSONqemuMonitorJSONGetMigrationCacheSize(const void *opaque)
1909
{
1910 1911
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1912
    unsigned long long cacheSize;
1913
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1914

1915
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1916 1917 1918 1919 1920 1921 1922
        return -1;

    if (qemuMonitorTestAddItem(test, "query-migrate-cache-size",
                               "{"
                               "    \"return\": 67108864,"
                               "    \"id\": \"libvirt-12\""
                               "}") < 0)
1923
        return -1;
1924 1925 1926

    if (qemuMonitorJSONGetMigrationCacheSize(qemuMonitorTestGetMonitor(test),
                                             &cacheSize) < 0)
1927
        return -1;
1928 1929 1930 1931 1932

    if (cacheSize != 67108864) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Invalid cacheSize: %llu, expected 67108864",
                       cacheSize);
1933
        return -1;
1934 1935
    }

1936
    return 0;
1937 1938
}

1939
static int
1940
testQemuMonitorJSONqemuMonitorJSONGetMigrationStats(const void *opaque)
1941
{
1942 1943
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
1944
    int ret = -1;
1945
    qemuMonitorMigrationStats stats, expectedStats;
1946
    char *error = NULL;
1947
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
1948

1949
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
1950 1951
        return -1;

1952
    memset(&expectedStats, 0, sizeof(expectedStats));
1953

1954 1955 1956 1957 1958
    expectedStats.status = QEMU_MONITOR_MIGRATION_STATUS_ACTIVE;
    expectedStats.total_time = 47;
    expectedStats.ram_total = 1611038720;
    expectedStats.ram_remaining = 1605013504;
    expectedStats.ram_transferred = 3625548;
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971

    if (qemuMonitorTestAddItem(test, "query-migrate",
                               "{"
                               "    \"return\": {"
                               "        \"status\": \"active\","
                               "        \"total-time\": 47,"
                               "        \"ram\": {"
                               "            \"total\": 1611038720,"
                               "            \"remaining\": 1605013504,"
                               "            \"transferred\": 3625548"
                               "        }"
                               "    },"
                               "    \"id\": \"libvirt-13\""
1972 1973 1974 1975 1976 1977 1978 1979
                               "}") < 0 ||
        qemuMonitorTestAddItem(test, "query-migrate",
                               "{"
                               "    \"return\": {"
                               "        \"status\": \"failed\","
                               "        \"error-desc\": \"It's broken\""
                               "    },"
                               "    \"id\": \"libvirt-14\""
1980 1981 1982
                               "}") < 0)
        goto cleanup;

1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
    if (qemuMonitorJSONGetMigrationStats(qemuMonitorTestGetMonitor(test),
                                         &stats, &error) < 0)
        goto cleanup;

    if (memcmp(&stats, &expectedStats, sizeof(stats)) != 0 || error) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Invalid migration statistics");
        goto cleanup;
    }

    memset(&stats, 0, sizeof(stats));
    if (qemuMonitorJSONGetMigrationStats(qemuMonitorTestGetMonitor(test),
                                         &stats, &error) < 0)
1996 1997
        goto cleanup;

1998 1999
    if (stats.status != QEMU_MONITOR_MIGRATION_STATUS_ERROR ||
        STRNEQ_NULLABLE(error, "It's broken")) {
2000
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2001
                       "Invalid failed migration status");
2002 2003 2004 2005
        goto cleanup;
    }

    ret = 0;
2006
 cleanup:
2007
    VIR_FREE(error);
2008 2009 2010
    return ret;
}

2011
static int
2012
testHashEqualChardevInfo(const void *value1, const void *value2)
2013
{
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
    const qemuMonitorChardevInfo *info1 = value1;
    const qemuMonitorChardevInfo *info2 = value2;

    if (info1->state != info2->state)
        goto error;

    if (STRNEQ_NULLABLE(info1->ptyPath, info2->ptyPath))
        goto error;

    return 0;

 error:
    fprintf(stderr, "\n"
            "info1->state: %d info2->state: %d\n"
            "info1->ptyPath: %s info2->ptyPath: %s\n",
            info1->state, info2->state, info1->ptyPath, info2->ptyPath);
    return -1;
2031 2032
}

2033

2034
static int
2035
testQemuMonitorJSONqemuMonitorJSONGetChardevInfo(const void *opaque)
2036
{
2037 2038
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2039
    int ret = -1;
2040
    virHashTablePtr info = NULL, expectedInfo = NULL;
2041 2042 2043 2044
    qemuMonitorChardevInfo info0 = { NULL, VIR_DOMAIN_CHR_DEVICE_STATE_DEFAULT };
    qemuMonitorChardevInfo info1 = { (char *) "/dev/pts/21", VIR_DOMAIN_CHR_DEVICE_STATE_CONNECTED };
    qemuMonitorChardevInfo info2 = { (char *) "/dev/pts/20", VIR_DOMAIN_CHR_DEVICE_STATE_DEFAULT };
    qemuMonitorChardevInfo info3 = { NULL, VIR_DOMAIN_CHR_DEVICE_STATE_DISCONNECTED };
2045
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2046

2047
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2048 2049
        return -1;

2050
    if (!(info = virHashCreate(32, qemuMonitorChardevInfoFree)) ||
2051
        !(expectedInfo = virHashCreate(32, NULL)))
2052 2053
        goto cleanup;

2054 2055 2056 2057
    if (virHashAddEntry(expectedInfo, "charserial1", &info1) < 0 ||
        virHashAddEntry(expectedInfo, "charserial0", &info2) < 0 ||
        virHashAddEntry(expectedInfo, "charmonitor", &info0) < 0 ||
        virHashAddEntry(expectedInfo, "charserial2", &info3) < 0) {
2058
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2059
                       "Unable to create expectedInfo hash table");
2060 2061 2062 2063 2064 2065 2066 2067
        goto cleanup;
    }

    if (qemuMonitorTestAddItem(test, "query-chardev",
                               "{"
                               "    \"return\": ["
                               "        {"
                               "            \"filename\": \"pty:/dev/pts/21\","
2068 2069
                               "            \"label\": \"charserial1\","
                               "            \"frontend-open\": true"
2070 2071 2072 2073 2074 2075 2076 2077
                               "        },"
                               "        {"
                               "            \"filename\": \"pty:/dev/pts/20\","
                               "            \"label\": \"charserial0\""
                               "        },"
                               "        {"
                               "            \"filename\": \"unix:/var/lib/libvirt/qemu/gentoo.monitor,server\","
                               "            \"label\": \"charmonitor\""
2078 2079 2080 2081 2082
                               "        },"
                               "        {"
                               "            \"filename\": \"unix:/path/to/socket,server\","
                               "            \"label\": \"charserial2\","
                               "            \"frontend-open\": false"
2083 2084 2085 2086 2087 2088
                               "        }"
                               "    ],"
                               "    \"id\": \"libvirt-15\""
                               "}") < 0)
        goto cleanup;

2089 2090
    if (qemuMonitorJSONGetChardevInfo(qemuMonitorTestGetMonitor(test),
                                      info) < 0)
2091 2092
        goto cleanup;

2093
    if (!virHashEqual(info, expectedInfo, testHashEqualChardevInfo)) {
2094 2095 2096 2097 2098 2099
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Hashtable is different to the expected one");
        goto cleanup;
    }

    ret = 0;
2100
 cleanup:
2101 2102
    virHashFree(info);
    virHashFree(expectedInfo);
2103 2104 2105
    return ret;
}

2106 2107

static int
2108 2109
testValidateGetBlockIoThrottle(const virDomainBlockIoTuneInfo *info,
                               const virDomainBlockIoTuneInfo *expectedInfo)
2110 2111
{
#define VALIDATE_IOTUNE(field) \
2112
    if (info->field != expectedInfo->field) { \
2113
        virReportError(VIR_ERR_INTERNAL_ERROR, \
2114
                       "info->%s=%llu != expected=%llu", \
2115
                       #field, info->field, expectedInfo->field); \
2116 2117
        return -1; \
    } \
2118
    if (info->field##_max != expectedInfo->field##_max) { \
2119
        virReportError(VIR_ERR_INTERNAL_ERROR, \
2120
                       "info->%s_max=%llu != expected=%llu", \
2121
                       #field, info->field##_max, expectedInfo->field##_max); \
2122 2123
        return -1; \
    } \
2124
    if (info->field##_max_length != expectedInfo->field##_max_length) { \
2125
        virReportError(VIR_ERR_INTERNAL_ERROR, \
2126
                       "info->%s_max_length=%llu != expected=%llu", \
2127 2128
                       #field, info->field##_max_length, \
                       expectedInfo->field##_max_length); \
2129 2130 2131 2132 2133 2134 2135 2136
        return -1; \
    }
    VALIDATE_IOTUNE(total_bytes_sec);
    VALIDATE_IOTUNE(read_bytes_sec);
    VALIDATE_IOTUNE(write_bytes_sec);
    VALIDATE_IOTUNE(total_iops_sec);
    VALIDATE_IOTUNE(read_iops_sec);
    VALIDATE_IOTUNE(write_iops_sec);
2137
    if (info->size_iops_sec != expectedInfo->size_iops_sec) {
2138
        virReportError(VIR_ERR_INTERNAL_ERROR,
2139 2140
                       "info->size_iops_sec=%llu != expected=%llu",
                       info->size_iops_sec, expectedInfo->size_iops_sec);
2141 2142
        return -1;
    }
2143
    if (STRNEQ(info->group_name, expectedInfo->group_name)) {
2144
        virReportError(VIR_ERR_INTERNAL_ERROR,
2145 2146
                       "info->group_name=%s != expected=%s",
                       info->group_name, expectedInfo->group_name);
2147 2148 2149 2150 2151 2152 2153 2154
        return -1;
    }
#undef VALIDATE_IOTUNE

    return 0;
}


2155
static int
2156
testQemuMonitorJSONqemuMonitorJSONSetBlockIoThrottle(const void *opaque)
2157
{
2158 2159
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2160 2161
    int ret = -1;
    virDomainBlockIoTuneInfo info, expectedInfo;
2162
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2163

2164
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2165 2166
        return -1;

2167 2168 2169
    expectedInfo = (virDomainBlockIoTuneInfo) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, NULL, 15, 16, 17, 18, 19, 20};
    if (VIR_STRDUP(expectedInfo.group_name, "group14") < 0)
        return -1;
2170 2171 2172 2173 2174 2175 2176

    if (qemuMonitorTestAddItem(test, "query-block", queryBlockReply) < 0 ||
        qemuMonitorTestAddItemParams(test, "block_set_io_throttle",
                                     "{\"return\":{}}",
                                     "device", "\"drive-virtio-disk1\"",
                                     "bps", "1", "bps_rd", "2", "bps_wr", "3",
                                     "iops", "4", "iops_rd", "5", "iops_wr", "6",
2177 2178 2179 2180
                                     "bps_max", "7", "bps_rd_max", "8",
                                     "bps_wr_max", "9",
                                     "iops_max", "10", "iops_rd_max", "11",
                                     "iops_wr_max", "12", "iops_size", "13",
2181 2182 2183 2184 2185 2186 2187
                                     "group", "\"group14\"",
                                     "bps_max_length", "15",
                                     "bps_rd_max_length", "16",
                                     "bps_wr_max_length", "17",
                                     "iops_max_length", "18",
                                     "iops_rd_max_length", "19",
                                     "iops_wr_max_length", "20",
2188 2189 2190 2191
                                     NULL, NULL) < 0)
        goto cleanup;

    if (qemuMonitorJSONGetBlockIoThrottle(qemuMonitorTestGetMonitor(test),
2192
                                          "drive-virtio-disk0", NULL, &info) < 0)
2193 2194
        goto cleanup;

2195
    if (testValidateGetBlockIoThrottle(&info, &expectedInfo) < 0)
2196 2197 2198
        goto cleanup;

    if (qemuMonitorJSONSetBlockIoThrottle(qemuMonitorTestGetMonitor(test),
2199
                                          "drive-virtio-disk1", NULL, &info, true,
2200
                                          true, true) < 0)
2201 2202 2203
        goto cleanup;

    ret = 0;
2204
 cleanup:
2205 2206
    VIR_FREE(info.group_name);
    VIR_FREE(expectedInfo.group_name);
2207 2208 2209
    return ret;
}

2210
static int
2211
testQemuMonitorJSONqemuMonitorJSONGetTargetArch(const void *opaque)
2212
{
2213 2214
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2215 2216
    int ret = -1;
    char *arch;
2217
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2218

2219
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
        return -1;

    if (qemuMonitorTestAddItem(test, "query-target",
                               "{"
                               "    \"return\": {"
                               "        \"arch\": \"x86_64\""
                               "    },"
                               "    \"id\": \"libvirt-21\""
                               "}") < 0)
        goto cleanup;

    if (!(arch = qemuMonitorJSONGetTargetArch(qemuMonitorTestGetMonitor(test))))
        goto cleanup;

    if (STRNEQ(arch, "x86_64")) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected architecture %s, expecting x86_64",
                       arch);
        goto cleanup;
    }

    ret = 0;
2242
 cleanup:
2243 2244 2245 2246
    VIR_FREE(arch);
    return ret;
}

2247
static int
2248
testQemuMonitorJSONqemuMonitorJSONGetMigrationCapabilities(const void *opaque)
2249
{
2250 2251
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2252
    int ret = -1;
2253 2254
    const char *cap;
    char **caps = NULL;
2255
    virBitmapPtr bitmap = NULL;
2256
    virJSONValuePtr json = NULL;
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
    const char *reply =
        "{"
        "    \"return\": ["
        "        {"
        "            \"state\": false,"
        "            \"capability\": \"xbzrle\""
        "        }"
        "    ],"
        "    \"id\": \"libvirt-22\""
        "}";
2267
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2268

2269
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2270 2271 2272 2273 2274 2275 2276
        return -1;

    if (qemuMonitorTestAddItem(test, "query-migrate-capabilities", reply) < 0 ||
        qemuMonitorTestAddItem(test, "migrate-set-capabilities",
                               "{\"return\":{}}") < 0)
        goto cleanup;

2277 2278 2279 2280
    if (qemuMonitorGetMigrationCapabilities(qemuMonitorTestGetMonitor(test),
                                            &caps) < 0)
        goto cleanup;

2281
    cap = qemuMigrationCapabilityTypeToString(QEMU_MIGRATION_CAP_XBZRLE);
2282
    if (!virStringListHasString((const char **) caps, cap)) {
2283
        virReportError(VIR_ERR_INTERNAL_ERROR,
2284
                       "Expected capability %s is missing", cap);
2285 2286 2287
        goto cleanup;
    }

2288
    bitmap = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
2289 2290 2291
    if (!bitmap)
        goto cleanup;

2292
    ignore_value(virBitmapSetBit(bitmap, QEMU_MIGRATION_CAP_XBZRLE));
2293
    if (!(json = qemuMigrationCapsToJSON(bitmap, bitmap)))
2294 2295
        goto cleanup;

2296 2297 2298 2299
    ret = qemuMonitorJSONSetMigrationCapabilities(qemuMonitorTestGetMonitor(test),
                                                  json);
    json = NULL;

2300
 cleanup:
2301
    virJSONValueFree(json);
2302
    virStringListFree(caps);
2303
    virBitmapFree(bitmap);
2304 2305 2306
    return ret;
}

2307
static int
2308
testQemuMonitorJSONqemuMonitorJSONSendKey(const void *opaque)
2309
{
2310 2311
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2312
    unsigned int keycodes[] = {43, 26, 46, 32};
2313
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2314

2315
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2316 2317 2318 2319
        return -1;

    if (qemuMonitorTestAddItem(test, "send-key",
                               "{\"return\": {}, \"id\": \"libvirt-16\"}") < 0)
2320
        return -1;
2321 2322 2323

    if (qemuMonitorJSONSendKey(qemuMonitorTestGetMonitor(test),
                               0, keycodes, ARRAY_CARDINALITY(keycodes)) < 0)
2324
        return -1;
2325

2326
    return 0;
2327 2328
}

2329
static int
2330
testQemuMonitorJSONqemuMonitorJSONSendKeyHoldtime(const void *opaque)
2331
{
2332 2333
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2334
    unsigned int keycodes[] = {43, 26, 46, 32};
2335
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2336

2337
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
        return -1;

    if (qemuMonitorTestAddItemParams(test, "send-key",
                                     "{\"return\":{}}",
                                     "hold-time", "31337",
                                     "keys", "[{\"type\":\"number\",\"data\":43},"
                                              "{\"type\":\"number\",\"data\":26},"
                                              "{\"type\":\"number\",\"data\":46},"
                                              "{\"type\":\"number\",\"data\":32}]",
                                     NULL, NULL) < 0)
2348
        return -1;
2349 2350 2351 2352

    if (qemuMonitorJSONSendKey(qemuMonitorTestGetMonitor(test),
                               31337, keycodes,
                               ARRAY_CARDINALITY(keycodes)) < 0)
2353
        return -1;
2354

2355
    return 0;
2356 2357
}

2358
static int
2359
testQemuMonitorJSONqemuMonitorSupportsActiveCommit(const void *opaque)
2360
{
2361 2362
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
    const char *error1 =
        "{"
        "  \"error\": {"
        "    \"class\": \"DeviceNotFound\","
        "    \"desc\": \"Device 'bogus' not found\""
        "  }"
        "}";
    const char *error2 =
        "{"
        "  \"error\": {"
        "    \"class\": \"GenericError\","
        "    \"desc\": \"Parameter 'top' is missing\""
        "  }"
        "}";
2377
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2378

2379
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2380 2381 2382 2383 2384
        return -1;

    if (qemuMonitorTestAddItemParams(test, "block-commit", error1,
                                     "device", "\"bogus\"",
                                     NULL, NULL) < 0)
2385
        return -1;
2386 2387

    if (!qemuMonitorSupportsActiveCommit(qemuMonitorTestGetMonitor(test)))
2388
        return -1;
2389 2390 2391 2392

    if (qemuMonitorTestAddItemParams(test, "block-commit", error2,
                                     "device", "\"bogus\"",
                                     NULL, NULL) < 0)
2393
        return -1;
2394 2395

    if (qemuMonitorSupportsActiveCommit(qemuMonitorTestGetMonitor(test)))
2396
        return -1;
2397

2398
    return 0;
2399 2400
}

2401
static int
2402
testQemuMonitorJSONqemuMonitorJSONGetDumpGuestMemoryCapability(const void *opaque)
2403
{
2404 2405
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
    int cap;
    const char *reply =
        "{"
        "    \"return\": {"
        "        \"formats\": ["
        "            \"elf\","
        "            \"kdump-zlib\","
        "            \"kdump-lzo\","
        "            \"kdump-snappy\""
        "        ]"
        "    },"
        "    \"id\": \"libvirt-9\""
        "}";
2419
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2420

2421
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2422 2423 2424 2425
        return -1;

    if (qemuMonitorTestAddItem(test, "query-dump-guest-memory-capability",
                               reply) < 0)
2426
        return -1;
2427 2428 2429 2430 2431 2432 2433 2434

    cap = qemuMonitorJSONGetDumpGuestMemoryCapability(
                                    qemuMonitorTestGetMonitor(test), "elf");

    if (cap != 1) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected capability: %d, expecting 1",
                       cap);
2435
        return -1;
2436 2437
    }

2438
    return 0;
2439
}
2440 2441 2442 2443

struct testCPUData {
    const char *name;
    virDomainXMLOptionPtr xmlopt;
2444
    virHashTablePtr schema;
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
};


static int
testQemuMonitorJSONGetCPUData(const void *opaque)
{
    const struct testCPUData *data = opaque;
    virCPUDataPtr cpuData = NULL;
    char *jsonFile = NULL;
    char *dataFile = NULL;
    char *jsonStr = NULL;
    char *actual = NULL;
    int ret = -1;
2458
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2459

2460
    if (!(test = qemuMonitorTestNewSchema(data->xmlopt, data->schema)))
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
        return -1;

    if (virAsprintf(&jsonFile,
                    "%s/qemumonitorjsondata/qemumonitorjson-getcpu-%s.json",
                    abs_srcdir, data->name) < 0 ||
        virAsprintf(&dataFile,
                    "%s/qemumonitorjsondata/qemumonitorjson-getcpu-%s.data",
                    abs_srcdir, data->name) < 0)
        goto cleanup;

2471
    if (virTestLoadFile(jsonFile, &jsonStr) < 0)
2472 2473
        goto cleanup;

2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
    if (qemuMonitorTestAddItem(test, "qom-list",
                               "{"
                               "    \"return\": ["
                               "        {"
                               "            \"name\": \"filtered-features\","
                               "            \"type\": \"X86CPUFeatureWordInfo\""
                               "        },"
                               "        {"
                               "            \"name\": \"feature-words\","
                               "            \"type\": \"X86CPUFeatureWordInfo\""
                               "        }"
                               "    ],"
                               "    \"id\": \"libvirt-19\""
                               "}") < 0)
        goto cleanup;

2490 2491 2492
    if (qemuMonitorTestAddItem(test, "qom-get", jsonStr) < 0)
        goto cleanup;

2493 2494
    if (qemuMonitorJSONGetGuestCPUx86(qemuMonitorTestGetMonitor(test),
                                      &cpuData, NULL) < 0)
2495 2496
        goto cleanup;

J
Jiri Denemark 已提交
2497
    if (!(actual = virCPUDataFormat(cpuData)))
2498 2499
        goto cleanup;

2500
    if (virTestCompareToFile(actual, dataFile) < 0)
2501 2502 2503
        goto cleanup;

    ret = 0;
2504
 cleanup:
2505 2506 2507 2508
    VIR_FREE(jsonFile);
    VIR_FREE(dataFile);
    VIR_FREE(jsonStr);
    VIR_FREE(actual);
J
Jiri Denemark 已提交
2509
    virCPUDataFree(cpuData);
2510 2511 2512
    return ret;
}

2513 2514 2515
static int
testQemuMonitorJSONGetNonExistingCPUData(const void *opaque)
{
2516 2517
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2518 2519
    virCPUDataPtr cpuData = NULL;
    int rv, ret = -1;
2520
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2521

2522
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
        return -1;

    if (qemuMonitorTestAddItem(test, "qom-list",
                               "{"
                               "    \"id\": \"libvirt-7\","
                               "    \"error\": {"
                               "        \"class\": \"CommandNotFound\","
                               "        \"desc\": \"The command qom-list has not been found\""
                               "    }"
                               "}") < 0)
        goto cleanup;

2535 2536
    rv = qemuMonitorJSONGetGuestCPUx86(qemuMonitorTestGetMonitor(test),
                                       &cpuData, NULL);
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
    if (rv != -2) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected return value %d, expecting -2", rv);
        goto cleanup;
    }

    if (cpuData) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected allocation of data = %p, expecting NULL",
                       cpuData);
        goto cleanup;
    }

    ret = 0;
2551
 cleanup:
J
Jiri Denemark 已提交
2552
    virCPUDataFree(cpuData);
2553 2554
    return ret;
}
2555

2556
static int
2557
testQemuMonitorJSONGetIOThreads(const void *opaque)
2558
{
2559 2560
    const testGenericData *data = opaque;
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
2561
    qemuMonitorIOThreadInfoPtr *info;
2562 2563 2564
    int ninfo = 0;
    int ret = -1;
    size_t i;
2565
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2566

2567
    if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema)))
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
        return -1;

    if (qemuMonitorTestAddItem(test, "query-iothreads",
                               "{ "
                               "  \"return\": [ "
                               "   { "
                               "     \"id\": \"iothread1\", "
                               "     \"thread-id\": 30992 "
                               "   }, "
                               "   { "
                               "     \"id\": \"iothread2\", "
                               "     \"thread-id\": 30993 "
                               "   } "
                               "  ]"
                               "}") < 0)
        goto cleanup;

    if ((ninfo = qemuMonitorGetIOThreads(qemuMonitorTestGetMonitor(test),
                                         &info)) < 0)
        goto cleanup;

    if (ninfo != 2) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "ninfo %d is not 2", ninfo);
        goto cleanup;
    }

2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
#define CHECK(i, wantiothread_id, wantthread_id) \
    do { \
        if (info[i]->iothread_id != (wantiothread_id)) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "iothread_id %u is not %u", \
                           info[i]->iothread_id, (wantiothread_id)); \
            goto cleanup; \
        } \
        if (info[i]->thread_id != (wantthread_id)) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
                           "thread_id %d is not %d", \
                           info[i]->thread_id, (wantthread_id)); \
            goto cleanup; \
        } \
2609 2610
    } while (0)

2611 2612
    CHECK(0, 1, 30992);
    CHECK(1, 2, 30993);
2613 2614 2615 2616 2617 2618 2619

#undef CHECK

    ret = 0;

 cleanup:
    for (i = 0; i < ninfo; i++)
2620
        VIR_FREE(info[i]);
2621 2622 2623 2624 2625
    VIR_FREE(info);

    return ret;
}

2626 2627 2628 2629
struct testCPUInfoData {
    const char *name;
    size_t maxvcpus;
    virDomainXMLOptionPtr xmlopt;
2630
    bool fast;
2631
    virHashTablePtr schema;
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
};


static char *
testQemuMonitorCPUInfoFormat(qemuMonitorCPUInfoPtr vcpus,
                             size_t nvcpus)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    qemuMonitorCPUInfoPtr vcpu;
    size_t i;

    for (i = 0; i < nvcpus; i++) {
        vcpu = vcpus + i;

        virBufferAsprintf(&buf, "[vcpu libvirt-id='%zu']\n", i);
        virBufferAdjustIndent(&buf, 4);

2649 2650 2651
        virBufferAsprintf(&buf, "online=%s\n", vcpu->online ? "yes" : "no");
        virBufferAsprintf(&buf, "hotpluggable=%s\n", vcpu->hotpluggable ? "yes" : "no");

2652 2653 2654 2655 2656
        if (vcpu->tid)
            virBufferAsprintf(&buf, "thread-id='%llu'\n",
                              (unsigned long long) vcpu->tid);

        if (vcpu->id != 0)
2657
            virBufferAsprintf(&buf, "enable-id='%d'\n", vcpu->id);
2658

2659 2660 2661
        if (vcpu->qemu_id != -1)
            virBufferAsprintf(&buf, "query-cpus-id='%d'\n", vcpu->qemu_id);

2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
        if (vcpu->type)
            virBufferAsprintf(&buf, "type='%s'\n", vcpu->type);

        if (vcpu->alias)
            virBufferAsprintf(&buf, "alias='%s'\n", vcpu->alias);
        if (vcpu->qom_path)
            virBufferAsprintf(&buf, "qom_path='%s'\n", vcpu->qom_path);

        if (vcpu->socket_id != -1 || vcpu->core_id != -1 ||
            vcpu->thread_id != -1 || vcpu->vcpus != 0) {
            virBufferAddLit(&buf, "topology:");
            if (vcpu->socket_id != -1)
                virBufferAsprintf(&buf, " socket='%d'", vcpu->socket_id);
            if (vcpu->core_id != -1)
                virBufferAsprintf(&buf, " core='%d'", vcpu->core_id);
            if (vcpu->thread_id != -1)
                virBufferAsprintf(&buf, " thread='%d'", vcpu->thread_id);
2679 2680
            if (vcpu->node_id != -1)
                virBufferAsprintf(&buf, " node='%d'", vcpu->node_id);
2681 2682 2683 2684 2685
            if (vcpu->vcpus != 0)
                virBufferAsprintf(&buf, " vcpus='%u'", vcpu->vcpus);
            virBufferAddLit(&buf, "\n");
        }

2686 2687 2688
        if (vcpu->halted)
            virBufferAddLit(&buf, "halted\n");

2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
        virBufferAdjustIndent(&buf, -4);
    }

    return virBufferContentAndReset(&buf);
}


static int
testQemuMonitorCPUInfo(const void *opaque)
{
    const struct testCPUInfoData *data = opaque;
2700
    virDomainObjPtr vm = NULL;
2701 2702 2703 2704 2705 2706
    char *queryCpusFile = NULL;
    char *queryHotpluggableFile = NULL;
    char *dataFile = NULL;
    char *queryCpusStr = NULL;
    char *queryHotpluggableStr = NULL;
    char *actual = NULL;
2707
    const char *queryCpusFunction;
2708 2709 2710
    qemuMonitorCPUInfoPtr vcpus = NULL;
    int rc;
    int ret = -1;
2711
    VIR_AUTOPTR(qemuMonitorTest) test = NULL;
2712

2713
    if (!(test = qemuMonitorTestNewSchema(data->xmlopt, data->schema)))
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
        return -1;

    if (virAsprintf(&queryCpusFile,
                    "%s/qemumonitorjsondata/qemumonitorjson-cpuinfo-%s-cpus.json",
                    abs_srcdir, data->name) < 0 ||
        virAsprintf(&queryHotpluggableFile,
                    "%s/qemumonitorjsondata/qemumonitorjson-cpuinfo-%s-hotplug.json",
                    abs_srcdir, data->name) < 0 ||
        virAsprintf(&dataFile,
                    "%s/qemumonitorjsondata/qemumonitorjson-cpuinfo-%s.data",
                    abs_srcdir, data->name) < 0)
        goto cleanup;

    if (virTestLoadFile(queryCpusFile, &queryCpusStr) < 0)
        goto cleanup;

    if (virTestLoadFile(queryHotpluggableFile, &queryHotpluggableStr) < 0)
        goto cleanup;

    if (qemuMonitorTestAddItem(test, "query-hotpluggable-cpus",
                               queryHotpluggableStr) < 0)
        goto cleanup;

2737 2738 2739 2740 2741 2742
    if (data->fast)
        queryCpusFunction = "query-cpus-fast";
    else
        queryCpusFunction = "query-cpus";

    if (qemuMonitorTestAddItem(test, queryCpusFunction, queryCpusStr) < 0)
2743 2744
        goto cleanup;

2745 2746
    vm = qemuMonitorTestGetDomainObj(test);
    if (!vm)
2747
        goto cleanup;
2748

2749
    rc = qemuMonitorGetCPUInfo(qemuMonitorTestGetMonitor(test),
2750
                               &vcpus, data->maxvcpus, true, data->fast);
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772

    if (rc < 0)
        goto cleanup;

    actual = testQemuMonitorCPUInfoFormat(vcpus, data->maxvcpus);

    if (virTestCompareToFile(actual, dataFile) < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_FREE(queryCpusFile);
    VIR_FREE(queryHotpluggableFile);
    VIR_FREE(dataFile);
    VIR_FREE(queryCpusStr);
    VIR_FREE(queryHotpluggableStr);
    VIR_FREE(actual);
    qemuMonitorCPUInfoFree(vcpus, data->maxvcpus);
    return ret;
}


2773 2774 2775 2776
static int
testBlockNodeNameDetectFormat(void *payload,
                              const void *name,
                              void *opaque)
2777
{
2778 2779 2780
    qemuBlockNodeNameBackingChainDataPtr entry = payload;
    const char *diskalias = name;
    virBufferPtr buf = opaque;
2781 2782 2783

    virBufferSetIndent(buf, 0);

2784 2785
    virBufferAdd(buf, diskalias, -1);
    virBufferAddLit(buf, "\n");
2786

2787
    while (entry) {
2788 2789 2790
        virBufferAsprintf(buf, "filename    : '%s'\n", entry->qemufilename);
        virBufferAsprintf(buf, "format node : '%s'\n",
                          NULLSTR(entry->nodeformat));
2791
        virBufferAsprintf(buf, "format drv  : '%s'\n", NULLSTR(entry->drvformat));
2792 2793
        virBufferAsprintf(buf, "storage node: '%s'\n",
                          NULLSTR(entry->nodestorage));
2794
        virBufferAsprintf(buf, "storage drv : '%s'\n", NULLSTR(entry->drvstorage));
2795 2796 2797

        virBufferAdjustIndent(buf, 2);

2798
        entry = entry->backing;
2799 2800 2801 2802
    }

    virBufferSetIndent(buf, 0);
    virBufferAddLit(buf, "\n");
2803
    return 0;
2804 2805 2806 2807 2808 2809
}


static int
testBlockNodeNameDetect(const void *opaque)
{
2810
    const char *testname = opaque;
2811
    const char *pathprefix = "qemumonitorjsondata/qemumonitorjson-nodename-";
2812 2813
    char *resultFile = NULL;
    char *actual = NULL;
2814
    virJSONValuePtr namedNodesJson = NULL;
2815
    virJSONValuePtr blockstatsJson = NULL;
2816 2817 2818 2819
    virHashTablePtr nodedata = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    int ret = -1;

2820
    if (virAsprintf(&resultFile, "%s/%s%s.result",
2821
                    abs_srcdir, pathprefix, testname) < 0)
2822 2823
        goto cleanup;

2824
    if (!(namedNodesJson = virTestLoadFileJSON(pathprefix, testname,
2825
                                               "-named-nodes.json", NULL)))
2826 2827
        goto cleanup;

2828
    if (!(blockstatsJson = virTestLoadFileJSON(pathprefix, testname,
2829 2830 2831 2832 2833
                                               "-blockstats.json", NULL)))
        goto cleanup;

    if (!(nodedata = qemuBlockNodeNameGetBackingChain(namedNodesJson,
                                                      blockstatsJson)))
2834 2835
        goto cleanup;

2836
    virHashForEach(nodedata, testBlockNodeNameDetectFormat, &buf);
2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853

    virBufferTrim(&buf, "\n", -1);

    if (virBufferCheckError(&buf) < 0)
        goto cleanup;

    actual = virBufferContentAndReset(&buf);

    if (virTestCompareToFile(actual, resultFile) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    VIR_FREE(resultFile);
    VIR_FREE(actual);
    virHashFree(nodedata);
2854
    virJSONValueFree(namedNodesJson);
2855
    virJSONValueFree(blockstatsJson);
2856 2857 2858 2859 2860

    return ret;
}


2861 2862 2863 2864 2865 2866
struct testQAPISchemaData {
    virHashTablePtr schema;
    const char *name;
    const char *query;
    const char *json;
    bool success;
2867 2868
    int rc;
    bool replyobj;
2869 2870 2871
};


2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
static int
testQAPISchemaQuery(const void *opaque)
{
    const struct testQAPISchemaData *data = opaque;
    virJSONValuePtr replyobj = NULL;
    int rc;

    rc = virQEMUQAPISchemaPathGet(data->query, data->schema, &replyobj);

    if (data->rc != rc || data->replyobj != !!replyobj) {
        VIR_TEST_VERBOSE("\n success: expected '%d' got '%d', replyobj: expected '%d' got '%d'",
                         data->rc, rc, data->replyobj, !!replyobj);
        return -1;
    }

    return 0;
}


2891
static int
2892
testQAPISchemaValidate(const void *opaque)
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912
{
    const struct testQAPISchemaData *data = opaque;
    virBuffer debug = VIR_BUFFER_INITIALIZER;
    virJSONValuePtr schemaroot;
    virJSONValuePtr json = NULL;
    int ret = -1;

    if (virQEMUQAPISchemaPathGet(data->query, data->schema, &schemaroot) < 0)
        goto cleanup;

    if (!(json = virJSONValueFromString(data->json)))
        goto cleanup;

    if ((testQEMUSchemaValidate(json, schemaroot, data->schema, &debug) == 0) != data->success) {
        if (!data->success)
            VIR_TEST_VERBOSE("\nschema validation should have failed\n");
    } else {
        ret = 0;
    }

2913
    if (virTestGetDebug() >= 3 ||
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927
        (ret < 0 && virTestGetVerbose())) {
        char *debugstr = virBufferContentAndReset(&debug);
        fprintf(stderr, "\n%s\n", debugstr);
        VIR_FREE(debugstr);
    }


 cleanup:
    virBufferFreeAndReset(&debug);
    virJSONValueFree(json);
    return ret;
}


2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
static void
testQueryJobsPrintJob(virBufferPtr buf,
                      qemuMonitorJobInfoPtr job)
{
    virBufferAddLit(buf, "[job]\n");
    virBufferAsprintf(buf, "id=%s\n", NULLSTR(job->id));
    virBufferAsprintf(buf, "type=%s\n", NULLSTR(qemuMonitorJobTypeToString(job->type)));
    virBufferAsprintf(buf, "status=%s\n", NULLSTR(qemuMonitorJobStatusTypeToString(job->status)));
    virBufferAsprintf(buf, "error=%s\n", NULLSTR(job->error));
    virBufferAddLit(buf, "\n");
}


struct testQueryJobsData {
    const char *name;
    virDomainXMLOptionPtr xmlopt;
};


static int
testQueryJobs(const void *opaque)
{
    const struct testQueryJobsData *data = opaque;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(data->xmlopt);
    VIR_AUTOFREE(char *) filenameJSON = NULL;
    VIR_AUTOFREE(char *) fileJSON = NULL;
    VIR_AUTOFREE(char *) filenameResult = NULL;
    VIR_AUTOFREE(char *) actual = NULL;
    qemuMonitorJobInfoPtr *jobs = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    size_t njobs = 0;
    size_t i;
    int ret = -1;

2962 2963 2964
    if (!test)
        return -1;

2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
    if (virAsprintf(&filenameJSON,
                    abs_srcdir "/qemumonitorjsondata/query-jobs-%s.json",
                    data->name) < 0 ||
        virAsprintf(&filenameResult,
                    abs_srcdir "/qemumonitorjsondata/query-jobs-%s.result",
                    data->name) < 0)
        goto cleanup;

    if (virTestLoadFile(filenameJSON, &fileJSON) < 0)
        goto cleanup;

    if (qemuMonitorTestAddItem(test, "query-jobs", fileJSON) < 0)
        goto cleanup;

    if (qemuMonitorJSONGetJobInfo(qemuMonitorTestGetMonitor(test),
                                  &jobs, &njobs) < 0)
        goto cleanup;

    for (i = 0; i < njobs; i++)
        testQueryJobsPrintJob(&buf, jobs[i]);

    virBufferTrim(&buf, "\n", -1);

    if (virBufferCheckError(&buf) < 0)
        goto cleanup;

    actual = virBufferContentAndReset(&buf);

    if (virTestCompareToFile(actual, filenameResult) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
2999 3000 3001
    for (i = 0; i < njobs; i++)
        qemuMonitorJobInfoFree(jobs[i]);
    VIR_FREE(jobs);
3002 3003 3004 3005 3006
    qemuMonitorTestFree(test);
    return ret;
}


3007 3008 3009 3010
static int
mymain(void)
{
    int ret = 0;
3011
    virQEMUDriver driver;
3012
    testQemuMonitorJSONSimpleFuncData simpleFunc;
3013
    struct testQAPISchemaData qapiData;
3014 3015
    virJSONValuePtr metaschema = NULL;
    char *metaschemastr = NULL;
3016

3017
#if !WITH_YAJL
J
Ján Tomko 已提交
3018
    fputs("libvirt not compiled with JSON support, skipping this test\n", stderr);
3019 3020 3021
    return EXIT_AM_SKIP;
#endif

3022
    if (virThreadInitialize() < 0 ||
3023
        qemuTestDriverInit(&driver) < 0)
3024
        return EXIT_FAILURE;
3025 3026 3027

    virEventRegisterDefaultImpl();

3028 3029 3030 3031 3032 3033
    if (!(qapiData.schema = testQEMUSchemaLoad())) {
        VIR_TEST_VERBOSE("failed to load qapi schema\n");
        ret = -1;
        goto cleanup;
    }

3034
#define DO_TEST(name) \
3035 3036 3037 3038 3039
    do { \
        testGenericData data = { driver.xmlopt, qapiData.schema }; \
        if (virTestRun(# name, testQemuMonitorJSON ## name, &data) < 0) \
            ret = -1; \
    } while (0)
3040

3041
#define DO_TEST_SIMPLE(CMD, FNC, ...) \
3042
    simpleFunc = (testQemuMonitorJSONSimpleFuncData) {.cmd = CMD, .func = FNC, \
3043 3044 3045
                                       .xmlopt = driver.xmlopt, \
                                       .schema = qapiData.schema, \
                                       __VA_ARGS__ }; \
3046
    if (virTestRun(# FNC, testQemuMonitorJSONSimpleFunc, &simpleFunc) < 0) \
3047 3048
        ret = -1

3049
#define DO_TEST_GEN(name, ...) \
3050
    simpleFunc = (testQemuMonitorJSONSimpleFuncData) {.xmlopt = driver.xmlopt, \
3051
                                                      .schema = qapiData.schema \
3052 3053
                                                     __VA_ARGS__ }; \
    if (virTestRun(# name, testQemuMonitorJSON ## name, &simpleFunc) < 0) \
3054 3055
        ret = -1

3056
#define DO_TEST_CPU_DATA(name) \
3057
    do { \
3058
        struct testCPUData data = { name, driver.xmlopt, qapiData.schema }; \
3059 3060 3061
        const char *label = "GetCPUData(" name ")"; \
        if (virTestRun(label, testQemuMonitorJSONGetCPUData, &data) < 0) \
            ret = -1; \
3062 3063
    } while (0)

3064 3065
#define DO_TEST_CPU_INFO(name, maxvcpus) \
    do { \
3066 3067
        struct testCPUInfoData data = {name, maxvcpus, driver.xmlopt, false, \
                                       qapiData.schema}; \
3068 3069 3070 3071 3072 3073 3074
        if (virTestRun("GetCPUInfo(" name ")", testQemuMonitorCPUInfo, \
                       &data) < 0) \
            ret = -1; \
    } while (0)

#define DO_TEST_CPU_INFO_FAST(name, maxvcpus) \
    do { \
3075 3076
        struct testCPUInfoData data = {name, maxvcpus, driver.xmlopt, true, \
                                       qapiData.schema }; \
3077 3078 3079
        if (virTestRun("GetCPUInfo(" name ")", testQemuMonitorCPUInfo, \
                       &data) < 0) \
            ret = -1; \
3080 3081
    } while (0)

3082
    DO_TEST(GetStatus);
3083
    DO_TEST(GetVersion);
3084
    DO_TEST(GetMachines);
3085
    DO_TEST(GetCPUDefinitions);
3086
    DO_TEST(GetCommands);
3087
    DO_TEST(GetTPMModels);
3088
    DO_TEST(GetCommandLineOptionParameters);
3089
    if (qemuMonitorJSONTestAttachChardev(driver.xmlopt, qapiData.schema) < 0)
3090
        ret = -1;
3091
    DO_TEST(DetachChardev);
3092
    DO_TEST(GetListPaths);
3093
    DO_TEST(GetObjectProperty);
3094
    DO_TEST(SetObjectProperty);
3095
    DO_TEST(GetDeviceAliases);
3096
    DO_TEST(CPU);
3097
    DO_TEST(GetNonExistingCPUData);
3098
    DO_TEST(GetIOThreads);
3099
    DO_TEST_SIMPLE("qmp_capabilities", qemuMonitorJSONSetCapabilities);
3100
    DO_TEST_SIMPLE("system_powerdown", qemuMonitorJSONSystemPowerdown);
3101 3102 3103 3104 3105
    DO_TEST_SIMPLE("system_reset", qemuMonitorJSONSystemReset);
    DO_TEST_SIMPLE("migrate_cancel", qemuMonitorJSONMigrateCancel);
    DO_TEST_SIMPLE("inject-nmi", qemuMonitorJSONInjectNMI);
    DO_TEST_SIMPLE("system_wakeup", qemuMonitorJSONSystemWakeup);
    DO_TEST_SIMPLE("nbd-server-stop", qemuMonitorJSONNBDServerStop);
3106
    DO_TEST_SIMPLE("rtc-reset-reinjection", qemuMonitorJSONRTCResetReinjection);
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
    DO_TEST_GEN(qemuMonitorJSONSetLink);
    DO_TEST_GEN(qemuMonitorJSONBlockResize);
    DO_TEST_GEN(qemuMonitorJSONSetPassword);
    DO_TEST_GEN(qemuMonitorJSONExpirePassword);
    DO_TEST_GEN(qemuMonitorJSONSetBalloon);
    DO_TEST_GEN(qemuMonitorJSONSetCPU);
    DO_TEST_GEN(qemuMonitorJSONEjectMedia);
    DO_TEST_GEN(qemuMonitorJSONChangeMedia);
    DO_TEST_GEN(qemuMonitorJSONSaveVirtualMemory);
    DO_TEST_GEN(qemuMonitorJSONSavePhysicalMemory);
    DO_TEST_GEN(qemuMonitorJSONSetMigrationSpeed);
    DO_TEST_GEN(qemuMonitorJSONSetMigrationDowntime);
    DO_TEST_GEN(qemuMonitorJSONMigrate);
    DO_TEST_GEN(qemuMonitorJSONDump);
    DO_TEST_GEN(qemuMonitorJSONGraphicsRelocate);
    DO_TEST_GEN(qemuMonitorJSONAddNetdev);
    DO_TEST_GEN(qemuMonitorJSONRemoveNetdev);
    DO_TEST_GEN(qemuMonitorJSONDelDevice);
    DO_TEST_GEN(qemuMonitorJSONAddDevice);
    DO_TEST_GEN(qemuMonitorJSONDriveMirror);
3127
    DO_TEST_GEN(qemuMonitorJSONBlockdevMirror);
3128
    DO_TEST_GEN(qemuMonitorJSONBlockStream);
3129 3130 3131 3132 3133 3134
    DO_TEST_GEN(qemuMonitorJSONBlockCommit);
    DO_TEST_GEN(qemuMonitorJSONDrivePivot);
    DO_TEST_GEN(qemuMonitorJSONScreendump);
    DO_TEST_GEN(qemuMonitorJSONOpenGraphics);
    DO_TEST_GEN(qemuMonitorJSONNBDServerAdd);
    DO_TEST_GEN(qemuMonitorJSONDetachCharDev);
3135 3136 3137 3138
    DO_TEST_GEN(qemuMonitorJSONBlockdevTrayOpen);
    DO_TEST_GEN(qemuMonitorJSONBlockdevTrayClose);
    DO_TEST_GEN(qemuMonitorJSONBlockdevMediumRemove);
    DO_TEST_GEN(qemuMonitorJSONBlockdevMediumInsert);
E
Eric Blake 已提交
3139 3140 3141
    DO_TEST_GEN(qemuMonitorJSONAddBitmap);
    DO_TEST_GEN(qemuMonitorJSONEnableBitmap);
    DO_TEST_GEN(qemuMonitorJSONDeleteBitmap);
3142
    DO_TEST_GEN(qemuMonitorJSONJobDismiss);
3143
    DO_TEST_GEN(qemuMonitorJSONJobCancel);
3144
    DO_TEST_GEN(qemuMonitorJSONJobComplete);
3145
    DO_TEST(qemuMonitorJSONGetBalloonInfo);
3146
    DO_TEST(qemuMonitorJSONGetBlockInfo);
3147
    DO_TEST(qemuMonitorJSONGetAllBlockStatsInfo);
3148
    DO_TEST(qemuMonitorJSONGetMigrationCacheSize);
3149
    DO_TEST(qemuMonitorJSONGetMigrationStats);
3150
    DO_TEST(qemuMonitorJSONGetChardevInfo);
3151
    DO_TEST(qemuMonitorJSONSetBlockIoThrottle);
3152
    DO_TEST(qemuMonitorJSONGetTargetArch);
3153
    DO_TEST(qemuMonitorJSONGetMigrationCapabilities);
3154
    DO_TEST(qemuMonitorJSONQueryCPUs);
3155
    DO_TEST(qemuMonitorJSONGetVirtType);
3156
    DO_TEST(qemuMonitorJSONSendKey);
3157
    DO_TEST(qemuMonitorJSONGetDumpGuestMemoryCapability);
3158
    DO_TEST(qemuMonitorJSONSendKeyHoldtime);
3159
    DO_TEST(qemuMonitorSupportsActiveCommit);
3160
    DO_TEST(qemuMonitorJSONNBDServerStart);
E
Eric Blake 已提交
3161
    DO_TEST(qemuMonitorJSONMergeBitmaps);
3162

3163 3164
    DO_TEST_CPU_DATA("host");
    DO_TEST_CPU_DATA("full");
3165
    DO_TEST_CPU_DATA("ecx");
3166

3167
    DO_TEST_CPU_INFO("x86-basic-pluggable", 8);
3168
    DO_TEST_CPU_INFO("x86-full", 11);
3169
    DO_TEST_CPU_INFO("x86-node-full", 8);
3170
    DO_TEST_CPU_INFO_FAST("x86-full-fast", 11);
3171

3172 3173 3174
    DO_TEST_CPU_INFO("ppc64-basic", 24);
    DO_TEST_CPU_INFO("ppc64-hotplug-1", 24);
    DO_TEST_CPU_INFO("ppc64-hotplug-2", 24);
3175
    DO_TEST_CPU_INFO("ppc64-hotplug-4", 24);
3176
    DO_TEST_CPU_INFO("ppc64-no-threads", 16);
3177

3178 3179
    DO_TEST_CPU_INFO_FAST("s390-fast", 2);

3180 3181 3182 3183 3184
#define DO_TEST_BLOCK_NODE_DETECT(testname) \
    do { \
        if (virTestRun("node-name-detect(" testname ")", \
                       testBlockNodeNameDetect, testname) < 0) \
            ret = -1; \
3185 3186
    } while (0)

3187
    DO_TEST_BLOCK_NODE_DETECT("basic");
3188
    DO_TEST_BLOCK_NODE_DETECT("same-backing");
3189
    DO_TEST_BLOCK_NODE_DETECT("relative");
3190
    DO_TEST_BLOCK_NODE_DETECT("gluster");
3191
    DO_TEST_BLOCK_NODE_DETECT("blockjob");
3192
    DO_TEST_BLOCK_NODE_DETECT("luks");
3193
    DO_TEST_BLOCK_NODE_DETECT("iscsi");
3194 3195
    DO_TEST_BLOCK_NODE_DETECT("old");
    DO_TEST_BLOCK_NODE_DETECT("empty");
3196 3197 3198

#undef DO_TEST_BLOCK_NODE_DETECT

3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
#define DO_TEST_QAPI_QUERY(nme, qry, scc, rplobj) \
    do { \
        qapiData.name = nme; \
        qapiData.query = qry; \
        qapiData.rc = scc; \
        qapiData.replyobj = rplobj; \
        if (virTestRun("qapi schema query" nme, testQAPISchemaQuery, &qapiData) < 0)\
            ret = -1; \
    } while (0)

3209 3210 3211 3212 3213 3214
    DO_TEST_QAPI_QUERY("command", "blockdev-add", 1, true);
    DO_TEST_QAPI_QUERY("event", "RTC_CHANGE", 1, true);
    DO_TEST_QAPI_QUERY("object property", "screendump/arg-type/device", 1, true);
    DO_TEST_QAPI_QUERY("optional property", "block-commit/arg-type/*top", 1, true);
    DO_TEST_QAPI_QUERY("variant", "blockdev-add/arg-type/+file", 1, true);
    DO_TEST_QAPI_QUERY("variant property", "blockdev-add/arg-type/+file/filename", 1, true);
3215
    DO_TEST_QAPI_QUERY("enum value", "query-status/ret-type/status/^debug", 1, false);
3216
    DO_TEST_QAPI_QUERY("builtin type", "query-qmp-schema/ret-type/name/!string", 1, false);
3217 3218 3219
    DO_TEST_QAPI_QUERY("alternate variant 1", "blockdev-add/arg-type/+qcow2/backing/!null", 1, false);
    DO_TEST_QAPI_QUERY("alternate variant 2", "blockdev-add/arg-type/+qcow2/backing/!string", 1, false);
    DO_TEST_QAPI_QUERY("alternate variant 3", "blockdev-add/arg-type/+qcow2/backing/+file/filename", 1, true);
3220 3221 3222 3223

    DO_TEST_QAPI_QUERY("nonexistent command", "nonexistent", 0, false);
    DO_TEST_QAPI_QUERY("nonexistent attr", "screendump/arg-type/nonexistent", 0, false);
    DO_TEST_QAPI_QUERY("nonexistent variant", "blockdev-add/arg-type/+nonexistent", 0, false);
3224 3225
    DO_TEST_QAPI_QUERY("nonexistent enum value", "query-status/ret-type/status/^nonexistentdebug", 0, false);
    DO_TEST_QAPI_QUERY("broken query for enum value", "query-status/ret-type/status/^debug/test", -1, false);
3226
    DO_TEST_QAPI_QUERY("builtin type", "query-qmp-schema/ret-type/name/!number", 0, false);
3227 3228 3229 3230

#undef DO_TEST_QAPI_QUERY


3231
#define DO_TEST_QAPI_VALIDATE(nme, rootquery, scc, jsonstr) \
3232 3233 3234 3235 3236
    do { \
        qapiData.name = nme; \
        qapiData.query = rootquery; \
        qapiData.success = scc; \
        qapiData.json = jsonstr; \
3237
        if (virTestRun("qapi schema validate" nme, testQAPISchemaValidate, &qapiData) < 0)\
3238 3239 3240
            ret = -1; \
    } while (0)

3241

3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266
    DO_TEST_QAPI_VALIDATE("string", "trace-event-get-state/arg-type", true,
                          "{\"name\":\"test\"}");
    DO_TEST_QAPI_VALIDATE("all attrs", "trace-event-get-state/arg-type", true,
                          "{\"name\":\"test\", \"vcpu\":123}");
    DO_TEST_QAPI_VALIDATE("attr type mismatch", "trace-event-get-state/arg-type", false,
                          "{\"name\":123}");
    DO_TEST_QAPI_VALIDATE("missing mandatory attr", "trace-event-get-state/arg-type", false,
                          "{\"vcpu\":123}");
    DO_TEST_QAPI_VALIDATE("attr name not present", "trace-event-get-state/arg-type", false,
                          "{\"name\":\"test\", \"blah\":123}");
    DO_TEST_QAPI_VALIDATE("variant", "blockdev-add/arg-type", true,
                          "{\"driver\":\"file\", \"filename\":\"ble\"}");
    DO_TEST_QAPI_VALIDATE("variant wrong", "blockdev-add/arg-type", false,
                          "{\"driver\":\"filefilefilefile\", \"filename\":\"ble\"}");
    DO_TEST_QAPI_VALIDATE("variant missing mandatory", "blockdev-add/arg-type", false,
                          "{\"driver\":\"file\", \"pr-manager\":\"ble\"}");
    DO_TEST_QAPI_VALIDATE("variant missing discriminator", "blockdev-add/arg-type", false,
                          "{\"node-name\":\"dfgfdg\"}");
    DO_TEST_QAPI_VALIDATE("alternate 1", "blockdev-add/arg-type", true,
                          "{\"driver\":\"qcow2\","
                          "\"file\": { \"driver\":\"file\", \"filename\":\"ble\"}}");
    DO_TEST_QAPI_VALIDATE("alternate 2", "blockdev-add/arg-type", true,
                          "{\"driver\":\"qcow2\",\"file\": \"somepath\"}");
    DO_TEST_QAPI_VALIDATE("alternate 2", "blockdev-add/arg-type", false,
                          "{\"driver\":\"qcow2\",\"file\": 1234}");
3267

3268 3269 3270
    if (!(metaschema = testQEMUSchemaGetLatest()) ||
        !(metaschemastr = virJSONValueToString(metaschema, false))) {
        VIR_TEST_VERBOSE("failed to load latest qapi schema\n");
3271 3272 3273 3274
        ret = -1;
        goto cleanup;
    }

3275
    DO_TEST_QAPI_VALIDATE("schema-meta", "query-qmp-schema/ret-type", true,
3276
                        metaschemastr);
3277 3278


3279
#undef DO_TEST_QAPI_VALIDATE
3280

3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
#define DO_TEST_QUERY_JOBS(name) \
    do { \
        struct testQueryJobsData data = { name, driver.xmlopt}; \
        if (virTestRun("query-jobs-" name, testQueryJobs, &data) < 0) \
            ret = -1; \
    } while (0)

    DO_TEST_QUERY_JOBS("empty");
    DO_TEST_QUERY_JOBS("create");

#undef DO_TEST_QUERY_JOBS

3293
 cleanup:
3294 3295
    VIR_FREE(metaschemastr);
    virJSONValueFree(metaschema);
3296 3297
    virHashFree(qapiData.schema);
    qemuTestDriverFree(&driver);
3298 3299 3300
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

3301
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virdeterministichash"))