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

#include "internal.h"
#include "testutils.h"
#include "datatypes.h"
#include "storage/storage_backend.h"
#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 43 44 45 46
                          const char *inputvolxml,
                          const char *cmdline,
                          unsigned int flags,
                          int imgformat)
{
    char *volXmlData = NULL;
    char *poolXmlData = NULL;
47
    char *inputpoolXmlData = NULL;
48 49 50 51 52 53 54 55 56 57 58 59
    char *inputvolXmlData = NULL;
    char *expectedCmdline = NULL;
    char *actualCmdline = NULL;
    int ret = -1;

    int len;

    virCommandPtr cmd = NULL;
    virConnectPtr conn;

    virStorageVolDefPtr vol = NULL, inputvol = NULL;
    virStoragePoolDefPtr pool = NULL;
60
    virStoragePoolDefPtr inputpool = NULL;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    virStoragePoolObj poolobj = {.def = NULL };


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

    if (virtTestLoadFile(poolxml, &poolXmlData) < 0)
        goto cleanup;
    if (virtTestLoadFile(volxml, &volXmlData) < 0)
        goto cleanup;
    if (inputvolxml &&
        virtTestLoadFile(inputvolxml, &inputvolXmlData) < 0)
        goto cleanup;

    if (!(pool = virStoragePoolDefParseString(poolXmlData)))
        goto cleanup;

    poolobj.def = pool;

80 81 82 83 84 85 86
    if (inputpoolxml) {
        if (virtTestLoadFile(inputpoolxml, &inputpoolXmlData) < 0)
            goto cleanup;
        if (!(inputpool = virStoragePoolDefParseString(inputpoolXmlData)))
            goto cleanup;
    }

87 88 89 90
    if (!(vol = virStorageVolDefParseString(pool, volXmlData)))
        goto cleanup;

    if (inputvolxml &&
91
        !(inputvol = virStorageVolDefParseString(inputpool, inputvolXmlData)))
92 93
        goto cleanup;

94 95 96
    testSetVolumeType(vol, pool);
    testSetVolumeType(inputvol, inputpool);

97 98
    cmd = virStorageBackendCreateQemuImgCmd(conn, &poolobj, vol, inputvol,
                                            flags, create_tool, imgformat);
99
    if (!cmd) {
100 101 102 103 104 105 106
        if (shouldFail) {
            virResetLastError();
            ret = 0;
        }
        goto cleanup;
    }

107 108 109
    if (!(actualCmdline = virCommandToString(cmd)))
        goto cleanup;

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    len = virtTestLoadFile(cmdline, &expectedCmdline);
    if (len < 0)
        goto cleanup;
    if (len && expectedCmdline[len-1] == '\n')
        expectedCmdline[len-1] = '\0';

    if (STRNEQ_NULLABLE(expectedCmdline, actualCmdline)) {
        virtTestDifference(stderr, expectedCmdline, actualCmdline);
        goto cleanup;
    }

    ret = 0;

cleanup:
    virStoragePoolDefFree(pool);
125
    virStoragePoolDefFree(inputpool);
126 127 128 129 130
    virStorageVolDefFree(vol);
    virStorageVolDefFree(inputvol);
    virCommandFree(cmd);
    VIR_FREE(actualCmdline);
    VIR_FREE(expectedCmdline);
131
    VIR_FREE(inputpoolXmlData);
132 133 134 135
    VIR_FREE(poolXmlData);
    VIR_FREE(volXmlData);
    VIR_FREE(inputvolXmlData);
    virObjectUnref(conn);
136 137 138 139 140 141 142
    return ret;
}

struct testInfo {
    bool shouldFail;
    const char *pool;
    const char *vol;
143
    const char *inputpool;
144 145 146 147 148 149 150 151 152 153 154 155
    const char *inputvol;
    const char *cmdline;
    unsigned int flags;
    int imgformat;
};

static int
testCompareXMLToArgvHelper(const void *data)
{
    int result = -1;
    const struct testInfo *info = data;
    char *poolxml = NULL;
156
    char *inputpoolxml = NULL;
157 158 159 160 161
    char *volxml = NULL;
    char *inputvolxml = NULL;
    char *cmdline = NULL;

    if (info->inputvol &&
162
        virAsprintf(&inputvolxml, "%s/storagevolxml2xmlin/%s.xml",
163 164
                    abs_srcdir, info->inputvol) < 0)
        goto cleanup;
165 166 167 168
    if (info->inputpool &&
        virAsprintf(&inputpoolxml, "%s/storagepoolxml2xmlin/%s.xml",
                    abs_srcdir, info->inputpool) < 0)
        goto cleanup;
169
    if (virAsprintf(&poolxml, "%s/storagepoolxml2xmlin/%s.xml",
170
                    abs_srcdir, info->pool) < 0 ||
171
        virAsprintf(&volxml, "%s/storagevolxml2xmlin/%s.xml",
172 173 174 175 176 177 178 179
                    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,
180 181
                                       inputpoolxml, inputvolxml,
                                       cmdline, info->flags,
182 183 184 185 186 187
                                       info->imgformat);

cleanup:
    VIR_FREE(poolxml);
    VIR_FREE(volxml);
    VIR_FREE(inputvolxml);
188
    VIR_FREE(inputpoolxml);
189 190 191 192 193 194 195 196 197
    VIR_FREE(cmdline);

    return result;
}

enum {
    FMT_NONE = 0,
    FMT_FLAG,
    FMT_OPTIONS,
198
    FMT_COMPAT,
199 200 201 202 203 204 205 206 207 208
};



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

209 210
#define DO_TEST_FULL(shouldFail, pool, vol, inputpool, inputvol, cmdline,    \
                     flags, imgformat)                                       \
211
    do {                                                                     \
212 213
        struct testInfo info = { shouldFail, pool, vol, inputpool, inputvol, \
                                 cmdline, flags, imgformat };                \
214 215 216 217
        if (virtTestRun("Storage Vol XML-2-argv " cmdline,                   \
                        1, testCompareXMLToArgvHelper, &info) < 0)           \
            ret = -1;                                                        \
       }                                                                     \
218 219
    while (0);

220 221 222 223 224 225 226
#define DO_TEST(pool, ...)                                                 \
    DO_TEST_FULL(false, pool, __VA_ARGS__)

#define DO_TEST_FAIL(pool, ...)                                            \
    DO_TEST_FULL(true, pool, __VA_ARGS__)

    DO_TEST("pool-dir", "vol-qcow2",
227
            NULL, NULL,
228 229
            "qcow2", 0, FMT_OPTIONS);
    DO_TEST_FAIL("pool-dir", "vol-qcow2",
230
                 NULL, NULL,
231 232
                 "qcow2-prealloc", flags, FMT_OPTIONS);
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
233
            NULL, NULL,
234
            "qcow2-nobacking-prealloc", flags, FMT_OPTIONS);
235
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
236
            "pool-dir", "vol-file",
237
            "qcow2-nobacking-convert-prealloc", flags, FMT_OPTIONS);
238
    DO_TEST_FAIL("pool-dir", "vol-qcow2",
239
                 "pool-dir", "vol-file",
240 241
                 "qcow2-convert-prealloc", flags, FMT_OPTIONS);
    DO_TEST("pool-dir", "vol-qcow2",
242
            NULL, NULL,
243 244
            "qcow2-flag", 0, FMT_FLAG);
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
245
            NULL, NULL,
246
            "qcow2-nobacking-flag", 0, FMT_FLAG);
247
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
248
            "pool-dir", "vol-file",
249
            "qcow2-nobacking-convert-flag", 0, FMT_FLAG);
250
    DO_TEST("pool-dir", "vol-qcow2",
251
            NULL, NULL,
252 253
            "qcow2-none", 0, FMT_NONE);
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
254
            NULL, NULL,
255
            "qcow2-nobacking-none", 0, FMT_NONE);
256
    DO_TEST("pool-dir", "vol-qcow2-nobacking",
257
            "pool-dir", "vol-file",
258
            "qcow2-nobacking-convert-none", 0, FMT_NONE);
259
    DO_TEST("pool-dir", "vol-qcow2-lazy",
260
            NULL, NULL,
261 262
            "qcow2-lazy", 0, FMT_OPTIONS);
    DO_TEST("pool-dir", "vol-qcow2-1.1",
263
            NULL, NULL,
264 265
            "qcow2-1.1", 0, FMT_OPTIONS);
    DO_TEST_FAIL("pool-dir", "vol-qcow2-0.10-lazy",
266
                 NULL, NULL,
267
                 "qcow2-0.10-lazy", 0, FMT_OPTIONS);
268 269 270 271 272 273
    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);
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    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);

300 301 302 303
    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)