pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk__xe_copy_attrs_test.c
Go to the documentation of this file.
1  /*
2  * Copyright 2022-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 
13 
14 #include <glib.h>
15 
16 static void
17 null_args(void **state)
18 {
19  // This test dumps core via CRM_CHECK()
20  xmlNode *xml = pcmk__xe_create(NULL, "test");
21 
22  assert_int_equal(pcmk__xe_copy_attrs(NULL, NULL, pcmk__xaf_none), EINVAL);
23  assert_int_equal(pcmk__xe_copy_attrs(NULL, xml, pcmk__xaf_none), EINVAL);
24  assert_int_equal(pcmk__xe_copy_attrs(xml, NULL, pcmk__xaf_none), EINVAL);
25  assert_ptr_equal(xml->properties, NULL);
26 
27  free_xml(xml);
28 }
29 
30 static void
31 no_source_attrs(void **state)
32 {
33  xmlNode *src = pcmk__xe_create(NULL, "test");
34  xmlNode *target = pcmk__xe_create(NULL, "test");
35 
36  // Ensure copying from empty source doesn't create target properties
37  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
38  pcmk_rc_ok);
39  assert_ptr_equal(target->properties, NULL);
40 
41  // Ensure copying from empty source doesn't delete target attributes
42  crm_xml_add(target, "attr", "value");
43  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
44  pcmk_rc_ok);
45  assert_string_equal(crm_element_value(target, "attr"), "value");
46 
47  free_xml(src);
49 }
50 
51 static void
52 copy_one(void **state)
53 {
54  xmlNode *src = pcmk__xe_create(NULL, "test");
55  xmlNode *target = pcmk__xe_create(NULL, "test");
56 
57  crm_xml_add(src, "attr", "value");
58 
59  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
60  pcmk_rc_ok);
61  assert_string_equal(crm_element_value(src, "attr"),
62  crm_element_value(target, "attr"));
63 
64  free_xml(src);
66 }
67 
68 static void
69 copy_multiple(void **state)
70 {
71  xmlNode *src = pcmk__xe_create(NULL, "test");
72  xmlNode *target = pcmk__xe_create(NULL, "test");
73 
75  "attr1", "value1",
76  "attr2", "value2",
77  "attr3", "value3",
78  NULL);
79 
80  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
81  pcmk_rc_ok);
82  assert_string_equal(crm_element_value(src, "attr1"),
83  crm_element_value(target, "attr1"));
84  assert_string_equal(crm_element_value(src, "attr2"),
85  crm_element_value(target, "attr2"));
86  assert_string_equal(crm_element_value(src, "attr3"),
87  crm_element_value(target, "attr3"));
88 
89  free_xml(src);
91 }
92 
93 static void
94 overwrite(void **state)
95 {
96  xmlNode *src = pcmk__xe_create(NULL, "test");
97  xmlNode *target = pcmk__xe_create(NULL, "test");
98 
99  crm_xml_add(src, "attr", "src_value");
100  crm_xml_add(target, "attr", "target_value");
101 
102  // Overwrite enabled by default
103  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
104  pcmk_rc_ok);
105  assert_string_equal(crm_element_value(src, "attr"),
106  crm_element_value(target, "attr"));
107  free_xml(src);
108  free_xml(target);
109 }
110 
111 static void
112 no_overwrite(void **state)
113 {
114  xmlNode *src = pcmk__xe_create(NULL, "test");
115  xmlNode *target = pcmk__xe_create(NULL, "test");
116 
117  crm_xml_add(src, "attr", "src_value");
118  crm_xml_add(target, "attr", "target_value");
119 
120  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_no_overwrite),
121  pcmk_rc_ok);
122  assert_string_not_equal(crm_element_value(src, "attr"),
123  crm_element_value(target, "attr"));
124 
125  // no_overwrite doesn't prevent copy if there's no conflict
126  pcmk__xe_remove_attr(target, "attr");
127 
128  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_no_overwrite),
129  pcmk_rc_ok);
130  assert_string_equal(crm_element_value(src, "attr"),
131  crm_element_value(target, "attr"));
132 
133  free_xml(src);
134  free_xml(target);
135 }
136 
137 static void
138 score_update(void **state)
139 {
140  xmlNode *src = pcmk__xe_create(NULL, "test");
141  xmlNode *target = pcmk__xe_create(NULL, "test");
142 
143  crm_xml_add(src, "plus_plus_attr", "plus_plus_attr++");
144  crm_xml_add(src, "plus_two_attr", "plus_two_attr+=2");
145  crm_xml_add(target, "plus_plus_attr", "1");
146  crm_xml_add(target, "plus_two_attr", "1");
147 
148  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_score_update),
149  pcmk_rc_ok);
150  assert_string_equal(crm_element_value(target, "plus_plus_attr"), "2");
151  assert_string_equal(crm_element_value(target, "plus_two_attr"), "3");
152 
153  free_xml(src);
154  free_xml(target);
155 }
156 
157 static void
158 no_score_update(void **state)
159 {
160  xmlNode *src = pcmk__xe_create(NULL, "test");
161  xmlNode *target = pcmk__xe_create(NULL, "test");
162 
163  crm_xml_add(src, "plus_plus_attr", "plus_plus_attr++");
164  crm_xml_add(src, "plus_two_attr", "plus_two_attr+=2");
165  crm_xml_add(target, "plus_plus_attr", "1");
166  crm_xml_add(target, "plus_two_attr", "1");
167 
168  // Score update disabled by default
169  assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
170  pcmk_rc_ok);
171  assert_string_equal(crm_element_value(target, "plus_plus_attr"),
172  "plus_plus_attr++");
173  assert_string_equal(crm_element_value(target, "plus_two_attr"),
174  "plus_two_attr+=2");
175 
176  free_xml(src);
177  free_xml(target);
178 }
179 
181  cmocka_unit_test(null_args),
182  cmocka_unit_test(no_source_attrs),
183  cmocka_unit_test(copy_one),
184  cmocka_unit_test(copy_multiple),
185  cmocka_unit_test(overwrite),
186  cmocka_unit_test(no_overwrite),
187  cmocka_unit_test(score_update),
188  cmocka_unit_test(no_score_update));
int pcmk__xe_copy_attrs(xmlNode *target, const xmlNode *src, uint32_t flags)
Definition: xml.c:584
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition: nvpair.c:301
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:446
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:74
void pcmk__xe_remove_attr(xmlNode *element, const char *name)
Definition: xml.c:652
Flag has no effect.
Definition: xml_internal.h:438
PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL, cmocka_unit_test(null_args), cmocka_unit_test(no_source_attrs), cmocka_unit_test(copy_one), cmocka_unit_test(copy_multiple), cmocka_unit_test(overwrite), cmocka_unit_test(no_overwrite), cmocka_unit_test(score_update), cmocka_unit_test(no_score_update))
void free_xml(xmlNode *child)
Definition: xml.c:867
void pcmk__xe_set_props(xmlNodePtr node,...) G_GNUC_NULL_TERMINATED
Definition: xml.c:2279
const char * target
Definition: pcmk_fence.c:29
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
Definition: xml.c:720
Don&#39;t overwrite existing values.
Definition: xml_internal.h:441