pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk__build_schema_xml_node_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2023-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 General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
12#include <crm/common/xml.h>
15
16#include <glib.h>
17
18const char *rngs1[] = { "pacemaker-3.0.rng", "status-1.0.rng", "alerts-2.10.rng",
19 "nvset-2.9.rng", "score.rng", "rule-2.9.rng",
20 "tags-1.3.rng", "acls-2.0.rng", "fencing-2.4.rng",
21 "constraints-3.0.rng", "resources-3.0.rng", "nvset-3.0.rng",
22 "nodes-3.0.rng", "options-3.0.rng", NULL };
23
24const char *rngs2[] = { "pacemaker-2.0.rng", "status-1.0.rng", "tags-1.3.rng",
25 "acls-2.0.rng", "fencing-1.2.rng", "constraints-1.2.rng",
26 "rule.rng", "score.rng", "resources-1.3.rng",
27 "nvset-1.3.rng", "nodes-1.3.rng", "options-1.0.rng",
28 "nvset.rng", "cib-1.2.rng", NULL };
29
30const char *rngs3[] = { "pacemaker-2.1.rng", "constraints-2.1.rng", NULL };
31
32static int
33setup(void **state)
34{
35 setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1);
37 return 0;
38}
39
40static int
41teardown(void **state)
42{
44 unsetenv("PCMK_schema_directory");
45 return 0;
46}
47
48static void
49invalid_name(void **state)
50{
51 GList *already_included = NULL;
52 xmlNode *parent = pcmk__xe_create(NULL, PCMK__XA_SCHEMAS);
53
54 pcmk__build_schema_xml_node(parent, "pacemaker-9.0", &already_included);
55 assert_null(parent->children);
56 assert_null(already_included);
58}
59
60static void
61single_schema(void **state)
62{
63 GList *already_included = NULL;
64 xmlNode *parent = pcmk__xe_create(NULL, PCMK__XA_SCHEMAS);
65 xmlNode *schema_node = NULL;
66 xmlNode *file_node = NULL;
67 int i = 0;
68
69 pcmk__build_schema_xml_node(parent, "pacemaker-3.0", &already_included);
70
71 assert_non_null(already_included);
72 assert_non_null(parent->children);
73
74 /* Test that the result looks like this:
75 *
76 * <schemas>
77 * <schema version="pacemaker-3.0">
78 * <file path="pacemaker-3.0.rng">CDATA</file>
79 * <file path="status-1.0.rng">CDATA</file>
80 * ...
81 * </schema>
82 * </schemas>
83 */
84 schema_node = pcmk__xe_first_child(parent, NULL, NULL, NULL);
85 assert_string_equal("pacemaker-3.0",
87
88 file_node = pcmk__xe_first_child(schema_node, NULL, NULL, NULL);
89 while (file_node != NULL && rngs1[i] != NULL) {
90 assert_string_equal(rngs1[i],
92 assert_int_equal(pcmk__xml_first_child(file_node)->type, XML_CDATA_SECTION_NODE);
93
94 file_node = pcmk__xe_next(file_node, NULL);
95 i++;
96 }
97
98 g_list_free_full(already_included, free);
100}
101
102static void
103multiple_schemas(void **state)
104{
105 GList *already_included = NULL;
106 xmlNode *parent = pcmk__xe_create(NULL, PCMK__XA_SCHEMAS);
107 xmlNode *schema_node = NULL;
108 xmlNode *file_node = NULL;
109 int i = 0;
110
111 pcmk__build_schema_xml_node(parent, "pacemaker-2.0", &already_included);
112 pcmk__build_schema_xml_node(parent, "pacemaker-2.1", &already_included);
113
114 assert_non_null(already_included);
115 assert_non_null(parent->children);
116
117 /* Like single_schema, but make sure files aren't included multiple times
118 * when the function is called repeatedly.
119 */
120 schema_node = pcmk__xe_first_child(parent, NULL, NULL, NULL);
121 assert_string_equal("pacemaker-2.0",
122 crm_element_value(schema_node, PCMK_XA_VERSION));
123
124 file_node = pcmk__xe_first_child(schema_node, NULL, NULL, NULL);
125 while (file_node != NULL && rngs2[i] != NULL) {
126 assert_string_equal(rngs2[i],
127 crm_element_value(file_node, PCMK_XA_PATH));
128 assert_int_equal(pcmk__xml_first_child(file_node)->type, XML_CDATA_SECTION_NODE);
129
130 file_node = pcmk__xe_next(file_node, NULL);
131 i++;
132 }
133
134 schema_node = pcmk__xe_next(schema_node, NULL);
135 assert_string_equal("pacemaker-2.1",
136 crm_element_value(schema_node, PCMK_XA_VERSION));
137
138 file_node = pcmk__xe_first_child(schema_node, NULL, NULL, NULL);
139 i = 0;
140
141 while (file_node != NULL && rngs3[i] != NULL) {
142 assert_string_equal(rngs3[i],
143 crm_element_value(file_node, PCMK_XA_PATH));
144 assert_int_equal(pcmk__xml_first_child(file_node)->type, XML_CDATA_SECTION_NODE);
145
146 file_node = pcmk__xe_next(file_node, NULL);
147 i++;
148 }
149
150 g_list_free_full(already_included, free);
152}
153
154PCMK__UNIT_TEST(setup, teardown,
155 cmocka_unit_test(invalid_name),
156 cmocka_unit_test(single_schema),
157 cmocka_unit_test(multiple_schemas))
const char * parent
Definition cib.c:27
enum pcmk_ipc_server type
Definition cpg.c:3
const char * rngs2[]
const char * rngs3[]
const char * rngs1[]
void pcmk__build_schema_xml_node(xmlNode *parent, const char *name, GList **already_included)
Definition schemas.c:1528
int pcmk__xml_test_teardown_group(void **state)
Definition unittest.c:105
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int pcmk__xml_test_setup_group(void **state)
Definition unittest.c:87
Wrappers for and extensions to libxml2.
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
Definition xml_element.c:43
xmlNode * pcmk__xe_next(const xmlNode *node, const char *element_name)
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
void pcmk__xml_free(xmlNode *xml)
Definition xml.c:816
#define PCMK_XA_PATH
Definition xml_names.h:355
#define PCMK_XA_VERSION
Definition xml_names.h:444
#define PCMK__XA_SCHEMAS