[Libvir] PATCH: Use macros to remove duplicate test code

The test suite files contain alot of boilerplate duplicated code. With a few more macros I can significantly cut down the size of the test code. I also change abs_top_srcdir to abs_srcdir, and automatically initialize it to 'getcwd' if the env variable is missing. This lets me run the test cases directly, rather than having to go via 'make check'. statstest.c | 207 ++++++++++++++++++++++------------------------------- test_conf.sh | 16 +++- xencapstest.c | 10 -- xmconfigtest.c | 220 ++++++++++++--------------------------------------------- 4 files changed, 149 insertions(+), 304 deletions(-) Dan. Index: statstest.c =================================================================== RCS file: /data/cvs/libvirt/tests/statstest.c,v retrieving revision 1.5 diff -u -p -r1.5 statstest.c --- statstest.c 10 Apr 2008 16:53:29 -0000 1.5 +++ statstest.c 18 Apr 2008 00:29:55 -0000 @@ -7,6 +7,8 @@ #include "stats_linux.h" #include "internal.h" +#include "testutils.h" + #if WITH_XEN static void testQuietError(void *userData ATTRIBUTE_UNUSED, virErrorPtr error ATTRIBUTE_UNUSED) @@ -21,13 +23,26 @@ static int testDevice(const char *path, int actual = xenLinuxDomainDeviceID(NULL, 1, path); if (actual == expect) { - fprintf(stderr, "%-14s == %-6d OK\n", path, expect); return 0; } else { - fprintf(stderr, "%-14s == %-6d (%-6d) FAILED\n", path, expect, actual); + if (getenv("DEBUG_TESTS")) + fprintf(stderr, "Expect %-6d Actual %-6d\n", expect, actual); return -1; } } + +struct testInfo +{ + const char *dev; + int num; +}; + +static int testDeviceHelper(const void *data) +{ + const struct testInfo *info = data; + return testDevice(info->dev, info->num); +} + #endif int @@ -42,85 +57,67 @@ main(void) if (!getenv("DEBUG_TESTS")) virSetErrorFunc(NULL, testQuietError); +#define DO_TEST(dev, num) \ + do { \ + struct testInfo info = { dev, num }; \ + if (virtTestRun("Device " dev " -> " # num, \ + 1, testDeviceHelper, &info) < 0) \ + ret = -1; \ + } while (0) + /******************************** * Xen paravirt disks ********************************/ /* first valid disk */ - if (testDevice("xvda", 51712) < 0) - ret = -1; - if (testDevice("xvda1", 51713) < 0) - ret = -1; - if (testDevice("xvda15", 51727) < 0) - ret = -1; + DO_TEST("xvda", 51712); + DO_TEST("xvda1", 51713); + DO_TEST("xvda15", 51727); /* Last valid disk */ - if (testDevice("xvdp", 51952) < 0) - ret = -1; - if (testDevice("xvdp1", 51953) < 0) - ret = -1; - if (testDevice("xvdp15", 51967) < 0) - ret = -1; + DO_TEST("xvdp", 51952); + DO_TEST("xvdp1", 51953); + DO_TEST("xvdp15", 51967); /* Disk letter to large */ - if (testDevice("xvdq", -1) < 0) - ret = -1; + DO_TEST("xvdq", -1); /* missing disk letter */ - if (testDevice("xvd1", -1) < 0) - ret = -1; + DO_TEST("xvd1", -1); /* partition to large */ - if (testDevice("xvda16", -1) < 0) - ret = -1; + DO_TEST("xvda16", -1); /* partition to small */ - if (testDevice("xvda0", -1) < 0) - ret = -1; + DO_TEST("xvda0", -1); /* leading zeros */ - if (testDevice("xvda01", -1) < 0) - ret = -1; + DO_TEST("xvda01", -1); /* leading + */ - if (testDevice("xvda+1", -1) < 0) - ret = -1; + DO_TEST("xvda+1", -1); /* leading - */ - if (testDevice("xvda-1", -1) < 0) - ret = -1; + DO_TEST("xvda-1", -1); /******************************** * IDE disks ********************************/ /* odd numbered disk */ - if (testDevice("hda", 768) < 0) - ret = -1; - if (testDevice("hda1", 769) < 0) - ret = -1; - if (testDevice("hda63", 831) < 0) - ret = -1; + DO_TEST("hda", 768); + DO_TEST("hda1", 769); + DO_TEST("hda63", 831); /* even number disk */ - if (testDevice("hdd", 5695) < 0) - ret = -1; - if (testDevice("hdd1", 5696) < 0) - ret = -1; - if (testDevice("hdd63", 5758) < 0) - ret = -1; + DO_TEST("hdd", 5695); + DO_TEST("hdd1", 5696); + DO_TEST("hdd63", 5758); /* last valid disk */ - if (testDevice("hdt", 23359) < 0) - ret = -1; - if (testDevice("hdt1", 23360) < 0) - ret = -1; - if (testDevice("hdt63", 23422) < 0) - ret = -1; + DO_TEST("hdt", 23359); + DO_TEST("hdt1", 23360); + DO_TEST("hdt63", 23422); /* Disk letter to large */ - if (testDevice("hdu", -1) < 0) - ret = -1; + DO_TEST("hdu", -1); /* missing disk letter */ - if (testDevice("hd1", -1) < 0) - ret = -1; + DO_TEST("hd1", -1); /* partition to large */ - if (testDevice("hda64", -1) < 0) - ret = -1; + DO_TEST("hda64", -1); /* partition to small */ - if (testDevice("hda0", -1) < 0) - ret = -1; + DO_TEST("hda0", -1); @@ -129,89 +126,55 @@ main(void) ********************************/ /* first valid disk */ - if (testDevice("sda", 2048) < 0) - ret = -1; - if (testDevice("sda1", 2049) < 0) - ret = -1; - if (testDevice("sda15", 2063) < 0) - ret = -1; + DO_TEST("sda", 2048); + DO_TEST("sda1", 2049); + DO_TEST("sda15", 2063); /* last valid disk of first SCSI major number */ - if (testDevice("sdp", 2288) < 0) - ret = -1; - if (testDevice("sdp1", 2289) < 0) - ret = -1; - if (testDevice("sdp15", 2303) < 0) - ret = -1; + DO_TEST("sdp", 2288); + DO_TEST("sdp1", 2289); + DO_TEST("sdp15", 2303); /* first valid disk of second SCSI major number */ - if (testDevice("sdq", 16640) < 0) - ret = -1; - if (testDevice("sdq1", 16641) < 0) - ret = -1; - if (testDevice("sdq15", 16655) < 0) - ret = -1; + DO_TEST("sdq", 16640); + DO_TEST("sdq1", 16641); + DO_TEST("sdq15", 16655); /* last valid single letter disk */ - if (testDevice("sdz", 16784) < 0) - ret = -1; - if (testDevice("sdz1", 16785) < 0) - ret = -1; - if (testDevice("sdz15", 16799) < 0) - ret = -1; + DO_TEST("sdz", 16784); + DO_TEST("sdz1", 16785); + DO_TEST("sdz15", 16799); /* first valid dual letter disk */ - if (testDevice("sdaa", 16800) < 0) - ret = -1; - if (testDevice("sdaa1", 16801) < 0) - ret = -1; - if (testDevice("sdaa15", 16815) < 0) - ret = -1; + DO_TEST("sdaa", 16800); + DO_TEST("sdaa1", 16801); + DO_TEST("sdaa15", 16815); /* second valid dual letter disk */ - if (testDevice("sdab", 16816) < 0) - ret = -1; - if (testDevice("sdab1", 16817) < 0) - ret = -1; - if (testDevice("sdab15", 16831) < 0) - ret = -1; + DO_TEST("sdab", 16816); + DO_TEST("sdab1", 16817); + DO_TEST("sdab15", 16831); /* first letter of second sequence of dual letter disk */ - if (testDevice("sdba", 17216) < 0) - ret = -1; - if (testDevice("sdba1", 17217) < 0) - ret = -1; - if (testDevice("sdba15", 17231) < 0) - ret = -1; + DO_TEST("sdba", 17216); + DO_TEST("sdba1", 17217); + DO_TEST("sdba15", 17231); /* last valid dual letter disk */ - if (testDevice("sdiv", 34800) < 0) - ret = -1; - if (testDevice("sdiv1", 34801) < 0) - ret = -1; - if (testDevice("sdiv15", 34815) < 0) - ret = -1; + DO_TEST("sdiv", 34800); + DO_TEST("sdiv1", 34801); + DO_TEST("sdiv15", 34815); /* Disk letter to large */ - if (testDevice("sdix", -1) < 0) - ret = -1; + DO_TEST("sdix", -1); /* missing disk letter */ - if (testDevice("sd1", -1) < 0) - ret = -1; + DO_TEST("sd1", -1); /* partition to large */ - if (testDevice("sda16", -1) < 0) - ret = -1; + DO_TEST("sda16", -1); /* partition to small */ - if (testDevice("sda0", -1) < 0) - ret = -1; + DO_TEST("sda0", -1); /* Path stripping */ - if (testDevice("/dev", -1) < 0) - ret = -1; - if (testDevice("/dev/", -1) < 0) - ret = -1; - if (testDevice("/dev/xvd", -1) < 0) - ret = -1; - if (testDevice("/dev/xvda", 51712) < 0) - ret = -1; - if (testDevice("/dev/xvda1", 51713) < 0) - ret = -1; - if (testDevice("/dev/xvda15", 51727) < 0) - ret = -1; + DO_TEST("/dev", -1); + DO_TEST("/dev/", -1); + DO_TEST("/dev/xvd", -1); + DO_TEST("/dev/xvda", 51712); + DO_TEST("/dev/xvda1", 51713); + DO_TEST("/dev/xvda15", 51727); #endif exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE); Index: test_conf.sh =================================================================== RCS file: /data/cvs/libvirt/tests/test_conf.sh,v retrieving revision 1.4 diff -u -p -r1.4 test_conf.sh --- test_conf.sh 5 Feb 2008 19:27:37 -0000 1.4 +++ test_conf.sh 18 Apr 2008 00:29:55 -0000 @@ -1,8 +1,15 @@ #!/bin/bash -set -x +#set -x +set -e +if [ -z "$abs_srcdir" ]; then + abs_srcdir=`pwd` +fi + NOK=0 -for f in $abs_top_srcdir/tests/confdata/*.conf +i=1 +for f in $abs_srcdir/confdata/*.conf do + n=`echo $f | sed -e "s,$abs_srcdir/confdata/,,"` ./conftest $f > conftest.$$ outfile=`echo "$f" | sed s+\.conf$+\.out+` diff $outfile conftest.$$ > /dev/null @@ -11,11 +18,12 @@ do if [ -n "$DEBUG_TESTS" ]; then diff -u $outfile conftest.$$ fi - echo "$f FAILED" + printf "%2d) %-60s ... %s\n" $i $n "FAILED" NOK=1 else - echo "$f OK" + printf "%2d) %-60s ... %s\n" $i $n "OK" fi + i=`expr $i + 1` done rm -f conftest.$$ exit $NOK Index: xencapstest.c =================================================================== RCS file: /data/cvs/libvirt/tests/xencapstest.c,v retrieving revision 1.11 diff -u -p -r1.11 xencapstest.c --- xencapstest.c 10 Apr 2008 16:54:54 -0000 1.11 +++ xencapstest.c 18 Apr 2008 00:29:56 -0000 @@ -50,14 +50,10 @@ static int testCompareFiles(const char * if (!(actualxml = xenHypervisorMakeCapabilitiesXML(NULL, hostmachine, fp1, fp2))) goto fail; - if (getenv("DEBUG_TESTS") && - STRNEQ(expectxml, actualxml)) { - printf("In test file %s:\n", capabilities); - printf("Expect %d '%s'\n", (int)strlen(expectxml), expectxml); - printf("Actual %d '%s'\n", (int)strlen(actualxml), actualxml); + if (STRNEQ(expectxml, actualxml)) { + virtTestDifference(stderr, expectxml, actualxml); + goto fail; } - if (strcmp(expectxml, actualxml)) - goto fail; ret = 0; Index: xmconfigtest.c =================================================================== RCS file: /data/cvs/libvirt/tests/xmconfigtest.c,v retrieving revision 1.13 diff -u -p -r1.13 xmconfigtest.c --- xmconfigtest.c 10 Apr 2008 16:53:29 -0000 1.13 +++ xmconfigtest.c 18 Apr 2008 00:29:56 -0000 @@ -25,6 +25,7 @@ #include <stdio.h> #include <string.h> +#include <unistd.h> #ifdef WITH_XEN @@ -35,11 +36,11 @@ #include "conf.h" static char *progname; -static char *abs_top_srcdir; +static char *abs_srcdir; #define MAX_FILE 4096 -static int testCompareParseXML(const char *xmcfg_rel, const char *xml_rel, +static int testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion) { char xmlData[MAX_FILE]; char xmcfgData[MAX_FILE]; @@ -53,11 +54,6 @@ static int testCompareParseXML(const cha int wrote = MAX_FILE; void *old_priv = NULL; struct _xenUnifiedPrivate priv; - char xmcfg[PATH_MAX]; - char xml[PATH_MAX]; - - snprintf(xmcfg, sizeof xmcfg - 1, "%s/tests/%s", abs_top_srcdir, xmcfg_rel); - snprintf(xml, sizeof xml - 1, "%s/tests/%s", abs_top_srcdir, xml_rel); conn = virConnectOpenReadOnly("test:///default"); if (!conn) goto fail; @@ -80,11 +76,8 @@ static int testCompareParseXML(const cha goto fail; gotxmcfgPtr[wrote] = '\0'; - if (strcmp(xmcfgData, gotxmcfgData)) { - if (getenv("DEBUG_TESTS")) { - printf("Expect %d '%s'\n", (int)strlen(xmcfgData), xmcfgData); - printf("Actual %d '%s'\n", (int)strlen(gotxmcfgData), gotxmcfgData); - } + if (STRNEQ(xmcfgData, gotxmcfgData)) { + virtTestDifference(stderr, xmcfgData, gotxmcfgData); goto fail; } @@ -102,7 +95,7 @@ static int testCompareParseXML(const cha return ret; } -static int testCompareFormatXML(const char *xmcfg_rel, const char *xml_rel, +static int testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion) { char xmlData[MAX_FILE]; char xmcfgData[MAX_FILE]; @@ -114,11 +107,6 @@ static int testCompareFormatXML(const ch virConnectPtr conn; void *old_priv; struct _xenUnifiedPrivate priv; - char xmcfg[PATH_MAX]; - char xml[PATH_MAX]; - - snprintf(xmcfg, sizeof xmcfg - 1, "%s/tests/%s", abs_top_srcdir, xmcfg_rel); - snprintf(xml, sizeof xml - 1, "%s/tests/%s", abs_top_srcdir, xml_rel); conn = virConnectOpenReadOnly("test:///default"); if (!conn) goto fail; @@ -163,102 +151,25 @@ static int testCompareFormatXML(const ch return ret; } -static int testCompareParavirtOldPVFBFormat(const void *data ATTRIBUTE_UNUSED) { - return testCompareFormatXML("xmconfigdata/test-paravirt-old-pvfb.cfg", - "xmconfigdata/test-paravirt-old-pvfb.xml", - 2); -} -static int testCompareParavirtOldPVFBParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-paravirt-old-pvfb.cfg", - "xmconfigdata/test-paravirt-old-pvfb.xml", - 2); -} - -static int testCompareParavirtNewPVFBFormat(const void *data ATTRIBUTE_UNUSED) { - return testCompareFormatXML("xmconfigdata/test-paravirt-new-pvfb.cfg", - "xmconfigdata/test-paravirt-new-pvfb.xml", - 3); -} -static int testCompareParavirtNewPVFBParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-paravirt-new-pvfb.cfg", - "xmconfigdata/test-paravirt-new-pvfb.xml", - 3); -} - -static int testCompareFullvirtOldCDROMFormat(const void *data ATTRIBUTE_UNUSED) { - return testCompareFormatXML("xmconfigdata/test-fullvirt-old-cdrom.cfg", - "xmconfigdata/test-fullvirt-old-cdrom.xml", - 1); -} -static int testCompareFullvirtOldCDROMParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-fullvirt-old-cdrom.cfg", - "xmconfigdata/test-fullvirt-old-cdrom.xml", - 1); -} - -static int testCompareFullvirtNewCDROMFormat(const void *data ATTRIBUTE_UNUSED) { - return testCompareFormatXML("xmconfigdata/test-fullvirt-new-cdrom.cfg", - "xmconfigdata/test-fullvirt-new-cdrom.xml", - 2); -} -static int testCompareFullvirtNewCDROMParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-fullvirt-new-cdrom.cfg", - "xmconfigdata/test-fullvirt-new-cdrom.xml", - 2); -} - -static int testCompareFullvirtClockUTCFormat(const void *data ATTRIBUTE_UNUSED) { - return testCompareFormatXML("xmconfigdata/test-fullvirt-utc.cfg", - "xmconfigdata/test-fullvirt-utc.xml", - 2); -} -static int testCompareFullvirtClockUTCParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-fullvirt-utc.cfg", - "xmconfigdata/test-fullvirt-utc.xml", - 2); -} - -static int testCompareFullvirtClockLocaltimeFormat(const void *data ATTRIBUTE_UNUSED) { - return testCompareFormatXML("xmconfigdata/test-fullvirt-localtime.cfg", - "xmconfigdata/test-fullvirt-localtime.xml", - 2); -} - -static int testCompareFullvirtClockLocaltimeParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-fullvirt-localtime.cfg", - "xmconfigdata/test-fullvirt-localtime.xml", - 2); -} - -static int testCompareFullvirtInputUSBTabletFormat(const void *data ATTRIBUTE_UNUSED) { - return testCompareFormatXML("xmconfigdata/test-fullvirt-usbtablet.cfg", - "xmconfigdata/test-fullvirt-usbtablet.xml", - 2); -} +struct testInfo { + const char *name; + int version; + int mode; +}; -static int testCompareFullvirtInputUSBTabletParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-fullvirt-usbtablet.cfg", - "xmconfigdata/test-fullvirt-usbtablet.xml", - 2); -} - -static int testCompareFullvirtInputUSBTabletNoBusParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-fullvirt-usbtablet.cfg", - "xmconfigdata/test-fullvirt-usbtablet-no-bus.xml", - 2); -} - -static int testCompareFullvirtInputUSBMouseFormat(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-fullvirt-usbmouse.cfg", - "xmconfigdata/test-fullvirt-usbmouse.xml", - 2); -} - -static int testCompareFullvirtInputUSBMouseParse(const void *data ATTRIBUTE_UNUSED) { - return testCompareParseXML("xmconfigdata/test-fullvirt-usbmouse.cfg", - "xmconfigdata/test-fullvirt-usbmouse.xml", - 2); +static int testCompareHelper(const void *data) { + const struct testInfo *info = data; + char xml[PATH_MAX]; + char cfg[PATH_MAX]; + snprintf(xml, PATH_MAX, "%s/xmconfigdata/test-%s.xml", + abs_srcdir, info->name); + snprintf(cfg, PATH_MAX, "%s/xmconfigdata/test-%s.cfg", + abs_srcdir, info->name); + if (info->mode == 0) + return testCompareParseXML(cfg, xml, info->version); + else + return testCompareFormatXML(cfg, xml, info->version); } @@ -266,6 +177,7 @@ int main(int argc, char **argv) { int ret = 0; + char cwd[PATH_MAX]; progname = argv[0]; @@ -274,65 +186,31 @@ main(int argc, char **argv) exit(EXIT_FAILURE); } - abs_top_srcdir = getenv("abs_top_srcdir"); - if (!abs_top_srcdir) - return 1; - - /* Config -> XML */ - if (virtTestRun("Paravirt old PVFB (Format)", - 1, testCompareParavirtOldPVFBFormat, NULL) != 0) - ret = -1; - if (virtTestRun("Paravirt new PVFB (Format)", - 1, testCompareParavirtNewPVFBFormat, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt old PVFB (Format)", - 1, testCompareFullvirtOldCDROMFormat, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt new PVFB (Format)", - 1, testCompareFullvirtNewCDROMFormat, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt clock Localtime (Format)", - 1, testCompareFullvirtClockLocaltimeFormat, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt clock UTC (Format)", - 1, testCompareFullvirtClockUTCFormat, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt USB mouse (Format)", - 1, testCompareFullvirtInputUSBMouseFormat, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt USB tablet (Format)", - 1, testCompareFullvirtInputUSBTabletFormat, NULL) != 0) - ret = -1; - - /* XML -> Config */ - if (virtTestRun("Paravirt old PVFB (Parse)", - 1, testCompareParavirtOldPVFBParse, NULL) != 0) - ret = -1; - if (virtTestRun("Paravirt new PVFB (Parse)", - 1, testCompareParavirtNewPVFBParse, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt old PVFB (Parse)", - 1, testCompareFullvirtOldCDROMParse, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt new PVFB (Parse)", - 1, testCompareFullvirtNewCDROMParse, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt clock Localtime (Parse)", - 1, testCompareFullvirtClockLocaltimeParse, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt clock UTC (Parse)", - 1, testCompareFullvirtClockUTCParse, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt USB mouse (Parse)", - 1, testCompareFullvirtInputUSBMouseParse, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt USB tablet (Parse)", - 1, testCompareFullvirtInputUSBTabletParse, NULL) != 0) - ret = -1; - if (virtTestRun("Fullvirt USB tablet no bus (Parse)", - 1, testCompareFullvirtInputUSBTabletNoBusParse, NULL) != 0) - ret = -1; - + abs_srcdir = getenv("abs_srcdir"); + if (!abs_srcdir) + abs_srcdir = getcwd(cwd, sizeof(cwd)); + + +#define DO_TEST(name, version) \ + do { \ + struct testInfo info0 = { name, version, 0 }; \ + struct testInfo info1 = { name, version, 1 }; \ + if (virtTestRun("Xen XM-2-XML Parse " name, \ + 1, testCompareHelper, &info0) < 0) \ + ret = -1; \ + if (virtTestRun("Xen XM-2-XML Format " name, \ + 1, testCompareHelper, &info1) < 0) \ + ret = -1; \ + } while (0) + + DO_TEST("paravirt-old-pvfb", 2); + DO_TEST("paravirt-new-pvfb", 3); + DO_TEST("fullvirt-old-cdrom", 1); + DO_TEST("fullvirt-new-cdrom", 2); + DO_TEST("fullvirt-utc", 2); + DO_TEST("fullvirt-localtime", 2); + DO_TEST("fullvirt-usbtablet", 2); + DO_TEST("fullvirt-usbmouse", 2); exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE); } -- |: Red Hat, Engineering, Boston -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

On Fri, Apr 18, 2008 at 01:32:49AM +0100, Daniel P. Berrange wrote:
The test suite files contain alot of boilerplate duplicated code. With a few more macros I can significantly cut down the size of the test code.
I also change abs_top_srcdir to abs_srcdir, and automatically initialize it to 'getcwd' if the env variable is missing. This lets me run the test cases directly, rather than having to go via 'make check'.
statstest.c | 207 ++++++++++++++++++++++------------------------------- test_conf.sh | 16 +++- xencapstest.c | 10 -- xmconfigtest.c | 220 ++++++++++++--------------------------------------------- 4 files changed, 149 insertions(+), 304 deletions(-)
+1 Rich. -- Richard Jones, Emerging Technologies, Red Hat http://et.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v

"Daniel P. Berrange" <berrange@redhat.com> wrote:
The test suite files contain alot of boilerplate duplicated code. With a few more macros I can significantly cut down the size of the test code.
I also change abs_top_srcdir to abs_srcdir, and automatically initialize it to 'getcwd' if the env variable is missing. This lets me run the test cases directly, rather than having to go via 'make check'.
Nice work. All looks fine.

"Daniel P. Berrange" <berrange@redhat.com> wrote: ...
Index: test_conf.sh =================================================================== RCS file: /data/cvs/libvirt/tests/test_conf.sh,v retrieving revision 1.4 diff -u -p -r1.4 test_conf.sh --- test_conf.sh 5 Feb 2008 19:27:37 -0000 1.4 +++ test_conf.sh 18 Apr 2008 00:29:55 -0000 @@ -1,8 +1,15 @@ #!/bin/bash -set -x +#set -x +set -e +if [ -z "$abs_srcdir" ]; then + abs_srcdir=`pwd` +fi + NOK=0 -for f in $abs_top_srcdir/tests/confdata/*.conf +i=1 +for f in $abs_srcdir/confdata/*.conf do + n=`echo $f | sed -e "s,$abs_srcdir/confdata/,,"` ./conftest $f > conftest.$$ outfile=`echo "$f" | sed s+\.conf$+\.out+` diff $outfile conftest.$$ > /dev/null @@ -11,11 +18,12 @@ do if [ -n "$DEBUG_TESTS" ]; then diff -u $outfile conftest.$$ fi - echo "$f FAILED" + printf "%2d) %-60s ... %s\n" $i $n "FAILED" NOK=1 else - echo "$f OK" + printf "%2d) %-60s ... %s\n" $i $n "OK" fi + i=`expr $i + 1` done rm -f conftest.$$ exit $NOK
Actually, I went to factor out the duplicate printf uses, then noticed some underquoting problems (i.e., when $abs_srcdir expands to something pathological, including e.g., semicolons or other shell meta-characters), so started to fix those, and then went ahead and converted to using test-lib.sh, so that this test now creates temporaries in its own private directory (better for running tests in parallel). As such, it doesn't have to use temp file names including ".$$" and no longer has to remove them explicitly. Also, there's no need to rely on /bin/bash in this particular case. Here's the new version. It also required the following small changes to TEST_ENVIRONMENT in Makefile.am. (however, if an older automake we care about doesn't have abs_srcdir, the RHS will have to be `cd '$(srcdir)'; pwd` instead) Dan, you're welcome to fold these changes into your patch. Or just commit what you have and I'll do this separately. ---------------------------------- #!/bin/sh if test "$VERBOSE" = yes; then set -x virsh --version fi . $srcdir/test-lib.sh set -e if test "x$abs_srcdir" = x; then abs_srcdir=`pwd` abs_builddir=`pwd` fi fail=0 i=1 data_dir=$abs_srcdir/confdata for f in $(cd "$data_dir" && echo *.conf) do "$abs_builddir/conftest" "$data_dir/$f" > "$f-actual" expected="$data_dir"/`echo "$f" | sed s+\.conf$+\.out+` if compare "$expected" "$f-actual"; then msg=OK else msg=FAILED fail=1 fi printf "%2d) %-60s ... %s\n" $i "$f" $msg i=`expr $i + 1` done (exit $fail); exit $fail ---------------------------------- diff --git a/tests/Makefile.am b/tests/Makefile.am index ca12b84..95ca4e5 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -64,6 +64,8 @@ path_add = $$abs_top_builddir/src$(PATH_SEPARATOR)$$abs_top_builddir/qemud # abs_top_{src/build}dir variables, so don't rely # on them here. Fake them with 'pwd' TESTS_ENVIRONMENT = \ + abs_srcdir='$(abs_srcdir)' \ + abs_builddir=`cd '$(builddir)'; pwd` \ abs_top_builddir=`cd '$(top_builddir)'; pwd` \ abs_top_srcdir=`cd '$(top_srcdir)'; pwd` \ PATH="$(path_add)$(PATH_SEPARATOR)$$PATH" \

On Fri, Apr 18, 2008 at 01:32:49AM +0100, Daniel P. Berrange wrote:
The test suite files contain alot of boilerplate duplicated code. With a few more macros I can significantly cut down the size of the test code.
I also change abs_top_srcdir to abs_srcdir, and automatically initialize it to 'getcwd' if the env variable is missing. This lets me run the test cases directly, rather than having to go via 'make check'.
Looks quite cleaner to me, +1 Daniel -- Red Hat Virtualization group http://redhat.com/virtualization/ Daniel Veillard | virtualization library http://libvirt.org/ veillard@redhat.com | libxml GNOME XML XSLT toolkit http://xmlsoft.org/ http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
participants (4)
-
Daniel P. Berrange
-
Daniel Veillard
-
Jim Meyering
-
Richard W.M. Jones