pacemaker 2.1.8-2.1.8~rc1
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
unittest_internal.h
Go to the documentation of this file.
1/*
2 * Copyright 2022-2024 the Pacemaker project contributors
3 *
4 * The version control history for this file may have further details.
5 *
6 * This source code is licensed under the GNU Lesser General Public License
7 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8 */
9
10#include <signal.h>
11#include <stdarg.h>
12#include <stddef.h>
13#include <stdint.h>
14#include <setjmp.h>
15#include <sys/resource.h>
16#include <sys/types.h>
17#include <sys/wait.h>
18#include <unistd.h>
19
20#include <cmocka.h>
21
22#include <crm/common/xml.h>
23
24#ifndef CRM_COMMON_UNITTEST_INTERNAL__H
25#define CRM_COMMON_UNITTEST_INTERNAL__H
26
27/* internal unit testing related utilities */
28
29#if (PCMK__WITH_COVERAGE == 1)
30/* This function isn't exposed anywhere. The following prototype was taken from
31 * /usr/lib/gcc/x86_64-redhat-linux/??/include/gcov.h
32 */
33extern void __gcov_dump(void);
34#else
35#define __gcov_dump()
36#endif
37
51void pcmk__assert_validates(xmlNode *xml);
52
63int pcmk__xml_test_setup_group(void **state);
64
81char *pcmk__cib_test_copy_cib(const char *in_file);
82
97void pcmk__cib_test_cleanup(char *out_path);
98
119#define pcmk__assert_asserts(stmt) \
120 do { \
121 pid_t p = fork(); \
122 if (p == 0) { \
123 struct rlimit cores = { 0, 0 }; \
124 setrlimit(RLIMIT_CORE, &cores); \
125 stmt; \
126 __gcov_dump(); \
127 _exit(0); \
128 } else if (p > 0) { \
129 int wstatus = 0; \
130 if (waitpid(p, &wstatus, 0) == -1) { \
131 fail_msg("waitpid failed"); \
132 } \
133 if (!(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT)) { \
134 fail_msg("statement terminated in child without asserting"); \
135 } \
136 } else { \
137 fail_msg("unable to fork for assert test"); \
138 } \
139 } while (0);
140
148#define pcmk__assert_aborts(stmt) pcmk__assert_asserts(stmt)
149
165#define pcmk__assert_exits(rc, stmt) \
166 do { \
167 pid_t p = fork(); \
168 if (p == 0) { \
169 struct rlimit cores = { 0, 0 }; \
170 setrlimit(RLIMIT_CORE, &cores); \
171 stmt; \
172 __gcov_dump(); \
173 _exit(CRM_EX_NONE); \
174 } else if (p > 0) { \
175 int wstatus = 0; \
176 if (waitpid(p, &wstatus, 0) == -1) { \
177 fail_msg("waitpid failed"); \
178 } \
179 if (!WIFEXITED(wstatus)) { \
180 fail_msg("statement terminated abnormally"); \
181 } else if (WEXITSTATUS(wstatus) != rc) { \
182 fail_msg("statement exited with %d, not expected %d", WEXITSTATUS(wstatus), rc); \
183 } \
184 } else { \
185 fail_msg("unable to fork for assert test"); \
186 } \
187 } while (0);
188
189/* Generate the main function of most unit test files. Typically, group_setup
190 * and group_teardown will be NULL. The rest of the arguments are a list of
191 * calls to cmocka_unit_test or cmocka_unit_test_setup_teardown to run the
192 * individual unit tests.
193 */
194#define PCMK__UNIT_TEST(group_setup, group_teardown, ...) \
195int \
196main(int argc, char **argv) \
197{ \
198 const struct CMUnitTest t[] = { \
199 __VA_ARGS__ \
200 }; \
201 cmocka_set_message_output(CM_OUTPUT_TAP); \
202 return cmocka_run_group_tests(t, group_setup, group_teardown); \
203}
204
205#endif /* CRM_COMMON_UNITTEST_INTERNAL__H */
void pcmk__cib_test_cleanup(char *out_path)
Definition unittest.c:121
#define __gcov_dump()
int pcmk__xml_test_setup_group(void **state)
Definition unittest.c:74
void pcmk__assert_validates(xmlNode *xml)
Definition unittest.c:20
char * pcmk__cib_test_copy_cib(const char *in_file)
Definition unittest.c:86
Wrappers for and extensions to libxml2.