qemumonitorjsontest.c 79.6 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 24
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "testutils.h"
#include "testutilsqemu.h"
#include "qemumonitortestutils.h"
25
#include "qemu/qemu_conf.h"
26
#include "qemu/qemu_monitor_json.h"
27
#include "virthread.h"
28
#include "virerror.h"
29
#include "virstring.h"
30
#include "cpu/cpu.h"
31 32 33 34


#define VIR_FROM_THIS VIR_FROM_NONE

35 36 37 38 39 40 41 42 43
typedef struct _testQemuMonitorJSONSimpleFuncData testQemuMonitorJSONSimpleFuncData;
typedef testQemuMonitorJSONSimpleFuncData *testQemuMonitorJSONSimpleFuncDataPtr;
struct _testQemuMonitorJSONSimpleFuncData {
    const char *cmd;
    int (* func) (qemuMonitorPtr mon);
    virDomainXMLOptionPtr xmlopt;
    const char *reply;
};

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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
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,"
"                \"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\""
"        }"
"    ],"
"    \"id\": \"libvirt-10\""
"}";

115 116 117
static int
testQemuMonitorJSONGetStatus(const void *data)
{
118
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
119
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    int ret = -1;
    bool running = false;
    virDomainPausedReason reason = 0;

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "query-status",
                               "{ "
                               "    \"return\": { "
                               "        \"status\": \"running\", "
                               "        \"singlestep\": false, "
                               "        \"running\": true "
                               "    } "
                               "}") < 0)
        goto cleanup;
    if (qemuMonitorTestAddItem(test, "query-status",
                               "{ "
                               "    \"return\": { "
                               "        \"singlestep\": false, "
                               "        \"running\": false "
                               "    } "
                               "}") < 0)
        goto cleanup;
    if (qemuMonitorTestAddItem(test, "query-status",
                               "{ "
                               "    \"return\": { "
                               "        \"status\": \"inmigrate\", "
                               "        \"singlestep\": false, "
                               "        \"running\": false "
                               "    } "
                               "}") < 0)
        goto cleanup;

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
        goto cleanup;

    if (!running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not true");
        goto cleanup;
    }

    if (reason != VIR_DOMAIN_PAUSED_UNKNOWN) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Reason was unexpectedly set to %d", reason);
        goto cleanup;
    }

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
        goto cleanup;

    if (running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not false");
        goto cleanup;
    }

    if (reason != VIR_DOMAIN_PAUSED_UNKNOWN) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Reason was unexpectedly set to %d", reason);
        goto cleanup;
    }

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
        goto cleanup;

    if (running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not false");
        goto cleanup;
    }

    if (reason != VIR_DOMAIN_PAUSED_MIGRATION) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Reason was unexpectedly set to %d", reason);
        goto cleanup;
    }

    ret = 0;

204
 cleanup:
205 206 207 208
    qemuMonitorTestFree(test);
    return ret;
}

209 210 211
static int
testQemuMonitorJSONGetVersion(const void *data)
{
212
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
213
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
214 215 216 217
    int ret = -1;
    int major;
    int minor;
    int micro;
218
    char *package = NULL;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

    if (!test)
        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;
    }
275
    VIR_FREE(package);
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

    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;

306
 cleanup:
307
    qemuMonitorTestFree(test);
308
    VIR_FREE(package);
309 310 311
    return ret;
}

312 313 314
static int
testQemuMonitorJSONGetMachines(const void *data)
{
315
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
316
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
317 318
    int ret = -1;
    qemuMonitorMachineInfoPtr *info;
319
    int ninfo = 0;
320
    const char *null = NULL;
321
    size_t i;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 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

    if (!test)
        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;
    }

#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;                                               \
        }                                                               \
    } 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;

384
 cleanup:
385
    qemuMonitorTestFree(test);
386 387 388 389
    for (i = 0; i < ninfo; i++)
        qemuMonitorMachineInfoFree(info[i]);
    VIR_FREE(info);

390 391 392 393
    return ret;
}


394 395 396
static int
testQemuMonitorJSONGetCPUDefinitions(const void *data)
{
397
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
398
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
399 400
    int ret = -1;
    char **cpus = NULL;
401
    int ncpus = 0;
402
    size_t i;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "query-cpu-definitions",
                               "{ "
                               "  \"return\": [ "
                               "   { "
                               "     \"name\": \"qemu64\" "
                               "   }, "
                               "   { "
                               "     \"name\": \"Opteron_G4\" "
                               "   }, "
                               "   { "
                               "     \"name\": \"Westmere\" "
                               "   } "
                               "  ]"
                               "}") < 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;
    }

#define CHECK(i, wantname)                                              \
    do {                                                                \
        if (STRNEQ(cpus[i], (wantname))) {                              \
            virReportError(VIR_ERR_INTERNAL_ERROR,                      \
                           "name %s is not %s",                         \
                           cpus[i], (wantname));                        \
            goto cleanup;                                               \
        }                                                               \
    } while (0)

    CHECK(0, "qemu64");
    CHECK(1, "Opteron_G4");
    CHECK(2, "Westmere");

#undef CHECK

    ret = 0;

451
 cleanup:
452
    qemuMonitorTestFree(test);
453 454 455
    for (i = 0; i < ncpus; i++)
        VIR_FREE(cpus[i]);
    VIR_FREE(cpus);
456 457 458 459
    return ret;
}


460 461 462
static int
testQemuMonitorJSONGetCommands(const void *data)
{
463
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
464
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
465 466
    int ret = -1;
    char **commands = NULL;
467
    int ncommands = 0;
468
    size_t i;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

    if (!test)
        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;
    }

#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;                                               \
        }                                                               \
    } while (0)

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

#undef CHECK
    ret = 0;

516
 cleanup:
517
    qemuMonitorTestFree(test);
518 519 520
    for (i = 0; i < ncommands; i++)
        VIR_FREE(commands[i]);
    VIR_FREE(commands);
521 522 523 524
    return ret;
}


525 526 527
static int
testQemuMonitorJSONGetTPMModels(const void *data)
{
E
Eric Blake 已提交
528
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
529
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
    int ret = -1;
    char **tpmmodels = NULL;
    int ntpmmodels = 0;

    if (!test)
        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;
    }

#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;                                               \
        }                                                               \
    } while (0)

    CHECK(0, "passthrough");

#undef CHECK

    ret = 0;

571
 cleanup:
572 573 574 575 576 577
    qemuMonitorTestFree(test);
    virStringFreeList(tpmmodels);
    return ret;
}


578 579 580
static int
testQemuMonitorJSONGetCommandLineOptionParameters(const void *data)
{
E
Eric Blake 已提交
581
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
582
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
583 584 585
    int ret = -1;
    char **params = NULL;
    int nparams = 0;
586
    bool found = false;
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

    if (!test)
        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",
608 609
                                                             &params,
                                                             NULL)) < 0)
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
        goto cleanup;

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

#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;                                               \
        }                                                               \
    } while (0)

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

#undef CHECK

    virStringFreeList(params);
    params = NULL;

    /* present but empty */
    if ((nparams = qemuMonitorGetCommandLineOptionParameters(qemuMonitorTestGetMonitor(test),
                                                             "acpi",
639 640
                                                             &params,
                                                             &found)) < 0)
641 642 643 644 645 646 647
        goto cleanup;

    if (nparams != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "nparams was %d, expected 0", nparams);
        goto cleanup;
    }
648 649 650 651 652
    if (!found) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "found was false, expected true");
        goto cleanup;
    }
653 654 655 656 657 658 659 660 661 662 663 664
    if (params && params[0]) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "unexpected array contents");
        goto cleanup;
    }

    virStringFreeList(params);
    params = NULL;

    /* no such option */
    if ((nparams = qemuMonitorGetCommandLineOptionParameters(qemuMonitorTestGetMonitor(test),
                                                             "foobar",
665 666
                                                             &params,
                                                             &found)) < 0)
667 668 669 670 671 672 673
        goto cleanup;

    if (nparams != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "nparams was %d, expected 0", nparams);
        goto cleanup;
    }
674 675 676 677 678
    if (found) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "found was true, expected false");
        goto cleanup;
    }
679 680 681 682 683 684 685 686
    if (params && params[0]) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "unexpected array contents");
        goto cleanup;
    }

    ret = 0;

687
 cleanup:
688 689 690 691 692
    qemuMonitorTestFree(test);
    virStringFreeList(params);
    return ret;
}

693 694 695
static int
testQemuMonitorJSONAttachChardev(const void *data)
{
E
Eric Blake 已提交
696
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
697
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    virDomainChrSourceDef chr;
    int ret = 0;

    if (!test)
        return -1;

#define DO_CHECK(chrID, reply, fail)                                \
    if (qemuMonitorTestAddItem(test, "chardev-add", reply) < 0)     \
        goto cleanup;                                               \
    if (qemuMonitorAttachCharDev(qemuMonitorTestGetMonitor(test),   \
                                     chrID, &chr) < 0)              \
        ret = fail ? ret  : -1;                                     \
    else                                                            \
        ret = fail ? -1 : ret;                                      \

#define CHECK(chrID, reply) \
    DO_CHECK(chrID, reply, false)

#define CHECK_FAIL(chrID, reply) \
    DO_CHECK(chrID, reply, true)

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_NULL };
    CHECK("chr_null", "{\"return\": {}}");

722
    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_VC };
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    CHECK("chr_vc", "{\"return\": {}}");

#define PTY_PATH "/dev/ttyS0"
    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_PTY };
    CHECK("chr_pty", "{\"return\": {\"pty\" : \"" PTY_PATH "\"}}");
    if (STRNEQ_NULLABLE(PTY_PATH, chr.data.file.path)) {
        VIR_FREE(chr.data.file.path);
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "expected PTY path: %s got: %s",
                       PTY_PATH, NULLSTR(chr.data.file.path));
        ret = -1;
    }
    VIR_FREE(chr.data.file.path);

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_PTY };
    CHECK_FAIL("chr_pty_fail", "{\"return\": {}}");
#undef PTY_PATH

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_FILE };
    CHECK("chr_file", "{\"return\": {}}");

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_DEV };
    CHECK("chr_dev", "{\"return\": {}}");

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_TCP };
    CHECK("chr_tcp", "{\"return\": {}}");

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_UDP };
    CHECK("chr_udp", "{\"return\": {}}");

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_UNIX };
    CHECK("chr_unix", "{\"return\": {}}");

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_SPICEVMC };
    CHECK_FAIL("chr_spicevmc", "{\"return\": {}}");

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_PIPE };
    CHECK_FAIL("chr_pipe", "{\"return\": {}}");

    chr = (virDomainChrSourceDef) { .type = VIR_DOMAIN_CHR_TYPE_STDIO };
    CHECK_FAIL("chr_stdio", "{\"return\": {}}");

#undef CHECK
#undef CHECK_FAIL
#undef DO_CHECK

769
 cleanup:
770 771 772
    qemuMonitorTestFree(test);
    return ret;
}
773

774 775 776
static int
testQemuMonitorJSONDetachChardev(const void *data)
{
E
Eric Blake 已提交
777
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
778
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
779 780 781 782 783 784 785 786 787 788 789 790 791 792
    int ret = -1;

    if (!test)
        return ret;

    if (qemuMonitorTestAddItem(test, "chardev-remove", "{\"return\": {}}") < 0)
        goto cleanup;

    if (qemuMonitorDetachCharDev(qemuMonitorTestGetMonitor(test),
                                 "dummy_chrID") < 0)
        goto cleanup;

    ret = 0;

793
 cleanup:
794 795 796 797
    qemuMonitorTestFree(test);
    return ret;
}

798 799 800 801 802 803 804 805 806 807 808 809
/*
 * 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
testQemuMonitorJSONGetListPaths(const void *data)
{
E
Eric Blake 已提交
810
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
811
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
    int ret = -1;
    qemuMonitorJSONListPathPtr *paths;
    int npaths = 0;
    size_t i;

    if (!test)
        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;
    }

#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;                                               \
        }                                                               \
    } while (0)

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

#undef CHECK

    ret = 0;

866
 cleanup:
867 868 869 870 871 872 873 874
    qemuMonitorTestFree(test);
    for (i = 0; i < npaths; i++)
        qemuMonitorJSONListPathFree(paths[i]);
    VIR_FREE(paths);
    return ret;
}


875 876 877 878 879 880 881 882 883 884 885 886 887
/*
 * 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
testQemuMonitorJSONGetObjectProperty(const void *data)
{
E
Eric Blake 已提交
888
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
889
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
    int ret = -1;
    qemuMonitorJSONObjectProperty prop;

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "qom-get",
                               "{ \"return\": true }") < 0)
        goto cleanup;

    /* 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)
        goto cleanup;

    if (!prop.val.b) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "expected true, but false returned");
        goto cleanup;
    }

    ret = 0;
916
 cleanup:
917 918 919 920 921
    qemuMonitorTestFree(test);
    return ret;
}


922 923 924 925 926 927 928 929 930
/*
 * 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
testQemuMonitorJSONSetObjectProperty(const void *data)
{
E
Eric Blake 已提交
931
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
932
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
    int ret = -1;
    qemuMonitorJSONObjectProperty prop;

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "qom-set",
                               "{ \"return\": {} }") < 0)
        goto cleanup;
    if (qemuMonitorTestAddItem(test, "qom-get",
                               "{ \"return\": true }") < 0)
        goto cleanup;

    /* 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)
        goto cleanup;

    /* 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)
        goto cleanup;

    if (!prop.val.b) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "expected true, but false returned");
        goto cleanup;
    }

    ret = 0;
974
 cleanup:
975 976 977 978 979
    qemuMonitorTestFree(test);
    return ret;
}


980 981 982
static int
testQemuMonitorJSONGetDeviceAliases(const void *data)
{
E
Eric Blake 已提交
983
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
984
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
985 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 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    int ret = -1;
    char **aliases = NULL;
    char **alias;
    const char *expected[] = {
        "virtio-disk25", "video0", "serial0", "ide0-0-0", "usb", NULL };

    if (!test)
        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;
    for (alias = aliases; *alias; alias++) {
        if (!virStringArrayHasString((char **) expected, *alias)) {
            fprintf(stderr, "got unexpected device alias '%s'\n", *alias);
            ret = -1;
        }
    }
    for (alias = (char **) expected; *alias; alias++) {
        if (!virStringArrayHasString(aliases, *alias)) {
            fprintf(stderr, "missing expected alias '%s'\n", *alias);
            ret = -1;
        }
    }

1034
 cleanup:
1035
    virStringFreeList(aliases);
1036
    qemuMonitorTestFree(test);
1037 1038 1039
    return ret;
}

1040 1041 1042
static int
testQemuMonitorJSONCPU(const void *data)
{
E
Eric Blake 已提交
1043
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
1044 1045 1046 1047 1048
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    bool running = false;
    virDomainPausedReason reason = 0;

1049 1050 1051
    if (!test)
        return -1;

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    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)
        goto cleanup;

    if (qemuMonitorJSONStopCPUs(qemuMonitorTestGetMonitor(test)) < 0)
        goto cleanup;

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
        goto cleanup;

    if (running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not false");
        goto cleanup;
    }

    if (qemuMonitorJSONStartCPUs(qemuMonitorTestGetMonitor(test), NULL) < 0)
        goto cleanup;

    if (qemuMonitorGetStatus(qemuMonitorTestGetMonitor(test),
                             &running, &reason) < 0)
        goto cleanup;

    if (!running) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Running was not true");
        goto cleanup;
    }

    ret = 0;

1094
 cleanup:
1095 1096 1097
    qemuMonitorTestFree(test);
    return ret;
}
1098

1099 1100 1101
static int
testQemuMonitorJSONSimpleFunc(const void *opaque)
{
E
Eric Blake 已提交
1102 1103
    testQemuMonitorJSONSimpleFuncDataPtr data =
        (testQemuMonitorJSONSimpleFuncDataPtr) opaque;
1104 1105 1106 1107 1108
    virDomainXMLOptionPtr xmlopt = data->xmlopt;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    const char *reply = data->reply;
    int ret = -1;

1109 1110 1111
    if (!test)
        return -1;

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
    if (!reply)
        reply = "{\"return\":{}}";

    if (qemuMonitorTestAddItem(test, data->cmd, reply) < 0)
        goto cleanup;

    if (data->func(qemuMonitorTestGetMonitor(test)) < 0)
        goto cleanup;

    ret = 0;
1122
 cleanup:
1123 1124 1125 1126
    qemuMonitorTestFree(test);
    return ret;
}

1127 1128 1129 1130
#define GEN_TEST_FUNC(funcName, ...)                                    \
static int                                                              \
testQemuMonitorJSON ## funcName(const void *opaque)                     \
{                                                                       \
E
Eric Blake 已提交
1131
    const testQemuMonitorJSONSimpleFuncData *data = opaque;             \
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    virDomainXMLOptionPtr xmlopt = data->xmlopt;                        \
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);   \
    const char *reply = data->reply;                                    \
    int ret = -1;                                                       \
                                                                        \
    if (!test)                                                          \
        return -1;                                                      \
                                                                        \
    if (!reply)                                                         \
        reply = "{\"return\":{}}";                                      \
                                                                        \
    if (qemuMonitorTestAddItem(test, data->cmd, reply) < 0)             \
        goto cleanup;                                                   \
                                                                        \
    if (funcName(qemuMonitorTestGetMonitor(test), __VA_ARGS__) < 0)     \
        goto cleanup;                                                   \
                                                                        \
    ret = 0;                                                            \
cleanup:                                                                \
    qemuMonitorTestFree(test);                                          \
    return ret;                                                         \
}

GEN_TEST_FUNC(qemuMonitorJSONSetLink, "vnet0", VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN)
GEN_TEST_FUNC(qemuMonitorJSONBlockResize, "vda", 123456)
GEN_TEST_FUNC(qemuMonitorJSONSetVNCPassword, "secret_password")
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)
GEN_TEST_FUNC(qemuMonitorJSONChangeMedia, "hdc", "/foo/bar", NULL)
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")
1171
GEN_TEST_FUNC(qemuMonitorJSONDump, "dummy_protocol", "dummy_memory_dump_format")
1172 1173 1174 1175 1176 1177 1178 1179 1180
GEN_TEST_FUNC(qemuMonitorJSONGraphicsRelocate, VIR_DOMAIN_GRAPHICS_TYPE_SPICE,
              "localhost", 12345, 12346, NULL)
GEN_TEST_FUNC(qemuMonitorJSONAddNetdev, "some_dummy_netdevstr")
GEN_TEST_FUNC(qemuMonitorJSONRemoveNetdev, "net0")
GEN_TEST_FUNC(qemuMonitorJSONDelDevice, "ide0")
GEN_TEST_FUNC(qemuMonitorJSONAddDevice, "some_dummy_devicestr")
GEN_TEST_FUNC(qemuMonitorJSONSetDrivePassphrase, "vda", "secret_passhprase")
GEN_TEST_FUNC(qemuMonitorJSONDriveMirror, "vdb", "/foo/bar", NULL, 1024,
              VIR_DOMAIN_BLOCK_REBASE_SHALLOW | VIR_DOMAIN_BLOCK_REBASE_REUSE_EXT)
1181
GEN_TEST_FUNC(qemuMonitorJSONBlockCommit, "vdb", "/foo/bar1", "/foo/bar2", NULL, 1024)
1182 1183 1184 1185 1186 1187 1188
GEN_TEST_FUNC(qemuMonitorJSONDrivePivot, "vdb", NULL, NULL)
GEN_TEST_FUNC(qemuMonitorJSONScreendump, "/foo/bar")
GEN_TEST_FUNC(qemuMonitorJSONOpenGraphics, "spice", "spicefd", false)
GEN_TEST_FUNC(qemuMonitorJSONNBDServerStart, "localhost", 12345)
GEN_TEST_FUNC(qemuMonitorJSONNBDServerAdd, "vda", true)
GEN_TEST_FUNC(qemuMonitorJSONDetachCharDev, "serial1")

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258

static int
testQemuMonitorJSONqemuMonitorJSONGetCPUInfo(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    pid_t *cpupids = NULL;
    pid_t expected_cpupids[] = {17622, 17624, 17626, 17628};
    int ncpupids;
    size_t i;

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "query-cpus",
                               "{"
                               "    \"return\": ["
                               "        {"
                               "            \"current\": true,"
                               "            \"CPU\": 0,"
                               "            \"pc\": -2130530478,"
                               "            \"halted\": true,"
                               "            \"thread_id\": 17622"
                               "        },"
                               "        {"
                               "            \"current\": false,"
                               "            \"CPU\": 1,"
                               "            \"pc\": -2130530478,"
                               "            \"halted\": true,"
                               "            \"thread_id\": 17624"
                               "        },"
                               "        {"
                               "            \"current\": false,"
                               "            \"CPU\": 2,"
                               "            \"pc\": -2130530478,"
                               "            \"halted\": true,"
                               "            \"thread_id\": 17626"
                               "        },"
                               "        {"
                               "            \"current\": false,"
                               "            \"CPU\": 3,"
                               "            \"pc\": -2130530478,"
                               "            \"halted\": true,"
                               "            \"thread_id\": 17628"
                               "        }"
                               "    ],"
                               "    \"id\": \"libvirt-7\""
                               "}") < 0)
        goto cleanup;

    ncpupids = qemuMonitorJSONGetCPUInfo(qemuMonitorTestGetMonitor(test), &cpupids);

    if (ncpupids != 4) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Expecting ncpupids = 4 but got %d", ncpupids);
        goto cleanup;
    }

    for (i = 0; i < ncpupids; i++) {
        if (cpupids[i] != expected_cpupids[i]) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "Expecting cpupids[%zu] = %d but got %d",
                           i, expected_cpupids[i], cpupids[i]);
            goto cleanup;
        }
    }

    ret = 0;

1259
 cleanup:
1260 1261 1262 1263 1264
    VIR_FREE(cpupids);
    qemuMonitorTestFree(test);
    return ret;
}

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
static int
testQemuMonitorJSONqemuMonitorJSONGetBalloonInfo(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    unsigned long long currmem;

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "query-balloon",
                               "{"
                               "    \"return\": {"
                               "        \"actual\": 4294967296"
                               "    },"
                               "    \"id\": \"libvirt-9\""
                               "}") < 0)
        goto cleanup;

    if (qemuMonitorJSONGetBalloonInfo(qemuMonitorTestGetMonitor(test), &currmem) < 0)
        goto cleanup;

E
Eric Blake 已提交
1288
    if (currmem != (4294967296ULL/1024)) {
1289 1290 1291 1292 1293 1294 1295
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected currmem value: %llu", currmem);
        goto cleanup;
    }

    ret = 0;

1296
 cleanup:
1297 1298 1299 1300
    qemuMonitorTestFree(test);
    return ret;
}

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
static int
testQemuMonitorJSONqemuMonitorJSONGetVirtType(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    int virtType;

    if (!test)
        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)
        goto cleanup;

    if (qemuMonitorJSONGetVirtType(qemuMonitorTestGetMonitor(test), &virtType) < 0)
        goto cleanup;

    if (virtType != VIR_DOMAIN_VIRT_KVM) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected virt type: %d, expecting %d", virtType, VIR_DOMAIN_VIRT_KVM);
        goto cleanup;
    }

    if (qemuMonitorJSONGetVirtType(qemuMonitorTestGetMonitor(test), &virtType) < 0)
        goto cleanup;

    if (virtType != VIR_DOMAIN_VIRT_QEMU) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected virt type: %d, expecting %d", virtType, VIR_DOMAIN_VIRT_QEMU);
    }

    ret = 0;
1348
 cleanup:
1349 1350 1351 1352
    qemuMonitorTestFree(test);
    return ret;
}

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
static int
testHashEqualQemuDomainDiskInfo(const void *value1, const void *value2)
{
    const struct qemuDomainDiskInfo *info1 = value1, *info2 = value2;

    return memcmp(info1, info2, sizeof(*info1));
}

static int
testQemuMonitorJSONqemuMonitorJSONGetBlockInfo(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    virHashTablePtr blockDevices = NULL, expectedBlockDevices = NULL;
    struct qemuDomainDiskInfo *info;

    if (!test)
        return -1;

E
Eric Blake 已提交
1373 1374
    if (!(blockDevices = virHashCreate(32, virHashValueFree)) ||
        !(expectedBlockDevices = virHashCreate(32, virHashValueFree)))
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
        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->locked = true;
    info->removable = true;
    if (virHashAddEntry(expectedBlockDevices, "ide0-1-0", info) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Unable to create expectedBlockDevices hash table");
        goto cleanup;
    }

1406
    if (qemuMonitorTestAddItem(test, "query-block", queryBlockReply) < 0)
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
        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;
1419
 cleanup:
1420 1421 1422 1423 1424 1425
    virHashFree(blockDevices);
    virHashFree(expectedBlockDevices);
    qemuMonitorTestFree(test);
    return ret;
}

1426 1427 1428 1429 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 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
static int
testQemuMonitorJSONqemuMonitorJSONGetBlockStatsInfo(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    long long rd_req, rd_bytes, rd_total_times;
    long long wr_req, wr_bytes, wr_total_times;
    long long flush_req, flush_total_times, errs;
    int nparams;
    unsigned long long extent;

    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_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\": 1004952,"
        "                \"flush_operations\": 0,"
        "                \"wr_operations\": 0,"
        "                \"rd_bytes\": 49250,"
        "                \"rd_operations\": 16"
        "            }"
        "        }"
        "    ],"
        "    \"id\": \"libvirt-11\""
        "}";

    if (!test)
        return -1;

    /* fill in seven times - we are gonna ask seven times later on */
    if (qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0 ||
        qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0 ||
        qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0 ||
        qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0 ||
        qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0 ||
        qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0 ||
        qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0)
        goto cleanup;

#define CHECK0(var, value) \
    if (var != value) { \
        virReportError(VIR_ERR_INTERNAL_ERROR, \
                       "Invalid " #var " value: %lld, expected %d", \
                       var, value); \
        goto cleanup; \
    }

#define CHECK(RD_REQ, RD_BYTES, RD_TOTAL_TIMES, WR_REQ, WR_BYTES, WR_TOTAL_TIMES, \
              FLUSH_REQ, FLUSH_TOTAL_TIMES, ERRS) \
    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) \
    CHECK0(flush_total_times, FLUSH_TOTAL_TIMES) \
    CHECK0(errs, ERRS)

    if (qemuMonitorJSONGetBlockStatsInfo(qemuMonitorTestGetMonitor(test), "virtio-disk0",
                                         &rd_req, &rd_bytes, &rd_total_times,
                                         &wr_req, &wr_bytes, &wr_total_times,
                                         &flush_req, &flush_total_times, &errs) < 0)
        goto cleanup;

    CHECK(1279, 28505088, 640616474, 174, 2845696, 530699221, 0, 0, -1)

    if (qemuMonitorJSONGetBlockStatsInfo(qemuMonitorTestGetMonitor(test), "virtio-disk1",
                                         &rd_req, &rd_bytes, &rd_total_times,
                                         &wr_req, &wr_bytes, &wr_total_times,
                                         &flush_req, &flush_total_times, &errs) < 0)
        goto cleanup;

    CHECK(85, 348160, 8232156, 0, 0, 0, 0, 0, -1)

    if (qemuMonitorJSONGetBlockStatsInfo(qemuMonitorTestGetMonitor(test), "ide0-1-0",
                                         &rd_req, &rd_bytes, &rd_total_times,
                                         &wr_req, &wr_bytes, &wr_total_times,
                                         &flush_req, &flush_total_times, &errs) < 0)
        goto cleanup;

    CHECK(16, 49250, 1004952, 0, 0, 0, 0, 0, -1)

    if (qemuMonitorJSONGetBlockStatsParamsNumber(qemuMonitorTestGetMonitor(test),
                                                 &nparams) < 0)
        goto cleanup;

    if (nparams != 8) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Invalid number of stats: %d, expected 8",
                       nparams);
        goto cleanup;
    }

    if (qemuMonitorJSONGetBlockExtent(qemuMonitorTestGetMonitor(test), "virtio-disk0",
                                      &extent) < 0)
        goto cleanup;

E
Eric Blake 已提交
1598
    if (extent != 5256018944ULL) {
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Invalid extent: %llu, expected 5256018944",
                       extent);
        goto cleanup;
    }

    if (qemuMonitorJSONGetBlockExtent(qemuMonitorTestGetMonitor(test), "virtio-disk1",
                                      &extent) < 0)
        goto cleanup;

    if (extent != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Invalid extent: %llu, expected 0",
                       extent);
        goto cleanup;
    }

    if (qemuMonitorJSONGetBlockExtent(qemuMonitorTestGetMonitor(test), "ide0-1-0",
                                      &extent) < 0)
        goto cleanup;

    if (extent != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Invalid extent: %llu, expected 0",
                       extent);
        goto cleanup;
    }

    ret = 0;

#undef CHECK
#undef CHECK0

1632
 cleanup:
1633 1634 1635 1636
    qemuMonitorTestFree(test);
    return ret;
}

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
static int
testQemuMonitorJSONqemuMonitorJSONGetMigrationCacheSize(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    unsigned long long cacheSize;

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "query-migrate-cache-size",
                               "{"
                               "    \"return\": 67108864,"
                               "    \"id\": \"libvirt-12\""
                               "}") < 0)
        goto cleanup;

    if (qemuMonitorJSONGetMigrationCacheSize(qemuMonitorTestGetMonitor(test),
                                             &cacheSize) < 0)
        goto cleanup;

    if (cacheSize != 67108864) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Invalid cacheSize: %llu, expected 67108864",
                       cacheSize);
        goto cleanup;
    }

    ret = 0;

1668
 cleanup:
1669 1670 1671 1672
    qemuMonitorTestFree(test);
    return ret;
}

1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
static int
testQemuMonitorJSONqemuMonitorJSONGetMigrationStatus(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    qemuMonitorMigrationStatus status, expectedStatus;

    if (!test)
        return -1;

    memset(&expectedStatus, 0, sizeof(expectedStatus));

    expectedStatus.status = QEMU_MONITOR_MIGRATION_STATUS_ACTIVE;
    expectedStatus.total_time = 47;
    expectedStatus.ram_total = 1611038720;
    expectedStatus.ram_remaining = 1605013504;
    expectedStatus.ram_transferred = 3625548;

    if (qemuMonitorTestAddItem(test, "query-migrate",
                               "{"
                               "    \"return\": {"
                               "        \"status\": \"active\","
                               "        \"total-time\": 47,"
                               "        \"ram\": {"
                               "            \"total\": 1611038720,"
                               "            \"remaining\": 1605013504,"
                               "            \"transferred\": 3625548"
                               "        }"
                               "    },"
                               "    \"id\": \"libvirt-13\""
                               "}") < 0)
        goto cleanup;

    if (qemuMonitorJSONGetMigrationStatus(qemuMonitorTestGetMonitor(test), &status) < 0)
        goto cleanup;

    if (memcmp(&status, &expectedStatus, sizeof(status)) != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Invalid migration status");
        goto cleanup;
    }

    ret = 0;
1717
 cleanup:
1718 1719 1720 1721
    qemuMonitorTestFree(test);
    return ret;
}

1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
static int
testQemuMonitorJSONqemuMonitorJSONGetSpiceMigrationStatus(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    bool spiceMigrated;

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "query-spice",
                               "{"
                               "    \"return\": {"
                               "        \"migrated\": true,"
                               "        \"enabled\": false,"
                               "        \"mouse-mode\": \"client\""
                               "    },"
                               "    \"id\": \"libvirt-14\""
                               "}") < 0)
        goto cleanup;

    if (qemuMonitorJSONGetSpiceMigrationStatus(qemuMonitorTestGetMonitor(test),
                                               &spiceMigrated) < 0)
        goto cleanup;

    if (!spiceMigrated) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Invalid spice migration status: %d, expecting 1",
                       spiceMigrated);
        goto cleanup;
    }

    ret = 0;
1756
 cleanup:
1757 1758 1759 1760
    qemuMonitorTestFree(test);
    return ret;
}

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
static int
testHashEqualString(const void *value1, const void *value2)
{
    return strcmp(value1, value2);
}

static int
testQemuMonitorJSONqemuMonitorJSONGetPtyPaths(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    virHashTablePtr paths = NULL, expectedPaths = NULL;

    if (!test)
        return -1;

    if (!(paths = virHashCreate(32, (virHashDataFree) free)) ||
        !(expectedPaths = virHashCreate(32, NULL)))
        goto cleanup;

    if (virHashAddEntry(expectedPaths, "charserial1", (void *) "/dev/pts/21") < 0 ||
        virHashAddEntry(expectedPaths, "charserial0", (void *) "/dev/pts/20") < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Unable to create expectedPaths hash table");
        goto cleanup;
    }

    if (qemuMonitorTestAddItem(test, "query-chardev",
                               "{"
                               "    \"return\": ["
                               "        {"
                               "            \"filename\": \"pty:/dev/pts/21\","
                               "            \"label\": \"charserial1\""
                               "        },"
                               "        {"
                               "            \"filename\": \"pty:/dev/pts/20\","
                               "            \"label\": \"charserial0\""
                               "        },"
                               "        {"
                               "            \"filename\": \"unix:/var/lib/libvirt/qemu/gentoo.monitor,server\","
                               "            \"label\": \"charmonitor\""
                               "        }"
                               "    ],"
                               "    \"id\": \"libvirt-15\""
                               "}") < 0)
        goto cleanup;

    if (qemuMonitorJSONGetPtyPaths(qemuMonitorTestGetMonitor(test),
                                   paths) < 0)
        goto cleanup;

    if (!virHashEqual(paths, expectedPaths, testHashEqualString)) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Hashtable is different to the expected one");
        goto cleanup;
    }

    ret = 0;
1820
 cleanup:
1821 1822 1823 1824 1825 1826
    virHashFree(paths);
    virHashFree(expectedPaths);
    qemuMonitorTestFree(test);
    return ret;
}

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
static int
testQemuMonitorJSONqemuMonitorJSONSetBlockIoThrottle(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    virDomainBlockIoTuneInfo info, expectedInfo;

    if (!test)
        return -1;

    expectedInfo = (virDomainBlockIoTuneInfo) {1, 2, 3, 4, 5, 6};

    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",
                                     NULL, NULL) < 0)
        goto cleanup;

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

    if (memcmp(&info, &expectedInfo, sizeof(info) != 0)) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       "Invalid @info");
        goto cleanup;
    }

    if (qemuMonitorJSONSetBlockIoThrottle(qemuMonitorTestGetMonitor(test),
                                          "drive-virtio-disk1", &info) < 0)
        goto cleanup;

    ret = 0;
1864
 cleanup:
1865 1866 1867 1868
    qemuMonitorTestFree(test);
    return ret;
}

1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
static int
testQemuMonitorJSONqemuMonitorJSONGetTargetArch(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    char *arch;

    if (!test)
        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;
1900
 cleanup:
1901 1902 1903 1904 1905
    VIR_FREE(arch);
    qemuMonitorTestFree(test);
    return ret;
}

1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
static int
testQemuMonitorJSONqemuMonitorJSONGetMigrationCapability(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    int cap;
    const char *reply =
        "{"
        "    \"return\": ["
        "        {"
        "            \"state\": false,"
        "            \"capability\": \"xbzrle\""
        "        }"
        "    ],"
        "    \"id\": \"libvirt-22\""
        "}";

    if (!test)
        return -1;

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

    cap = qemuMonitorJSONGetMigrationCapability(qemuMonitorTestGetMonitor(test),
                                              QEMU_MONITOR_MIGRATION_CAPS_XBZRLE);
    if (cap != 1) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected capability: %d, expecting 1",
                       cap);
        goto cleanup;
    }

    if (qemuMonitorJSONSetMigrationCapability(qemuMonitorTestGetMonitor(test),
                                              QEMU_MONITOR_MIGRATION_CAPS_XBZRLE) < 0)
        goto cleanup;

    ret = 0;
1946
 cleanup:
1947 1948 1949 1950
    qemuMonitorTestFree(test);
    return ret;
}

1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
static int
testQemuMonitorJSONqemuMonitorJSONSendKey(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    unsigned int keycodes[] = {43, 26, 46, 32};

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "send-key",
                               "{\"return\": {}, \"id\": \"libvirt-16\"}") < 0)
        goto cleanup;

    if (qemuMonitorJSONSendKey(qemuMonitorTestGetMonitor(test),
                               0, keycodes, ARRAY_CARDINALITY(keycodes)) < 0)
        goto cleanup;

    ret = 0;
1971
 cleanup:
1972 1973 1974 1975
    qemuMonitorTestFree(test);
    return ret;
}

1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
static int
testQemuMonitorJSONqemuMonitorJSONSendKeyHoldtime(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    unsigned int keycodes[] = {43, 26, 46, 32};

    if (!test)
        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)
        goto cleanup;

    if (qemuMonitorJSONSendKey(qemuMonitorTestGetMonitor(test),
                               31337, keycodes,
                               ARRAY_CARDINALITY(keycodes)) < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    qemuMonitorTestFree(test);
    return ret;
}

2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
static int
testQemuMonitorJSONqemuMonitorSupportsActiveCommit(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    const char *error1 =
        "{"
        "  \"error\": {"
        "    \"class\": \"DeviceNotFound\","
        "    \"desc\": \"Device 'bogus' not found\""
        "  }"
        "}";
    const char *error2 =
        "{"
        "  \"error\": {"
        "    \"class\": \"GenericError\","
        "    \"desc\": \"Parameter 'top' is missing\""
        "  }"
        "}";

    if (!test)
        return -1;

    if (qemuMonitorTestAddItemParams(test, "block-commit", error1,
                                     "device", "\"bogus\"",
                                     NULL, NULL) < 0)
        goto cleanup;

    if (!qemuMonitorSupportsActiveCommit(qemuMonitorTestGetMonitor(test)))
        goto cleanup;

    if (qemuMonitorTestAddItemParams(test, "block-commit", error2,
                                     "device", "\"bogus\"",
                                     NULL, NULL) < 0)
        goto cleanup;

    if (qemuMonitorSupportsActiveCommit(qemuMonitorTestGetMonitor(test)))
        goto cleanup;

    ret = 0;
 cleanup:
    qemuMonitorTestFree(test);
    return ret;
}

2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
static int
testQemuMonitorJSONqemuMonitorJSONGetDumpGuestMemoryCapability(const void *data)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    int ret = -1;
    int cap;
    const char *reply =
        "{"
        "    \"return\": {"
        "        \"formats\": ["
        "            \"elf\","
        "            \"kdump-zlib\","
        "            \"kdump-lzo\","
        "            \"kdump-snappy\""
        "        ]"
        "    },"
        "    \"id\": \"libvirt-9\""
        "}";

    if (!test)
        return -1;

    if (qemuMonitorTestAddItem(test, "query-dump-guest-memory-capability",
                               reply) < 0)
        goto cleanup;

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

    if (cap != 1) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Unexpected capability: %d, expecting 1",
                       cap);
        goto cleanup;
    }

    ret = 0;
2092
 cleanup:
2093 2094 2095
    qemuMonitorTestFree(test);
    return ret;
}
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130

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


static int
testQemuMonitorJSONGetCPUData(const void *opaque)
{
    const struct testCPUData *data = opaque;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, data->xmlopt);
    virCPUDataPtr cpuData = NULL;
    char *jsonFile = NULL;
    char *dataFile = NULL;
    char *jsonStr = NULL;
    char *expected = NULL;
    char *actual = NULL;
    int ret = -1;

    if (!test)
        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;

    if (virtTestLoadFile(jsonFile, &jsonStr) < 0 ||
        virtTestLoadFile(dataFile, &expected) < 0)
        goto cleanup;

2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
    if (qemuMonitorTestAddItem(test, "qom-list",
                               "{"
                               "    \"return\": ["
                               "        {"
                               "            \"name\": \"filtered-features\","
                               "            \"type\": \"X86CPUFeatureWordInfo\""
                               "        },"
                               "        {"
                               "            \"name\": \"feature-words\","
                               "            \"type\": \"X86CPUFeatureWordInfo\""
                               "        }"
                               "    ],"
                               "    \"id\": \"libvirt-19\""
                               "}") < 0)
        goto cleanup;

2147 2148 2149
    if (qemuMonitorTestAddItem(test, "qom-get", jsonStr) < 0)
        goto cleanup;

2150 2151 2152
    if (qemuMonitorJSONGetGuestCPU(qemuMonitorTestGetMonitor(test),
                                   VIR_ARCH_X86_64,
                                   &cpuData) < 0)
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
        goto cleanup;

    if (!(actual = cpuDataFormat(cpuData)))
        goto cleanup;

    if (STRNEQ(expected, actual)) {
        virtTestDifference(stderr, expected, actual);
        goto cleanup;
    }

    ret = 0;
2164
 cleanup:
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
    VIR_FREE(jsonFile);
    VIR_FREE(dataFile);
    VIR_FREE(jsonStr);
    VIR_FREE(expected);
    VIR_FREE(actual);
    cpuDataFree(cpuData);
    qemuMonitorTestFree(test);
    return ret;
}

2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
static int
testQemuMonitorJSONGetNonExistingCPUData(const void *opaque)
{
    virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr) opaque;
    qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
    virCPUDataPtr cpuData = NULL;
    int rv, ret = -1;

    if (!test)
        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;

    rv = qemuMonitorJSONGetGuestCPU(qemuMonitorTestGetMonitor(test),
                                   VIR_ARCH_X86_64,
                                   &cpuData);
    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;
2213
 cleanup:
2214 2215 2216 2217
    qemuMonitorTestFree(test);
    cpuDataFree(cpuData);
    return ret;
}
2218

2219 2220 2221 2222
static int
mymain(void)
{
    int ret = 0;
2223
    virDomainXMLOptionPtr xmlopt;
2224
    testQemuMonitorJSONSimpleFuncData simpleFunc;
2225

2226
#if !WITH_YAJL
E
Eric Blake 已提交
2227
    fputs("libvirt not compiled with yajl, skipping this test\n", stderr);
2228 2229 2230
    return EXIT_AM_SKIP;
#endif

2231
    if (virThreadInitialize() < 0 ||
2232
        !(xmlopt = virQEMUDriverCreateXMLConf(NULL)))
2233
        return EXIT_FAILURE;
2234 2235 2236

    virEventRegisterDefaultImpl();

2237 2238
#define DO_TEST(name)                                                   \
    if (virtTestRun(# name, testQemuMonitorJSON ## name, xmlopt) < 0)   \
2239 2240
        ret = -1

2241
#define DO_TEST_SIMPLE(CMD, FNC, ...)                                   \
2242 2243
    simpleFunc = (testQemuMonitorJSONSimpleFuncData) {.cmd = CMD, .func = FNC, \
                                              .xmlopt = xmlopt, __VA_ARGS__ }; \
2244
    if (virtTestRun(# FNC, testQemuMonitorJSONSimpleFunc, &simpleFunc) < 0)    \
2245 2246
        ret = -1

2247 2248 2249 2250 2251
#define DO_TEST_GEN(name, ...) \
    simpleFunc = (testQemuMonitorJSONSimpleFuncData) {.xmlopt = xmlopt, __VA_ARGS__ }; \
    if (virtTestRun(# name, testQemuMonitorJSON ## name, &simpleFunc) < 0) \
        ret = -1

2252 2253 2254 2255 2256 2257 2258 2259
#define DO_TEST_CPU_DATA(name) \
    do {                                                                  \
        struct testCPUData data = { name, xmlopt };                       \
        const char *label = "GetCPUData(" name ")";                       \
        if (virtTestRun(label, testQemuMonitorJSONGetCPUData, &data) < 0) \
            ret = -1;                                                     \
    } while (0)

2260
    DO_TEST(GetStatus);
2261
    DO_TEST(GetVersion);
2262
    DO_TEST(GetMachines);
2263
    DO_TEST(GetCPUDefinitions);
2264
    DO_TEST(GetCommands);
2265
    DO_TEST(GetTPMModels);
2266
    DO_TEST(GetCommandLineOptionParameters);
2267
    DO_TEST(AttachChardev);
2268
    DO_TEST(DetachChardev);
2269
    DO_TEST(GetListPaths);
2270
    DO_TEST(GetObjectProperty);
2271
    DO_TEST(SetObjectProperty);
2272
    DO_TEST(GetDeviceAliases);
2273
    DO_TEST(CPU);
2274
    DO_TEST(GetNonExistingCPUData);
2275
    DO_TEST_SIMPLE("qmp_capabilities", qemuMonitorJSONSetCapabilities);
2276
    DO_TEST_SIMPLE("system_powerdown", qemuMonitorJSONSystemPowerdown);
2277 2278 2279 2280 2281
    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);
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
    DO_TEST_GEN(qemuMonitorJSONSetLink);
    DO_TEST_GEN(qemuMonitorJSONBlockResize);
    DO_TEST_GEN(qemuMonitorJSONSetVNCPassword);
    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(qemuMonitorJSONSetDrivePassphrase);
    DO_TEST_GEN(qemuMonitorJSONDriveMirror);
    DO_TEST_GEN(qemuMonitorJSONBlockCommit);
    DO_TEST_GEN(qemuMonitorJSONDrivePivot);
    DO_TEST_GEN(qemuMonitorJSONScreendump);
    DO_TEST_GEN(qemuMonitorJSONOpenGraphics);
    DO_TEST_GEN(qemuMonitorJSONNBDServerStart);
    DO_TEST_GEN(qemuMonitorJSONNBDServerAdd);
    DO_TEST_GEN(qemuMonitorJSONDetachCharDev);
2311
    DO_TEST(qemuMonitorJSONGetBalloonInfo);
2312
    DO_TEST(qemuMonitorJSONGetBlockInfo);
2313
    DO_TEST(qemuMonitorJSONGetBlockStatsInfo);
2314
    DO_TEST(qemuMonitorJSONGetMigrationCacheSize);
2315
    DO_TEST(qemuMonitorJSONGetMigrationStatus);
2316
    DO_TEST(qemuMonitorJSONGetSpiceMigrationStatus);
2317
    DO_TEST(qemuMonitorJSONGetPtyPaths);
2318
    DO_TEST(qemuMonitorJSONSetBlockIoThrottle);
2319
    DO_TEST(qemuMonitorJSONGetTargetArch);
2320
    DO_TEST(qemuMonitorJSONGetMigrationCapability);
2321
    DO_TEST(qemuMonitorJSONGetCPUInfo);
2322
    DO_TEST(qemuMonitorJSONGetVirtType);
2323
    DO_TEST(qemuMonitorJSONSendKey);
2324
    DO_TEST(qemuMonitorJSONGetDumpGuestMemoryCapability);
2325
    DO_TEST(qemuMonitorJSONSendKeyHoldtime);
2326
    DO_TEST(qemuMonitorSupportsActiveCommit);
2327

2328 2329 2330
    DO_TEST_CPU_DATA("host");
    DO_TEST_CPU_DATA("full");

2331
    virObjectUnref(xmlopt);
2332 2333 2334 2335 2336

    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)