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
#pragma once
K
Karel Zak 已提交
22

23 24 25 26 27 28
#include "viralloc.h"
#include "virfile.h"
#include "virstring.h"
#include "virjson.h"
#include "capabilities.h"
#include "domain_conf.h"
29

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

33
/* Work around lack of gnulib support for fprintf %z */
34 35 36 37
#ifndef NO_LIBVIRT
# undef fprintf
# define fprintf virFilePrintf
#endif
38

E
Eric Blake 已提交
39
extern char *progname;
40 41

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

46 47
extern virArch virTestHostArch;

48 49 50
int virTestRun(const char *title,
               int (*body)(const void *data),
               const void *data);
51
int virTestLoadFile(const char *file, char **buf);
52
char *virTestLoadFilePath(const char *p, ...)
53
    G_GNUC_NULL_TERMINATED;
54
virJSONValuePtr virTestLoadFileJSON(const char *p, ...)
55
    G_GNUC_NULL_TERMINATED;
56

57
int virTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);
58

59
void virTestClearCommandPath(char *cmdset);
60

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

85 86
unsigned int virTestGetDebug(void);
unsigned int virTestGetVerbose(void);
87
unsigned int virTestGetExpensive(void);
88
unsigned int virTestGetRegenerate(void);
89
void virTestPropagateLibvirtError(void);
90

91
#define VIR_TEST_DEBUG(fmt, ...) \
92 93
    do { \
        if (virTestGetDebug()) \
94
            fprintf(stderr, fmt "\n", ## __VA_ARGS__); \
95 96
    } while (0)

97
#define VIR_TEST_VERBOSE(fmt, ...) \
98 99
    do { \
        if (virTestGetVerbose()) \
100
            fprintf(stderr, fmt "\n", ## __VA_ARGS__); \
101 102
    } while (0)

103
char *virTestLogContentAndReset(void);
104

105
void virTestQuiesceLibvirtErrors(bool always);
106

107
void virTestCounterReset(const char *prefix);
108
const char *virTestCounterNext(void);
109

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

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

121 122
#ifdef __APPLE__
# define PRELOAD_VAR "DYLD_INSERT_LIBRARIES"
123 124
# define FORCE_FLAT_NAMESPACE \
            setenv("DYLD_FORCE_FLAT_NAMESPACE", "1", 1);
125
# define MOCK_EXT ".dylib"
126 127
#else
# define PRELOAD_VAR "LD_PRELOAD"
128
# define FORCE_FLAT_NAMESPACE
129
# define MOCK_EXT ".so"
130 131
#endif

132
#define VIR_TEST_PRELOAD(lib) \
133
    do { \
134
        const char *preload = getenv(PRELOAD_VAR); \
135 136 137 138 139 140 141 142
        if (preload == NULL || strstr(preload, lib) == NULL) { \
            char *newenv; \
            if (!virFileIsExecutable(lib)) { \
                perror(lib); \
                return EXIT_FAILURE; \
            } \
            if (!preload) { \
                newenv = (char *) lib; \
143 144
            } else { \
                newenv = g_strdup_printf("%s:%s", lib, preload); \
145
            } \
146
            setenv(PRELOAD_VAR, newenv, 1); \
147
            FORCE_FLAT_NAMESPACE \
148 149
            execv(argv[0], argv); \
        } \
150 151
    } while (0)

152
#define VIR_TEST_MAIN_PRELOAD(func, ...) \
153 154
    int main(int argc, char **argv) { \
        return virTestMain(argc, argv, func, __VA_ARGS__, NULL); \
155 156
    }

157 158
#define VIR_TEST_MOCK(mock) (abs_builddir "/.libs/lib" mock "mock" MOCK_EXT)

159
virCapsPtr virTestGenericCapsInit(void);
160 161
int virTestCapsBuildNUMATopology(virCapsPtr caps,
                                 int seq);
162 163
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);

164 165 166 167 168 169 170 171
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;

172 173 174 175
int testCompareDomXML2XMLFiles(virCapsPtr caps,
                               virDomainXMLOptionPtr xmlopt,
                               const char *inxml,
                               const char *outfile,
176
                               bool live,
177 178
                               unsigned int parseFlags,
                               testCompareDomXML2XMLResult expectResult);