testutils.h 5.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 "viralloc.h"
27 28
# include "virfile.h"
# include "virstring.h"
29
# include "virjson.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 56
char *virTestLoadFilePath(const char *p, ...)
    ATTRIBUTE_SENTINEL;
57 58 59
virJSONValuePtr virTestLoadFileJSON(const char *p, ...)
    ATTRIBUTE_SENTINEL;

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

62
void virTestClearCommandPath(char *cmdset);
63

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

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

93 94 95 96
# define VIR_TEST_DEBUG(...) \
    do { \
        if (virTestGetDebug()) \
            fprintf(stderr, __VA_ARGS__); \
97 98
    } while (0)

99 100 101 102
# define VIR_TEST_VERBOSE(...) \
    do { \
        if (virTestGetVerbose()) \
            fprintf(stderr, __VA_ARGS__); \
103 104
    } while (0)

105
char *virTestLogContentAndReset(void);
106

107
void virTestQuiesceLibvirtErrors(bool always);
108

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

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

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

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

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

148
virCapsPtr virTestGenericCapsInit(void);
149 150
int virTestCapsBuildNUMATopology(virCapsPtr caps,
                                 int seq);
151 152
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);

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_FORMAT,
    TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_COMPARE,
} testCompareDomXML2XMLResult;

161 162 163 164
int testCompareDomXML2XMLFiles(virCapsPtr caps,
                               virDomainXMLOptionPtr xmlopt,
                               const char *inxml,
                               const char *outfile,
165
                               bool live,
166 167
                               unsigned int parseFlags,
                               testCompareDomXML2XMLResult expectResult);
168

169
#endif /* __VIR_TEST_UTILS_H__ */