qemublocktest.c 44.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>


#include "testutils.h"
J
Ján Tomko 已提交
21 22 23 24 25 26 27 28 29
#include "testutilsqemu.h"
#include "testutilsqemuschema.h"
#include "virstoragefile.h"
#include "virstring.h"
#include "virlog.h"
#include "qemu/qemu_block.h"
#include "qemu/qemu_qapi.h"
#include "qemu/qemu_monitor_json.h"
#include "qemu/qemu_backup.h"
30
#include "qemu/qemu_checkpoint.h"
31

J
Ján Tomko 已提交
32
#include "qemu/qemu_command.h"
33

J
Ján Tomko 已提交
34 35
#define LIBVIRT_SNAPSHOT_CONF_PRIV_H_ALLOW
#include "conf/snapshot_conf_priv.h"
36

J
Ján Tomko 已提交
37
#define VIR_FROM_THIS VIR_FROM_NONE
38 39 40 41 42 43

VIR_LOG_INIT("tests.storagetest");

struct testBackingXMLjsonXMLdata {
    int type;
    const char *xml;
44 45 46
    bool legacy;
    virHashTablePtr schema;
    virJSONValuePtr schemaroot;
47 48 49 50 51 52
};

static int
testBackingXMLjsonXML(const void *args)
{
    const struct testBackingXMLjsonXMLdata *data = args;
J
Ján Tomko 已提交
53 54
    g_autoptr(xmlDoc) xml = NULL;
    g_autoptr(xmlXPathContext) ctxt = NULL;
55
    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
J
Ján Tomko 已提交
56 57
    g_autoptr(virJSONValue) backendprops = NULL;
    g_autoptr(virJSONValue) wrapper = NULL;
58 59 60
    g_autofree char *propsstr = NULL;
    g_autofree char *protocolwrapper = NULL;
    g_autofree char *actualxml = NULL;
61 62
    g_autoptr(virStorageSource) xmlsrc = NULL;
    g_autoptr(virStorageSource) jsonsrc = NULL;
63
    g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
64

65
    if (!(xmlsrc = virStorageSourceNew()))
66 67 68 69 70
        return -1;

    xmlsrc->type = data->type;

    if (!(xml = virXMLParseStringCtxt(data->xml, "(test storage source XML)", &ctxt)))
71
        return -1;
72

73
    if (virDomainStorageSourceParse(ctxt->node, ctxt, xmlsrc, 0, NULL) < 0) {
74
        fprintf(stderr, "failed to parse disk source xml\n");
75
        return -1;
76 77
    }

78 79 80
    if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc,
                                                               data->legacy,
                                                               false,
81
                                                               false))) {
82
        fprintf(stderr, "failed to format disk source json\n");
83
        return -1;
84 85
    }

86 87 88 89 90 91 92 93 94 95 96 97 98
    if (!data->legacy) {
        if (testQEMUSchemaValidate(backendprops, data->schemaroot,
                                   data->schema, &debug) < 0) {
            g_autofree char *debugmsg = virBufferContentAndReset(&debug);
            g_autofree char *debugprops = virJSONValueToString(backendprops, true);

            VIR_TEST_VERBOSE("json does not conform to QAPI schema");
            VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
                           debugprops, NULLSTR(debugmsg));
            return -1;
        }
    }

99
    if (virJSONValueObjectCreate(&wrapper, "a:file", &backendprops, NULL) < 0)
100
        return -1;
101 102

    if (!(propsstr = virJSONValueToString(wrapper, false)))
103
        return -1;
104

105
    protocolwrapper = g_strdup_printf("json:%s", propsstr);
106

107 108
    if (virStorageSourceNewFromBackingAbsolute(protocolwrapper,
                                               &jsonsrc) < 0) {
109
        fprintf(stderr, "failed to parse disk json\n");
110
        return -1;
111 112
    }

113
    if (virDomainDiskSourceFormat(&buf, jsonsrc, "source", 0, false, 0, true,
114
                                  NULL) < 0 ||
115 116
        !(actualxml = virBufferContentAndReset(&buf))) {
        fprintf(stderr, "failed to format disk source xml\n");
117
        return -1;
118 119 120 121 122 123 124
    }

    if (STRNEQ(actualxml, data->xml)) {
        fprintf(stderr, "\n expected storage source xml:\n'%s'\n"
                        "actual storage source xml:\n%s\n"
                        "intermediate json:\n%s\n",
                        data->xml, actualxml, protocolwrapper);
125
        return -1;
126 127
    }

128
    return 0;
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
static const char *testJSONtoJSONPath = abs_srcdir "/qemublocktestdata/jsontojson/";

struct testJSONtoJSONData {
    const char *name;
    virHashTablePtr schema;
    virJSONValuePtr schemaroot;
};

static int
testJSONtoJSON(const void *args)
{
    const struct testJSONtoJSONData *data = args;
    g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
    g_autoptr(virJSONValue) jsonsrcout = NULL;
    g_autoptr(virStorageSource) src = NULL;
    g_autofree char *actual = NULL;
    g_autofree char *in = NULL;
    g_autofree char *infile = g_strdup_printf("%s%s-in.json", testJSONtoJSONPath,
                                              data->name);
    g_autofree char *outfile = g_strdup_printf("%s%s-out.json", testJSONtoJSONPath,
                                              data->name);

    if (virTestLoadFile(infile, &in) < 0)
        return -1;

    if (virStorageSourceNewFromBackingAbsolute(in, &src) < 0) {
        fprintf(stderr, "failed to parse disk json\n");
        return -1;
    }

    if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src, false, false, true))) {
        fprintf(stderr, "failed to format disk source json\n");
        return -1;
    }

    if (!(actual = virJSONValueToString(jsonsrcout, true)))
        return -1;

    if (testQEMUSchemaValidate(jsonsrcout, data->schemaroot,
                               data->schema, &debug) < 0) {
        g_autofree char *debugmsg = virBufferContentAndReset(&debug);

        VIR_TEST_VERBOSE("json does not conform to QAPI schema");
        VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
                       actual, NULLSTR(debugmsg));
        return -1;
    }

    return virTestCompareToFile(actual, outfile);
}

182

183 184 185 186 187 188 189 190 191 192
struct testQemuDiskXMLToJSONData {
    virQEMUDriverPtr driver;
    virHashTablePtr schema;
    virJSONValuePtr schemaroot;
    const char *name;
    bool fail;

    virJSONValuePtr *props;
    size_t nprops;

193 194 195
    virJSONValuePtr *propssrc;
    size_t npropssrc;

196 197 198 199 200 201 202 203 204 205 206 207
    virQEMUCapsPtr qemuCaps;
};


static void
testQemuDiskXMLToPropsClear(struct testQemuDiskXMLToJSONData *data)
{
    size_t i;

    for (i = 0; i < data->nprops; i++)
        virJSONValueFree(data->props[i]);

208 209 210
    for (i = 0; i < data->npropssrc; i++)
        virJSONValueFree(data->propssrc[i]);

211 212
    data->nprops = 0;
    VIR_FREE(data->props);
213 214
    data->npropssrc = 0;
    VIR_FREE(data->propssrc);
215 216 217
}


218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
static int
testQemuDiskXMLToJSONFakeSecrets(virStorageSourcePtr src)
{
    qemuDomainStorageSourcePrivatePtr srcpriv;

    if (!src->privateData &&
        !(src->privateData = qemuDomainStorageSourcePrivateNew()))
        return -1;

    srcpriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);

    if (src->auth) {
        if (VIR_ALLOC(srcpriv->secinfo) < 0)
            return -1;

        srcpriv->secinfo->type = VIR_DOMAIN_SECRET_INFO_TYPE_AES;
234
        srcpriv->secinfo->s.aes.username = g_strdup(src->auth->username);
235

236 237
        srcpriv->secinfo->s.aes.alias = g_strdup_printf("%s-secalias",
                                                        NULLSTR(src->nodestorage));
238 239 240 241 242 243 244
    }

    if (src->encryption) {
        if (VIR_ALLOC(srcpriv->encinfo) < 0)
            return -1;

        srcpriv->encinfo->type = VIR_DOMAIN_SECRET_INFO_TYPE_AES;
245 246
        srcpriv->encinfo->s.aes.alias = g_strdup_printf("%s-encalias",
                                                        NULLSTR(src->nodeformat));
247 248 249 250 251 252
    }

    return 0;
}


253 254 255 256 257 258
static const char *testQemuDiskXMLToJSONPath = abs_srcdir "/qemublocktestdata/xml2json/";

static int
testQemuDiskXMLToProps(const void *opaque)
{
    struct testQemuDiskXMLToJSONData *data = (void *) opaque;
259
    g_autoptr(virDomainDef) vmdef = NULL;
260 261
    virDomainDiskDefPtr disk = NULL;
    virStorageSourcePtr n;
262 263
    virJSONValuePtr formatProps = NULL;
    virJSONValuePtr storageProps = NULL;
J
Ján Tomko 已提交
264
    g_autoptr(virJSONValue) storageSrcOnlyProps = NULL;
265 266 267 268
    char *xmlpath = NULL;
    char *xmlstr = NULL;
    int ret = -1;

269
    xmlpath = g_strdup_printf("%s%s.xml", testQemuDiskXMLToJSONPath, data->name);
270 271 272 273 274

    if (virTestLoadFile(xmlpath, &xmlstr) < 0)
        goto cleanup;

    /* qemu stores node names in the status XML portion */
275
    if (!(disk = virDomainDiskDefParse(xmlstr, data->driver->xmlopt,
276 277 278
                                       VIR_DOMAIN_DEF_PARSE_STATUS)))
        goto cleanup;

279 280 281 282 283
    if (!(vmdef = virDomainDefNew()) ||
        virDomainDiskInsert(vmdef, disk) < 0)
        goto cleanup;

    if (qemuCheckDiskConfig(disk, vmdef, data->qemuCaps) < 0 ||
284
        qemuDomainDeviceDefValidateDisk(disk, data->qemuCaps) < 0) {
285
        VIR_TEST_VERBOSE("invalid configuration for disk");
286 287 288 289
        goto cleanup;
    }

    for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) {
290 291 292
        if (testQemuDiskXMLToJSONFakeSecrets(n) < 0)
            goto cleanup;

293 294 295
        if (qemuDomainValidateStorageSource(n, data->qemuCaps) < 0)
            goto cleanup;

296
        qemuDomainPrepareDiskSourceData(disk, n);
297

298
        if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n, n->backingStore)) ||
299 300
            !(storageSrcOnlyProps = qemuBlockStorageSourceGetBackendProps(n, false, true, true)) ||
            !(storageProps = qemuBlockStorageSourceGetBackendProps(n, false, false, true))) {
301
            if (!data->fail) {
302
                VIR_TEST_VERBOSE("failed to generate qemu blockdev props");
303 304 305
                goto cleanup;
            }
        } else if (data->fail) {
306
            VIR_TEST_VERBOSE("qemu blockdev props should have failed");
307 308 309
            goto cleanup;
        }

310
        if (VIR_APPEND_ELEMENT(data->props, data->nprops, formatProps) < 0 ||
311 312
            VIR_APPEND_ELEMENT(data->props, data->nprops, storageProps) < 0 ||
            VIR_APPEND_ELEMENT(data->propssrc, data->npropssrc, storageSrcOnlyProps) < 0)
313 314 315 316 317 318
            goto cleanup;
    }

    ret = 0;

 cleanup:
319 320
    virJSONValueFree(formatProps);
    virJSONValueFree(storageProps);
321 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
    VIR_FREE(xmlpath);
    VIR_FREE(xmlstr);
    return ret;
}


static int
testQemuDiskXMLToPropsValidateSchema(const void *opaque)
{
    struct testQemuDiskXMLToJSONData *data = (void *) opaque;
    virBuffer debug = VIR_BUFFER_INITIALIZER;
    char *propsstr = NULL;
    char *debugmsg = NULL;
    int ret = 0;
    size_t i;

    if (data->fail)
        return EXIT_AM_SKIP;

    for (i = 0; i < data->nprops; i++) {
        if (testQEMUSchemaValidate(data->props[i], data->schemaroot,
                                   data->schema, &debug) < 0) {
            debugmsg = virBufferContentAndReset(&debug);
            propsstr = virJSONValueToString(data->props[i], true);
            VIR_TEST_VERBOSE("json does not conform to QAPI schema");
            VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
                           propsstr, NULLSTR(debugmsg));
            VIR_FREE(debugmsg);
            VIR_FREE(propsstr);
            ret = -1;
        }

        virBufferFreeAndReset(&debug);
    }
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

    for (i = 0; i < data->npropssrc; i++) {
        if (testQEMUSchemaValidate(data->propssrc[i], data->schemaroot,
                                   data->schema, &debug) < 0) {
            debugmsg = virBufferContentAndReset(&debug);
            propsstr = virJSONValueToString(data->propssrc[i], true);
            VIR_TEST_VERBOSE("json does not conform to QAPI schema");
            VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
                           propsstr, NULLSTR(debugmsg));
            VIR_FREE(debugmsg);
            VIR_FREE(propsstr);
            ret = -1;
        }

        virBufferFreeAndReset(&debug);
    }

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    return ret;
}


static int
testQemuDiskXMLToPropsValidateFile(const void *opaque)
{
    struct testQemuDiskXMLToJSONData *data = (void *) opaque;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *jsonpath = NULL;
    char *actual = NULL;
    int ret = -1;
    size_t i;

    if (data->fail)
        return EXIT_AM_SKIP;

389
    jsonpath = g_strdup_printf("%s%s.json", testQemuDiskXMLToJSONPath, data->name);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

    for (i = 0; i < data->nprops; i++) {
        char *jsonstr;

        if (!(jsonstr = virJSONValueToString(data->props[i], true)))
            goto cleanup;

        virBufferAdd(&buf, jsonstr, -1);
        VIR_FREE(jsonstr);
    }

    actual = virBufferContentAndReset(&buf);

    ret = virTestCompareToFile(actual, jsonpath);

 cleanup:
    VIR_FREE(jsonpath);
    VIR_FREE(actual);
    return ret;
}


412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
struct testQemuImageCreateData {
    const char *name;
    const char *backingname;
    virHashTablePtr schema;
    virJSONValuePtr schemaroot;
    virQEMUDriverPtr driver;
    virQEMUCapsPtr qemuCaps;
};

static const char *testQemuImageCreatePath = abs_srcdir "/qemublocktestdata/imagecreate/";

static virStorageSourcePtr
testQemuImageCreateLoadDiskXML(const char *name,
                               virDomainXMLOptionPtr xmlopt)

{
    virDomainSnapshotDiskDefPtr diskdef = NULL;
J
Ján Tomko 已提交
429 430
    g_autoptr(xmlDoc) doc = NULL;
    g_autoptr(xmlXPathContext) ctxt = NULL;
431
    xmlNodePtr node;
432
    g_autofree char *xmlpath = NULL;
433 434
    virStorageSourcePtr ret = NULL;

435
    xmlpath = g_strdup_printf("%s%s.xml", testQemuImageCreatePath, name);
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

    if (!(doc = virXMLParseFileCtxt(xmlpath, &ctxt)))
        return NULL;

    if (!(node = virXPathNode("//disk", ctxt))) {
        VIR_TEST_VERBOSE("failed to find <source> element\n");
        return NULL;
    }

    if (VIR_ALLOC(diskdef) < 0)
        return NULL;

    if (virDomainSnapshotDiskDefParseXML(node, ctxt, diskdef,
                                         VIR_DOMAIN_DEF_PARSE_STATUS,
                                         xmlopt) == 0)
451
        ret = g_steal_pointer(&diskdef->src);
452 453 454 455 456 457 458 459 460 461

    virDomainSnapshotDiskDefFree(diskdef);
    return ret;
}


static int
testQemuImageCreate(const void *opaque)
{
    struct testQemuImageCreateData *data = (void *) opaque;
J
Ján Tomko 已提交
462 463
    g_autoptr(virJSONValue) protocolprops = NULL;
    g_autoptr(virJSONValue) formatprops = NULL;
464
    g_autoptr(virStorageSource) src = NULL;
465 466
    g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
    g_auto(virBuffer) actualbuf = VIR_BUFFER_INITIALIZER;
467 468 469 470
    g_autofree char *jsonprotocol = NULL;
    g_autofree char *jsonformat = NULL;
    g_autofree char *actual = NULL;
    g_autofree char *jsonpath = NULL;
471 472 473 474 475 476 477 478 479 480 481 482 483

    if (!(src = testQemuImageCreateLoadDiskXML(data->name, data->driver->xmlopt)))
        return -1;

    if (data->backingname &&
        !(src->backingStore = testQemuImageCreateLoadDiskXML(data->backingname,
                                                             data->driver->xmlopt)))
        return -1;

    if (testQemuDiskXMLToJSONFakeSecrets(src) < 0)
        return -1;

    /* fake some sizes */
484 485
    src->capacity = UINT_MAX * 2ULL;
    src->physical = UINT_MAX + 1ULL;
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

    if (qemuDomainValidateStorageSource(src, data->qemuCaps) < 0)
        return -1;

    if (qemuBlockStorageSourceCreateGetStorageProps(src, &protocolprops) < 0)
        return -1;

    if (qemuBlockStorageSourceCreateGetFormatProps(src, src->backingStore, &formatprops) < 0)
        return -1;

    if (formatprops) {
        if (!(jsonformat = virJSONValueToString(formatprops, true)))
            return -1;

        if (testQEMUSchemaValidate(formatprops, data->schemaroot, data->schema,
                                   &debug) < 0) {
502
            g_autofree char *debugmsg = virBufferContentAndReset(&debug);
503 504 505 506 507 508 509 510 511 512 513 514 515 516
            VIR_TEST_VERBOSE("blockdev-create format json does not conform to QAPI schema");
            VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
                           jsonformat, NULLSTR(debugmsg));
            return -1;
        }
        virBufferFreeAndReset(&debug);
    }

    if (protocolprops) {
        if (!(jsonprotocol = virJSONValueToString(protocolprops, true)))
            return -1;

        if (testQEMUSchemaValidate(protocolprops, data->schemaroot, data->schema,
                                   &debug) < 0) {
517
            g_autofree char *debugmsg = virBufferContentAndReset(&debug);
518 519 520 521 522 523 524 525 526 527
            VIR_TEST_VERBOSE("blockdev-create protocol json does not conform to QAPI schema");
            VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
                           jsonprotocol, NULLSTR(debugmsg));
            return -1;
        }
        virBufferFreeAndReset(&debug);
    }

    virBufferStrcat(&actualbuf, "protocol:\n", NULLSTR(jsonprotocol),
                    "\nformat:\n", NULLSTR(jsonformat), NULL);
J
Ján Tomko 已提交
528
    virBufferTrim(&actualbuf, "\n");
529 530
    virBufferAddLit(&actualbuf, "\n");

531
    jsonpath = g_strdup_printf("%s%s.json", testQemuImageCreatePath, data->name);
532 533 534 535 536 537 538 539

    if (!(actual = virBufferContentAndReset(&actualbuf)))
        return -1;

    return virTestCompareToFile(actual, jsonpath);
}


540 541 542 543
static int
testQemuDiskXMLToPropsValidateFileSrcOnly(const void *opaque)
{
    struct testQemuDiskXMLToJSONData *data = (void *) opaque;
544
    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
545 546
    g_autofree char *jsonpath = NULL;
    g_autofree char *actual = NULL;
547 548 549 550 551
    size_t i;

    if (data->fail)
        return EXIT_AM_SKIP;

552 553
    jsonpath = g_strdup_printf("%s%s-srconly.json", testQemuDiskXMLToJSONPath,
                               data->name);
554 555

    for (i = 0; i < data->npropssrc; i++) {
556
        g_autofree char *jsonstr = NULL;
557 558 559 560 561 562 563 564 565 566 567 568 569

        if (!(jsonstr = virJSONValueToString(data->propssrc[i], true)))
            return -1;

        virBufferAdd(&buf, jsonstr, -1);
    }

    actual = virBufferContentAndReset(&buf);

    return virTestCompareToFile(actual, jsonpath);
}


570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 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
static const char *bitmapDetectPrefix = "qemublocktestdata/bitmap/";

static void
testQemuDetectBitmapsWorker(virHashTablePtr nodedata,
                            const char *nodename,
                            virBufferPtr buf)
{
    qemuBlockNamedNodeDataPtr data;
    size_t i;

    if (!(data = virHashLookup(nodedata, nodename)))
        return;

    virBufferAsprintf(buf, "%s:\n", nodename);
    virBufferAdjustIndent(buf, 1);

    for (i = 0; i < data->nbitmaps; i++) {
        qemuBlockNamedNodeDataBitmapPtr bitmap = data->bitmaps[i];

        virBufferAsprintf(buf, "%8s: record:%d busy:%d persist:%d inconsist:%d gran:%llu dirty:%llu\n",
                          bitmap->name, bitmap->recording, bitmap->busy,
                          bitmap->persistent, bitmap->inconsistent,
                          bitmap->granularity, bitmap->dirtybytes);
    }

    virBufferAdjustIndent(buf, -1);
}


static int
testQemuDetectBitmaps(const void *opaque)
{
    const char *name = opaque;
    g_autoptr(virJSONValue) nodedatajson = NULL;
    g_autoptr(virHashTable) nodedata = NULL;
    g_autofree char *actual = NULL;
    g_autofree char *expectpath = NULL;
    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
    size_t i;

    expectpath = g_strdup_printf("%s/%s%s.out", abs_srcdir,
                                 bitmapDetectPrefix, name);

    if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, name,
                                             ".json", NULL)))
        return -1;

    if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
        VIR_TEST_VERBOSE("failed to load nodedata JSON");
        return -1;
    }

    /* we detect for the first 30 nodenames for simplicity */
    for (i = 0; i < 30; i++) {
        g_autofree char *nodename = g_strdup_printf("libvirt-%zu-format", i);

        testQemuDetectBitmapsWorker(nodedata, nodename, &buf);
    }

    actual = virBufferContentAndReset(&buf);

    return virTestCompareToFile(actual, expectpath);
}


635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 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 769 770 771 772 773
static virStorageSourcePtr
testQemuBackupIncrementalBitmapCalculateGetFakeImage(size_t idx)
{
   virStorageSourcePtr ret;

   if (!(ret = virStorageSourceNew()))
       abort();

   ret->type = VIR_STORAGE_TYPE_FILE;
   ret->format = VIR_STORAGE_FILE_QCOW2;
   ret->path = g_strdup_printf("/image%zu", idx);
   ret->nodestorage = g_strdup_printf("libvirt-%zu-storage", idx);
   ret->nodeformat = g_strdup_printf("libvirt-%zu-format", idx);

   return ret;
}


static virStorageSourcePtr
testQemuBackupIncrementalBitmapCalculateGetFakeChain(void)
{
    virStorageSourcePtr ret;
    virStorageSourcePtr n;
    size_t i;

    n = ret = testQemuBackupIncrementalBitmapCalculateGetFakeImage(1);

    for (i = 2; i < 10; i++) {
        n->backingStore = testQemuBackupIncrementalBitmapCalculateGetFakeImage(i);
        n = n->backingStore;
    }

    return ret;
}


typedef virDomainMomentDefPtr testMomentList;

static void
testMomentListFree(testMomentList *list)
{
    testMomentList *tmp = list;

    if (!list)
        return;

    while (*tmp) {
        virObjectUnref(*tmp);
        tmp++;
    }

    g_free(list);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC(testMomentList, testMomentListFree);

static virDomainMomentDefPtr
testQemuBackupGetIncrementalMoment(const char *name)
{
    virDomainCheckpointDefPtr checkpoint = NULL;

    if (!(checkpoint = virDomainCheckpointDefNew()))
        abort();

    checkpoint->parent.name = g_strdup(name);

    return (virDomainMomentDefPtr) checkpoint;
}


static virDomainMomentDefPtr *
testQemuBackupGetIncremental(const char *incFrom)
{
    const char *checkpoints[] = {"current", "d", "c", "b", "a"};
    virDomainMomentDefPtr *incr;
    size_t i;

    incr = g_new0(virDomainMomentDefPtr, G_N_ELEMENTS(checkpoints) + 1);

    for (i = 0; i < G_N_ELEMENTS(checkpoints); i++) {
        incr[i] = testQemuBackupGetIncrementalMoment(checkpoints[i]);

        if (STREQ(incFrom, checkpoints[i]))
            break;
    }

    return incr;
}

static const char *backupDataPrefix = "qemublocktestdata/backupmerge/";

struct testQemuBackupIncrementalBitmapCalculateData {
    const char *name;
    virStorageSourcePtr chain;
    const char *incremental;
    const char *nodedatafile;
};


static int
testQemuBackupIncrementalBitmapCalculate(const void *opaque)
{
    const struct testQemuBackupIncrementalBitmapCalculateData *data = opaque;
    g_autoptr(virJSONValue) nodedatajson = NULL;
    g_autoptr(virHashTable) nodedata = NULL;
    g_autoptr(virJSONValue) mergebitmaps = NULL;
    g_autofree char *actual = NULL;
    g_autofree char *expectpath = NULL;
    g_autoptr(testMomentList) incremental = NULL;

    expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir,
                                 backupDataPrefix, data->name);

    if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->nodedatafile,
                                             ".json", NULL)))
        return -1;

    if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
        VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
        return -1;
    }

    incremental = testQemuBackupGetIncremental(data->incremental);

    if (!(mergebitmaps = qemuBackupDiskPrepareOneBitmapsChain(incremental,
                                                              data->chain,
                                                              nodedata,
                                                              "testdisk"))) {
        VIR_TEST_VERBOSE("failed to calculate merged bitmaps");
        return -1;
    }

    if (!(actual = virJSONValueToString(mergebitmaps, true)))
        return -1;

    return virTestCompareToFile(actual, expectpath);
}


774 775 776 777 778 779 780
static const char *checkpointDeletePrefix = "qemublocktestdata/checkpointdelete/";

struct testQemuCheckpointDeleteMergeData {
    const char *name;
    virStorageSourcePtr chain;
    const char *deletebitmap;
    const char *parentbitmap;
781
    const char *nodedatafile;
782 783 784 785 786 787 788 789 790 791
};


static int
testQemuCheckpointDeleteMerge(const void *opaque)
{
    const struct testQemuCheckpointDeleteMergeData *data = opaque;
    g_autofree char *actual = NULL;
    g_autofree char *expectpath = NULL;
    g_autoptr(virJSONValue) actions = NULL;
792 793
    g_autoptr(virJSONValue) nodedatajson = NULL;
    g_autoptr(virHashTable) nodedata = NULL;
794 795 796
    g_autoptr(GSList) reopenimages = NULL;
    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
    GSList *tmp;
797 798 799 800

    expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir,
                                 checkpointDeletePrefix, data->name);

801 802
    if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->nodedatafile,
                                             ".json", NULL)))
803 804
        return -1;

805 806 807 808 809
    if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
        VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
        return -1;
    }

810
    actions = virJSONValueNewArray();
811 812

    if (qemuCheckpointDiscardDiskBitmaps(data->chain,
813
                                         nodedata,
814 815
                                         data->deletebitmap,
                                         data->parentbitmap,
816
                                         actions,
817 818
                                         "testdisk",
                                         &reopenimages) < 0) {
819 820 821 822
        VIR_TEST_VERBOSE("failed to generate checkpoint delete transaction\n");
        return -1;
    }

823
    if (virJSONValueToBuffer(actions, &buf, true) < 0)
824 825
        return -1;

826 827 828 829 830 831 832 833 834 835 836
    if (reopenimages) {
        virBufferAddLit(&buf, "reopen nodes:\n");

        for (tmp = reopenimages; tmp; tmp = tmp->next) {
            virStorageSourcePtr src = tmp->data;
            virBufferAsprintf(&buf, "%s\n", src->nodeformat);
        }
    }

    actual = virBufferContentAndReset(&buf);

837 838 839 840
    return virTestCompareToFile(actual, expectpath);
}


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 866 867 868 869 870 871 872 873 874 875
struct testQemuBlockBitmapValidateData {
    const char *name;
    const char *bitmapname;
    virStorageSourcePtr chain;
    bool expect;
};

static int
testQemuBlockBitmapValidate(const void *opaque)
{
    const struct testQemuBlockBitmapValidateData *data = opaque;
    g_autoptr(virJSONValue) nodedatajson = NULL;
    g_autoptr(virHashTable) nodedata = NULL;
    bool actual;

    if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->name,
                                             ".json", NULL)))
        return -1;

    if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
        VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
        return -1;
    }

    actual = qemuBlockBitmapChainIsValid(data->chain, data->bitmapname, nodedata);

    if (actual != data->expect) {
        VIR_TEST_VERBOSE("expected rv:'%d' actual rv:'%d'\n", data->expect, actual);
        return -1;
    }

    return 0;
}


876 877 878 879 880 881 882 883 884 885 886 887 888 889 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 916 917 918 919 920 921 922 923 924 925
static const char *blockcopyPrefix = "qemublocktestdata/bitmapblockcopy/";

struct testQemuBlockBitmapBlockcopyData {
    const char *name;
    bool shallow;
    virStorageSourcePtr chain;
    const char *nodedatafile;
};


static int
testQemuBlockBitmapBlockcopy(const void *opaque)
{
    const struct testQemuBlockBitmapBlockcopyData *data = opaque;
    g_autofree char *actual = NULL;
    g_autofree char *expectpath = NULL;
    g_autoptr(virJSONValue) actions = NULL;
    g_autoptr(virJSONValue) nodedatajson = NULL;
    g_autoptr(virHashTable) nodedata = NULL;
    g_autoptr(virStorageSource) fakemirror = virStorageSourceNew();

    if (!fakemirror)
        return -1;

    fakemirror->nodeformat = g_strdup("mirror-format-node");

    expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir,
                                 blockcopyPrefix, data->name);

    if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->nodedatafile,
                                             ".json", NULL)))
        return -1;

    if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
        VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
        return -1;
    }

    if (qemuBlockBitmapsHandleBlockcopy(data->chain, fakemirror, nodedata,
                                        data->shallow, &actions) < 0)
        return -1;

    if (actions &&
        !(actual = virJSONValueToString(actions, true)))
        return -1;

    return virTestCompareToFile(actual, expectpath);
}


926 927 928 929
static int
mymain(void)
{
    int ret = 0;
930
    virQEMUDriver driver;
931
    struct testBackingXMLjsonXMLdata xmljsonxmldata;
932
    struct testQemuDiskXMLToJSONData diskxmljsondata;
933
    struct testJSONtoJSONData jsontojsondata;
934
    struct testQemuImageCreateData imagecreatedata;
935
    struct testQemuBackupIncrementalBitmapCalculateData backupbitmapcalcdata;
936
    struct testQemuCheckpointDeleteMergeData checkpointdeletedata;
937
    struct testQemuBlockBitmapValidateData blockbitmapvalidatedata;
938
    struct testQemuBlockBitmapBlockcopyData blockbitmapblockcopydata;
939 940
    char *capslatest_x86_64 = NULL;
    virQEMUCapsPtr caps_x86_64 = NULL;
941
    g_autoptr(virHashTable) qmp_schema_x86_64 = NULL;
942
    virJSONValuePtr qmp_schemaroot_x86_64_blockdev_add = NULL;
943
    g_autoptr(virStorageSource) bitmapSourceChain = NULL;
944 945 946 947

    if (qemuTestDriverInit(&driver) < 0)
        return EXIT_FAILURE;

948 949
    bitmapSourceChain = testQemuBackupIncrementalBitmapCalculateGetFakeChain();

950
    diskxmljsondata.driver = &driver;
951
    imagecreatedata.driver = &driver;
952

953
    if (!(capslatest_x86_64 = testQemuGetLatestCapsForArch("x86_64", "xml")))
954 955
        return EXIT_FAILURE;

956
    VIR_TEST_VERBOSE("\nlatest caps x86_64: %s", capslatest_x86_64);
957 958 959 960 961 962

    if (!(caps_x86_64 = qemuTestParseCapabilitiesArch(virArchFromString("x86_64"),
                                                      capslatest_x86_64)))
        return EXIT_FAILURE;

    diskxmljsondata.qemuCaps = caps_x86_64;
963
    imagecreatedata.qemuCaps = caps_x86_64;
964

965 966 967 968 969
    if (!(qmp_schema_x86_64 = testQEMUSchemaLoad("x86_64"))) {
        ret = -1;
        goto cleanup;
    }

970 971 972 973 974 975 976 977 978
    if (virQEMUQAPISchemaPathGet("blockdev-add/arg-type",
                                 qmp_schema_x86_64,
                                 &qmp_schemaroot_x86_64_blockdev_add) < 0 ||
        !qmp_schemaroot_x86_64_blockdev_add) {
        VIR_TEST_VERBOSE("failed to find schema entry for blockdev-add");
        ret = -1;
        goto cleanup;
    }

979 980
    virTestCounterReset("qemu storage source xml->json->xml ");

J
Ján Tomko 已提交
981
#define TEST_JSON_FORMAT(tpe, xmlstr) \
982
    do { \
983 984
        xmljsonxmldata.type = tpe; \
        xmljsonxmldata.xml = xmlstr; \
985 986 987 988
        xmljsonxmldata.legacy = true; \
        if (virTestRun(virTestCounterNext(), testBackingXMLjsonXML, \
                       &xmljsonxmldata) < 0) \
        xmljsonxmldata.legacy = false; \
989 990
        if (virTestRun(virTestCounterNext(), testBackingXMLjsonXML, \
                       &xmljsonxmldata) < 0) \
991 992 993
            ret = -1; \
    } while (0)

J
Ján Tomko 已提交
994
#define TEST_JSON_FORMAT_NET(xmlstr) \
995 996
    TEST_JSON_FORMAT(VIR_STORAGE_TYPE_NETWORK, xmlstr)

997 998 999
    xmljsonxmldata.schema = qmp_schema_x86_64;
    xmljsonxmldata.schemaroot = qmp_schemaroot_x86_64_blockdev_add;

1000 1001 1002 1003 1004
    TEST_JSON_FORMAT(VIR_STORAGE_TYPE_FILE, "<source file='/path/to/file'/>\n");

    /* type VIR_STORAGE_TYPE_BLOCK is not tested since it parses back to 'file' */
    /* type VIR_STORAGE_TYPE_DIR it is a 'format' driver in qemu */

1005 1006 1007
    TEST_JSON_FORMAT_NET("<source protocol='http' name=''>\n"
                         "  <host name='example.com' port='80'/>\n"
                         "</source>\n");
1008 1009 1010 1011 1012 1013
    TEST_JSON_FORMAT_NET("<source protocol='http' name='file'>\n"
                         "  <host name='example.com' port='80'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='https' name='file'>\n"
                         "  <host name='example.com' port='432'/>\n"
                         "</source>\n");
1014 1015 1016 1017 1018 1019
    TEST_JSON_FORMAT_NET("<source protocol='https' name='file'>\n"
                         "  <host name='example.com' port='432'/>\n"
                         "  <ssl verify='no'/>\n"
                         "  <readahead size='1024'/>\n"
                         "  <timeout seconds='1337'/>\n"
                         "</source>\n");
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    TEST_JSON_FORMAT_NET("<source protocol='gluster' name='vol/file'>\n"
                         "  <host name='example.com' port='24007'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='gluster' name='testvol/img.qcow2'>\n"
                         "  <host name='example.com' port='1234'/>\n"
                         "  <host transport='unix' socket='/path/socket'/>\n"
                         "  <host name='example.com' port='24007'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='nbd'>\n"
                         "  <host transport='unix' socket='/path/to/socket'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='nbd' name='blah'>\n"
                         "  <host name='example.org' port='6000'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='nbd'>\n"
                         "  <host name='example.org' port='6000'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='ssh' name='blah'>\n"
                         "  <host name='example.org' port='6000'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='rbd' name='libvirt/test'>\n"
                         "  <host name='example.com' port='1234'/>\n"
                         "  <host name='example2.com'/>\n"
                         "  <snapshot name='snapshotname'/>\n"
                         "  <config file='/path/to/conf'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='iscsi' name='iqn.2016-12.com.virttest:emulated-iscsi-noauth.target/0'>\n"
                         "  <host name='test.org' port='3260'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='iscsi' name='iqn.2016-12.com.virttest:emulated-iscsi-noauth.target/6'>\n"
                         "  <host name='test.org' port='1234'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='sheepdog' name='test'>\n"
                         "  <host name='example.com' port='321'/>\n"
                         "</source>\n");
    TEST_JSON_FORMAT_NET("<source protocol='vxhs' name='c6718f6b-0401-441d-a8c3-1f0064d75ee0'>\n"
                         "  <host name='example.com' port='9999'/>\n"
                         "</source>\n");

J
Ján Tomko 已提交
1059
#define TEST_DISK_TO_JSON_FULL(nme, fl) \
1060 1061 1062 1063
    do { \
        diskxmljsondata.name = nme; \
        diskxmljsondata.props = NULL; \
        diskxmljsondata.nprops = 0; \
1064 1065
        diskxmljsondata.propssrc = NULL; \
        diskxmljsondata.npropssrc = 0; \
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
        diskxmljsondata.fail = fl; \
        if (virTestRun("disk xml to props " nme, testQemuDiskXMLToProps, \
                       &diskxmljsondata) < 0) \
            ret = -1; \
        if (virTestRun("disk xml to props validate schema " nme, \
                       testQemuDiskXMLToPropsValidateSchema, &diskxmljsondata) < 0) \
            ret = -1; \
        if (virTestRun("disk xml to props validate file " nme, \
                       testQemuDiskXMLToPropsValidateFile,  &diskxmljsondata) < 0) \
            ret = -1; \
1076 1077 1078
        if (virTestRun("disk xml to props source only validate file " nme, \
                       testQemuDiskXMLToPropsValidateFileSrcOnly,  &diskxmljsondata) < 0) \
            ret = -1; \
1079 1080 1081
        testQemuDiskXMLToPropsClear(&diskxmljsondata); \
    } while (0)

J
Ján Tomko 已提交
1082
#define TEST_DISK_TO_JSON(nme) TEST_DISK_TO_JSON_FULL(nme, false)
1083

1084
    diskxmljsondata.schema = qmp_schema_x86_64;
1085
    diskxmljsondata.schemaroot = qmp_schemaroot_x86_64_blockdev_add;
1086 1087 1088 1089

    TEST_DISK_TO_JSON_FULL("nodename-long-format", true);
    TEST_DISK_TO_JSON_FULL("nodename-long-protocol", true);

1090
    TEST_DISK_TO_JSON("file-raw-noopts");
1091 1092 1093 1094 1095 1096 1097
    TEST_DISK_TO_JSON("file-bochs-noopts");
    TEST_DISK_TO_JSON("file-cloop-noopts");
    TEST_DISK_TO_JSON("file-dmg-noopts");
    TEST_DISK_TO_JSON("file-ploop-noopts");
    TEST_DISK_TO_JSON("file-vdi-noopts");
    TEST_DISK_TO_JSON("file-vhd-noopts");
    TEST_DISK_TO_JSON("file-vpc-noopts");
1098

1099
    TEST_DISK_TO_JSON("file-backing_basic-noopts");
1100 1101
    TEST_DISK_TO_JSON("dir-fat-readonly");
    TEST_DISK_TO_JSON("dir-fat-floppy");
1102 1103
    TEST_DISK_TO_JSON("file-raw-aio_native");
    TEST_DISK_TO_JSON("file-backing_basic-aio_threads");
1104
    TEST_DISK_TO_JSON("file-raw-luks");
1105 1106 1107
    TEST_DISK_TO_JSON("file-qcow2-backing-chain-noopts");
    TEST_DISK_TO_JSON("file-qcow2-backing-chain-unterminated");
    TEST_DISK_TO_JSON("file-qcow2-backing-chain-encryption");
1108
    TEST_DISK_TO_JSON("network-qcow2-backing-chain-encryption_auth");
1109

1110 1111 1112 1113 1114
    TEST_DISK_TO_JSON("file-backing_basic-unmap");
    TEST_DISK_TO_JSON("file-backing_basic-unmap-detect");
    TEST_DISK_TO_JSON("file-backing_basic-unmap-ignore");
    TEST_DISK_TO_JSON("file-backing_basic-detect");

1115 1116 1117 1118 1119 1120 1121
    TEST_DISK_TO_JSON("file-backing_basic-cache-none");
    TEST_DISK_TO_JSON("file-backing_basic-cache-writethrough");
    TEST_DISK_TO_JSON("file-backing_basic-cache-writeback");
    TEST_DISK_TO_JSON("file-backing_basic-cache-directsync");
    TEST_DISK_TO_JSON("file-backing_basic-cache-unsafe");
    TEST_DISK_TO_JSON("network-qcow2-backing-chain-cache-unsafe");
    TEST_DISK_TO_JSON("dir-fat-cache");
1122
    TEST_DISK_TO_JSON("network-nbd-tls");
1123

1124
    TEST_DISK_TO_JSON("block-raw-noopts");
1125
    TEST_DISK_TO_JSON("block-raw-reservations");
1126

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
#define TEST_JSON_TO_JSON(nme) \
    do { \
        jsontojsondata.name = nme; \
        if (virTestRun("JSON to JSON " nme, testJSONtoJSON, \
                       &jsontojsondata) < 0) \
            ret = -1; \
    } while (0)

    jsontojsondata.schema = qmp_schema_x86_64;
    jsontojsondata.schemaroot = qmp_schemaroot_x86_64_blockdev_add;

    TEST_JSON_TO_JSON("curl-libguestfs");
1139
    TEST_JSON_TO_JSON("ssh-passthrough-libguestfs");
1140

J
Ján Tomko 已提交
1141
#define TEST_IMAGE_CREATE(testname, testbacking) \
1142 1143 1144 1145 1146 1147 1148
    do { \
        imagecreatedata.name = testname; \
        imagecreatedata.backingname = testbacking; \
        if (virTestRun("image create xml to props " testname, testQemuImageCreate, \
                       &imagecreatedata) < 0) \
            ret = -1; \
    } while (0)
1149 1150 1151

    imagecreatedata.schema = qmp_schema_x86_64;

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    if (virQEMUQAPISchemaPathGet("blockdev-create/arg-type/options",
                                 imagecreatedata.schema,
                                 &imagecreatedata.schemaroot) < 0 ||
        !imagecreatedata.schemaroot) {
        VIR_TEST_VERBOSE("failed to find schema entry for blockdev-create\n");
        ret = -1;
        goto cleanup;
    }

    TEST_IMAGE_CREATE("raw", NULL);
    TEST_IMAGE_CREATE("raw-nbd", NULL);
    TEST_IMAGE_CREATE("luks-noopts", NULL);
    TEST_IMAGE_CREATE("luks-encopts", NULL);
    TEST_IMAGE_CREATE("qcow2", NULL);
    TEST_IMAGE_CREATE("qcow2-luks-noopts", NULL);
    TEST_IMAGE_CREATE("qcow2-luks-encopts", NULL);
    TEST_IMAGE_CREATE("qcow2-backing-raw", "raw");
    TEST_IMAGE_CREATE("qcow2-backing-raw-nbd", "raw-nbd");
    TEST_IMAGE_CREATE("qcow2-backing-luks", "luks-noopts");
    TEST_IMAGE_CREATE("qcow2-luks-encopts-backing", "qcow2");
1172 1173
    TEST_IMAGE_CREATE("qcow2-backing-raw-slice", "raw-slice");
    TEST_IMAGE_CREATE("qcow2-backing-qcow2-slice", "qcow2-slice");
1174 1175 1176 1177 1178 1179

    TEST_IMAGE_CREATE("network-gluster-qcow2", NULL);
    TEST_IMAGE_CREATE("network-rbd-qcow2", NULL);
    TEST_IMAGE_CREATE("network-ssh-qcow2", NULL);
    TEST_IMAGE_CREATE("network-sheepdog-qcow2", NULL);

J
Ján Tomko 已提交
1180
#define TEST_BITMAP_DETECT(testname) \
1181 1182 1183 1184 1185 1186 1187
    do { \
        if (virTestRun("bitmap detect " testname, \
                       testQemuDetectBitmaps, testname) < 0) \
            ret = -1; \
    } while (0)

    TEST_BITMAP_DETECT("basic");
1188
    TEST_BITMAP_DETECT("synthetic");
1189
    TEST_BITMAP_DETECT("snapshots");
1190
    TEST_BITMAP_DETECT("snapshots-synthetic-checkpoint");
1191
    TEST_BITMAP_DETECT("snapshots-synthetic-broken");
1192

J
Ján Tomko 已提交
1193
#define TEST_BACKUP_BITMAP_CALCULATE(testname, source, incrbackup, named) \
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    do { \
        backupbitmapcalcdata.name = testname; \
        backupbitmapcalcdata.chain = source; \
        backupbitmapcalcdata.incremental = incrbackup; \
        backupbitmapcalcdata.nodedatafile = named; \
        if (virTestRun("incremental backup bitmap " testname, \
                       testQemuBackupIncrementalBitmapCalculate, \
                       &backupbitmapcalcdata) < 0) \
            ret = -1; \
    } while (0)

    TEST_BACKUP_BITMAP_CALCULATE("basic-flat", bitmapSourceChain, "current", "basic");
    TEST_BACKUP_BITMAP_CALCULATE("basic-intermediate", bitmapSourceChain, "d", "basic");
    TEST_BACKUP_BITMAP_CALCULATE("basic-deep", bitmapSourceChain, "a", "basic");

1209 1210 1211 1212
    TEST_BACKUP_BITMAP_CALCULATE("snapshot-flat", bitmapSourceChain, "current", "snapshots");
    TEST_BACKUP_BITMAP_CALCULATE("snapshot-intermediate", bitmapSourceChain, "d", "snapshots");
    TEST_BACKUP_BITMAP_CALCULATE("snapshot-deep", bitmapSourceChain, "a", "snapshots");

1213
#define TEST_CHECKPOINT_DELETE_MERGE(testname, delbmp, parbmp, named) \
1214 1215 1216 1217 1218
    do { \
        checkpointdeletedata.name = testname; \
        checkpointdeletedata.chain = bitmapSourceChain; \
        checkpointdeletedata.deletebitmap = delbmp; \
        checkpointdeletedata.parentbitmap = parbmp; \
1219
        checkpointdeletedata.nodedatafile = named; \
1220 1221 1222 1223 1224
        if (virTestRun("checkpoint delete " testname, \
                       testQemuCheckpointDeleteMerge, &checkpointdeletedata) < 0) \
        ret = -1; \
    } while (0)

1225 1226 1227 1228 1229
    TEST_CHECKPOINT_DELETE_MERGE("basic-noparent", "a", NULL, "basic");
    TEST_CHECKPOINT_DELETE_MERGE("basic-intermediate1", "b", "a", "basic");
    TEST_CHECKPOINT_DELETE_MERGE("basic-intermediate2", "c", "b", "basic");
    TEST_CHECKPOINT_DELETE_MERGE("basic-intermediate3", "d", "c", "basic");
    TEST_CHECKPOINT_DELETE_MERGE("basic-current", "current", "d", "basic");
1230

1231 1232 1233 1234 1235 1236
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-noparent", "a", NULL, "snapshots");
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-intermediate1", "b", "a", "snapshots");
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-intermediate2", "c", "b", "snapshots");
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-intermediate3", "d", "c", "snapshots");
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-current", "current", "d", "snapshots");

1237 1238 1239 1240 1241 1242
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-synthetic-checkpoint-noparent", "a", NULL, "snapshots-synthetic-checkpoint");
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-synthetic-checkpoint-intermediate1", "b", "a", "snapshots-synthetic-checkpoint");
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-synthetic-checkpoint-intermediate2", "c", "b", "snapshots-synthetic-checkpoint");
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-synthetic-checkpoint-intermediate3", "d", "c", "snapshots-synthetic-checkpoint");
    TEST_CHECKPOINT_DELETE_MERGE("snapshots-synthetic-checkpoint-current", "current", "d", "snapshots-synthetic-checkpoint");

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
#define TEST_BITMAP_VALIDATE(testname, bitmap, rc) \
    do { \
        blockbitmapvalidatedata.name = testname; \
        blockbitmapvalidatedata.chain = bitmapSourceChain; \
        blockbitmapvalidatedata.bitmapname = bitmap; \
        blockbitmapvalidatedata.expect = rc; \
        if (virTestRun("bitmap validate " testname " " bitmap, \
                       testQemuBlockBitmapValidate, \
                       &blockbitmapvalidatedata) < 0) \
            ret = -1; \
    } while (0)
1254

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
    TEST_BITMAP_VALIDATE("basic", "a", true);
    TEST_BITMAP_VALIDATE("basic", "b", true);
    TEST_BITMAP_VALIDATE("basic", "c", true);
    TEST_BITMAP_VALIDATE("basic", "d", true);
    TEST_BITMAP_VALIDATE("basic", "current", true);

    TEST_BITMAP_VALIDATE("snapshots", "a", true);
    TEST_BITMAP_VALIDATE("snapshots", "b", true);
    TEST_BITMAP_VALIDATE("snapshots", "c", true);
    TEST_BITMAP_VALIDATE("snapshots", "d", true);
    TEST_BITMAP_VALIDATE("snapshots", "current", true);

    TEST_BITMAP_VALIDATE("synthetic", "a", false);
    TEST_BITMAP_VALIDATE("synthetic", "b", true);
    TEST_BITMAP_VALIDATE("synthetic", "c", true);
    TEST_BITMAP_VALIDATE("synthetic", "d", true);
    TEST_BITMAP_VALIDATE("synthetic", "current", true);

    TEST_BITMAP_VALIDATE("snapshots-synthetic-checkpoint", "a", true);
    TEST_BITMAP_VALIDATE("snapshots-synthetic-checkpoint", "b", true);
    TEST_BITMAP_VALIDATE("snapshots-synthetic-checkpoint", "c", true);
    TEST_BITMAP_VALIDATE("snapshots-synthetic-checkpoint", "d", true);
    TEST_BITMAP_VALIDATE("snapshots-synthetic-checkpoint", "current", true);
1278 1279 1280 1281 1282 1283 1284

    TEST_BITMAP_VALIDATE("snapshots-synthetic-broken", "a", false);
    TEST_BITMAP_VALIDATE("snapshots-synthetic-broken", "b", true);
    TEST_BITMAP_VALIDATE("snapshots-synthetic-broken", "c", true);
    TEST_BITMAP_VALIDATE("snapshots-synthetic-broken", "d", false);
    TEST_BITMAP_VALIDATE("snapshots-synthetic-broken", "current", true);

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
#define TEST_BITMAP_BLOCKCOPY(testname, shllw, ndf) \
    do { \
        blockbitmapblockcopydata.name = testname; \
        blockbitmapblockcopydata.shallow = shllw; \
        blockbitmapblockcopydata.nodedatafile = ndf; \
        blockbitmapblockcopydata.chain = bitmapSourceChain;\
        if (virTestRun("bitmap block copy " testname, \
                       testQemuBlockBitmapBlockcopy, \
                       &blockbitmapblockcopydata) < 0) \
            ret = -1; \
    } while (0)

    TEST_BITMAP_BLOCKCOPY("basic-shallow", true, "basic");
    TEST_BITMAP_BLOCKCOPY("basic-deep", false, "basic");

    TEST_BITMAP_BLOCKCOPY("snapshots-shallow", true, "snapshots");
    TEST_BITMAP_BLOCKCOPY("snapshots-deep", false, "snapshots");

1303 1304 1305 1306 1307
 cleanup:
    qemuTestDriverFree(&driver);
    VIR_FREE(capslatest_x86_64);
    virObjectUnref(caps_x86_64);

1308 1309 1310
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

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