testutils.h 5.6 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

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

44 45
extern virArch virTestHostArch;

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

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
int virTestCompareToFile(const char *actual,
77
                         const char *filename);
78 79 80 81
int virTestCompareToString(const char *expect,
                           const char *actual);
int virTestCompareToULL(unsigned long long expect,
                        unsigned long long actual);
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
void virTestPropagateLibvirtError(void);
88

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

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

101
char *virTestLogContentAndReset(void);
102

103
void virTestQuiesceLibvirtErrors(bool always);
104

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

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

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

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

130
#define VIR_TEST_PRELOAD(libs) \
131
    do { \
132
        const char *preload = getenv(PRELOAD_VAR); \
133
        if (preload == NULL || strstr(preload, libs) == NULL) { \
134 135
            char *newenv; \
            if (!preload) { \
136
                newenv = (char *) libs; \
137
            } else { \
138
                newenv = g_strdup_printf("%s:%s", libs, preload); \
139
            } \
140
            g_setenv(PRELOAD_VAR, newenv, TRUE); \
141
            FORCE_FLAT_NAMESPACE \
142 143
            execv(argv[0], argv); \
        } \
144 145
    } while (0)

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

151 152
#define VIR_TEST_MOCK(mock) (abs_builddir "/.libs/lib" mock "mock" MOCK_EXT)

153
virCapsPtr virTestGenericCapsInit(void);
154
virCapsHostNUMAPtr virTestCapsBuildNUMATopology(int seq);
155 156
virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);

157 158 159 160 161 162 163 164
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;

165 166 167 168
int testCompareDomXML2XMLFiles(virCapsPtr caps,
                               virDomainXMLOptionPtr xmlopt,
                               const char *inxml,
                               const char *outfile,
169
                               bool live,
170 171
                               unsigned int parseFlags,
                               testCompareDomXML2XMLResult expectResult);