pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__add_time_from_xml_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 <libxml/tree.h> // xmlNode
13 
15 
16 #include <crm/common/iso8601.h>
18 #include <crm/common/xml.h>
19 #include "../../crmcommon_private.h"
20 
21 #define ALL_VALID "<duration id=\"duration1\" years=\"1\" months=\"2\" " \
22  "weeks=\"3\" days=\"-1\" hours=\"1\" minutes=\"1\" " \
23  "seconds=\"1\" />"
24 
25 #define YEARS_INVALID "<duration id=\"duration1\" years=\"not-a-number\" />"
26 
27 #define YEARS_TOO_BIG "<duration id=\"duration1\" years=\"2222222222\" />"
28 
29 #define YEARS_TOO_SMALL "<duration id=\"duration1\" years=\"-2222222222\" />"
30 
31 static void
32 null_time_invalid(void **state)
33 {
34  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
35 
36  assert_int_equal(pcmk__add_time_from_xml(NULL, pcmk__time_years, xml),
37  EINVAL);
38  free_xml(xml);
39 }
40 
41 static void
42 null_xml_ok(void **state)
43 {
44  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
45  crm_time_t *reference = pcmk_copy_time(t);
46 
47  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, NULL),
48  pcmk_rc_ok);
49  assert_int_equal(crm_time_compare(t, reference), 0);
50 
51  crm_time_free(t);
52  crm_time_free(reference);
53 }
54 
55 static void
56 invalid_component(void **state)
57 {
58  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
59 
60  assert_int_equal(pcmk__add_time_from_xml(NULL, pcmk__time_unknown, xml),
61  EINVAL);
62  free_xml(xml);
63 }
64 
65 static void
66 missing_attr(void **state)
67 {
68  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
69  crm_time_t *reference = pcmk_copy_time(t);
70  xmlNode *xml = pcmk__xml_parse(YEARS_INVALID);
71 
72  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_months, xml),
73  pcmk_rc_ok);
74  assert_int_equal(crm_time_compare(t, reference), 0);
75 
76  crm_time_free(t);
77  crm_time_free(reference);
78  free_xml(xml);
79 }
80 
81 static void
82 invalid_attr(void **state)
83 {
84  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
85  crm_time_t *reference = pcmk_copy_time(t);
86  xmlNode *xml = pcmk__xml_parse(YEARS_INVALID);
87 
88  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, xml),
90  assert_int_equal(crm_time_compare(t, reference), 0);
91 
92  crm_time_free(t);
93  crm_time_free(reference);
94  free_xml(xml);
95 }
96 
97 static void
98 out_of_range_attr(void **state)
99 {
100  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
101  crm_time_t *reference = pcmk_copy_time(t);
102  xmlNode *xml = NULL;
103 
105  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, xml), ERANGE);
106  assert_int_equal(crm_time_compare(t, reference), 0);
107  free_xml(xml);
108 
110  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, xml), ERANGE);
111  assert_int_equal(crm_time_compare(t, reference), 0);
112  free_xml(xml);
113 
114  crm_time_free(t);
115  crm_time_free(reference);
116 }
117 
118 static void
119 add_years(void **state)
120 {
121  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
122  crm_time_t *reference = crm_time_new("2025-01-01 15:00:00");
123  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
124 
125  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, xml),
126  pcmk_rc_ok);
127  assert_int_equal(crm_time_compare(t, reference), 0);
128 
129  crm_time_free(t);
130  crm_time_free(reference);
131  free_xml(xml);
132 }
133 
134 static void
135 add_months(void **state)
136 {
137  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
138  crm_time_t *reference = crm_time_new("2024-03-01 15:00:00");
139  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
140 
141  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_months, xml),
142  pcmk_rc_ok);
143  assert_int_equal(crm_time_compare(t, reference), 0);
144 
145  crm_time_free(t);
146  crm_time_free(reference);
147  free_xml(xml);
148 }
149 
150 static void
151 add_weeks(void **state)
152 {
153  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
154  crm_time_t *reference = crm_time_new("2024-01-22 15:00:00");
155  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
156 
157  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_weeks, xml),
158  pcmk_rc_ok);
159  assert_int_equal(crm_time_compare(t, reference), 0);
160 
161  crm_time_free(t);
162  crm_time_free(reference);
163  free_xml(xml);
164 }
165 
166 static void
167 add_days(void **state)
168 {
169  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
170  crm_time_t *reference = crm_time_new("2023-12-31 15:00:00");
171  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
172 
173  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_days, xml),
174  pcmk_rc_ok);
175  assert_int_equal(crm_time_compare(t, reference), 0);
176 
177  crm_time_free(t);
178  crm_time_free(reference);
179  free_xml(xml);
180 }
181 
182 static void
183 add_hours(void **state)
184 {
185  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
186  crm_time_t *reference = crm_time_new("2024-01-01 16:00:00");
187  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
188 
189  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_hours, xml),
190  pcmk_rc_ok);
191  assert_int_equal(crm_time_compare(t, reference), 0);
192 
193  crm_time_free(t);
194  crm_time_free(reference);
195  free_xml(xml);
196 }
197 
198 static void
199 add_minutes(void **state)
200 {
201  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
202  crm_time_t *reference = crm_time_new("2024-01-01 15:01:00");
203  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
204 
205  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_minutes, xml),
206  pcmk_rc_ok);
207  assert_int_equal(crm_time_compare(t, reference), 0);
208 
209  crm_time_free(t);
210  crm_time_free(reference);
211  free_xml(xml);
212 }
213 
214 static void
215 add_seconds(void **state)
216 {
217  crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
218  crm_time_t *reference = crm_time_new("2024-01-01 15:00:01");
219  xmlNode *xml = pcmk__xml_parse(ALL_VALID);
220 
221  assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_seconds, xml),
222  pcmk_rc_ok);
223  assert_int_equal(crm_time_compare(t, reference), 0);
224 
225  crm_time_free(t);
226  crm_time_free(reference);
227  free_xml(xml);
228 }
229 
231  cmocka_unit_test(null_time_invalid),
232  cmocka_unit_test(null_xml_ok),
233  cmocka_unit_test(invalid_component),
234  cmocka_unit_test(missing_attr),
235  cmocka_unit_test(invalid_attr),
236  cmocka_unit_test(out_of_range_attr),
237  cmocka_unit_test(add_years),
238  cmocka_unit_test(add_months),
239  cmocka_unit_test(add_weeks),
240  cmocka_unit_test(add_days),
241  cmocka_unit_test(add_hours),
242  cmocka_unit_test(add_minutes),
243  cmocka_unit_test(add_seconds));
#define YEARS_TOO_SMALL
struct crm_time_s crm_time_t
Definition: iso8601.h:32
PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group, cmocka_unit_test(null_time_invalid), cmocka_unit_test(null_xml_ok), cmocka_unit_test(invalid_component), cmocka_unit_test(missing_attr), cmocka_unit_test(invalid_attr), cmocka_unit_test(out_of_range_attr), cmocka_unit_test(add_years), cmocka_unit_test(add_months), cmocka_unit_test(add_weeks), cmocka_unit_test(add_days), cmocka_unit_test(add_hours), cmocka_unit_test(add_minutes), cmocka_unit_test(add_seconds))
#define ALL_VALID
crm_time_t * pcmk_copy_time(const crm_time_t *source)
Definition: iso8601.c:1471
#define YEARS_TOO_BIG
G_GNUC_INTERNAL int pcmk__add_time_from_xml(crm_time_t *t, enum pcmk__time_component component, const xmlNode *xml)
Definition: iso8601.c:1615
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
Wrappers for and extensions to libxml2.
ISO_8601 Date handling.
void free_xml(xmlNode *child)
Definition: xml.c:958
int pcmk__xml_test_teardown_group(void **state)
Definition: unittest.c:104
xmlNode * pcmk__xml_parse(const char *input)
Definition: xml_io.c:245
int crm_time_compare(const crm_time_t *a, const crm_time_t *b)
Definition: iso8601.c:1736
#define YEARS_INVALID
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:112
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:150