storagevolxml2argvtest.c 9.1 KB
Newer Older
1 2 3 4 5
#include <config.h>

#include "internal.h"
#include "testutils.h"
#include "datatypes.h"
6
#include "storage/storage_util.h"
7
#include "testutilsqemu.h"
8
#include "virstring.h"
9

10 11
#define VIR_FROM_THIS VIR_FROM_NONE

12 13
const char create_tool[] = "qemu-img";

14 15 16 17 18
/* createVol sets this on volume creation */
static void
testSetVolumeType(virStorageVolDefPtr vol,
                  virStoragePoolDefPtr pool)
{
19
    if (!vol || !pool)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
        return;

    switch (pool->type) {
    case VIR_STORAGE_POOL_DIR:
    case VIR_STORAGE_POOL_FS:
    case VIR_STORAGE_POOL_NETFS:
        vol->type = VIR_STORAGE_VOL_FILE;
        return;

    case VIR_STORAGE_POOL_LOGICAL:
        vol->type = VIR_STORAGE_VOL_BLOCK;
        return;
    }
}

35 36 37 38
static int
testCompareXMLToArgvFiles(bool shouldFail,
                          const char *poolxml,
                          const char *volxml,
39
                          const char *inputpoolxml,
40 41 42
                          const char *inputvolxml,
                          const char *cmdline,
                          unsigned int flags,
43 44
                          int imgformat,
                          unsigned long parse_flags)
45 46 47 48 49 50 51 52
{
    char *actualCmdline = NULL;
    int ret = -1;

    virCommandPtr cmd = NULL;
    virConnectPtr conn;

    virStorageVolDefPtr vol = NULL, inputvol = NULL;
53
    virStoragePoolDefPtr def = NULL;
54
    virStoragePoolDefPtr inputpool = NULL;
55
    virStoragePoolObjPtr obj = NULL;
56 57 58 59

    if (!(conn = virGetConnect()))
        goto cleanup;

60
    if (!(def = virStoragePoolDefParseFile(poolxml)))
61 62
        goto cleanup;

63 64 65 66 67
    if (!(obj = virStoragePoolObjNew())) {
        virStoragePoolDefFree(def);
        goto cleanup;
    }
    virStoragePoolObjSetDef(obj, def);
68

69
    if (inputpoolxml) {
C
Cole Robinson 已提交
70
        if (!(inputpool = virStoragePoolDefParseFile(inputpoolxml)))
71 72 73
            goto cleanup;
    }

74 75 76
    if (inputvolxml)
        parse_flags |= VIR_VOL_XML_PARSE_NO_CAPACITY;

77
    if (!(vol = virStorageVolDefParseFile(def, volxml, parse_flags)))
78 79 80
        goto cleanup;

    if (inputvolxml &&
C
Cole Robinson 已提交
81
        !(inputvol = virStorageVolDefParseFile(inputpool, inputvolxml, 0)))
82 83
        goto cleanup;

84
    testSetVolumeType(vol, def);
85 86
    testSetVolumeType(inputvol, inputpool);

87
    cmd = virStorageBackendCreateQemuImgCmdFromVol(conn, obj, vol,
88
                                                   inputvol, flags,
89 90
                                                   create_tool, imgformat,
                                                   NULL);
91
    if (!cmd) {
92 93 94 95 96 97 98
        if (shouldFail) {
            virResetLastError();
            ret = 0;
        }
        goto cleanup;
    }

99 100 101
    if (!(actualCmdline = virCommandToString(cmd)))
        goto cleanup;

102
    if (virTestCompareToFile(actualCmdline, cmdline) < 0)
103 104 105 106
        goto cleanup;

    ret = 0;

107
 cleanup:
108
    virStoragePoolDefFree(inputpool);
109 110 111 112
    virStorageVolDefFree(vol);
    virStorageVolDefFree(inputvol);
    virCommandFree(cmd);
    VIR_FREE(actualCmdline);
J
John Ferlan 已提交
113 114
    if (obj)
        virStoragePoolObjUnlock(obj);
115
    virStoragePoolObjFree(obj);
116
    virObjectUnref(conn);
117 118 119 120 121 122 123
    return ret;
}

struct testInfo {
    bool shouldFail;
    const char *pool;
    const char *vol;
124
    const char *inputpool;
125 126 127 128
    const char *inputvol;
    const char *cmdline;
    unsigned int flags;
    int imgformat;
129
    unsigned long parseflags;
130 131 132 133 134 135 136 137
};

static int
testCompareXMLToArgvHelper(const void *data)
{
    int result = -1;
    const struct testInfo *info = data;
    char *poolxml = NULL;
138
    char *inputpoolxml = NULL;
139 140 141 142 143
    char *volxml = NULL;
    char *inputvolxml = NULL;
    char *cmdline = NULL;

    if (info->inputvol &&
144
        virAsprintf(&inputvolxml, "%s/storagevolxml2xmlin/%s.xml",
145 146
                    abs_srcdir, info->inputvol) < 0)
        goto cleanup;
147 148 149 150
    if (info->inputpool &&
        virAsprintf(&inputpoolxml, "%s/storagepoolxml2xmlin/%s.xml",
                    abs_srcdir, info->inputpool) < 0)
        goto cleanup;
151
    if (virAsprintf(&poolxml, "%s/storagepoolxml2xmlin/%s.xml",
152
                    abs_srcdir, info->pool) < 0 ||
153
        virAsprintf(&volxml, "%s/storagevolxml2xmlin/%s.xml",
154 155 156 157 158 159 160 161
                    abs_srcdir, info->vol) < 0) {
        goto cleanup;
    }
    if (virAsprintf(&cmdline, "%s/storagevolxml2argvdata/%s.argv",
                    abs_srcdir, info->cmdline) < 0 && !info->shouldFail)
        goto cleanup;

    result = testCompareXMLToArgvFiles(info->shouldFail, poolxml, volxml,
162 163
                                       inputpoolxml, inputvolxml,
                                       cmdline, info->flags,
164
                                       info->imgformat, info->parseflags);
165

166
 cleanup:
167 168 169
    VIR_FREE(poolxml);
    VIR_FREE(volxml);
    VIR_FREE(inputvolxml);
170
    VIR_FREE(inputpoolxml);
171 172 173 174 175 176
    VIR_FREE(cmdline);

    return result;
}

enum {
177
    FMT_OPTIONS = 0,
178
    FMT_COMPAT,
179 180 181 182 183 184 185 186 187 188
};



static int
mymain(void)
{
    int ret = 0;
    unsigned int flags = VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;

189 190
#define DO_TEST_FULL(shouldFail, parseflags, pool, vol, inputpool, inputvol, \
                     cmdline, flags, imgformat)                              \
191
    do {                                                                     \
192
        struct testInfo info = { shouldFail, pool, vol, inputpool, inputvol, \
193
                                 cmdline, flags, imgformat, parseflags };    \
194 195
        if (virTestRun("Storage Vol XML-2-argv " cmdline,                    \
                       testCompareXMLToArgvHelper, &info) < 0)               \
196 197
            ret = -1;                                                        \
       }                                                                     \
198 199
    while (0);

200
#define DO_TEST(pool, ...)                                                 \
201
    DO_TEST_FULL(false, 0, pool, __VA_ARGS__)
202 203

#define DO_TEST_FAIL(pool, ...)                                            \
204
    DO_TEST_FULL(true, 0, pool, __VA_ARGS__)
205 206

    DO_TEST("pool-dir", "vol-qcow2",
207
            NULL, NULL,
208 209
            "qcow2", 0, FMT_OPTIONS);
    DO_TEST_FAIL("pool-dir", "vol-qcow2",
210
                 NULL, NULL,
211 212
                 "qcow2-prealloc", flags, FMT_OPTIONS);
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
213
            NULL, NULL,
214
            "qcow2-nobacking-prealloc", flags, FMT_OPTIONS);
215
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
216
            "pool-dir", "vol-file",
217
            "qcow2-nobacking-convert-prealloc", flags, FMT_OPTIONS);
218 219 220
    DO_TEST_FAIL("pool-dir", "vol-qcow2",
                 "pool-dir", "vol-file",
                 "qcow2-convert-nobacking", 0, FMT_OPTIONS);
221
    DO_TEST_FAIL("pool-dir", "vol-qcow2",
222
                 "pool-dir", "vol-file",
223 224
                 "qcow2-convert-prealloc", flags, FMT_OPTIONS);
    DO_TEST("pool-dir", "vol-qcow2-lazy",
225
            NULL, NULL,
226 227
            "qcow2-lazy", 0, FMT_OPTIONS);
    DO_TEST("pool-dir", "vol-qcow2-1.1",
228
            NULL, NULL,
229 230
            "qcow2-1.1", 0, FMT_OPTIONS);
    DO_TEST_FAIL("pool-dir", "vol-qcow2-0.10-lazy",
231
                 NULL, NULL,
232
                 "qcow2-0.10-lazy", 0, FMT_OPTIONS);
233 234 235 236 237 238
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
            "pool-logical", "vol-logical",
            "qcow2-from-logical", 0, FMT_OPTIONS);
    DO_TEST("pool-logical", "vol-logical",
            "pool-dir", "vol-qcow2-nobacking",
            "logical-from-qcow2", 0, FMT_OPTIONS);
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    DO_TEST("pool-dir", "vol-qcow2",
            NULL, NULL,
            "qcow2-compat", 0, FMT_COMPAT);
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
            NULL, NULL,
            "qcow2-nobacking-prealloc-compat", flags, FMT_COMPAT);
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
            "pool-dir", "vol-file",
            "qcow2-nobacking-convert-prealloc-compat", flags, FMT_COMPAT);
    DO_TEST("pool-dir", "vol-qcow2-lazy",
            NULL, NULL,
            "qcow2-lazy", 0, FMT_COMPAT);
    DO_TEST("pool-dir", "vol-qcow2-1.1",
            NULL, NULL,
            "qcow2-1.1", 0, FMT_COMPAT);
    DO_TEST_FAIL("pool-dir", "vol-qcow2-0.10-lazy",
                 NULL, NULL,
                 "qcow2-0.10-lazy", 0, FMT_COMPAT);
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
            "pool-logical", "vol-logical",
            "qcow2-from-logical-compat", 0, FMT_COMPAT);
    DO_TEST("pool-logical", "vol-logical",
            "pool-dir", "vol-qcow2-nobacking",
            "logical-from-qcow2", 0, FMT_COMPAT);
C
Chunyan Liu 已提交
264 265 266 267 268 269
    DO_TEST("pool-dir", "vol-qcow2-nocow",
            NULL, NULL,
            "qcow2-nocow", 0, FMT_OPTIONS);
    DO_TEST("pool-dir", "vol-qcow2-nocow",
            NULL, NULL,
            "qcow2-nocow-compat", 0, FMT_COMPAT);
270 271 272
    DO_TEST("pool-dir", "vol-qcow2-nocapacity",
            "pool-dir", "vol-file",
            "qcow2-nocapacity-convert-prealloc", flags, FMT_OPTIONS);
273 274 275
    DO_TEST("pool-dir", "vol-qcow2-zerocapacity",
            NULL, NULL,
            "qcow2-zerocapacity", 0, FMT_COMPAT);
276 277 278
    DO_TEST_FULL(false, VIR_VOL_XML_PARSE_OPT_CAPACITY,
                 "pool-dir", "vol-qcow2-nocapacity-backing", NULL, NULL,
                 "qcow2-nocapacity", 0, FMT_OPTIONS);
279

280 281 282 283 284 285 286
    DO_TEST("pool-dir", "vol-file-iso",
            NULL, NULL,
            "iso", 0, FMT_OPTIONS);
    DO_TEST("pool-dir", "vol-file",
            "pool-dir", "vol-file-iso",
            "iso-input", 0, FMT_OPTIONS);

287
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
288 289
}

290
VIR_TEST_MAIN(mymain)