pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__xe_add_node_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 
16 static void
17 bad_input(void **state) {
18  xmlNode *node = NULL;
19 
21 
22  node = pcmk__xe_create(NULL, "test");
23 
24  pcmk__xe_add_node(node, NULL, 0);
25  assert_null(xmlHasProp(node, (pcmkXmlStr) PCMK__XA_ATTR_HOST));
26  assert_null(xmlHasProp(node, (pcmkXmlStr) PCMK__XA_ATTR_HOST_ID));
27 
28  pcmk__xe_add_node(node, NULL, -100);
29  assert_null(xmlHasProp(node, (pcmkXmlStr) PCMK__XA_ATTR_HOST));
30  assert_null(xmlHasProp(node, (pcmkXmlStr) PCMK__XA_ATTR_HOST_ID));
31 
32  free_xml(node);
33 }
34 
35 static void
36 expected_input(void **state) {
37  xmlNode *node = pcmk__xe_create(NULL, "test");
38  int i;
39 
40  pcmk__xe_add_node(node, "somenode", 47);
41  assert_string_equal("somenode",
43  assert_int_equal(pcmk_rc_ok,
45  assert_int_equal(i, 47);
46 
47  free_xml(node);
48 }
49 
50 static void
51 repeated_use(void **state) {
52  xmlNode *node = pcmk__xe_create(NULL, "test");
53  int i;
54 
55  /* Later calls override settings from earlier calls. */
56  pcmk__xe_add_node(node, "nodeA", 1);
57  pcmk__xe_add_node(node, "nodeB", 2);
58  pcmk__xe_add_node(node, "nodeC", 3);
59 
60  assert_string_equal("nodeC", crm_element_value(node, PCMK__XA_ATTR_HOST));
61  assert_int_equal(pcmk_rc_ok,
63  assert_int_equal(i, 3);
64 
65  free_xml(node);
66 }
67 
69  cmocka_unit_test(bad_input),
70  cmocka_unit_test(expected_input),
71  cmocka_unit_test(repeated_use))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:494
#define PCMK__XA_ATTR_HOST_ID
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:458
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
Wrappers for and extensions to libxml2.
#define pcmk__assert_asserts(stmt)
void free_xml(xmlNode *child)
Definition: xml.c:958
int pcmk__xml_test_teardown_group(void **state)
Definition: unittest.c:104
const xmlChar * pcmkXmlStr
Definition: xml.h:41
#define PCMK__XA_ATTR_HOST
void pcmk__xe_add_node(xmlNode *xml, const char *node, int nodeid)
Definition: nodes.c:127
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
Definition: xml.c:770