pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk__xe_first_child_test.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 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
16const char *str1 =
17 "<xml>\n"
18 " <!-- This is an A node -->\n"
19 " <nodeA attrA=\"123\" " PCMK_XA_ID "=\"1\">\n"
20 " content\n"
21 " </nodeA>\n"
22 " <!-- This is an A node -->\n"
23 " <nodeA attrA=\"456\" " PCMK_XA_ID "=\"2\">\n"
24 " content\n"
25 " </nodeA>\n"
26 " <!-- This is an A node -->\n"
27 " <nodeA attrB=\"XYZ\" " PCMK_XA_ID "=\"3\">\n"
28 " content\n"
29 " </nodeA>\n"
30 " <!-- This is a B node -->\n"
31 " <nodeB attrA=\"123\" " PCMK_XA_ID "=\"4\">\n"
32 " content\n"
33 " </nodeB>\n"
34 " <!-- This is a B node -->\n"
35 " <nodeB attrB=\"ABC\" " PCMK_XA_ID "=\"5\">\n"
36 " content\n"
37 " </nodeB>\n"
38 "</xml>";
39
40static void
41bad_input(void **state) {
42 xmlNode *xml = pcmk__xml_parse(str1);
43
44 assert_null(pcmk__xe_first_child(NULL, NULL, NULL, NULL));
45 assert_null(pcmk__xe_first_child(NULL, NULL, NULL, "attrX"));
46
47 pcmk__xml_free(xml);
48}
49
50static void
51not_found(void **state) {
52 xmlNode *xml = pcmk__xml_parse(str1);
53
54 /* No node with an attrX attribute */
55 assert_null(pcmk__xe_first_child(xml, NULL, "attrX", NULL));
56 /* No nodeX node */
57 assert_null(pcmk__xe_first_child(xml, "nodeX", NULL, NULL));
58 /* No nodeA node with attrX */
59 assert_null(pcmk__xe_first_child(xml, "nodeA", "attrX", NULL));
60 /* No nodeA node with attrA=XYZ */
61 assert_null(pcmk__xe_first_child(xml, "nodeA", "attrA", "XYZ"));
62
63 pcmk__xml_free(xml);
64}
65
66static void
67find_attrB(void **state) {
68 xmlNode *xml = pcmk__xml_parse(str1);
69 xmlNode *result = NULL;
70
71 /* Find the first node with attrB */
72 result = pcmk__xe_first_child(xml, NULL, "attrB", NULL);
73 assert_non_null(result);
74 assert_string_equal(crm_element_value(result, PCMK_XA_ID), "3");
75
76 /* Find the first nodeB with attrB */
77 result = pcmk__xe_first_child(xml, "nodeB", "attrB", NULL);
78 assert_non_null(result);
79 assert_string_equal(crm_element_value(result, PCMK_XA_ID), "5");
80
81 pcmk__xml_free(xml);
82}
83
84static void
85find_attrA_matching(void **state) {
86 xmlNode *xml = pcmk__xml_parse(str1);
87 xmlNode *result = NULL;
88
89 /* Find attrA=456 */
90 result = pcmk__xe_first_child(xml, NULL, "attrA", "456");
91 assert_non_null(result);
92 assert_string_equal(crm_element_value(result, PCMK_XA_ID), "2");
93
94 /* Find a nodeB with attrA=123 */
95 result = pcmk__xe_first_child(xml, "nodeB", "attrA", "123");
96 assert_non_null(result);
97 assert_string_equal(crm_element_value(result, PCMK_XA_ID), "4");
98
99 pcmk__xml_free(xml);
100}
101
103 cmocka_unit_test(bad_input),
104 cmocka_unit_test(not_found),
105 cmocka_unit_test(find_attrB),
106 cmocka_unit_test(find_attrA_matching));
const char * str1
pcmk__action_result_t result
Definition pcmk_fence.c:37
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
void pcmk__xml_free(xmlNode *xml)
Definition xml.c:816
xmlNode * pcmk__xml_parse(const char *input)
Definition xml_io.c:167
#define PCMK_XA_ID
Definition xml_names.h:301