testutils.h 6.5 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
bool virTestOOMActive(void);
50

51 52 53
int virTestRun(const char *title,
               int (*body)(const void *data),
               const void *data);
54
int virTestLoadFile(const char *file, char **buf);
55
int virTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);
56

57
void virTestClearCommandPath(char *cmdset);
58

59 60 61
int virTestDifference(FILE *stream,
                      const char *expect,
                      const char *actual);
62 63 64 65 66
int virTestDifferenceFull(FILE *stream,
                          const char *expect,
                          const char *expectName,
                          const char *actual,
                          const char *actualName);
67 68 69 70 71
int virTestDifferenceFullNoRegenerate(FILE *stream,
                                      const char *expect,
                                      const char *expectName,
                                      const char *actual,
                                      const char *actualName);
72 73 74 75
int virTestDifferenceBin(FILE *stream,
                         const char *expect,
                         const char *actual,
                         size_t length);
76 77
int virTestCompareToFile(const char *strcontent,
                         const char *filename);
J
Jim Fehlig 已提交
78 79
int virTestCompareToString(const char *strcontent,
                           const char *strsrc);
80 81
int virTestCompareToULL(unsigned long long content,
                        unsigned long long src);
K
Karel Zak 已提交
82

83 84
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
85
unsigned int virTestGetExpensive(void);
86
unsigned int virTestGetRegenerate(void);
87

88 89 90 91 92 93 94 95 96 97 98 99
# 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)

100
char *virTestLogContentAndReset(void);
101

102
void virTestQuiesceLibvirtErrors(bool always);
103

104
void virTestCounterReset(const char *prefix);
105
const char *virTestCounterNext(void);
106

107 108 109 110
int virTestMain(int argc,
                char **argv,
                int (*func)(void),
                ...);
111

E
Eric Blake 已提交
112
/* Setup, then call func() */
113
# define VIR_TEST_MAIN(func)                            \
114
    int main(int argc, char **argv) {                   \
115
        return virTestMain(argc, argv, func, NULL);     \
116 117
    }

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

138
# define VIR_TEST_MAIN_PRELOAD(func, ...)                               \
139
    int main(int argc, char **argv) {                                   \
140
        return virTestMain(argc, argv, func, __VA_ARGS__, NULL);        \
141 142
    }

143 144 145
virCapsPtr virTestGenericCapsInit(void);
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);

146 147 148 149 150 151 152 153 154
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;

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

167
#endif /* __VIR_TEST_UTILS_H__ */