/*
* 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
* .
*/
#include
#include "storage_source.h"
#include "testutils.h"
#include "testutilsqemu.h"
#include "testutilsqemuschema.h"
#include "virstoragefile.h"
#include "virstring.h"
#include "virlog.h"
#include "qemu/qemu_block.h"
#include "qemu/qemu_qapi.h"
#include "qemu/qemu_monitor_json.h"
#include "qemu/qemu_backup.h"
#include "qemu/qemu_checkpoint.h"
#include "qemu/qemu_validate.h"
#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");
struct testBackingXMLjsonXMLdata {
int type;
const char *xml;
bool legacy;
GHashTable *schema;
virJSONValue *schemaroot;
};
static int
testBackingXMLjsonXML(const void *args)
{
const struct testBackingXMLjsonXMLdata *data = args;
g_autoptr(xmlDoc) xml = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autoptr(virJSONValue) backendprops = NULL;
g_autoptr(virJSONValue) wrapper = NULL;
g_autofree char *propsstr = NULL;
g_autofree char *protocolwrapper = NULL;
g_autofree char *actualxml = NULL;
g_autoptr(virStorageSource) xmlsrc = NULL;
g_autoptr(virStorageSource) jsonsrc = NULL;
g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
unsigned int backendpropsflags = 0;
if (data->legacy)
backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY;
xmlsrc = virStorageSourceNew();
xmlsrc->type = data->type;
if (!(xml = virXMLParseStringCtxt(data->xml, "(test storage source XML)", &ctxt)))
return -1;
if (virDomainStorageSourceParse(ctxt->node, ctxt, xmlsrc, 0, NULL) < 0) {
fprintf(stderr, "failed to parse disk source xml\n");
return -1;
}
if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc,
backendpropsflags))) {
fprintf(stderr, "failed to format disk source json\n");
return -1;
}
if (!data->legacy) {
if (testQEMUSchemaValidate(backendprops, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
g_autofree char *debugprops = virJSONValueToString(backendprops, true);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
debugprops, NULLSTR(debugmsg));
return -1;
}
}
if (virJSONValueObjectCreate(&wrapper, "a:file", &backendprops, NULL) < 0)
return -1;
if (!(propsstr = virJSONValueToString(wrapper, false)))
return -1;
protocolwrapper = g_strdup_printf("json:%s", propsstr);
if (virStorageSourceNewFromBackingAbsolute(protocolwrapper,
&jsonsrc) < 0) {
fprintf(stderr, "failed to parse disk json\n");
return -1;
}
if (virDomainDiskSourceFormat(&buf, jsonsrc, "source", 0, false, 0,
false, false, NULL) < 0 ||
!(actualxml = virBufferContentAndReset(&buf))) {
fprintf(stderr, "failed to format disk source xml\n");
return -1;
}
if (STRNEQ(actualxml, data->xml)) {
fprintf(stderr, "\n expected storage source xml:\n'%s'\n"
"actual storage source xml:\n%s\n"
"intermediate json:\n%s\n",
data->xml, actualxml, protocolwrapper);
return -1;
}
return 0;
}
static const char *testJSONtoJSONPath = abs_srcdir "/qemublocktestdata/jsontojson/";
struct testJSONtoJSONData {
const char *name;
GHashTable *schema;
virJSONValue *schemaroot;
};
static int
testJSONtoJSON(const void *args)
{
const struct testJSONtoJSONData *data = args;
g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
g_autoptr(virJSONValue) jsonsrcout = NULL;
g_autoptr(virStorageSource) src = NULL;
g_autofree char *actual = NULL;
g_autofree char *in = NULL;
g_autofree char *infile = g_strdup_printf("%s%s-in.json", testJSONtoJSONPath,
data->name);
g_autofree char *outfile = g_strdup_printf("%s%s-out.json", testJSONtoJSONPath,
data->name);
if (virTestLoadFile(infile, &in) < 0)
return -1;
if (virStorageSourceNewFromBackingAbsolute(in, &src) < 0) {
fprintf(stderr, "failed to parse disk json\n");
return -1;
}
if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src,
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY))) {
fprintf(stderr, "failed to format disk source json\n");
return -1;
}
if (!(actual = virJSONValueToString(jsonsrcout, true)))
return -1;
if (testQEMUSchemaValidate(jsonsrcout, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
actual, NULLSTR(debugmsg));
return -1;
}
return virTestCompareToFile(actual, outfile);
}
struct testQemuDiskXMLToJSONImageData {
virJSONValue *formatprops;
virJSONValue *storageprops;
virJSONValue *storagepropssrc;
char *backingstore;
};
struct testQemuDiskXMLToJSONData {
virQEMUDriver *driver;
GHashTable *schema;
virJSONValue *schemaroot;
const char *name;
bool fail;
struct testQemuDiskXMLToJSONImageData *images;
size_t nimages;
virQEMUCaps *qemuCaps;
};
static void
testQemuDiskXMLToPropsClear(struct testQemuDiskXMLToJSONData *data)
{
size_t i;
for (i = 0; i < data->nimages; i++) {
virJSONValueFree(data->images[i].formatprops);
virJSONValueFree(data->images[i].storageprops);
virJSONValueFree(data->images[i].storagepropssrc);
g_free(data->images[i].backingstore);
}
data->nimages = 0;
VIR_FREE(data->images);
}
static int
testQemuDiskXMLToJSONFakeSecrets(virStorageSource *src)
{
qemuDomainStorageSourcePrivate *srcpriv;
if (!src->privateData &&
!(src->privateData = qemuDomainStorageSourcePrivateNew()))
return -1;
srcpriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);
if (src->auth) {
srcpriv->secinfo = g_new0(qemuDomainSecretInfo, 1);
srcpriv->secinfo->type = VIR_DOMAIN_SECRET_INFO_TYPE_AES;
srcpriv->secinfo->s.aes.username = g_strdup(src->auth->username);
srcpriv->secinfo->s.aes.alias = g_strdup_printf("%s-secalias",
NULLSTR(src->nodestorage));
}
if (src->encryption) {
srcpriv->encinfo = g_new0(qemuDomainSecretInfo, 1);
srcpriv->encinfo->type = VIR_DOMAIN_SECRET_INFO_TYPE_AES;
srcpriv->encinfo->s.aes.alias = g_strdup_printf("%s-encalias",
NULLSTR(src->nodeformat));
}
return 0;
}
static const char *testQemuDiskXMLToJSONPath = abs_srcdir "/qemublocktestdata/xml2json/";
static int
testQemuDiskXMLToProps(const void *opaque)
{
struct testQemuDiskXMLToJSONData *data = (void *) opaque;
g_autoptr(virDomainDef) vmdef = NULL;
virDomainDiskDef *disk = NULL;
virStorageSource *n;
g_autoptr(virJSONValue) formatProps = NULL;
g_autoptr(virJSONValue) storageProps = NULL;
g_autoptr(virJSONValue) storageSrcOnlyProps = NULL;
g_autofree char *xmlpath = NULL;
g_autofree char *xmlstr = NULL;
xmlpath = g_strdup_printf("%s%s.xml", testQemuDiskXMLToJSONPath, data->name);
if (virTestLoadFile(xmlpath, &xmlstr) < 0)
return -1;
/* qemu stores node names in the status XML portion */
if (!(disk = virDomainDiskDefParse(xmlstr, data->driver->xmlopt,
VIR_DOMAIN_DEF_PARSE_STATUS)))
return -1;
if (!(vmdef = virDomainDefNew(data->driver->xmlopt)))
return -1;
virDomainDiskInsert(vmdef, disk);
if (qemuValidateDomainDeviceDefDisk(disk, vmdef, data->qemuCaps) < 0) {
VIR_TEST_VERBOSE("invalid configuration for disk");
return -1;
}
for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) {
g_autofree char *backingstore = NULL;
unsigned int backendpropsflagsnormal = QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY;
unsigned int backendpropsflagstarget = QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY |
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY;
if (testQemuDiskXMLToJSONFakeSecrets(n) < 0)
return -1;
if (qemuDomainValidateStorageSource(n, data->qemuCaps, false) < 0)
return -1;
qemuDomainPrepareDiskSourceData(disk, n);
if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n, n->backingStore)) ||
!(storageSrcOnlyProps = qemuBlockStorageSourceGetBackendProps(n, backendpropsflagstarget)) ||
!(storageProps = qemuBlockStorageSourceGetBackendProps(n, backendpropsflagsnormal)) ||
!(backingstore = qemuBlockGetBackingStoreString(n, true))) {
if (!data->fail) {
VIR_TEST_VERBOSE("failed to generate qemu blockdev props");
return -1;
}
} else if (data->fail) {
VIR_TEST_VERBOSE("qemu blockdev props should have failed");
return -1;
}
VIR_REALLOC_N(data->images, data->nimages + 1);
data->images[data->nimages].formatprops = g_steal_pointer(&formatProps);
data->images[data->nimages].storageprops = g_steal_pointer(&storageProps);
data->images[data->nimages].storagepropssrc = g_steal_pointer(&storageSrcOnlyProps);
data->images[data->nimages].backingstore = g_steal_pointer(&backingstore);
data->nimages++;
}
return 0;
}
static int
testQemuDiskXMLToPropsValidateSchema(const void *opaque)
{
struct testQemuDiskXMLToJSONData *data = (void *) opaque;
int ret = 0;
size_t i;
if (data->fail)
return EXIT_AM_SKIP;
for (i = 0; i < data->nimages; i++) {
g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
if (testQEMUSchemaValidate(data->images[i].formatprops, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
g_autofree char *propsstr = virJSONValueToString(data->images[i].formatprops, true);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
propsstr, NULLSTR(debugmsg));
ret = -1;
}
virBufferFreeAndReset(&debug);
if (testQEMUSchemaValidate(data->images[i].storageprops, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
g_autofree char *propsstr = virJSONValueToString(data->images[i].storageprops, true);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
propsstr, NULLSTR(debugmsg));
ret = -1;
}
virBufferFreeAndReset(&debug);
if (testQEMUSchemaValidate(data->images[i].storagepropssrc, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
g_autofree char *propsstr = virJSONValueToString(data->images[i].storagepropssrc, true);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
propsstr, NULLSTR(debugmsg));
ret = -1;
}
}
return ret;
}
static int
testQemuDiskXMLToPropsValidateFile(const void *opaque)
{
struct testQemuDiskXMLToJSONData *data = (void *) opaque;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autofree char *jsonpath = NULL;
g_autofree char *actual = NULL;
size_t i;
if (data->fail)
return EXIT_AM_SKIP;
jsonpath = g_strdup_printf("%s%s.json", testQemuDiskXMLToJSONPath, data->name);
for (i = 0; i < data->nimages; i++) {
g_autofree char *formatprops = NULL;
g_autofree char *storageprops = NULL;
if (!(formatprops = virJSONValueToString(data->images[i].formatprops, true)))
return -1;
if (!(storageprops = virJSONValueToString(data->images[i].storageprops, true)))
return -1;
virBufferStrcat(&buf, formatprops, storageprops, NULL);
}
actual = virBufferContentAndReset(&buf);
return virTestCompareToFile(actual, jsonpath);
}
static int
testQemuDiskXMLToPropsValidateFileSrcOnly(const void *opaque)
{
struct testQemuDiskXMLToJSONData *data = (void *) opaque;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autofree char *jsonpath = NULL;
g_autofree char *actual = NULL;
size_t i;
if (data->fail)
return EXIT_AM_SKIP;
jsonpath = g_strdup_printf("%s%s-srconly.json", testQemuDiskXMLToJSONPath,
data->name);
for (i = 0; i < data->nimages; i++) {
g_autofree char *jsonstr = NULL;
virBufferAddLit(&buf, "(\n");
virBufferAdjustIndent(&buf, 2);
virBufferAddLit(&buf, "source only properties:\n");
if (!(jsonstr = virJSONValueToString(data->images[i].storagepropssrc, true)))
return -1;
virBufferAddStr(&buf, jsonstr);
virBufferAddLit(&buf, "backing store string:\n");
virBufferAddStr(&buf, data->images[i].backingstore);
virBufferTrim(&buf, "\n");
virBufferAdjustIndent(&buf, -2);
virBufferAddLit(&buf, "\n)\n");
}
actual = virBufferContentAndReset(&buf);
return virTestCompareToFile(actual, jsonpath);
}
struct testQemuImageCreateData {
const char *name;
const char *backingname;
GHashTable *schema;
virJSONValue *schemaroot;
virQEMUDriver *driver;
virQEMUCaps *qemuCaps;
};
static const char *testQemuImageCreatePath = abs_srcdir "/qemublocktestdata/imagecreate/";
static virStorageSource *
testQemuImageCreateLoadDiskXML(const char *name,
virDomainXMLOption *xmlopt)
{
virDomainSnapshotDiskDef *diskdef = NULL;
g_autoptr(xmlDoc) doc = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL;
xmlNodePtr node;
g_autofree char *xmlpath = NULL;
virStorageSource *ret = NULL;
xmlpath = g_strdup_printf("%s%s.xml", testQemuImageCreatePath, name);
if (!(doc = virXMLParseFileCtxt(xmlpath, &ctxt)))
return NULL;
if (!(node = virXPathNode("//disk", ctxt))) {
VIR_TEST_VERBOSE("failed to find