pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk__xe_set_score_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 #include "crmcommon_private.h" // pcmk__xe_set_score()
17 
29 static void
30 assert_set_score(const char *initial, const char *new,
31  const char *reference_val, int reference_rc)
32 {
33  const char *name = "X";
34  xmlNode *test_xml = pcmk__xe_create(NULL, "test_xml");
35 
36  crm_xml_add(test_xml, name, initial);
37  assert_int_equal(pcmk__xe_set_score(test_xml, name, new), reference_rc);
38  assert_string_equal(crm_element_value(test_xml, name), reference_val);
39 
40  free_xml(test_xml);
41 }
42 
43 static void
44 value_is_name_plus_plus(void **state)
45 {
46  assert_set_score("5", "X++", "6", pcmk_rc_ok);
47 }
48 
49 static void
50 value_is_name_plus_equals_integer(void **state)
51 {
52  assert_set_score("5", "X+=2", "7", pcmk_rc_ok);
53 }
54 
55 // NULL input
56 
57 static void
58 target_is_NULL(void **state)
59 {
60  // Dumps core via CRM_CHECK()
61  assert_int_equal(pcmk__xe_set_score(NULL, "X", "X++"), EINVAL);
62 }
63 
64 static void
65 name_is_NULL(void **state)
66 {
67  xmlNode *test_xml = pcmk__xe_create(NULL, "test_xml");
68 
69  crm_xml_add(test_xml, "X", "5");
70 
71  // Dumps core via CRM_CHECK()
72  assert_int_equal(pcmk__xe_set_score(test_xml, NULL, "X++"), EINVAL);
73  assert_string_equal(crm_element_value(test_xml, "X"), "5");
74 
75  free_xml(test_xml);
76 }
77 
78 static void
79 value_is_NULL(void **state)
80 {
81  assert_set_score("5", NULL, "5", pcmk_rc_ok);
82 }
83 
84 // the value input doesn't start with the name input
85 
86 static void
87 value_is_wrong_name(void **state)
88 {
89  assert_set_score("5", "Y++", "Y++", pcmk_rc_ok);
90 }
91 
92 static void
93 value_is_only_an_integer(void **state)
94 {
95  assert_set_score("5", "2", "2", pcmk_rc_ok);
96 }
97 
98 // non-integers
99 
100 static void
101 variable_is_initialized_to_be_non_numeric(void **state)
102 {
103  assert_set_score("hello", "X++", "1", pcmk_rc_ok);
104 }
105 
106 static void
107 variable_is_initialized_to_be_non_numeric_2(void **state)
108 {
109  assert_set_score("hello", "X+=2", "2", pcmk_rc_ok);
110 }
111 
112 static void
113 variable_is_initialized_to_be_numeric_and_decimal_point_containing(void **state)
114 {
115  assert_set_score("5.01", "X++", "6", pcmk_rc_ok);
116 }
117 
118 static void
119 variable_is_initialized_to_be_numeric_and_decimal_point_containing_2(void **state)
120 {
121  assert_set_score("5.50", "X++", "6", pcmk_rc_ok);
122 }
123 
124 static void
125 variable_is_initialized_to_be_numeric_and_decimal_point_containing_3(void **state)
126 {
127  assert_set_score("5.99", "X++", "6", pcmk_rc_ok);
128 }
129 
130 static void
131 value_is_non_numeric(void **state)
132 {
133  assert_set_score("5", "X+=hello", "5", pcmk_rc_ok);
134 }
135 
136 static void
137 value_is_numeric_and_decimal_point_containing(void **state)
138 {
139  assert_set_score("5", "X+=2.01", "7", pcmk_rc_ok);
140 }
141 
142 static void
143 value_is_numeric_and_decimal_point_containing_2(void **state)
144 {
145  assert_set_score("5", "X+=1.50", "6", pcmk_rc_ok);
146 }
147 
148 static void
149 value_is_numeric_and_decimal_point_containing_3(void **state)
150 {
151  assert_set_score("5", "X+=1.99", "6", pcmk_rc_ok);
152 }
153 
154 // undefined input
155 
156 static void
157 name_is_undefined(void **state)
158 {
159  assert_set_score(NULL, "X++", "X++", pcmk_rc_ok);
160 }
161 
162 // large input
163 
164 static void
165 assignment_result_is_too_large(void **state)
166 {
167  assert_set_score("5", "X+=100000000000", "1000000", pcmk_rc_ok);
168 }
169 
171  cmocka_unit_test(value_is_name_plus_plus),
172  cmocka_unit_test(value_is_name_plus_equals_integer),
173  cmocka_unit_test(target_is_NULL),
174  cmocka_unit_test(name_is_NULL),
175  cmocka_unit_test(value_is_NULL),
176  cmocka_unit_test(value_is_wrong_name),
177  cmocka_unit_test(value_is_only_an_integer),
178  cmocka_unit_test(variable_is_initialized_to_be_non_numeric),
179  cmocka_unit_test(variable_is_initialized_to_be_non_numeric_2),
180  cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing),
181  cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing_2),
182  cmocka_unit_test(variable_is_initialized_to_be_numeric_and_decimal_point_containing_3),
183  cmocka_unit_test(value_is_non_numeric),
184  cmocka_unit_test(value_is_numeric_and_decimal_point_containing),
185  cmocka_unit_test(value_is_numeric_and_decimal_point_containing_2),
186  cmocka_unit_test(value_is_numeric_and_decimal_point_containing_3),
187  cmocka_unit_test(name_is_undefined),
188  cmocka_unit_test(assignment_result_is_too_large))
const char * name
Definition: cib.c:26
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
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
G_GNUC_INTERNAL int pcmk__xe_set_score(xmlNode *target, const char *name, const char *value)
Definition: xml.c:525
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 free_xml(xmlNode *child)
Definition: xml.c:867
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
Definition: xml.c:720