testutils.h 4.0 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
# define EXIT_AM_SKIP 77 /* tell Automake we're skipping a test */
E
Eric Blake 已提交
32 33
# define EXIT_AM_HARDFAIL 99 /* tell Automake that the framework is broken */

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

E
Eric Blake 已提交
40
extern char *progname;
41 42 43 44 45

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

47 48
bool virtTestOOMActive(void);

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

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

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

68 69
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
70
unsigned int virTestGetExpensive(void);
71

72 73
char *virtTestLogContentAndReset(void);

74 75
void virtTestQuiesceLibvirtErrors(bool always);

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

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

86 87 88 89 90
# 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;                                               \
91 92 93 94
            if (!virFileIsExecutable(lib)) {                            \
                perror(lib);                                            \
                return EXIT_FAILURE;                                    \
            }                                                           \
95 96 97
            if (!preload) {                                             \
                newenv = (char *) lib;                                  \
            } else if (virAsprintf(&newenv, "%s:%s", lib, preload) < 0) {   \
98
                perror("virAsprintf");                                  \
99
                return EXIT_FAILURE;                                    \
100 101 102 103 104 105 106
            }                                                           \
            setenv("LD_PRELOAD", newenv, 1);                            \
            execv(argv[0], argv);                                       \
        }                                                               \
        return virtTestMain(argc, argv, func);                          \
    }

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