testutils.h 6.4 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
 *
 * Karel Zak <kzak@redhat.com>
 */

23 24
#ifndef __VIR_TEST_UTILS_H__
# define __VIR_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,
105 106
                 int (*func)(void),
                 ...);
107

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

114 115
# define VIRT_TEST_PRELOAD(lib)                                         \
    do {                                                                \
116 117 118
        const char *preload = getenv("LD_PRELOAD");                     \
        if (preload == NULL || strstr(preload, lib) == NULL) {          \
            char *newenv;                                               \
119 120 121 122
            if (!virFileIsExecutable(lib)) {                            \
                perror(lib);                                            \
                return EXIT_FAILURE;                                    \
            }                                                           \
123 124 125
            if (!preload) {                                             \
                newenv = (char *) lib;                                  \
            } else if (virAsprintf(&newenv, "%s:%s", lib, preload) < 0) {   \
126
                perror("virAsprintf");                                  \
127
                return EXIT_FAILURE;                                    \
128 129 130 131
            }                                                           \
            setenv("LD_PRELOAD", newenv, 1);                            \
            execv(argv[0], argv);                                       \
        }                                                               \
132 133
    } while (0)

134
# define VIRT_TEST_MAIN_PRELOAD(func, ...)                              \
135
    int main(int argc, char **argv) {                                   \
136
        return virtTestMain(argc, argv, func, __VA_ARGS__, NULL);       \
137 138
    }

139 140 141
virCapsPtr virTestGenericCapsInit(void);
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);

142 143 144 145 146 147 148 149 150
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_CB,
    TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_FORMAT,
    TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_COMPARE,
} testCompareDomXML2XMLResult;

151 152
typedef int (*testCompareDomXML2XMLPreFormatCallback)(virDomainDefPtr def,
                                                      const void *opaque);
153 154 155 156
int testCompareDomXML2XMLFiles(virCapsPtr caps,
                               virDomainXMLOptionPtr xmlopt,
                               const char *inxml,
                               const char *outfile,
157 158
                               bool live,
                               testCompareDomXML2XMLPreFormatCallback cb,
159
                               const void *opaque,
160 161
                               unsigned int parseFlags,
                               testCompareDomXML2XMLResult expectResult);
162

163
#endif /* __VIR_TEST_UTILS_H__ */