libvirt/tests/storagevolxml2xmltest.c

107 lines
2.9 KiB
C
Raw Normal View History

#include <config.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include "internal.h"
#include "testutils.h"
#include "storage_conf.h"
#include "testutilsqemu.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToXMLFiles(const char *poolxml, const char *inxml,
const char *outxml, unsigned int flags)
{
g_autofree char *actual = NULL;
g_autoptr(virStoragePoolDef) pool = NULL;
g_autoptr(virStorageVolDef) dev = NULL;
2015-04-23 15:10:15 +00:00
if (!(pool = virStoragePoolDefParseFile(poolxml)))
return -1;
2015-04-23 15:10:15 +00:00
if (!(dev = virStorageVolDefParseFile(pool, inxml, flags)))
return -1;
if (!(actual = virStorageVolDefFormat(pool, dev)))
return -1;
if (virTestCompareToFile(actual, outxml) < 0)
return -1;
return 0;
}
struct testInfo {
const char *pool;
const char *name;
unsigned int flags;
};
static int
testCompareXMLToXMLHelper(const void *data)
{
const struct testInfo *info = data;
g_autofree char *poolxml = NULL;
g_autofree char *inxml = NULL;
g_autofree char *outxml = NULL;
poolxml = g_strdup_printf("%s/storagepoolxml2xmlin/%s.xml",
abs_srcdir, info->pool);
inxml = g_strdup_printf("%s/storagevolxml2xmlin/%s.xml",
abs_srcdir, info->name);
outxml = g_strdup_printf("%s/storagevolxml2xmlout/%s.xml",
abs_srcdir, info->name);
return testCompareXMLToXMLFiles(poolxml, inxml, outxml, info->flags);
}
static int
tests: simplify common setup A few of the tests were missing basic sanity checks, while most of them were doing copy-and-paste initialization (in fact, some of them pasted the argc > 1 check more than once!). It's much nicer to do things in one common place, and minimizes the size of the next patch that fixes getcwd usage. * tests/testutils.h (EXIT_AM_HARDFAIL): New define. (progname, abs_srcdir): Define for all tests. (VIRT_TEST_MAIN): Change callback signature. * tests/testutils.c (virtTestMain): Do more common init. * tests/commandtest.c (mymain): Simplify. * tests/cputest.c (mymain): Likewise. * tests/esxutilstest.c (mymain): Likewise. * tests/eventtest.c (mymain): Likewise. * tests/hashtest.c (mymain): Likewise. * tests/networkxml2xmltest.c (mymain): Likewise. * tests/nodedevxml2xmltest.c (myname): Likewise. * tests/nodeinfotest.c (mymain): Likewise. * tests/nwfilterxml2xmltest.c (mymain): Likewise. * tests/qemuargv2xmltest.c (mymain): Likewise. * tests/qemuhelptest.c (mymain): Likewise. * tests/qemuxml2argvtest.c (mymain): Likewise. * tests/qemuxml2xmltest.c (mymain): Likewise. * tests/qparamtest.c (mymain): Likewise. * tests/sexpr2xmltest.c (mymain): Likewise. * tests/sockettest.c (mymain): Likewise. * tests/statstest.c (mymain): Likewise. * tests/storagepoolxml2xmltest.c (mymain): Likewise. * tests/storagevolxml2xmltest.c (mymain): Likewise. * tests/virbuftest.c (mymain): Likewise. * tests/virshtest.c (mymain): Likewise. * tests/vmx2xmltest.c (mymain): Likewise. * tests/xencapstest.c (mymain): Likewise. * tests/xmconfigtest.c (mymain): Likewise. * tests/xml2sexprtest.c (mymain): Likewise. * tests/xml2vmxtest.c (mymain): Likewise.
2011-04-29 16:21:20 +00:00
mymain(void)
{
int ret = 0;
#define DO_TEST_FULL(pool, name, flags) \
do { \
struct testInfo info = { pool, name, flags }; \
if (virTestRun("Storage Vol XML-2-XML " name, \
testCompareXMLToXMLHelper, &info) < 0) \
ret = -1; \
} \
while (0);
#define DO_TEST(pool, name) DO_TEST_FULL(pool, name, 0)
DO_TEST("pool-dir", "vol-file");
storage: use valid XML for awkward volume names $ touch /var/lib/libvirt/images/'a<b>c' $ virsh pool-refresh default $ virsh vol-dumpxml 'a<b>c' default | head -n2 <volume> <name>a<b>c</name> Oops. That's not valid XML. And when we fix the XML generation, it fails RelaxNG validation. I'm also tired of seeing <key>(null)</key> in the example output for volume xml; while we used NULLSTR() to avoid a NULL deref rather than relying on glibc's printf extension behavior, it's even better if we avoid the issue in the first place. But this requires being careful that we don't invalidate any storage backends that were relying on key being unassigned during virStoragVolCreateXML[From]. I would have split this into two patches (one for escaping, one for avoiding <key>(null)</key>), but since they both end up touching a lot of the same test files, I ended up merging it into one. Note that this patch allows pretty much any volume name that can appear in a directory (excluding . and .. because those are special), but does nothing to change the current (unenforced) RelaxNG claim that pool names will consist only of letters, numbers, _, -, and +. Tightening the C code to match RelaxNG patterns and/or relaxing the grammar to match the C code for pool names is a task for another day (but remember, we DID recently tighten C code for domain names to exclude a leading '.'). * src/conf/storage_conf.c (virStoragePoolSourceFormat) (virStoragePoolDefFormat, virStorageVolTargetDefFormat) (virStorageVolDefFormat): Escape user-controlled strings. (virStorageVolDefParseXML): Parse key, for use in unit tests. * src/storage/storage_driver.c (storageVolCreateXML) (storageVolCreateXMLFrom): Ensure parsed key doesn't confuse volume creation. * docs/schemas/basictypes.rng (volName): Relax definition. * tests/storagepoolxml2xmltest.c (mymain): Test it. * tests/storagevolxml2xmltest.c (mymain): Likewise. * tests/storagepoolxml2xmlin/pool-dir-naming.xml: New file. * tests/storagepoolxml2xmlout/pool-dir-naming.xml: Likewise. * tests/storagevolxml2xmlin/vol-file-naming.xml: Likewise. * tests/storagevolxml2xmlout/vol-file-naming.xml: Likewise. * tests/storagevolxml2xmlout/vol-*.xml: Fix fallout. Signed-off-by: Eric Blake <eblake@redhat.com>
2013-11-21 00:04:05 +00:00
DO_TEST("pool-dir", "vol-file-naming");
DO_TEST("pool-dir", "vol-file-backing");
DO_TEST("pool-dir", "vol-file-iso");
DO_TEST("pool-dir", "vol-qcow2");
DO_TEST("pool-dir", "vol-qcow2-1.1");
DO_TEST("pool-dir", "vol-qcow2-lazy");
DO_TEST("pool-dir", "vol-qcow2-0.10-lazy");
DO_TEST("pool-dir", "vol-qcow2-nobacking");
DO_TEST("pool-dir", "vol-qcow2-encryption");
DO_TEST("pool-dir", "vol-qcow2-luks");
DO_TEST("pool-dir", "vol-qcow2-clusterSize");
DO_TEST("pool-dir", "vol-luks");
DO_TEST("pool-dir", "vol-luks-cipher");
DO_TEST("pool-disk", "vol-partition");
DO_TEST("pool-logical", "vol-logical");
DO_TEST("pool-logical", "vol-logical-backing");
DO_TEST("pool-sheepdog", "vol-sheepdog");
DO_TEST("pool-gluster", "vol-gluster-dir");
DO_TEST("pool-gluster", "vol-gluster-dir-neg-uid");
DO_TEST_FULL("pool-dir", "vol-qcow2-nocapacity",
VIR_VOL_XML_PARSE_NO_CAPACITY);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIR_TEST_MAIN(mymain)