pacemaker  2.1.9-49aab99839
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 
53 int pcmk__xml_test_setup_group(void **state);
54 int pcmk__xml_test_teardown_group(void **state);
55 
72 char *pcmk__cib_test_copy_cib(const char *in_file);
73 
88 void pcmk__cib_test_cleanup(char *out_path);
89 
90 void pcmk__test_init_logging(const char *name, const char *filename);
91 
113 #define pcmk__assert_asserts(stmt) \
114  do { \
115  pid_t p = fork(); \
116  if (p == 0) { \
117  struct rlimit cores = { 0, 0 }; \
118  setrlimit(RLIMIT_CORE, &cores); \
119  stmt; \
120  __gcov_dump(); \
121  _exit(0); \
122  } else if (p > 0) { \
123  int wstatus = 0; \
124  if (waitpid(p, &wstatus, 0) == -1) { \
125  fail_msg("waitpid failed"); \
126  } \
127  if (!(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT)) { \
128  fail_msg("statement terminated in child without asserting"); \
129  } \
130  } else { \
131  fail_msg("unable to fork for assert test"); \
132  } \
133  } while (0);
134 
142 #define pcmk__assert_aborts(stmt) pcmk__assert_asserts(stmt)
143 
159 #define pcmk__assert_exits(rc, stmt) \
160  do { \
161  pid_t p = fork(); \
162  if (p == 0) { \
163  struct rlimit cores = { 0, 0 }; \
164  setrlimit(RLIMIT_CORE, &cores); \
165  stmt; \
166  __gcov_dump(); \
167  _exit(CRM_EX_NONE); \
168  } else if (p > 0) { \
169  int wstatus = 0; \
170  if (waitpid(p, &wstatus, 0) == -1) { \
171  fail_msg("waitpid failed"); \
172  } \
173  if (!WIFEXITED(wstatus)) { \
174  fail_msg("statement terminated abnormally"); \
175  } else if (WEXITSTATUS(wstatus) != rc) { \
176  fail_msg("statement exited with %d, not expected %d", WEXITSTATUS(wstatus), rc); \
177  } \
178  } else { \
179  fail_msg("unable to fork for assert test"); \
180  } \
181  } while (0);
182 
183 /* Generate the main function of most unit test files. Typically, group_setup
184  * and group_teardown will be NULL. The rest of the arguments are a list of
185  * calls to cmocka_unit_test or cmocka_unit_test_setup_teardown to run the
186  * individual unit tests.
187  */
188 #define PCMK__UNIT_TEST(group_setup, group_teardown, ...) \
189 int \
190 main(int argc, char **argv) \
191 { \
192  const struct CMUnitTest t[] = { \
193  __VA_ARGS__ \
194  }; \
195  cmocka_set_message_output(CM_OUTPUT_TAP); \
196  return cmocka_run_group_tests(t, group_setup, group_teardown); \
197 }
198 
199 #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:164
#define __gcov_dump()
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
Wrappers for and extensions to libxml2.
int pcmk__xml_test_teardown_group(void **state)
Definition: unittest.c:104
void pcmk__cib_test_cleanup(char *out_path)
Definition: unittest.c:147
void pcmk__assert_validates(xmlNode *xml)
Definition: unittest.c:20
char * pcmk__cib_test_copy_cib(const char *in_file)
Definition: unittest.c:112