pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
unittest.c
Go to the documentation of this file.
1/*
2 * Copyright 2024-2025 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#include <libxml/parser.h> // xmlCleanupParser()
18
19// LCOV_EXCL_START
20
21void
23{
24 const char *schema_dir = NULL;
25 char *cmd = NULL;
26 gchar *out = NULL;
27 gchar *err = NULL;
28 gint status;
29 GError *gerr = NULL;
30 char *xmllint_input = crm_strdup_printf("%s/test-xmllint.XXXXXX",
32 int fd;
33 int rc;
34
35 fd = mkstemp(xmllint_input);
36 if (fd < 0) {
37 fail_msg("Could not create temp file: %s", strerror(errno));
38 }
39
40 rc = pcmk__xml2fd(fd, xml);
41 if (rc != pcmk_rc_ok) {
42 unlink(xmllint_input);
43 fail_msg("Could not write temp file: %s", pcmk_rc_str(rc));
44 }
45
46 close(fd);
47
48 /* This should be set as part of AM_TESTS_ENVIRONMENT in Makefile.am. */
49 schema_dir = getenv("PCMK_schema_directory");
50 if (schema_dir == NULL) {
51 unlink(xmllint_input);
52 fail_msg("PCMK_schema_directory is not set in test environment");
53 }
54
55 cmd = crm_strdup_printf("xmllint --relaxng %s/api/api-result.rng %s",
56 schema_dir, xmllint_input);
57
58 if (!g_spawn_command_line_sync(cmd, &out, &err, &status, &gerr)) {
59 unlink(xmllint_input);
60 fail_msg("Error occurred when performing validation: %s", gerr->message);
61 }
62
63 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
64 unlink(xmllint_input);
65 fail_msg("XML validation failed: %s\n%s\n", out, err);
66 }
67
68 free(cmd);
69 g_free(out);
70 g_free(err);
71 unlink(xmllint_input);
72 free(xmllint_input);
73}
74
86int
88{
90 return 0;
91}
92
104int
106{
108 xmlCleanupParser();
109 return 0;
110}
111
112char *
113pcmk__cib_test_copy_cib(const char *in_file)
114{
115 char *in_path = crm_strdup_printf("%s/%s", getenv("PCMK_CTS_CLI_DIR"), in_file);
116 char *out_path = NULL;
117 char *contents = NULL;
118 int fd;
119
120 /* Copy the CIB over to a temp location so we can modify it. */
121 out_path = crm_strdup_printf("%s/test-cib.XXXXXX", pcmk__get_tmpdir());
122
123 fd = mkstemp(out_path);
124 if (fd < 0) {
125 free(out_path);
126 return NULL;
127 }
128
129 if (pcmk__file_contents(in_path, &contents) != pcmk_rc_ok) {
130 free(out_path);
131 close(fd);
132 return NULL;
133 }
134
135 if (pcmk__write_sync(fd, contents) != pcmk_rc_ok) {
136 free(out_path);
137 free(in_path);
138 free(contents);
139 close(fd);
140 return NULL;
141 }
142
143 setenv("CIB_file", out_path, 1);
144 return out_path;
145}
146
147void
149{
150 unlink(out_path);
151 free(out_path);
152 unsetenv("CIB_file");
153}
154
164void
165pcmk__test_init_logging(const char *name, const char *filename)
166{
168 if (filename != NULL) {
169 pcmk__add_logfile(filename);
170 set_crm_log_level(LOG_DEBUG);
171 }
172}
173
174// LCOV_EXCL_STOP
const char * name
Definition cib.c:26
int pcmk__file_contents(const char *filename, char **contents)
Definition io.c:426
const char * pcmk__get_tmpdir(void)
Definition io.c:548
int pcmk__write_sync(int fd, const char *contents)
Definition io.c:495
unsigned int set_crm_log_level(unsigned int level)
Definition logging.c:1057
int pcmk__add_logfile(const char *filename)
Add a file to be used as a Pacemaker detail log.
Definition logging.c:331
void pcmk__cli_init_logging(const char *name, unsigned int verbosity)
Definition logging.c:1164
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
Definition results.c:617
@ pcmk_rc_ok
Definition results.h:159
void pcmk__schema_cleanup(void)
Definition schemas.c:712
void pcmk__schema_init(void)
Definition schemas.c:550
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
void pcmk__test_init_logging(const char *name, const char *filename)
Definition unittest.c:165
void pcmk__cib_test_cleanup(char *out_path)
Definition unittest.c:148
int pcmk__xml_test_teardown_group(void **state)
Definition unittest.c:105
int pcmk__xml_test_setup_group(void **state)
Definition unittest.c:87
void pcmk__assert_validates(xmlNode *xml)
Definition unittest.c:22
char * pcmk__cib_test_copy_cib(const char *in_file)
Definition unittest.c:113
int pcmk__xml2fd(int fd, xmlNode *cur)
Definition xml_io.c:583