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 48
int virTestRun(const char *title,
               int (*body)(const void *data),
               const void *data);
49
int virTestLoadFile(const char *file, char **buf);
50 51
char *virTestLoadFilePath(const char *p, ...)
    ATTRIBUTE_SENTINEL;
52 53 54
virJSONValuePtr virTestLoadFileJSON(const char *p, ...)
    ATTRIBUTE_SENTINEL;

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

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

94
#define VIR_TEST_VERBOSE(fmt, ...) \
95 96
    do { \
        if (virTestGetVerbose()) \
97
            fprintf(stderr, fmt "\n", ## __VA_ARGS__); \
98 99
    } 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 115
    int main(int argc, char **argv) { \
        return virTestMain(argc, argv, func, NULL); \
116 117
    }

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

129
#define VIR_TEST_PRELOAD(lib) \
130
    do { \
131
        const char *preload = getenv(PRELOAD_VAR); \
132 133 134 135 136 137 138 139 140 141 142 143
        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; \
            } \
144
            setenv(PRELOAD_VAR, newenv, 1); \
145
            FORCE_FLAT_NAMESPACE \
146 147
            execv(argv[0], argv); \
        } \
148 149
    } while (0)

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

155 156
#define VIR_TEST_MOCK(mock) (abs_builddir "/.libs/lib" mock "mock" MOCK_EXT)

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

162 163 164 165 166 167 168 169
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;

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