testutils.h 6.7 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
# include "virjson.h"
31 32
# include "capabilities.h"
# include "domain_conf.h"
33

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

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

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

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

50
bool virTestOOMActive(void);
51

52 53 54
int virTestRun(const char *title,
               int (*body)(const void *data),
               const void *data);
55
int virTestLoadFile(const char *file, char **buf);
56 57
char *virTestLoadFilePath(const char *p, ...)
    ATTRIBUTE_SENTINEL;
58 59 60
virJSONValuePtr virTestLoadFileJSON(const char *p, ...)
    ATTRIBUTE_SENTINEL;

61
int virTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);
62

63
void virTestClearCommandPath(char *cmdset);
64

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

89 90
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
91
unsigned int virTestGetExpensive(void);
92
unsigned int virTestGetRegenerate(void);
93

94 95 96 97 98 99 100 101 102 103 104 105
# 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)

106
char *virTestLogContentAndReset(void);
107

108
void virTestQuiesceLibvirtErrors(bool always);
109

110
void virTestCounterReset(const char *prefix);
111
const char *virTestCounterNext(void);
112

113 114 115 116
int virTestMain(int argc,
                char **argv,
                int (*func)(void),
                ...);
117

E
Eric Blake 已提交
118
/* Setup, then call func() */
119
# define VIR_TEST_MAIN(func)                            \
120
    int main(int argc, char **argv) {                   \
121
        return virTestMain(argc, argv, func, NULL);     \
122 123
    }

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

144
# define VIR_TEST_MAIN_PRELOAD(func, ...)                               \
145
    int main(int argc, char **argv) {                                   \
146
        return virTestMain(argc, argv, func, __VA_ARGS__, NULL);        \
147 148
    }

149 150 151
virCapsPtr virTestGenericCapsInit(void);
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);

152 153 154 155 156 157 158 159 160
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;

161 162
typedef int (*testCompareDomXML2XMLPreFormatCallback)(virDomainDefPtr def,
                                                      const void *opaque);
163 164 165 166
int testCompareDomXML2XMLFiles(virCapsPtr caps,
                               virDomainXMLOptionPtr xmlopt,
                               const char *inxml,
                               const char *outfile,
167 168
                               bool live,
                               testCompareDomXML2XMLPreFormatCallback cb,
169
                               const void *opaque,
170 171
                               unsigned int parseFlags,
                               testCompareDomXML2XMLResult expectResult);
172

173
#endif /* __VIR_TEST_UTILS_H__ */