pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
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  */
33 extern void __gcov_dump(void);
34 #else
35 #define __gcov_dump()
36 #endif
37 
51 void pcmk__assert_validates(xmlNode *xml);
52 
63 int pcmk__xml_test_setup_group(void **state);
64 
81 char *pcmk__cib_test_copy_cib(const char *in_file);
82 
97 void pcmk__cib_test_cleanup(char *out_path);
98 
99 void pcmk__test_init_logging(const char *name, const char *filename);
100 
121 #define pcmk__assert_asserts(stmt) \
122  do { \
123  pid_t p = fork(); \
124  if (p == 0) { \
125  struct rlimit cores = { 0, 0 }; \
126  setrlimit(RLIMIT_CORE, &cores); \
127  stmt; \
128  __gcov_dump(); \
129  _exit(0); \
130  } else if (p > 0) { \
131  int wstatus = 0; \
132  if (waitpid(p, &wstatus, 0) == -1) { \
133  fail_msg("waitpid failed"); \
134  } \
135  if (!(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT)) { \
136  fail_msg("statement terminated in child without asserting"); \
137  } \
138  } else { \
139  fail_msg("unable to fork for assert test"); \
140  } \
141  } while (0);
142 
150 #define pcmk__assert_aborts(stmt) pcmk__assert_asserts(stmt)
151 
167 #define pcmk__assert_exits(rc, stmt) \
168  do { \
169  pid_t p = fork(); \
170  if (p == 0) { \
171  struct rlimit cores = { 0, 0 }; \
172  setrlimit(RLIMIT_CORE, &cores); \
173  stmt; \
174  __gcov_dump(); \
175  _exit(CRM_EX_NONE); \
176  } else if (p > 0) { \
177  int wstatus = 0; \
178  if (waitpid(p, &wstatus, 0) == -1) { \
179  fail_msg("waitpid failed"); \
180  } \
181  if (!WIFEXITED(wstatus)) { \
182  fail_msg("statement terminated abnormally"); \
183  } else if (WEXITSTATUS(wstatus) != rc) { \
184  fail_msg("statement exited with %d, not expected %d", WEXITSTATUS(wstatus), rc); \
185  } \
186  } else { \
187  fail_msg("unable to fork for assert test"); \
188  } \
189  } while (0);
190 
191 /* Generate the main function of most unit test files. Typically, group_setup
192  * and group_teardown will be NULL. The rest of the arguments are a list of
193  * calls to cmocka_unit_test or cmocka_unit_test_setup_teardown to run the
194  * individual unit tests.
195  */
196 #define PCMK__UNIT_TEST(group_setup, group_teardown, ...) \
197 int \
198 main(int argc, char **argv) \
199 { \
200  const struct CMUnitTest t[] = { \
201  __VA_ARGS__ \
202  }; \
203  cmocka_set_message_output(CM_OUTPUT_TAP); \
204  return cmocka_run_group_tests(t, group_setup, group_teardown); \
205 }
206 
207 #endif /* CRM_COMMON_UNITTEST_INTERNAL__H */
const char * name
Definition: cib.c:26
void pcmk__test_init_logging(const char *name, const char *filename)
Definition: unittest.c:138
#define __gcov_dump()
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:74
Wrappers for and extensions to libxml2.
void pcmk__cib_test_cleanup(char *out_path)
Definition: unittest.c:121
void pcmk__assert_validates(xmlNode *xml)
Definition: unittest.c:20
char * pcmk__cib_test_copy_cib(const char *in_file)
Definition: unittest.c:86