virstoragetest.c 35.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2013-2014 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * 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/>.
 *
 * Author: Eric Blake <eblake@redhat.com>
 */

#include <config.h>

#include <stdlib.h>

#include "testutils.h"
#include "vircommand.h"
#include "virerror.h"
28
#include "virfile.h"
29 30
#include "virlog.h"
#include "virstoragefile.h"
31
#include "virstring.h"
32
#include "dirname.h"
33

34 35
#include "storage/storage_driver.h"

36 37
#define VIR_FROM_THIS VIR_FROM_NONE

38 39
VIR_LOG_INIT("tests.storagetest");

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#define datadir abs_builddir "/virstoragedata"

/* This test creates the following files, all in datadir:

 * raw: 1024-byte raw file
 * qcow2: qcow2 file with 'raw' as backing
 * wrap: qcow2 file with 'qcow2' as backing
 * qed: qed file with 'raw' as backing
 * sub/link1: symlink to qcow2
 * sub/link2: symlink to wrap
 *
 * Relative names to these files are known at compile time, but absolute
 * and canonical names depend on where the test is run; for convenience,
 * we pre-populate the computation of these names for use during the test.
*/

static char *qemuimg;
static char *absraw;
static char *canonraw;
static char *absqcow2;
static char *canonqcow2;
static char *abswrap;
E
Eric Blake 已提交
62
static char *canonwrap;
63
static char *absqed;
E
Eric Blake 已提交
64
static char *canonqed;
E
Eric Blake 已提交
65
static char *absdir;
E
Eric Blake 已提交
66
static char *canondir;
67 68 69 70 71 72 73 74 75 76 77
static char *abslink2;

static void
testCleanupImages(void)
{
    VIR_FREE(qemuimg);
    VIR_FREE(absraw);
    VIR_FREE(canonraw);
    VIR_FREE(absqcow2);
    VIR_FREE(canonqcow2);
    VIR_FREE(abswrap);
E
Eric Blake 已提交
78
    VIR_FREE(canonwrap);
79
    VIR_FREE(absqed);
E
Eric Blake 已提交
80
    VIR_FREE(canonqed);
E
Eric Blake 已提交
81
    VIR_FREE(absdir);
E
Eric Blake 已提交
82
    VIR_FREE(canondir);
83 84 85 86 87 88 89 90
    VIR_FREE(abslink2);

    if (chdir(abs_builddir) < 0) {
        fprintf(stderr, "unable to return to correct directory, refusing to "
                "clean up %s\n", datadir);
        return;
    }

91
    virFileDeleteTree(datadir);
92 93
}

94 95 96 97 98 99 100

static virStorageSourcePtr
testStorageFileGetMetadata(const char *path,
                           int format,
                           uid_t uid, gid_t gid,
                           bool allow_probe)
{
101
    struct stat st;
102 103 104 105 106 107 108 109
    virStorageSourcePtr ret = NULL;

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

    ret->type = VIR_STORAGE_TYPE_FILE;
    ret->format = format;

110 111 112 113 114 115 116 117 118
    if (stat(path, &st) == 0) {
        if (S_ISDIR(st.st_mode)) {
            ret->type = VIR_STORAGE_TYPE_DIR;
            ret->format = VIR_STORAGE_FILE_DIR;
        } else if (S_ISBLK(st.st_mode)) {
            ret->type = VIR_STORAGE_TYPE_BLOCK;
        }
    }

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    if (VIR_STRDUP(ret->relPath, path) < 0)
        goto error;

    if (!(ret->relDir = mdir_name(path))) {
        virReportOOMError();
        goto error;
    }

    if (!(ret->path = canonicalize_file_name(path))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "failed to resolve '%s'", path);
        goto error;
    }

    if (virStorageFileGetMetadata(ret, uid, gid, allow_probe) < 0)
        goto error;

    return ret;

 error:
    virStorageSourceFree(ret);
    return NULL;
}

142 143 144 145 146
static int
testPrepImages(void)
{
    int ret = EXIT_FAILURE;
    virCommandPtr cmd = NULL;
147 148
    char *buf = NULL;
    bool compat = false;
149 150 151 152

    qemuimg = virFindFileInPath("kvm-img");
    if (!qemuimg)
        qemuimg = virFindFileInPath("qemu-img");
153 154
    if (!qemuimg)
        goto skip;
155

156 157 158
    /* Clean up from any earlier failed tests */
    virFileDeleteTree(datadir);

159 160 161 162 163 164 165 166 167 168 169 170
    /* See if qemu-img supports '-o compat=xxx'.  If so, we force the
     * use of both v2 and v3 files; if not, it is v2 only but the test
     * still works. */
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2",
                               "-o?", "/dev/null", NULL);
    virCommandSetOutputBuffer(cmd, &buf);
    if (virCommandRun(cmd, NULL) < 0)
        goto skip;
    if (strstr(buf, "compat "))
        compat = true;
    VIR_FREE(buf);

171 172 173 174
    if (virAsprintf(&absraw, "%s/raw", datadir) < 0 ||
        virAsprintf(&absqcow2, "%s/qcow2", datadir) < 0 ||
        virAsprintf(&abswrap, "%s/wrap", datadir) < 0 ||
        virAsprintf(&absqed, "%s/qed", datadir) < 0 ||
E
Eric Blake 已提交
175
        virAsprintf(&absdir, "%s/dir", datadir) < 0 ||
176
        virAsprintf(&abslink2, "%s/sub/link2", datadir) < 0)
177 178 179 180 181 182
        goto cleanup;

    if (virFileMakePath(datadir "/sub") < 0) {
        fprintf(stderr, "unable to create directory %s\n", datadir "/sub");
        goto cleanup;
    }
E
Eric Blake 已提交
183 184 185 186
    if (virFileMakePath(datadir "/dir") < 0) {
        fprintf(stderr, "unable to create directory %s\n", datadir "/dir");
        goto cleanup;
    }
E
Eric Blake 已提交
187 188 189 190
    if (!(canondir = canonicalize_file_name(absdir))) {
        virReportOOMError();
        goto cleanup;
    }
191 192 193 194 195 196

    if (chdir(datadir) < 0) {
        fprintf(stderr, "unable to test relative backing chains\n");
        goto cleanup;
    }

197 198
    if (virAsprintf(&buf, "%1024d", 0) < 0 ||
        virFileWriteStr("raw", buf, 0600) < 0) {
199
        fprintf(stderr, "unable to create raw file\n");
200
        goto cleanup;
201
    }
202 203 204 205 206 207 208 209
    if (!(canonraw = canonicalize_file_name(absraw))) {
        virReportOOMError();
        goto cleanup;
    }

    /* Create a qcow2 wrapping relative raw; later on, we modify its
     * metadata to test other configurations */
    virCommandFree(cmd);
210 211 212 213
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2", NULL);
    virCommandAddArgFormat(cmd, "-obacking_file=raw,backing_fmt=raw%s",
                           compat ? ",compat=0.10" : "");
    virCommandAddArg(cmd, "qcow2");
214
    if (virCommandRun(cmd, NULL) < 0)
215
        goto skip;
216 217
    /* Make sure our later uses of 'qemu-img rebase' will work */
    virCommandFree(cmd);
218
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
219
                               "-F", "raw", "-b", "raw", "qcow2", NULL);
220 221
    if (virCommandRun(cmd, NULL) < 0)
        goto skip;
222 223 224 225 226 227 228 229
    if (!(canonqcow2 = canonicalize_file_name(absqcow2))) {
        virReportOOMError();
        goto cleanup;
    }

    /* Create a second qcow2 wrapping the first, to be sure that we
     * can correctly avoid insecure probing.  */
    virCommandFree(cmd);
230
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2", NULL);
231 232
    virCommandAddArgFormat(cmd, "-obacking_file=%s,backing_fmt=qcow2%s",
                           absqcow2, compat ? ",compat=1.1" : "");
233 234
    virCommandAddArg(cmd, "wrap");
    if (virCommandRun(cmd, NULL) < 0)
235
        goto skip;
E
Eric Blake 已提交
236 237 238 239
    if (!(canonwrap = canonicalize_file_name(abswrap))) {
        virReportOOMError();
        goto cleanup;
    }
240 241 242

    /* Create a qed file. */
    virCommandFree(cmd);
243
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qed", NULL);
244 245 246 247
    virCommandAddArgFormat(cmd, "-obacking_file=%s,backing_fmt=raw",
                           absraw);
    virCommandAddArg(cmd, "qed");
    if (virCommandRun(cmd, NULL) < 0)
248
        goto skip;
E
Eric Blake 已提交
249 250 251 252
    if (!(canonqed = canonicalize_file_name(absqed))) {
        virReportOOMError();
        goto cleanup;
    }
253

254
#ifdef HAVE_SYMLINK
255 256 257 258 259 260
    /* Create some symlinks in a sub-directory. */
    if (symlink("../qcow2", datadir "/sub/link1") < 0 ||
        symlink("../wrap", datadir "/sub/link2") < 0) {
        fprintf(stderr, "unable to create symlink");
        goto cleanup;
    }
261
#endif
262 263

    ret = 0;
264
 cleanup:
265
    VIR_FREE(buf);
266 267 268 269
    virCommandFree(cmd);
    if (ret)
        testCleanupImages();
    return ret;
270

271
 skip:
272 273 274
    fputs("qemu-img is too old; skipping this test\n", stderr);
    ret = EXIT_AM_SKIP;
    goto cleanup;
275 276
}

277 278 279 280 281
/* Many fields of virStorageFileMetadata have the same content whether
 * we access the file relatively or absolutely; but file names differ
 * depending on how the chain was opened.  For ease of testing, we
 * test both relative and absolute starts, and use a flag to say which
 * of the two variations to compare against.  */
282 283 284 285 286 287 288
typedef struct _testFileData testFileData;
struct _testFileData
{
    const char *expBackingStore;
    const char *expBackingStoreRaw;
    unsigned long long expCapacity;
    bool expEncrypted;
289 290
    const char *pathRel;
    const char *pathAbs;
291
    const char *path;
E
Eric Blake 已提交
292 293 294 295
    const char *relDirRel;
    const char *relDirAbs;
    int type;
    int format;
296 297 298 299 300 301 302
};

enum {
    EXP_PASS = 0,
    EXP_FAIL = 1,
    EXP_WARN = 2,
    ALLOW_PROBE = 4,
303
    ABS_START = 8,
304 305 306 307 308
};

struct testChainData
{
    const char *start;
309
    virStorageFileFormat format;
310
    const testFileData *files[4];
311 312 313 314 315 316 317 318 319
    int nfiles;
    unsigned int flags;
};

static int
testStorageChain(const void *args)
{
    const struct testChainData *data = args;
    int ret = -1;
320 321
    virStorageSourcePtr meta;
    virStorageSourcePtr elt;
322
    size_t i = 0;
E
Eric Blake 已提交
323
    char *broken = NULL;
324
    bool isAbs = !!(data->flags & ABS_START);
325

326 327
    meta = testStorageFileGetMetadata(data->start, data->format, -1, -1,
                                      (data->flags & ALLOW_PROBE) != 0);
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    if (!meta) {
        if (data->flags & EXP_FAIL) {
            virResetLastError();
            ret = 0;
        }
        goto cleanup;
    } else if (data->flags & EXP_FAIL) {
        fprintf(stderr, "call should have failed\n");
        goto cleanup;
    }
    if (data->flags & EXP_WARN) {
        if (!virGetLastError()) {
            fprintf(stderr, "call should have warned\n");
            goto cleanup;
        }
        virResetLastError();
E
Eric Blake 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356
        if (virStorageFileChainGetBroken(meta, &broken) || !broken) {
            fprintf(stderr, "call should identify broken part of chain\n");
            goto cleanup;
        }
    } else {
        if (virGetLastError()) {
            fprintf(stderr, "call should not have warned\n");
            goto cleanup;
        }
        if (virStorageFileChainGetBroken(meta, &broken) || broken) {
            fprintf(stderr, "chain should not be identified as broken\n");
            goto cleanup;
        }
357 358 359 360 361 362
    }

    elt = meta;
    while (elt) {
        char *expect = NULL;
        char *actual = NULL;
363
        const char *expPath;
E
Eric Blake 已提交
364
        const char *expRelDir;
365 366 367 368 369 370

        if (i == data->nfiles) {
            fprintf(stderr, "probed chain was too long\n");
            goto cleanup;
        }

371 372
        expPath = isAbs ? data->files[i]->pathAbs
            : data->files[i]->pathRel;
E
Eric Blake 已提交
373 374
        expRelDir = isAbs ? data->files[i]->relDirAbs
            : data->files[i]->relDirRel;
375
        if (virAsprintf(&expect,
376
                        "store:%s\nraw:%s\nother:%lld %d\n"
377
                        "relPath:%s\npath:%s\nrelDir:%s\ntype:%d %d\n",
378 379 380
                        NULLSTR(data->files[i]->expBackingStore),
                        NULLSTR(data->files[i]->expBackingStoreRaw),
                        data->files[i]->expCapacity,
381
                        data->files[i]->expEncrypted,
E
Eric Blake 已提交
382
                        NULLSTR(expPath),
383
                        NULLSTR(data->files[i]->path),
E
Eric Blake 已提交
384 385 386
                        NULLSTR(expRelDir),
                        data->files[i]->type,
                        data->files[i]->format) < 0 ||
387
            virAsprintf(&actual,
388
                        "store:%s\nraw:%s\nother:%lld %d\n"
389
                        "relPath:%s\npath:%s\nrelDir:%s\ntype:%d %d\n",
390
                        NULLSTR(elt->backingStore ? elt->backingStore->path : NULL),
391
                        NULLSTR(elt->backingStoreRaw),
392
                        elt->capacity, !!elt->encryption,
393
                        NULLSTR(elt->relPath),
394
                        NULLSTR(elt->path),
E
Eric Blake 已提交
395 396
                        NULLSTR(elt->relDir),
                        elt->type, elt->format) < 0) {
397 398 399 400 401
            VIR_FREE(expect);
            VIR_FREE(actual);
            goto cleanup;
        }
        if (STRNEQ(expect, actual)) {
402
            fprintf(stderr, "chain member %zu", i);
403 404 405 406 407 408 409
            virtTestDifference(stderr, expect, actual);
            VIR_FREE(expect);
            VIR_FREE(actual);
            goto cleanup;
        }
        VIR_FREE(expect);
        VIR_FREE(actual);
410
        elt = elt->backingStore;
411 412 413 414 415 416 417 418
        i++;
    }
    if (i != data->nfiles) {
        fprintf(stderr, "probed chain was too short\n");
        goto cleanup;
    }

    ret = 0;
419
 cleanup:
E
Eric Blake 已提交
420
    VIR_FREE(broken);
421
    virStorageSourceFree(meta);
422 423 424
    return ret;
}

E
Eric Blake 已提交
425 426
struct testLookupData
{
427
    virStorageSourcePtr chain;
428
    const char *target;
E
Eric Blake 已提交
429
    const char *name;
430
    unsigned int expIndex;
E
Eric Blake 已提交
431
    const char *expResult;
432
    virStorageSourcePtr expMeta;
E
Eric Blake 已提交
433 434 435 436 437 438 439 440
    const char *expParent;
};

static int
testStorageLookup(const void *args)
{
    const struct testLookupData *data = args;
    int ret = 0;
441
    virStorageSourcePtr result;
E
Eric Blake 已提交
442
    const char *actualParent;
443 444 445 446 447 448 449 450 451 452 453
    unsigned int idx;

    if (virStorageFileParseChainIndex(data->target, data->name, &idx) < 0 &&
        data->expIndex) {
        fprintf(stderr, "call should not have failed\n");
        ret = -1;
    }
    if (idx != data->expIndex) {
        fprintf(stderr, "index: expected %u, got %u\n", data->expIndex, idx);
        ret = -1;
    }
E
Eric Blake 已提交
454

455
     /* Test twice to ensure optional parameter doesn't cause NULL deref. */
456 457 458
    result = virStorageFileChainLookup(data->chain, NULL,
                                       idx ? NULL : data->name,
                                       idx, NULL);
E
Eric Blake 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472

    if (!data->expResult) {
        if (!virGetLastError()) {
            fprintf(stderr, "call should have failed\n");
            ret = -1;
        }
        virResetLastError();
    } else {
        if (virGetLastError()) {
            fprintf(stderr, "call should not have warned\n");
            ret = -1;
        }
    }

473 474 475 476 477 478 479
    if (!result) {
        if (data->expResult) {
            fprintf(stderr, "result 1: expected %s, got NULL\n",
                    data->expResult);
            ret = -1;
        }
    } else if (STRNEQ_NULLABLE(data->expResult, result->path)) {
E
Eric Blake 已提交
480
        fprintf(stderr, "result 1: expected %s, got %s\n",
481
                NULLSTR(data->expResult), NULLSTR(result->path));
E
Eric Blake 已提交
482 483 484
        ret = -1;
    }

485 486
    result = virStorageFileChainLookup(data->chain, data->chain,
                                       data->name, idx, &actualParent);
E
Eric Blake 已提交
487 488
    if (!data->expResult)
        virResetLastError();
489 490 491 492 493 494 495 496

    if (!result) {
        if (data->expResult) {
            fprintf(stderr, "result 2: expected %s, got NULL\n",
                    data->expResult);
            ret = -1;
        }
    } else if (STRNEQ_NULLABLE(data->expResult, result->path)) {
E
Eric Blake 已提交
497
        fprintf(stderr, "result 2: expected %s, got %s\n",
498
                NULLSTR(data->expResult), NULLSTR(result->path));
E
Eric Blake 已提交
499 500
        ret = -1;
    }
501
    if (data->expMeta != result) {
E
Eric Blake 已提交
502
        fprintf(stderr, "meta: expected %p, got %p\n",
503
                data->expMeta, result);
E
Eric Blake 已提交
504 505 506 507 508 509 510 511 512 513 514
        ret = -1;
    }
    if (STRNEQ_NULLABLE(data->expParent, actualParent)) {
        fprintf(stderr, "parent: expected %s, got %s\n",
                NULLSTR(data->expParent), NULLSTR(actualParent));
        ret = -1;
    }

    return ret;
}

515 516 517 518 519
static int
mymain(void)
{
    int ret;
    virCommandPtr cmd = NULL;
520
    struct testChainData data;
521
    virStorageSourcePtr chain = NULL;
522 523 524 525 526 527

    /* Prep some files with qemu-img; if that is not found on PATH, or
     * if it lacks support for qcow2 and qed, skip this test.  */
    if ((ret = testPrepImages()) != 0)
        return ret;

528
#define TEST_ONE_CHAIN(id, start, format, flags, ...)                \
529
    do {                                                             \
530 531 532 533
        size_t i;                                                    \
        memset(&data, 0, sizeof(data));                              \
        data = (struct testChainData){                               \
            start, format, { __VA_ARGS__ }, 0, flags,                \
534
        };                                                           \
535 536 537
        for (i = 0; i < ARRAY_CARDINALITY(data.files); i++)          \
            if (data.files[i])                                       \
                data.nfiles++;                                       \
538
        if (virtTestRun("Storage backing chain " id,                 \
539 540 541 542
                        testStorageChain, &data) < 0)                \
            ret = -1;                                                \
    } while (0)

543 544 545
#define VIR_FLATTEN_2(...) __VA_ARGS__
#define VIR_FLATTEN_1(_1) VIR_FLATTEN_2 _1

546 547 548
#define TEST_CHAIN(id, relstart, absstart, format, chain1, flags1,   \
                   chain2, flags2, chain3, flags3, chain4, flags4)   \
    do {                                                             \
549 550 551 552
        TEST_ONE_CHAIN(#id "a", relstart, format, flags1,            \
                       VIR_FLATTEN_1(chain1));                       \
        TEST_ONE_CHAIN(#id "b", relstart, format, flags2,            \
                       VIR_FLATTEN_1(chain2));                       \
553
        TEST_ONE_CHAIN(#id "c", absstart, format, flags3 | ABS_START,\
554
                       VIR_FLATTEN_1(chain3));                       \
555
        TEST_ONE_CHAIN(#id "d", absstart, format, flags4 | ABS_START,\
556
                       VIR_FLATTEN_1(chain4));                       \
557 558 559 560 561
    } while (0)

    /* The actual tests, in several groups. */

    /* Missing file */
562
    TEST_ONE_CHAIN("0", "bogus", VIR_STORAGE_FILE_RAW, EXP_FAIL);
563 564

    /* Raw image, whether with right format or no specified format */
565
    testFileData raw = {
566 567
        .pathRel = "raw",
        .pathAbs = canonraw,
568
        .path = canonraw,
E
Eric Blake 已提交
569 570 571 572
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
573
    };
574
    TEST_CHAIN(1, "raw", absraw, VIR_STORAGE_FILE_RAW,
575 576 577 578
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS,
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS);
579
    TEST_CHAIN(2, "raw", absraw, VIR_STORAGE_FILE_AUTO,
580 581 582 583
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS,
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS);
584 585

    /* Qcow2 file with relative raw backing, format provided */
586
    raw.pathAbs = "raw";
587
    testFileData qcow2 = {
588 589 590
        .expBackingStore = canonraw,
        .expBackingStoreRaw = "raw",
        .expCapacity = 1024,
591 592
        .pathRel = "qcow2",
        .pathAbs = canonqcow2,
593
        .path = canonqcow2,
E
Eric Blake 已提交
594 595 596 597
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
598 599 600 601
    };
    testFileData qcow2_as_raw = {
        .pathRel = "qcow2",
        .pathAbs = canonqcow2,
602
        .path = canonqcow2,
E
Eric Blake 已提交
603 604 605 606
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
607
    };
608
    TEST_CHAIN(3, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
609 610 611 612
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS,
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
613
    TEST_CHAIN(4, "qcow2", absqcow2, VIR_STORAGE_FILE_AUTO,
614
               (&qcow2_as_raw), EXP_PASS,
615
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS,
616
               (&qcow2_as_raw), EXP_PASS,
617
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
618 619 620 621 622 623 624

    /* Rewrite qcow2 file to use absolute backing name */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", absraw, "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
625
    qcow2.expBackingStoreRaw = absraw;
626 627
    raw.pathRel = absraw;
    raw.pathAbs = absraw;
E
Eric Blake 已提交
628
    raw.relDirRel = datadir;
629 630 631

    /* Qcow2 file with raw as absolute backing, backing format provided */
    TEST_CHAIN(5, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
632 633 634 635
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS,
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
636
    TEST_CHAIN(6, "qcow2", absqcow2, VIR_STORAGE_FILE_AUTO,
637
               (&qcow2_as_raw), EXP_PASS,
638
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS,
639
               (&qcow2_as_raw), EXP_PASS,
640
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
641 642

    /* Wrapped file access */
643 644 645 646
    testFileData wrap = {
        .expBackingStore = canonqcow2,
        .expBackingStoreRaw = absqcow2,
        .expCapacity = 1024,
647 648
        .pathRel = "wrap",
        .pathAbs = abswrap,
649
        .path = canonwrap,
E
Eric Blake 已提交
650 651 652 653
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
654
    };
655
    qcow2.pathRel = absqcow2;
E
Eric Blake 已提交
656
    qcow2.relDirRel = datadir;
657
    TEST_CHAIN(7, "wrap", abswrap, VIR_STORAGE_FILE_QCOW2,
658 659 660 661
               (&wrap, &qcow2, &raw), EXP_PASS,
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS,
               (&wrap, &qcow2, &raw), EXP_PASS,
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS);
662 663 664 665 666 667 668 669 670 671 672 673 674

    /* Rewrite qcow2 and wrap file to omit backing file type */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-b", absraw, "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-b", absqcow2, "wrap", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
675
    qcow2_as_raw.pathRel = absqcow2;
E
Eric Blake 已提交
676
    qcow2_as_raw.relDirRel = datadir;
677 678

    /* Qcow2 file with raw as absolute backing, backing format omitted */
679 680 681 682
    testFileData wrap_as_raw = {
        .expBackingStore = canonqcow2,
        .expBackingStoreRaw = absqcow2,
        .expCapacity = 1024,
683 684
        .pathRel = "wrap",
        .pathAbs = abswrap,
685
        .path = canonwrap,
E
Eric Blake 已提交
686 687 688 689
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
690
    };
691
    TEST_CHAIN(8, "wrap", abswrap, VIR_STORAGE_FILE_QCOW2,
692
               (&wrap_as_raw, &qcow2_as_raw), EXP_PASS,
693
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS,
694
               (&wrap_as_raw, &qcow2_as_raw), EXP_PASS,
695
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS);
696 697 698 699 700 701 702 703

    /* Rewrite qcow2 to a missing backing file, with backing type */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", datadir "/bogus",
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
704 705
    qcow2.expBackingStore = NULL;
    qcow2.expBackingStoreRaw = datadir "/bogus";
706
    qcow2.pathRel = "qcow2";
E
Eric Blake 已提交
707
    qcow2.relDirRel = ".";
708 709 710

    /* Qcow2 file with missing backing file but specified type */
    TEST_CHAIN(9, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
711 712 713 714
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN,
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
715 716 717 718 719 720 721 722 723 724

    /* Rewrite qcow2 to a missing backing file, without backing type */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-b", datadir "/bogus", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    /* Qcow2 file with missing backing file and no specified type */
    TEST_CHAIN(10, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
725 726 727 728
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN,
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
729 730 731 732 733 734 735 736

    /* Rewrite qcow2 to use an nbd: protocol as backend */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "nbd:example.org:6000",
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
737
    qcow2.expBackingStore = "nbd:example.org:6000";
738
    qcow2.expBackingStoreRaw = "nbd:example.org:6000";
739 740

    /* Qcow2 file with backing protocol instead of file */
741 742 743
    testFileData nbd = {
        .pathRel = "nbd:example.org:6000",
        .pathAbs = "nbd:example.org:6000",
744
        .path = "nbd:example.org:6000",
745 746 747
        .type = VIR_STORAGE_TYPE_NETWORK,
        .format = VIR_STORAGE_FILE_RAW,
    };
748
    TEST_CHAIN(11, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
749 750 751 752
               (&qcow2, &nbd), EXP_PASS,
               (&qcow2, &nbd), ALLOW_PROBE | EXP_PASS,
               (&qcow2, &nbd), EXP_PASS,
               (&qcow2, &nbd), ALLOW_PROBE | EXP_PASS);
753 754

    /* qed file */
755 756 757 758
    testFileData qed = {
        .expBackingStore = canonraw,
        .expBackingStoreRaw = absraw,
        .expCapacity = 1024,
759 760
        .pathRel = "qed",
        .pathAbs = absqed,
761
        .path = canonqed,
E
Eric Blake 已提交
762 763 764 765
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QED,
766 767 768 769
    };
    testFileData qed_as_raw = {
        .pathRel = "qed",
        .pathAbs = absqed,
770
        .path = canonqed,
E
Eric Blake 已提交
771 772 773 774
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
775
    };
776
    TEST_CHAIN(12, "qed", absqed, VIR_STORAGE_FILE_AUTO,
777
               (&qed_as_raw), EXP_PASS,
778
               (&qed, &raw), ALLOW_PROBE | EXP_PASS,
779
               (&qed_as_raw), EXP_PASS,
780
               (&qed, &raw), ALLOW_PROBE | EXP_PASS);
781

E
Eric Blake 已提交
782
    /* directory */
783
    testFileData dir = {
784 785
        .pathRel = "dir",
        .pathAbs = absdir,
786 787 788
        .path = canondir,
        .relDirRel = ".",
        .relDirAbs = datadir,
E
Eric Blake 已提交
789 790
        .type = VIR_STORAGE_TYPE_DIR,
        .format = VIR_STORAGE_FILE_DIR,
791
    };
E
Eric Blake 已提交
792 793 794 795 796 797 798 799 800 801 802
    TEST_CHAIN(13, "dir", absdir, VIR_STORAGE_FILE_AUTO,
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS,
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS);
    TEST_CHAIN(14, "dir", absdir, VIR_STORAGE_FILE_DIR,
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS,
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS);

803
#ifdef HAVE_SYMLINK
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    /* Rewrite qcow2 and wrap file to use backing names relative to a
     * symlink from a different directory */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "../raw", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "../sub/link1", "wrap",
                               NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    /* Behavior of symlinks to qcow2 with relative backing files */
820
    testFileData link1 = {
821 822 823
        .expBackingStore = canonraw,
        .expBackingStoreRaw = "../raw",
        .expCapacity = 1024,
824 825
        .pathRel = "../sub/link1",
        .pathAbs = "../sub/link1",
826
        .path = canonqcow2,
E
Eric Blake 已提交
827 828 829 830
        .relDirRel = "sub/../sub",
        .relDirAbs = datadir "/sub/../sub",
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
831
    };
832
    testFileData link2 = {
833 834 835
        .expBackingStore = canonqcow2,
        .expBackingStoreRaw = "../sub/link1",
        .expCapacity = 1024,
836 837
        .pathRel = "sub/link2",
        .pathAbs = abslink2,
838
        .path = canonwrap,
E
Eric Blake 已提交
839 840 841 842
        .relDirRel = "sub",
        .relDirAbs = datadir "/sub",
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
843
    };
844 845
    raw.pathRel = "../raw";
    raw.pathAbs = "../raw";
E
Eric Blake 已提交
846 847
    raw.relDirRel = "sub/../sub/..";
    raw.relDirAbs = datadir "/sub/../sub/..";
E
Eric Blake 已提交
848
    TEST_CHAIN(15, "sub/link2", abslink2, VIR_STORAGE_FILE_QCOW2,
849 850 851 852
               (&link2, &link1, &raw), EXP_PASS,
               (&link2, &link1, &raw), ALLOW_PROBE | EXP_PASS,
               (&link2, &link1, &raw), EXP_PASS,
               (&link2, &link1, &raw), ALLOW_PROBE | EXP_PASS);
853
#endif
E
Eric Blake 已提交
854 855 856 857 858 859 860

    /* Rewrite qcow2 to be a self-referential loop */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "qcow2", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
861 862
    qcow2.expBackingStore = NULL;
    qcow2.expBackingStoreRaw = "qcow2";
E
Eric Blake 已提交
863 864 865

    /* Behavior of an infinite loop chain */
    TEST_CHAIN(16, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
866 867 868 869
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN,
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
E
Eric Blake 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882

    /* Rewrite wrap and qcow2 to be mutually-referential loop */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "wrap", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", absqcow2, "wrap", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
883
    qcow2.expBackingStoreRaw = "wrap";
884
    qcow2.pathRel = absqcow2;
E
Eric Blake 已提交
885
    qcow2.relDirRel =  datadir;
E
Eric Blake 已提交
886 887 888

    /* Behavior of an infinite loop chain */
    TEST_CHAIN(17, "wrap", abswrap, VIR_STORAGE_FILE_QCOW2,
889 890 891 892
               (&wrap, &qcow2), EXP_WARN,
               (&wrap, &qcow2), ALLOW_PROBE | EXP_WARN,
               (&wrap, &qcow2), EXP_WARN,
               (&wrap, &qcow2), ALLOW_PROBE | EXP_WARN);
E
Eric Blake 已提交
893

E
Eric Blake 已提交
894 895 896 897 898 899 900
    /* Rewrite wrap and qcow2 back to 3-deep chain, absolute backing */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", absraw, "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

901
    /* Test behavior of chain lookups, absolute backing from relative start */
902 903
    chain = testStorageFileGetMetadata("wrap", VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
904 905 906 907 908
    if (!chain) {
        ret = -1;
        goto cleanup;
    }

909 910 911 912 913 914 915
#define TEST_LOOKUP_TARGET(id, target, name, index, result, meta, parent)   \
    do {                                                                    \
        struct testLookupData data2 = { chain, target, name, index,         \
                                        result, meta, parent, };            \
        if (virtTestRun("Chain lookup " #id,                                \
                        testStorageLookup, &data2) < 0)                     \
            ret = -1;                                                       \
E
Eric Blake 已提交
916
    } while (0)
917 918
#define TEST_LOOKUP(id, name, result, meta, parent)                         \
    TEST_LOOKUP_TARGET(id, NULL, name, 0, result, meta, parent)
E
Eric Blake 已提交
919 920

    TEST_LOOKUP(0, "bogus", NULL, NULL, NULL);
921 922
    TEST_LOOKUP(1, "wrap", chain->path, chain, NULL);
    TEST_LOOKUP(2, abswrap, chain->path, chain, NULL);
923
    TEST_LOOKUP(3, "qcow2", chain->backingStore->path, chain->backingStore,
924
                chain->path);
925
    TEST_LOOKUP(4, absqcow2, chain->backingStore->path, chain->backingStore,
926
                chain->path);
927 928 929 930 931 932
    TEST_LOOKUP(5, "raw", chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
    TEST_LOOKUP(6, absraw, chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
    TEST_LOOKUP(7, NULL, chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
E
Eric Blake 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946

    /* Rewrite wrap and qcow2 back to 3-deep chain, relative backing */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "raw", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "qcow2", "wrap", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

947
    /* Test behavior of chain lookups, relative backing from absolute start */
948
    virStorageSourceFree(chain);
949 950
    chain = testStorageFileGetMetadata(abswrap, VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
951 952 953 954 955 956
    if (!chain) {
        ret = -1;
        goto cleanup;
    }

    TEST_LOOKUP(8, "bogus", NULL, NULL, NULL);
957 958
    TEST_LOOKUP(9, "wrap", chain->path, chain, NULL);
    TEST_LOOKUP(10, abswrap, chain->path, chain, NULL);
959
    TEST_LOOKUP(11, "qcow2", chain->backingStore->path, chain->backingStore,
960
                chain->path);
961
    TEST_LOOKUP(12, absqcow2, chain->backingStore->path, chain->backingStore,
962
                chain->path);
963 964 965 966 967 968
    TEST_LOOKUP(13, "raw", chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
    TEST_LOOKUP(14, absraw, chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
    TEST_LOOKUP(15, NULL, chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
E
Eric Blake 已提交
969 970 971 972 973 974 975 976 977

    /* Use link to wrap with cross-directory relative backing */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "../qcow2", "wrap", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    /* Test behavior of chain lookups, relative backing */
978
    virStorageSourceFree(chain);
979 980
    chain = testStorageFileGetMetadata("sub/link2", VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
981 982 983 984 985 986
    if (!chain) {
        ret = -1;
        goto cleanup;
    }

    TEST_LOOKUP(16, "bogus", NULL, NULL, NULL);
987 988 989
    TEST_LOOKUP(17, "sub/link2", chain->path, chain, NULL);
    TEST_LOOKUP(18, "wrap", chain->path, chain, NULL);
    TEST_LOOKUP(19, abswrap, chain->path, chain, NULL);
990
    TEST_LOOKUP(20, "../qcow2", chain->backingStore->path, chain->backingStore,
991
                chain->path);
E
Eric Blake 已提交
992
    TEST_LOOKUP(21, "qcow2", NULL, NULL, NULL);
993
    TEST_LOOKUP(22, absqcow2, chain->backingStore->path, chain->backingStore,
994
                chain->path);
995 996 997 998 999 1000
    TEST_LOOKUP(23, "raw", chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
    TEST_LOOKUP(24, absraw, chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
    TEST_LOOKUP(25, NULL, chain->backingStore->backingStore->path,
                chain->backingStore->backingStore, chain->backingStore->path);
E
Eric Blake 已提交
1001

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    TEST_LOOKUP_TARGET(26, "vda", "bogus[1]", 0, NULL, NULL, NULL);
    TEST_LOOKUP_TARGET(27, "vda", "vda[-1]", 0, NULL, NULL, NULL);
    TEST_LOOKUP_TARGET(28, "vda", "vda[1][1]", 0, NULL, NULL, NULL);
    TEST_LOOKUP_TARGET(29, "vda", "wrap", 0, chain->path, chain, NULL);
    TEST_LOOKUP_TARGET(30, "vda", "vda[0]", 0, NULL, NULL, NULL);
    TEST_LOOKUP_TARGET(31, "vda", "vda[1]", 1,
                       chain->backingStore->path,
                       chain->backingStore,
                       chain->path);
    TEST_LOOKUP_TARGET(32, "vda", "vda[2]", 2,
                       chain->backingStore->backingStore->path,
                       chain->backingStore->backingStore,
                       chain->backingStore->path);
    TEST_LOOKUP_TARGET(33, "vda", "vda[3]", 3, NULL, NULL, NULL);

E
Eric Blake 已提交
1017
 cleanup:
1018
    /* Final cleanup */
1019
    virStorageSourceFree(chain);
1020 1021 1022 1023 1024 1025 1026
    testCleanupImages();
    virCommandFree(cmd);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)