pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
expand_plus_plus_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022 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 value_is_name_plus_plus(void **state)
18 {
19  const char *new_value;
20  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
21  crm_xml_add(test_xml, "X", "5");
22  expand_plus_plus(test_xml, "X", "X++");
23  new_value = crm_element_value(test_xml, "X");
24  assert_string_equal(new_value, "6");
25 }
26 
27 static void
28 value_is_name_plus_equals_integer(void **state)
29 {
30  const char *new_value;
31  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
32  crm_xml_add(test_xml, "X", "5");
33  expand_plus_plus(test_xml, "X", "X+=2");
34  new_value = crm_element_value(test_xml, "X");
35  assert_string_equal(new_value, "7");
36 }
37 
38 // NULL input
39 
40 static void
41 target_is_NULL(void **state)
42 {
43 
44  const char *new_value;
45  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
46  crm_xml_add(test_xml, "X", "5");
47  expand_plus_plus(NULL, "X", "X++");
48  new_value = crm_element_value(test_xml, "X");
49  assert_string_equal(new_value, "5");
50 }
51 
52 static void
53 name_is_NULL(void **state)
54 {
55  const char *new_value;
56  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
57  crm_xml_add(test_xml, "X", "5");
58  expand_plus_plus(test_xml, NULL, "X++");
59  new_value = crm_element_value(test_xml, "X");
60  assert_string_equal(new_value, "5");
61 }
62 
63 static void
64 value_is_NULL(void **state)
65 {
66  const char *new_value;
67  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
68  crm_xml_add(test_xml, "X", "5");
69  expand_plus_plus(test_xml, "X", NULL);
70  new_value = crm_element_value(test_xml, "X");
71  assert_string_equal(new_value, "5");
72 }
73 
74 // the value input doesn't start with the name input
75 
76 static void
77 value_is_wrong_name(void **state)
78 {
79  const char *new_value;
80  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
81  crm_xml_add(test_xml, "X", "5");
82  expand_plus_plus(test_xml, "X", "Y++");
83  new_value = crm_element_value(test_xml, "X");
84  assert_string_equal(new_value, "Y++");
85 }
86 
87 static void
88 value_is_only_an_integer(void **state)
89 {
90  const char *new_value;
91  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
92  crm_xml_add(test_xml, "X", "5");
93  expand_plus_plus(test_xml, "X", "2");
94  new_value = crm_element_value(test_xml, "X");
95  assert_string_equal(new_value, "2");
96 }
97 
98 // non-integers
99 
100 static void
101 variable_is_initialized_to_be_NULL(void **state)
102 {
103  const char *new_value;
104  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
105  crm_xml_add(test_xml, "X", NULL);
106  expand_plus_plus(test_xml, "X", "X++");
107  new_value = crm_element_value(test_xml, "X");
108  assert_string_equal(new_value, "X++");
109 }
110 
111 static void
112 variable_is_initialized_to_be_non_numeric(void **state)
113 {
114  const char *new_value;
115  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
116  crm_xml_add(test_xml, "X", "hello");
117  expand_plus_plus(test_xml, "X", "X++");
118  new_value = crm_element_value(test_xml, "X");
119  assert_string_equal(new_value, "1");
120 }
121 
122 static void
123 variable_is_initialized_to_be_non_numeric_2(void **state)
124 {
125  const char *new_value;
126  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
127  crm_xml_add(test_xml, "X", "hello");
128  expand_plus_plus(test_xml, "X", "X+=2");
129  new_value = crm_element_value(test_xml, "X");
130  assert_string_equal(new_value, "2");
131 }
132 
133 static void
134 variable_is_initialized_to_be_numeric_and_decimal_point_containing(void **state)
135 {
136  const char *new_value;
137  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
138  crm_xml_add(test_xml, "X", "5.01");
139  expand_plus_plus(test_xml, "X", "X++");
140  new_value = crm_element_value(test_xml, "X");
141  assert_string_equal(new_value, "6");
142 }
143 
144 static void
145 variable_is_initialized_to_be_numeric_and_decimal_point_containing_2(void **state)
146 {
147  const char *new_value;
148  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
149  crm_xml_add(test_xml, "X", "5.50");
150  expand_plus_plus(test_xml, "X", "X++");
151  new_value = crm_element_value(test_xml, "X");
152  assert_string_equal(new_value, "6");
153 }
154 
155 static void
156 variable_is_initialized_to_be_numeric_and_decimal_point_containing_3(void **state)
157 {
158  const char *new_value;
159  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
160  crm_xml_add(test_xml, "X", "5.99");
161  expand_plus_plus(test_xml, "X", "X++");
162  new_value = crm_element_value(test_xml, "X");
163  assert_string_equal(new_value, "6");
164 }
165 
166 static void
167 value_is_non_numeric(void **state)
168 {
169  const char *new_value;
170  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
171  crm_xml_add(test_xml, "X", "5");
172  expand_plus_plus(test_xml, "X", "X+=hello");
173  new_value = crm_element_value(test_xml, "X");
174  assert_string_equal(new_value, "5");
175 }
176 
177 static void
178 value_is_numeric_and_decimal_point_containing(void **state)
179 {
180  const char *new_value;
181  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
182  crm_xml_add(test_xml, "X", "5");
183  expand_plus_plus(test_xml, "X", "X+=2.01");
184  new_value = crm_element_value(test_xml, "X");
185  assert_string_equal(new_value, "7");
186 }
187 
188 static void
189 value_is_numeric_and_decimal_point_containing_2(void **state)
190 {
191  const char *new_value;
192  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
193  crm_xml_add(test_xml, "X", "5");
194  expand_plus_plus(test_xml, "X", "X+=1.50");
195  new_value = crm_element_value(test_xml, "X");
196  assert_string_equal(new_value, "6");
197 }
198 
199 static void
200 value_is_numeric_and_decimal_point_containing_3(void **state)
201 {
202  const char *new_value;
203  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
204  crm_xml_add(test_xml, "X", "5");
205  expand_plus_plus(test_xml, "X", "X+=1.99");
206  new_value = crm_element_value(test_xml, "X");
207  assert_string_equal(new_value, "6");
208 }
209 
210 // undefined input
211 
212 static void
213 name_is_undefined(void **state)
214 {
215  const char *new_value;
216  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
217  crm_xml_add(test_xml, "Y", "5");
218  expand_plus_plus(test_xml, "X", "X++");
219  new_value = crm_element_value(test_xml, "X");
220  assert_string_equal(new_value, "X++");
221 }
222 
223 // large input
224 
225 static void
226 assignment_result_is_too_large(void **state)
227 {
228  const char *new_value;
229  xmlNode *test_xml = create_xml_node(NULL, "test_xml");
230  crm_xml_add(test_xml, "X", "5");
231  expand_plus_plus(test_xml, "X", "X+=100000000000");
232  new_value = crm_element_value(test_xml, "X");
233  printf("assignment result is too large %s\n", new_value);
234  assert_string_equal(new_value, "1000000");
235 }
236 
237 PCMK__UNIT_TEST(NULL, NULL,
238  cmocka_unit_test(value_is_name_plus_plus),
239  cmocka_unit_test(value_is_name_plus_equals_integer),
240  cmocka_unit_test(target_is_NULL),
241  cmocka_unit_test(name_is_NULL),
242  cmocka_unit_test(value_is_NULL),
243  cmocka_unit_test(value_is_wrong_name),
244  cmocka_unit_test(value_is_only_an_integer),
245  cmocka_unit_test(variable_is_initialized_to_be_NULL),
246  cmocka_unit_test(variable_is_initialized_to_be_non_numeric),
247  cmocka_unit_test(variable_is_initialized_to_be_non_numeric_2),
248  cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing),
249  cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing_2),
250  cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing_3),
251  cmocka_unit_test(value_is_non_numeric),
252  cmocka_unit_test(value_is_numeric_and_decimal_point_containing),
253  cmocka_unit_test(value_is_numeric_and_decimal_point_containing_2),
254  cmocka_unit_test(value_is_numeric_and_decimal_point_containing_3),
255  cmocka_unit_test(name_is_undefined),
256  cmocka_unit_test(assignment_result_is_too_large))
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:302
PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(bad_input), cmocka_unit_test(not_found), cmocka_unit_test(find_attrB), cmocka_unit_test(find_attrA_matching))
void expand_plus_plus(xmlNode *target, const char *name, const char *value)
Update current XML attribute value per parsed integer assignment statement.
Definition: xml.c:523
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:447
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:638