testutils.h 5.8 KB
Newer Older
K
Karel Zak 已提交
1
/*
2
 * testutils.h: test utils
K
Karel Zak 已提交
3
 *
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 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
void virtTestClearCommandPath(char *cmdset);

59 60 61
int virtTestDifference(FILE *stream,
                       const char *expect,
                       const char *actual);
62 63 64 65 66
int virtTestDifferenceFull(FILE *stream,
                           const char *expect,
                           const char *expectName,
                           const char *actual,
                           const char *actualName);
67 68 69 70 71
int virtTestDifferenceFullNoRegenerate(FILE *stream,
                                       const char *expect,
                                       const char *expectName,
                                       const char *actual,
                                       const char *actualName);
72 73 74 75
int virtTestDifferenceBin(FILE *stream,
                          const char *expect,
                          const char *actual,
                          size_t length);
C
Cole Robinson 已提交
76 77
int virtTestCompareToFile(const char *strcontent,
                          const char *filename);
K
Karel Zak 已提交
78

79 80
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
81
unsigned int virTestGetExpensive(void);
82
unsigned int virTestGetRegenerate(void);
83

84 85 86 87 88 89 90 91 92 93 94 95
# 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)

96 97
char *virtTestLogContentAndReset(void);

98 99
void virtTestQuiesceLibvirtErrors(bool always);

100 101 102
void virtTestCounterReset(const char *prefix);
const char *virtTestCounterNext(void);

103 104
int virtTestMain(int argc,
                 char **argv,
E
Eric Blake 已提交
105
                 int (*func)(void));
106

E
Eric Blake 已提交
107 108 109 110
/* Setup, then call func() */
# define VIRT_TEST_MAIN(func)                   \
    int main(int argc, char **argv) {           \
        return virtTestMain(argc, argv, func);  \
111 112
    }

113 114 115 116 117
# 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;                                               \
118 119 120 121
            if (!virFileIsExecutable(lib)) {                            \
                perror(lib);                                            \
                return EXIT_FAILURE;                                    \
            }                                                           \
122 123 124
            if (!preload) {                                             \
                newenv = (char *) lib;                                  \
            } else if (virAsprintf(&newenv, "%s:%s", lib, preload) < 0) {   \
125
                perror("virAsprintf");                                  \
126
                return EXIT_FAILURE;                                    \
127 128 129 130 131 132 133
            }                                                           \
            setenv("LD_PRELOAD", newenv, 1);                            \
            execv(argv[0], argv);                                       \
        }                                                               \
        return virtTestMain(argc, argv, func);                          \
    }

134 135 136
virCapsPtr virTestGenericCapsInit(void);
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);

137 138
typedef int (*testCompareDomXML2XMLPreFormatCallback)(virDomainDefPtr def,
                                                      const void *opaque);
139 140 141 142
int testCompareDomXML2XMLFiles(virCapsPtr caps,
                               virDomainXMLOptionPtr xmlopt,
                               const char *inxml,
                               const char *outfile,
143 144
                               bool live,
                               testCompareDomXML2XMLPreFormatCallback cb,
145 146
                               const void *opaque,
                               unsigned int parseFlags);
147

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