libvirt/tests/testutils.h

165 lines
5.4 KiB
C
Raw Normal View History

2006-05-09 15:35:46 +00:00
/*
* testutils.h: test utils
2006-05-09 15:35:46 +00:00
*
* Copyright (C) 2005, 2008-2013 Red Hat, Inc.
2006-05-09 15:35:46 +00:00
*
* 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
* <http://www.gnu.org/licenses/>.
2006-05-09 15:35:46 +00:00
*/
#pragma once
2006-05-09 15:35:46 +00:00
#include "viralloc.h"
#include "virfile.h"
#include "virstring.h"
#include "virjson.h"
#include "capabilities.h"
#include "domain_conf.h"
#define EXIT_AM_SKIP 77 /* tell Automake we're skipping a test */
#define EXIT_AM_HARDFAIL 99 /* tell Automake that the framework is broken */
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
/* Work around lack of gnulib support for fprintf %z */
#ifndef NO_LIBVIRT
# undef fprintf
# define fprintf virFilePrintf
#endif
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
extern char *progname;
tests: guarantee abs_srcdir in all C tests While trying to debug a failure of virpcitest during 'make distcheck', I noticed that with a VPATH build, 'cd tests; ./virpcitest' fails for an entirely different reason. To reproduce the distcheck failure, I had to run 'cd tests; abs_srcdir=/path/to/src ./virpcitest'. But we document in HACKING that all of our tests are supposed to be runnable without requiring extra environment variables. The solution: hardcode the location of srcdir into the just-built binaries, rather than requiring make to prepopulate environment variables. With this, './virpcitest' passes even in a VPATH build (provided that $(srcdir) is writable; a followup patch will fix the conditions required by 'make distcheck'). [Note: the makefile must still pass on directory variables to the test environment of shell scripts, since those aren't compiled. So while this solves the case of a compiled test, it still requires environment variables to pass a VPATH build of any shell script test case that relies on srcdir.] * tests/Makefile.am (AM_CFLAGS): Define abs_srcdir in all compiled tests. * tests/testutils.h (abs_srcdir): Quit declaring. * tests/testutils.c (virtTestMain): Rely on define rather than environment variable. * tests/virpcimock.c (pci_device_new_from_stub): Rely on define. * tests/cputest.c (mymain): Adjust abs_top_srcdir default. * tests/qemuxml2argvtest.c (mymain): Likewise. * tests/qemuxmlnstest.c (mymain): Likewise. Signed-off-by: Eric Blake <eblake@redhat.com>
2013-11-27 21:31:53 +00:00
/* Makefile.am provides these two definitions */
#if !defined(abs_srcdir) || !defined(abs_builddir)
# error Fix Makefile.am
#endif
bool virTestOOMActive(void);
Introduce new OOM testing support The previous OOM testing support would re-run the entire "main" method each iteration, failing a different malloc each time. When a test suite has 'n' allocations, the number of repeats requires is (n * (n + 1) ) / 2. This gets very large, very quickly. This new OOM testing support instead integrates at the virtTestRun level, so each individual test case gets repeated, instead of the entire test suite. This means the values of 'n' are orders of magnitude smaller. The simple usage is $ VIR_TEST_OOM=1 ./qemuxml2argvtest ... 29) QEMU XML-2-ARGV clock-utc ... OK Test OOM for nalloc=36 .................................... OK 30) QEMU XML-2-ARGV clock-localtime ... OK Test OOM for nalloc=36 .................................... OK 31) QEMU XML-2-ARGV clock-france ... OK Test OOM for nalloc=38 ...................................... OK ... the second lines reports how many mallocs have to be failed, and thus how many repeats of the test will be run. If it crashes, then running under valgrind will often show the problem $ VIR_TEST_OOM=1 ../run valgrind ./qemuxml2argvtest When debugging problems it is also helpful to select an individual test case $ VIR_TEST_RANGE=30 VIR_TEST_OOM=1 ../run valgrind ./qemuxml2argvtest When things get really tricky, it is possible to request that just specific allocs are failed. eg to fail allocs 5 -> 12, use $ VIR_TEST_RANGE=30 VIR_TEST_OOM=1:5-12 ../run valgrind ./qemuxml2argvtest In the worse case, you might want to know the stack trace of the alloc which was failed then VIR_TEST_OOM_TRACE can be set. If it is set to 1 then it will only print if it thinks a mistake happened. This is often not reliable, so setting it to 2 will make it print the stack trace for every alloc that is failed. $ VIR_TEST_OOM_TRACE=2 VIR_TEST_RANGE=30 VIR_TEST_OOM=1:5-5 ../run valgrind ./qemuxml2argvtest 30) QEMU XML-2-ARGV clock-localtime ... OK Test OOM for nalloc=36 !virAllocN /home/berrange/src/virt/libvirt/src/util/viralloc.c:180 virHashCreateFull /home/berrange/src/virt/libvirt/src/util/virhash.c:144 virDomainDefParseXML /home/berrange/src/virt/libvirt/src/conf/domain_conf.c:11745 virDomainDefParseNode /home/berrange/src/virt/libvirt/src/conf/domain_conf.c:12646 virDomainDefParse /home/berrange/src/virt/libvirt/src/conf/domain_conf.c:12590 testCompareXMLToArgvFiles /home/berrange/src/virt/libvirt/tests/qemuxml2argvtest.c:106 virtTestRun /home/berrange/src/virt/libvirt/tests/testutils.c:250 mymain /home/berrange/src/virt/libvirt/tests/qemuxml2argvtest.c:418 (discriminator 2) virtTestMain /home/berrange/src/virt/libvirt/tests/testutils.c:750 ?? ??:0 _start ??:? FAILED Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-09-23 13:21:52 +00:00
int virTestRun(const char *title,
int (*body)(const void *data),
const void *data);
int virTestLoadFile(const char *file, char **buf);
char *virTestLoadFilePath(const char *p, ...)
ATTRIBUTE_SENTINEL;
virJSONValuePtr virTestLoadFileJSON(const char *p, ...)
ATTRIBUTE_SENTINEL;
int virTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);
void virTestClearCommandPath(char *cmdset);
int virTestDifference(FILE *stream,
const char *expect,
const char *actual);
int virTestDifferenceFull(FILE *stream,
const char *expect,
const char *expectName,
const char *actual,
const char *actualName);
int virTestDifferenceFullNoRegenerate(FILE *stream,
const char *expect,
const char *expectName,
const char *actual,
const char *actualName);
int virTestDifferenceBin(FILE *stream,
const char *expect,
const char *actual,
size_t length);
int virTestCompareToFile(const char *actual,
const char *filename);
int virTestCompareToString(const char *expect,
const char *actual);
int virTestCompareToULL(unsigned long long expect,
unsigned long long actual);
2006-05-09 15:35:46 +00:00
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
unsigned int virTestGetExpensive(void);
unsigned int virTestGetRegenerate(void);
#define VIR_TEST_DEBUG(...) \
do { \
if (virTestGetDebug()) \
fprintf(stderr, __VA_ARGS__); \
} while (0)
#define VIR_TEST_VERBOSE(...) \
do { \
if (virTestGetVerbose()) \
fprintf(stderr, __VA_ARGS__); \
} while (0)
char *virTestLogContentAndReset(void);
void virTestQuiesceLibvirtErrors(bool always);
void virTestCounterReset(const char *prefix);
const char *virTestCounterNext(void);
int virTestMain(int argc,
char **argv,
int (*func)(void),
...);
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
/* Setup, then call func() */
#define VIR_TEST_MAIN(func) \
int main(int argc, char **argv) { \
return virTestMain(argc, argv, func, NULL); \
}
#define VIR_TEST_PRELOAD(lib) \
do { \
const char *preload = getenv("LD_PRELOAD"); \
if (preload == NULL || strstr(preload, lib) == NULL) { \
char *newenv; \
if (!virFileIsExecutable(lib)) { \
perror(lib); \
return EXIT_FAILURE; \
} \
if (!preload) { \
newenv = (char *) lib; \
} else if (virAsprintf(&newenv, "%s:%s", lib, preload) < 0) { \
perror("virAsprintf"); \
return EXIT_FAILURE; \
} \
setenv("LD_PRELOAD", newenv, 1); \
execv(argv[0], argv); \
} \
} while (0)
#define VIR_TEST_MAIN_PRELOAD(func, ...) \
int main(int argc, char **argv) { \
return virTestMain(argc, argv, func, __VA_ARGS__, NULL); \
}
virCapsPtr virTestGenericCapsInit(void);
int virTestCapsBuildNUMATopology(virCapsPtr caps,
int seq);
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);
typedef enum {
TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS,
TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_PARSE,
TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_STABILITY,
TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_FORMAT,
TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_COMPARE,
} testCompareDomXML2XMLResult;
int testCompareDomXML2XMLFiles(virCapsPtr caps,
virDomainXMLOptionPtr xmlopt,
const char *inxml,
const char *outfile,
bool live,
unsigned int parseFlags,
testCompareDomXML2XMLResult expectResult);