pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
unittest.c
Go to the documentation of this file.
1 /*
2  * Copyright 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 <crm_internal.h>
11 
13 
14 #include <stdlib.h>
15 #include <unistd.h>
16 
17 // LCOV_EXCL_START
18 
19 void
21 {
22  const char *schema_dir = NULL;
23  char *cmd = NULL;
24  gchar *out = NULL;
25  gchar *err = NULL;
26  gint status;
27  GError *gerr = NULL;
28  char *xmllint_input = crm_strdup_printf("%s/test-xmllint.XXXXXX",
30  int fd;
31  int rc;
32 
33  fd = mkstemp(xmllint_input);
34  if (fd < 0) {
35  fail_msg("Could not create temp file: %s", strerror(errno));
36  }
37 
38  rc = pcmk__xml2fd(fd, xml);
39  if (rc != pcmk_rc_ok) {
40  unlink(xmllint_input);
41  fail_msg("Could not write temp file: %s", pcmk_rc_str(rc));
42  }
43 
44  close(fd);
45 
46  /* This should be set as part of AM_TESTS_ENVIRONMENT in Makefile.am. */
47  schema_dir = getenv("PCMK_schema_directory");
48  if (schema_dir == NULL) {
49  unlink(xmllint_input);
50  fail_msg("PCMK_schema_directory is not set in test environment");
51  }
52 
53  cmd = crm_strdup_printf("xmllint --relaxng %s/api/api-result.rng %s",
54  schema_dir, xmllint_input);
55 
56  if (!g_spawn_command_line_sync(cmd, &out, &err, &status, &gerr)) {
57  unlink(xmllint_input);
58  fail_msg("Error occurred when performing validation: %s", gerr->message);
59  }
60 
61  if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
62  unlink(xmllint_input);
63  fail_msg("XML validation failed: %s\n%s\n", out, err);
64  }
65 
66  free(cmd);
67  g_free(out);
68  g_free(err);
69  unlink(xmllint_input);
70  free(xmllint_input);
71 }
72 
73 int
75 {
76  /* This needs to be run before we run unit tests that manipulate XML.
77  * Without it, document private data won't get created, which can cause
78  * segmentation faults or assertions in functions related to change
79  * tracking and ACLs. There's no harm in doing this before all tests.
80  */
81  crm_xml_init();
82  return 0;
83 }
84 
85 char *
86 pcmk__cib_test_copy_cib(const char *in_file)
87 {
88  char *in_path = crm_strdup_printf("%s/%s", getenv("PCMK_CTS_CLI_DIR"), in_file);
89  char *out_path = NULL;
90  char *contents = NULL;
91  int fd;
92 
93  /* Copy the CIB over to a temp location so we can modify it. */
94  out_path = crm_strdup_printf("%s/test-cib.XXXXXX", pcmk__get_tmpdir());
95 
96  fd = mkstemp(out_path);
97  if (fd < 0) {
98  free(out_path);
99  return NULL;
100  }
101 
102  if (pcmk__file_contents(in_path, &contents) != pcmk_rc_ok) {
103  free(out_path);
104  close(fd);
105  return NULL;
106  }
107 
108  if (pcmk__write_sync(fd, contents) != pcmk_rc_ok) {
109  free(out_path);
110  free(in_path);
111  free(contents);
112  close(fd);
113  return NULL;
114  }
115 
116  setenv("CIB_file", out_path, 1);
117  return out_path;
118 }
119 
120 void
121 pcmk__cib_test_cleanup(char *out_path)
122 {
123  unlink(out_path);
124  free(out_path);
125  unsetenv("CIB_file");
126 }
127 
137 void
138 pcmk__test_init_logging(const char *name, const char *filename)
139 {
141  if (filename != NULL) {
142  pcmk__add_logfile(filename);
143  set_crm_log_level(LOG_DEBUG);
144  }
145 }
146 
147 // LCOV_EXCL_STOP
void pcmk__assert_validates(xmlNode *xml)
Definition: unittest.c:20
void pcmk__cib_test_cleanup(char *out_path)
Definition: unittest.c:121
const char * name
Definition: cib.c:26
void pcmk__test_init_logging(const char *name, const char *filename)
Definition: unittest.c:138
void crm_xml_init(void)
Initialize the CRM XML subsystem.
Definition: xml.c:2121
char * pcmk__cib_test_copy_cib(const char *in_file)
Definition: unittest.c:86
int pcmk__add_logfile(const char *filename)
Add a file to be used as a Pacemaker detail log.
Definition: logging.c:317
int pcmk__write_sync(int fd, const char *contents)
Definition: io.c:494
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
Definition: results.c:501
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
void pcmk__cli_init_logging(const char *name, unsigned int verbosity)
Definition: logging.c:1135
unsigned int set_crm_log_level(unsigned int level)
Definition: logging.c:1028
const char * pcmk__get_tmpdir(void)
Definition: io.c:547
int pcmk__file_contents(const char *filename, char **contents)
Definition: io.c:432
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:74
int pcmk__xml2fd(int fd, xmlNode *cur)
Definition: xml_io.c:713