tests: qemublock: Add testing of 'blockdev-create' generators

Test the output against the schema and also against what we expect.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2018-09-04 13:40:01 +02:00
parent 50e1e053a5
commit c1a1975e49
31 changed files with 581 additions and 0 deletions

View File

@ -30,6 +30,9 @@
# include "qemu/qemu_command.h"
# define LIBVIRT_SNAPSHOT_CONF_PRIV_H_ALLOW
# include "conf/snapshot_conf_priv.h"
# define VIR_FROM_THIS VIR_FROM_NONE
VIR_LOG_INIT("tests.storagetest");
@ -342,6 +345,138 @@ testQemuDiskXMLToPropsValidateFile(const void *opaque)
}
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;
VIR_AUTOPTR(xmlDoc) doc = NULL;
VIR_AUTOPTR(xmlXPathContext) ctxt = NULL;
xmlNodePtr node;
VIR_AUTOFREE(char *) xmlpath = NULL;
virStorageSourcePtr ret = NULL;
if (virAsprintf(&xmlpath, "%s%s.xml",
testQemuImageCreatePath, name) < 0)
return NULL;
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)
VIR_STEAL_PTR(ret, diskdef->src);
virDomainSnapshotDiskDefFree(diskdef);
return ret;
}
static int
testQemuImageCreate(const void *opaque)
{
struct testQemuImageCreateData *data = (void *) opaque;
VIR_AUTOPTR(virJSONValue) protocolprops = NULL;
VIR_AUTOPTR(virJSONValue) formatprops = NULL;
VIR_AUTOUNREF(virStorageSourcePtr) src = NULL;
VIR_AUTOCLEAN(virBuffer) debug = VIR_BUFFER_INITIALIZER;
VIR_AUTOCLEAN(virBuffer) actualbuf = VIR_BUFFER_INITIALIZER;
VIR_AUTOFREE(char *) jsonprotocol = NULL;
VIR_AUTOFREE(char *) jsonformat = NULL;
VIR_AUTOFREE(char *) actual = NULL;
VIR_AUTOFREE(char *) jsonpath = NULL;
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 */
src->capacity = 1337;
src->physical = 42;
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) {
VIR_AUTOFREE(char *) debugmsg = virBufferContentAndReset(&debug);
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) {
VIR_AUTOFREE(char *) debugmsg = virBufferContentAndReset(&debug);
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);
virBufferTrim(&actualbuf, "\n", -1);
virBufferAddLit(&actualbuf, "\n");
if (virAsprintf(&jsonpath, "%s%s.json",
testQemuImageCreatePath, data->name) < 0)
return -1;
if (!(actual = virBufferContentAndReset(&actualbuf)))
return -1;
return virTestCompareToFile(actual, jsonpath);
}
static int
testQemuDiskXMLToPropsValidateFileSrcOnly(const void *opaque)
{
@ -383,6 +518,7 @@ mymain(void)
virQEMUDriver driver;
struct testBackingXMLjsonXMLdata xmljsonxmldata;
struct testQemuDiskXMLToJSONData diskxmljsondata;
struct testQemuImageCreateData imagecreatedata;
char *capslatest_x86_64 = NULL;
virQEMUCapsPtr caps_x86_64 = NULL;
@ -390,6 +526,7 @@ mymain(void)
return EXIT_FAILURE;
diskxmljsondata.driver = &driver;
imagecreatedata.driver = &driver;
if (!(capslatest_x86_64 = testQemuGetLatestCapsForArch("x86_64", "xml")))
return EXIT_FAILURE;
@ -401,6 +538,7 @@ mymain(void)
return EXIT_FAILURE;
diskxmljsondata.qemuCaps = caps_x86_64;
imagecreatedata.qemuCaps = caps_x86_64;
virTestCounterReset("qemu storage source xml->json->xml ");
@ -548,6 +686,41 @@ mymain(void)
TEST_DISK_TO_JSON("block-raw-noopts");
TEST_DISK_TO_JSON("block-raw-reservations");
# define TEST_IMAGE_CREATE(testname, testbacking) \
do { \
imagecreatedata.name = testname; \
imagecreatedata.backingname = testbacking; \
if (virTestRun("image create xml to props " testname, testQemuImageCreate, \
&imagecreatedata) < 0) \
ret = -1; \
} while (0)
imagecreatedata.schema = diskxmljsondata.schema;
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");
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);
cleanup:
virHashFree(diskxmljsondata.schema);
qemuTestDriverFree(&driver);

View File

@ -0,0 +1,19 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.img",
"size": 42
}
format:
{
"key-secret": "0123456789ABCDEF0123456789ABCDE-encalias",
"cipher-alg": "serpent-256",
"cipher-mode": "cbc",
"hash-alg": "sha256",
"ivgen-alg": "plain64",
"ivgen-hash-alg": "sha256",
"driver": "luks",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337
}

View File

@ -0,0 +1,16 @@
<disk device='disk' name='vda'>
<driver type='raw'/>
<source file='/var/lib/libvirt/images/i.img'>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
<encryption format='luks'>
<secret type='passphrase' uuid='f52a81b2-424e-490c-823d-6bd4235bc572'/>
<cipher name='serpent' size='256' mode='cbc' hash='sha256'/>
<ivgen name='plain64' hash='sha256'/>
</encryption>
</source>
</disk>

View File

@ -0,0 +1,14 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.img",
"size": 42
}
format:
{
"key-secret": "0123456789ABCDEF0123456789ABCDE-encalias",
"driver": "luks",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337
}

View File

@ -0,0 +1,14 @@
<disk device='disk' name='vda'>
<driver type='raw'/>
<source file='/var/lib/libvirt/images/i.img'>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
<encryption format='luks'>
<secret type='passphrase' uuid='f52a81b2-424e-490c-823d-6bd4235bc572'/>
</encryption>
</source>
</disk>

View File

@ -0,0 +1,28 @@
protocol:
{
"driver": "gluster",
"location": {
"volume": "asdf",
"path": "i.qcow2",
"server": [
{
"type": "inet",
"host": "example.com",
"port": "1234"
},
{
"type": "inet",
"host": "alternate.example.com",
"port": "3214"
}
]
},
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337
}

View File

@ -0,0 +1,13 @@
<disk device='disk' name='vda' type='network'>
<driver type='qcow2'/>
<source protocol='gluster' name='asdf/i.qcow2'>
<host name='example.com' port='1234'/>
<host name='alternate.example.com' port='3214'/>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
</source>
</disk>

View File

@ -0,0 +1,26 @@
protocol:
{
"driver": "rbd",
"location": {
"pool": "asdf",
"image": "i.qcow2",
"server": [
{
"host": "example.com",
"port": "1234"
},
{
"host": "alternate.example.com",
"port": "4321"
}
]
},
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337
}

View File

@ -0,0 +1,13 @@
<disk device='disk' name='vda' type='network'>
<driver type='qcow2'/>
<source protocol='rbd' name='asdf/i.qcow2'>
<host name='example.com' port='1234'/>
<host name='alternate.example.com' port='4321'/>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
</source>
</disk>

View File

@ -0,0 +1,20 @@
protocol:
{
"driver": "sheepdog",
"location": {
"server": {
"type": "inet",
"host": "example.com",
"port": "1234"
},
"vdi": "asdf/i.qcow2"
},
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337
}

View File

@ -0,0 +1,12 @@
<disk device='disk' name='vda' type='network'>
<driver type='qcow2'/>
<source protocol='sheepdog' name='asdf/i.qcow2'>
<host name='example.com' port='1234'/>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
</source>
</disk>

View File

@ -0,0 +1,19 @@
protocol:
{
"driver": "ssh",
"location": {
"path": "asdf/i.qcow2",
"server": {
"host": "example.com",
"port": "1234"
}
},
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337
}

View File

@ -0,0 +1,12 @@
<disk device='disk' name='vda' type='network'>
<driver type='qcow2'/>
<source protocol='ssh' name='asdf/i.qcow2'>
<host name='example.com' port='1234'/>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
</source>
</disk>

View File

@ -0,0 +1,15 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.qcow2",
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337,
"backing-file": "/var/lib/libvirt/images/i.img",
"backing-fmt": "luks"
}

View File

@ -0,0 +1 @@
qcow2.xml

View File

@ -0,0 +1,15 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.qcow2",
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337,
"backing-file": "json:{\"driver\":\"nbd\",\"server\":{\"type\":\"inet\",\"host\":\"example.com\",\"port\":\"1234\"}}",
"backing-fmt": "raw"
}

View File

@ -0,0 +1 @@
qcow2.xml

View File

@ -0,0 +1,15 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.qcow2",
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337,
"backing-file": "/var/lib/libvirt/images/i.img",
"backing-fmt": "raw"
}

View File

@ -0,0 +1 @@
qcow2.xml

View File

@ -0,0 +1,24 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.qcow2",
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337,
"backing-file": "/var/lib/libvirt/images/i.qcow2",
"backing-fmt": "qcow2",
"encrypt": {
"key-secret": "0123456789ABCDEF0123456789ABCDE-encalias",
"cipher-alg": "serpent-256",
"cipher-mode": "cbc",
"hash-alg": "sha256",
"ivgen-alg": "plain64",
"ivgen-hash-alg": "sha256",
"format": "luks"
}
}

View File

@ -0,0 +1 @@
qcow2-luks-encopts.xml

View File

@ -0,0 +1,22 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.qcow2",
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337,
"encrypt": {
"key-secret": "0123456789ABCDEF0123456789ABCDE-encalias",
"cipher-alg": "serpent-256",
"cipher-mode": "cbc",
"hash-alg": "sha256",
"ivgen-alg": "plain64",
"ivgen-hash-alg": "sha256",
"format": "luks"
}
}

View File

@ -0,0 +1,16 @@
<disk device='disk' name='vda'>
<driver type='qcow2'/>
<source file='/var/lib/libvirt/images/i.qcow2'>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
<encryption format='luks'>
<secret type='passphrase' uuid='f52a81b2-424e-490c-823d-6bd4235bc572'/>
<cipher name='serpent' size='256' mode='cbc' hash='sha256'/>
<ivgen name='plain64' hash='sha256'/>
</encryption>
</source>
</disk>

View File

@ -0,0 +1,17 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.qcow2",
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337,
"encrypt": {
"key-secret": "0123456789ABCDEF0123456789ABCDE-encalias",
"format": "luks"
}
}

View File

@ -0,0 +1,14 @@
<disk device='disk' name='vda'>
<driver type='qcow2'/>
<source file='/var/lib/libvirt/images/i.qcow2'>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
<encryption format='luks'>
<secret type='passphrase' uuid='f52a81b2-424e-490c-823d-6bd4235bc572'/>
</encryption>
</source>
</disk>

View File

@ -0,0 +1,13 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.qcow2",
"size": 42
}
format:
{
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337
}

View File

@ -0,0 +1,11 @@
<disk device='disk' name='vda'>
<driver type='qcow2'/>
<source file='/var/lib/libvirt/images/i.qcow2'>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
</source>
</disk>

View File

@ -0,0 +1,4 @@
protocol:
<null>
format:
<null>

View File

@ -0,0 +1,12 @@
<disk device='disk' name='vda' type='network'>
<driver type='raw'/>
<source protocol='nbd'>
<host name='example.com' port='1234'/>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
</source>
</disk>

View File

@ -0,0 +1,9 @@
protocol:
{
"driver": "file",
"filename": "/var/lib/libvirt/images/i.img",
"size": 42
}
format:
<null>

View File

@ -0,0 +1,11 @@
<disk device='disk' name='vda'>
<driver type='raw'/>
<source file='/var/lib/libvirt/images/i.img'>
<privateData>
<nodenames>
<nodename type='storage' name='0123456789ABCDEF0123456789ABCDE'/>
<nodename type='format' name='0123456789ABCDEF0123456789ABCDE'/>
</nodenames>
</privateData>
</source>
</disk>