This source file includes following definitions.
- pcmk__assert_validates
- pcmk__xml_test_setup_group
- pcmk__xml_test_teardown_group
- pcmk__cib_test_copy_cib
- pcmk__cib_test_cleanup
- pcmk__test_init_logging
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <crm/common/unittest_internal.h>
13
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #include <libxml/parser.h>
18
19
20
21 void
22 pcmk__assert_validates(xmlNode *xml)
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",
31 pcmk__get_tmpdir());
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
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
75
76
77
78
79
80
81
82
83
84
85
86 int
87 pcmk__xml_test_setup_group(void **state)
88 {
89 pcmk__schema_init();
90 return 0;
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 int
105 pcmk__xml_test_teardown_group(void **state)
106 {
107 pcmk__schema_cleanup();
108 xmlCleanupParser();
109 return 0;
110 }
111
112 char *
113 pcmk__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
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
147 void
148 pcmk__cib_test_cleanup(char *out_path)
149 {
150 unlink(out_path);
151 free(out_path);
152 unsetenv("CIB_file");
153 }
154
155
156
157
158
159
160
161
162
163
164 void
165 pcmk__test_init_logging(const char *name, const char *filename)
166 {
167 pcmk__cli_init_logging(name, 0);
168 if (filename != NULL) {
169 pcmk__add_logfile(filename);
170 set_crm_log_level(LOG_DEBUG);
171 }
172 }
173
174