On 04/09/2014 09:41 PM, Eric Blake wrote: > I'm almost to the point where virStorageSource can track everything > that virStorageFileMetadata was used for, so that we can use one > struct instead of two. But while working on this today, I noticed > that virStorageFileChainLookup() does not have any unit tests, so > I'll be writing those before removing the two remaining redundant > fields. > It will look something like this (although I still need more test cases before turning it into a formal commit): diff --git i/tests/virstoragetest.c w/tests/virstoragetest.c index 13b5032..225d18d 100644 --- i/tests/virstoragetest.c +++ w/tests/virstoragetest.c @@ -375,12 +375,69 @@ testStorageChain(const void *args) return ret; } +struct testLookupData +{ + virStorageFileMetadataPtr chain; + const char *start; + const char *name; + const char *expResult; + virStorageFileMetadataPtr expMeta; + const char *expParent; +}; + +static int +testStorageLookup(const void *args) +{ + const struct testLookupData *data = args; + int ret = 0; + const char *actualResult; + virStorageFileMetadataPtr actualMeta; + const char *actualParent; + + /* This function is documented as giving results within chain, + * which means we can use pointer equality instead of STREQ. Test + * twice to ensure optional parameters don't cause NULL deref. */ + actualResult = virStorageFileChainLookup(data->chain, data->start, + data->name, NULL, NULL); + if (data->expResult != actualResult) { + fprintf(stderr, "result 1: expected %s(%p), got %s(%p)\n", + NULLSTR(data->expResult), data->expResult, + NULLSTR(actualResult), actualResult); + ret = -1; + } + + actualResult = virStorageFileChainLookup(data->chain, data->start, + data->name, &actualMeta, + &actualParent); + if (data->expResult != actualResult) { + fprintf(stderr, "result 2: expected %s(%p), got %s(%p)\n", + NULLSTR(data->expResult), data->expResult, + NULLSTR(actualResult), actualResult); + ret = -1; + } + if (data->expMeta != actualMeta) { + fprintf(stderr, "meta: expected %p, got %p\n", + data->expMeta, actualMeta); + ret = -1; + } + if (data->expParent != actualParent) { + fprintf(stderr, "parent: expected %s(%p), got %s(%p)\n", + NULLSTR(data->expParent), data->expParent, + NULLSTR(actualParent), actualParent); + ret = -1; + } + + return ret; +} + static int mymain(void) { int ret; virCommandPtr cmd = NULL; struct testChainData data; + virStorageFileMetadataPtr chain = NULL; + const char *start = NULL; /* Prep some files with qemu-img; if that is not found on PATH, or * if it lacks support for qcow2 and qed, skip this test. */ @@ -768,6 +825,26 @@ mymain(void) (&wrap, &qcow2), EXP_WARN, (&wrap, &qcow2), ALLOW_PROBE | EXP_WARN); + /* Test behavior of chain lookups */ + start = "wrap"; + chain = virStorageFileGetMetadata("wrap", VIR_STORAGE_FILE_QCOW2, + -1, -1, false); + if (!chain) + return EXIT_FAILURE; + +#define TEST_LOOKUP(id, name, result, meta, parent) \ + do { \ + struct testLookupData data2 = { chain, start, name, \ + result, meta, parent, }; \ + if (virtTestRun("Chain lookup " #id, \ + testStorageLookup, &data2) < 0) \ + ret = -1; \ + } while (0) + + TEST_LOOKUP(1, "wrap", start, chain, NULL); + TEST_LOOKUP(2, abswrap, start, chain, NULL); + TEST_LOOKUP(3, "qcow2", chain->backingStore, chain->backingMeta, start); + /* Final cleanup */ testCleanupImages(); virCommandFree(cmd); -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Attachment:
signature.asc
Description: OpenPGP digital signature
-- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list