testutils.h 4.1 KB
Newer Older
K
Karel Zak 已提交
1 2 3
/*
 * utils.c: test utils
 *
4
 * Copyright (C) 2005, 2008-2013 Red Hat, Inc.
K
Karel Zak 已提交
5
 *
O
Osier Yang 已提交
6 7 8 9 10 11 12 13 14 15 16
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
K
Karel Zak 已提交
19 20 21 22 23
 *
 * Karel Zak <kzak@redhat.com>
 */

#ifndef __VIT_TEST_UTILS_H__
24
# define __VIT_TEST_UTILS_H__
K
Karel Zak 已提交
25

26
# include <stdio.h>
27
# include "viralloc.h"
28 29
# include "virfile.h"
# include "virstring.h"
30 31
# include "capabilities.h"
# include "domain_conf.h"
32

33
# define EXIT_AM_SKIP 77 /* tell Automake we're skipping a test */
E
Eric Blake 已提交
34 35
# define EXIT_AM_HARDFAIL 99 /* tell Automake that the framework is broken */

36
/* Work around lack of gnulib support for fprintf %z */
E
Eric Blake 已提交
37 38 39 40
# ifndef NO_LIBVIRT
#  undef fprintf
#  define fprintf virFilePrintf
# endif
41

E
Eric Blake 已提交
42
extern char *progname;
43 44 45 46 47

/* Makefile.am provides these two definitions */
# if !defined(abs_srcdir) || !defined(abs_builddir)
#  error Fix Makefile.am
# endif
48

49 50
bool virtTestOOMActive(void);

51 52
void virtTestResult(const char *name, int ret, const char *msg, ...)
    ATTRIBUTE_FMT_PRINTF(3,4);
53 54 55
int virtTestRun(const char *title,
                int (*body)(const void *data),
                const void *data);
56 57
int virtTestLoadFile(const char *file, char **buf);
int virtTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);
58

59 60
int virtTestClearLineRegex(const char *pattern,
                           char *string);
K
Karel Zak 已提交
61

62 63 64
int virtTestDifference(FILE *stream,
                       const char *expect,
                       const char *actual);
65 66 67 68
int virtTestDifferenceBin(FILE *stream,
                          const char *expect,
                          const char *actual,
                          size_t length);
K
Karel Zak 已提交
69

70 71
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
72
unsigned int virTestGetExpensive(void);
73

74 75
char *virtTestLogContentAndReset(void);

76 77
void virtTestQuiesceLibvirtErrors(bool always);

78 79
int virtTestMain(int argc,
                 char **argv,
E
Eric Blake 已提交
80
                 int (*func)(void));
81

E
Eric Blake 已提交
82 83 84 85
/* Setup, then call func() */
# define VIRT_TEST_MAIN(func)                   \
    int main(int argc, char **argv) {           \
        return virtTestMain(argc, argv, func);  \
86 87
    }

88 89 90 91 92
# define VIRT_TEST_MAIN_PRELOAD(func, lib)                              \
    int main(int argc, char **argv) {                                   \
        const char *preload = getenv("LD_PRELOAD");                     \
        if (preload == NULL || strstr(preload, lib) == NULL) {          \
            char *newenv;                                               \
93 94 95 96
            if (!virFileIsExecutable(lib)) {                            \
                perror(lib);                                            \
                return EXIT_FAILURE;                                    \
            }                                                           \
97 98 99
            if (!preload) {                                             \
                newenv = (char *) lib;                                  \
            } else if (virAsprintf(&newenv, "%s:%s", lib, preload) < 0) {   \
100
                perror("virAsprintf");                                  \
101
                return EXIT_FAILURE;                                    \
102 103 104 105 106 107 108
            }                                                           \
            setenv("LD_PRELOAD", newenv, 1);                            \
            execv(argv[0], argv);                                       \
        }                                                               \
        return virtTestMain(argc, argv, func);                          \
    }

109 110 111
virCapsPtr virTestGenericCapsInit(void);
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);

K
Karel Zak 已提交
112
#endif /* __VIT_TEST_UTILS_H__ */