提交 56e1b4c3 编写于 作者: J Ján Tomko

storage: add test for qemu-img command line generation

上级 c6e87d1a
......@@ -74,6 +74,7 @@ EXTRA_DIST = \
storagepoolschematest \
storagepoolxml2xmlin \
storagepoolxml2xmlout \
storagevolxml2argvdata \
storagevolschematest \
storagevolxml2xmlin \
storagevolxml2xmlout \
......@@ -170,6 +171,8 @@ endif
test_programs += nwfilterxml2xmltest
test_programs += storagevolxml2argvtest
test_programs += storagevolxml2xmltest storagepoolxml2xmltest
test_programs += nodedevxml2xmltest
......@@ -470,6 +473,12 @@ nwfilterxml2xmltest_SOURCES = \
testutils.c testutils.h
nwfilterxml2xmltest_LDADD = $(LDADDS)
storagevolxml2argvtest_SOURCES = \
storagevolxml2argvtest.c \
testutils.c testutils.h
storagevolxml2argvtest_LDADD = \
../src/libvirt_driver_storage_impl.la $(LDADDS)
storagevolxml2xmltest_SOURCES = \
storagevolxml2xmltest.c \
testutils.c testutils.h
......
<pool type='dir'>
<name>virtimages</name>
<uuid>70a7eb15-6c34-ee9c-bf57-69e8e5ff3fb2</uuid>
<capacity>0</capacity>
<allocation>0</allocation>
<available>0</available>
<source>
</source>
<target>
<path>///var/////lib/libvirt/images//</path>
<permissions>
<mode>0700</mode>
<owner>-1</owner>
<group>-1</group>
<label>some_label_t</label>
</permissions>
</target>
</pool>
qemu-img create -f qcow2 -b /dev/null -F raw /var/lib/libvirt/images/OtherDemo.img 5242880K -e
qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/sparse.img /var/lib/libvirt/images/OtherDemo.img -e
qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/sparse.img /var/lib/libvirt/images/OtherDemo.img -e
qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/sparse.img /var/lib/libvirt/images/OtherDemo.img -o encryption=on,preallocation=metadata
qemu-img create -f qcow2 /var/lib/libvirt/images/OtherDemo.img 5242880K -e
qemu-img create -f qcow2 /var/lib/libvirt/images/OtherDemo.img 5242880K -e
qemu-img create -f qcow2 /var/lib/libvirt/images/OtherDemo.img 5242880K -o encryption=on,preallocation=metadata
qemu-img create -f qcow2 -b /dev/null /var/lib/libvirt/images/OtherDemo.img 5242880K -e
qemu-img create -f qcow2 -b /dev/null -o backing_fmt=raw,encryption=on /var/lib/libvirt/images/OtherDemo.img 5242880K
<volume>
<name>sparse.img</name>
<source/>
<capacity unit="TiB">1</capacity>
<allocation unit="bytes">0</allocation>
<target>
<path>/var/lib/libvirt/images/sparse.img</path>
<permissions>
<mode>0</mode>
<owner>0744</owner>
<group>0</group>
<label>virt_image_t</label>
</permissions>
<timestamps>
<atime>1341933637.273190990</atime>
<mtime>1341930622.047245868</mtime>
<ctime>1341930622.047245868</ctime>
</timestamps>
</target>
</volume>
<volume>
<name>OtherDemo.img</name>
<key>/var/lib/libvirt/images/OtherDemo.img</key>
<source>
</source>
<capacity unit="G">5</capacity>
<allocation>294912</allocation>
<target>
<path>/var/lib/libvirt/images/OtherDemo.img</path>
<format type='qcow2'/>
<permissions>
<mode>0644</mode>
<owner>0</owner>
<group>0</group>
<label>unconfined_u:object_r:virt_image_t:s0</label>
</permissions>
<encryption format='qcow'>
<secret type='passphrase' uuid='e78d4b51-a2af-485f-b0f5-afca709a80f4'/>
</encryption>
</target>
</volume>
<volume>
<name>OtherDemo.img</name>
<key>/var/lib/libvirt/images/OtherDemo.img</key>
<source>
</source>
<capacity unit="G">5</capacity>
<allocation>294912</allocation>
<target>
<path>/var/lib/libvirt/images/OtherDemo.img</path>
<format type='qcow2'/>
<permissions>
<mode>0644</mode>
<owner>0</owner>
<group>0</group>
<label>unconfined_u:object_r:virt_image_t:s0</label>
</permissions>
<encryption format='qcow'>
<secret type='passphrase' uuid='e78d4b51-a2af-485f-b0f5-afca709a80f4'/>
</encryption>
</target>
<backingStore>
<path>/dev/null</path>
<format type='raw'/>
<permissions>
<mode>0644</mode>
<owner>0</owner>
<group>0</group>
<label>unconfined_u:object_r:virt_image_t:s0</label>
</permissions>
</backingStore>
</volume>
#include <config.h>
#include "internal.h"
#include "testutils.h"
#include "datatypes.h"
#include "storage/storage_backend.h"
#include "testutilsqemu.h"
const char create_tool[] = "qemu-img";
static int
testCompareXMLToArgvFiles(bool shouldFail,
const char *poolxml,
const char *volxml,
const char *inputvolxml,
const char *cmdline,
unsigned int flags,
int imgformat)
{
char *volXmlData = NULL;
char *poolXmlData = NULL;
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;
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;
if (!(vol = virStorageVolDefParseString(pool, volXmlData)))
goto cleanup;
if (inputvolxml &&
!(inputvol = virStorageVolDefParseString(pool, inputvolXmlData)))
goto cleanup;
cmd = virStorageBackendCreateQemuImgCmd(conn, &poolobj, vol, inputvol,
flags, create_tool, imgformat);
actualCmdline = virCommandToString(cmd);
if (!actualCmdline) {
if (shouldFail) {
virResetLastError();
ret = 0;
}
goto cleanup;
}
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);
virStorageVolDefFree(vol);
virStorageVolDefFree(inputvol);
virCommandFree(cmd);
VIR_FREE(actualCmdline);
VIR_FREE(expectedCmdline);
return ret;
}
struct testInfo {
bool shouldFail;
const char *pool;
const char *vol;
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;
char *volxml = NULL;
char *inputvolxml = NULL;
char *cmdline = NULL;
if (info->inputvol &&
virAsprintf(&inputvolxml, "%s/storagevolxml2argvdata/%s.xml",
abs_srcdir, info->inputvol) < 0)
goto cleanup;
if (virAsprintf(&poolxml, "%s/storagevolxml2argvdata/%s.xml",
abs_srcdir, info->pool) < 0 ||
virAsprintf(&volxml, "%s/storagevolxml2argvdata/%s.xml",
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,
inputvolxml, cmdline, info->flags,
info->imgformat);
cleanup:
VIR_FREE(poolxml);
VIR_FREE(volxml);
VIR_FREE(inputvolxml);
VIR_FREE(cmdline);
return result;
}
enum {
FMT_NONE = 0,
FMT_FLAG,
FMT_OPTIONS,
};
static int
mymain(void)
{
int ret = 0;
unsigned int flags = VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
#define DO_TEST(shouldFail, pool, vol, inputvol, cmdline, flags, imgformat) \
do { \
struct testInfo info = { shouldFail, pool, vol, inputvol, cmdline, \
flags, imgformat }; \
if (virtTestRun("Storage Vol XML-2-argv " cmdline, \
1, testCompareXMLToArgvHelper, &info) < 0) \
ret = -1; \
} \
while (0);
DO_TEST(false, "pool-dir", "vol-qcow2", NULL, "qcow2", 0, FMT_OPTIONS);
DO_TEST(true, "pool-dir", "vol-qcow2", NULL, "qcow2-prealloc", flags,
FMT_OPTIONS);
DO_TEST(false, "pool-dir", "vol-qcow2-nobacking", NULL,
"qcow2-nobacking-prealloc", flags, FMT_OPTIONS);
DO_TEST(false, "pool-dir", "vol-qcow2-nobacking", "vol-file",
"qcow2-nobacking-convert-prealloc", flags, FMT_OPTIONS);
DO_TEST(true, "pool-dir", "vol-qcow2", "vol-file",
"qcow2-convert-prealloc", flags, FMT_OPTIONS);
DO_TEST(false, "pool-dir", "vol-qcow2", NULL, "qcow2-flag", 0, FMT_FLAG);
DO_TEST(false, "pool-dir", "vol-qcow2-nobacking", NULL,
"qcow2-nobacking-flag", 0, FMT_FLAG);
DO_TEST(false, "pool-dir", "vol-qcow2-nobacking", "vol-file",
"qcow2-nobacking-convert-flag", 0, FMT_FLAG);
DO_TEST(false, "pool-dir", "vol-qcow2", NULL, "qcow2-none", 0, FMT_NONE);
DO_TEST(false, "pool-dir", "vol-qcow2-nobacking", NULL,
"qcow2-nobacking-none", 0, FMT_NONE);
DO_TEST(false, "pool-dir", "vol-qcow2-nobacking", "vol-file",
"qcow2-nobacking-convert-none", 0, FMT_NONE);
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIRT_TEST_MAIN(mymain)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册