pacemaker  3.0.0-d8340737c4
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 #ifndef PCMK__CRM_COMMON_UNITTEST_INTERNAL__H
11 #define PCMK__CRM_COMMON_UNITTEST_INTERNAL__H
12 
13 #include <signal.h>
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <setjmp.h>
18 #include <sys/resource.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 
23 #include <cmocka.h>
24 
25 #include <crm/common/xml.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* internal unit testing related utilities */
32 
33 #if (PCMK__WITH_COVERAGE == 1)
34 /* This function isn't exposed anywhere. The following prototype was taken from
35  * /usr/lib/gcc/x86_64-redhat-linux/??/include/gcov.h
36  */
37 extern void __gcov_dump(void);
38 #else
39 #define __gcov_dump()
40 #endif
41 
55 void pcmk__assert_validates(xmlNode *xml);
56 
67 int pcmk__xml_test_setup_group(void **state);
68 
69 int pcmk__xml_test_teardown_group(void **state);
70 
87 char *pcmk__cib_test_copy_cib(const char *in_file);
88 
103 void pcmk__cib_test_cleanup(char *out_path);
104 
105 void pcmk__test_init_logging(const char *name, const char *filename);
106 
128 #define pcmk__assert_asserts(stmt) \
129  do { \
130  pid_t p = fork(); \
131  if (p == 0) { \
132  struct rlimit cores = { 0, 0 }; \
133  setrlimit(RLIMIT_CORE, &cores); \
134  stmt; \
135  __gcov_dump(); \
136  _exit(0); \
137  } else if (p > 0) { \
138  int wstatus = 0; \
139  if (waitpid(p, &wstatus, 0) == -1) { \
140  fail_msg("waitpid failed"); \
141  } \
142  if (!(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT)) { \
143  fail_msg("statement terminated in child without asserting"); \
144  } \
145  } else { \
146  fail_msg("unable to fork for assert test"); \
147  } \
148  } while (0);
149 
157 #define pcmk__assert_aborts(stmt) pcmk__assert_asserts(stmt)
158 
174 #define pcmk__assert_exits(rc, stmt) \
175  do { \
176  pid_t p = fork(); \
177  if (p == 0) { \
178  struct rlimit cores = { 0, 0 }; \
179  setrlimit(RLIMIT_CORE, &cores); \
180  stmt; \
181  __gcov_dump(); \
182  _exit(CRM_EX_NONE); \
183  } else if (p > 0) { \
184  int wstatus = 0; \
185  if (waitpid(p, &wstatus, 0) == -1) { \
186  fail_msg("waitpid failed"); \
187  } \
188  if (!WIFEXITED(wstatus)) { \
189  fail_msg("statement terminated abnormally"); \
190  } else if (WEXITSTATUS(wstatus) != rc) { \
191  fail_msg("statement exited with %d, not expected %d", WEXITSTATUS(wstatus), rc); \
192  } \
193  } else { \
194  fail_msg("unable to fork for assert test"); \
195  } \
196  } while (0);
197 
198 /* Generate the main function of most unit test files. Typically, group_setup
199  * and group_teardown will be NULL. The rest of the arguments are a list of
200  * calls to cmocka_unit_test or cmocka_unit_test_setup_teardown to run the
201  * individual unit tests.
202  */
203 #define PCMK__UNIT_TEST(group_setup, group_teardown, ...) \
204 int \
205 main(int argc, char **argv) \
206 { \
207  const struct CMUnitTest t[] = { \
208  __VA_ARGS__ \
209  }; \
210  cmocka_set_message_output(CM_OUTPUT_TAP); \
211  return cmocka_run_group_tests(t, group_setup, group_teardown); \
212 }
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 
218 #endif // PCMK__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