pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__evaluate_date_spec_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020-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 <errno.h>
13 #include <glib.h>
14 
15 #include <crm/common/xml.h>
18 #include "crmcommon_private.h"
19 
20 static void
21 run_one_test(const char *t, const char *x, int expected)
22 {
23  crm_time_t *tm = crm_time_new(t);
24  xmlNodePtr xml = pcmk__xml_parse(x);
25 
26  assert_int_equal(pcmk__evaluate_date_spec(xml, tm), expected);
27 
28  crm_time_free(tm);
29  free_xml(xml);
30 }
31 
32 static void
33 null_invalid(void **state)
34 {
35  xmlNodePtr xml = pcmk__xml_parse("<" PCMK_XE_DATE_SPEC " "
36  PCMK_XA_ID "='spec' "
37  PCMK_XA_YEARS "='2019'/>");
38  crm_time_t *tm = crm_time_new(NULL);
39 
40  assert_int_equal(pcmk__evaluate_date_spec(NULL, NULL), EINVAL);
41  assert_int_equal(pcmk__evaluate_date_spec(xml, NULL), EINVAL);
42  assert_int_equal(pcmk__evaluate_date_spec(NULL, tm), EINVAL);
43 
44  crm_time_free(tm);
45  free_xml(xml);
46 }
47 
48 static void
49 spec_id_missing(void **state)
50 {
51  // Currently acceptable
52  run_one_test("2020-01-01", "<date_spec years='2020'/>", pcmk_rc_ok);
53 }
54 
55 static void
56 invalid_range(void **state)
57 {
58  // Currently acceptable
59  run_one_test("2020-01-01", "<date_spec years='not-a-year' months='1'/>",
60  pcmk_rc_ok);
61 }
62 
63 static void
64 time_satisfies_year_spec(void **state)
65 {
66  run_one_test("2020-01-01",
67  "<date_spec " PCMK_XA_ID "='spec' years='2020'/>",
68  pcmk_rc_ok);
69 }
70 
71 static void
72 time_after_year_spec(void **state)
73 {
74  run_one_test("2020-01-01",
75  "<" PCMK_XE_DATE_SPEC " "
76  PCMK_XA_ID "='spec' "
77  PCMK_XA_YEARS "='2019'/>",
79 }
80 
81 static void
82 time_satisfies_year_range(void **state)
83 {
84  run_one_test("2020-01-01",
85  "<" PCMK_XE_DATE_SPEC " "
86  PCMK_XA_ID "='spec' "
87  PCMK_XA_YEARS "='2010-2030'/>",
88  pcmk_rc_ok);
89 }
90 
91 static void
92 time_before_year_range(void **state)
93 {
94  run_one_test("2000-01-01",
95  "<" PCMK_XE_DATE_SPEC " "
96  PCMK_XA_ID "='spec' "
97  PCMK_XA_YEARS "='2010-2030'/>",
99 }
100 
101 static void
102 time_after_year_range(void **state)
103 {
104  run_one_test("2020-01-01",
105  "<" PCMK_XE_DATE_SPEC " "
106  PCMK_XA_ID "='spec' "
107  PCMK_XA_YEARS "='2010-2015'/>",
109 }
110 
111 static void
112 range_without_start_year_passes(void **state)
113 {
114  run_one_test("2010-01-01",
115  "<" PCMK_XE_DATE_SPEC " "
116  PCMK_XA_ID "='spec' "
117  PCMK_XA_YEARS "='-2020'/>",
118  pcmk_rc_ok);
119 }
120 
121 static void
122 range_without_end_year_passes(void **state)
123 {
124  run_one_test("2010-01-01",
125  "<" PCMK_XE_DATE_SPEC " "
126  PCMK_XA_ID "='spec' "
127  PCMK_XA_YEARS "='2000-'/>",
128  pcmk_rc_ok);
129  run_one_test("2000-10-01",
130  "<" PCMK_XE_DATE_SPEC " "
131  PCMK_XA_ID "='spec' "
132  PCMK_XA_YEARS "='2000-'/>",
133  pcmk_rc_ok);
134 }
135 
136 static void
137 yeardays_satisfies(void **state)
138 {
139  run_one_test("2020-01-30",
140  "<" PCMK_XE_DATE_SPEC " "
141  PCMK_XA_ID "='spec' "
142  PCMK_XA_YEARDAYS "='30'/>",
143  pcmk_rc_ok);
144 }
145 
146 static void
147 time_after_yeardays_spec(void **state)
148 {
149  run_one_test("2020-02-15",
150  "<" PCMK_XE_DATE_SPEC " "
151  PCMK_XA_ID "='spec' "
152  PCMK_XA_YEARDAYS "='40'/>",
154 }
155 
156 static void
157 yeardays_feb_29_satisfies(void **state)
158 {
159  run_one_test("2016-02-29",
160  "<" PCMK_XE_DATE_SPEC " "
161  PCMK_XA_ID "='spec' "
162  PCMK_XA_YEARDAYS "='60'/>",
163  pcmk_rc_ok);
164 }
165 
166 static void
167 exact_ymd_satisfies(void **state)
168 {
169  run_one_test("2001-12-31",
170  "<" PCMK_XE_DATE_SPEC " "
171  PCMK_XA_ID "='spec' "
172  PCMK_XA_YEARS "='2001' "
173  PCMK_XA_MONTHS "='12' "
174  PCMK_XA_MONTHDAYS "='31'/>",
175  pcmk_rc_ok);
176 }
177 
178 static void
179 range_in_month_satisfies(void **state)
180 {
181  run_one_test("2001-06-10",
182  "<" PCMK_XE_DATE_SPEC " "
183  PCMK_XA_ID "='spec' "
184  PCMK_XA_YEARS "='2001' "
185  PCMK_XA_MONTHS "='6' "
186  PCMK_XA_MONTHDAYS "='1-10'/>",
187  pcmk_rc_ok);
188 }
189 
190 static void
191 exact_ymd_after_range(void **state)
192 {
193  run_one_test("2001-12-31",
194  "<" PCMK_XE_DATE_SPEC " "
195  PCMK_XA_ID "='spec' "
196  PCMK_XA_YEARS "='2001' "
197  PCMK_XA_MONTHS "='12' "
198  PCMK_XA_MONTHDAYS "='30'/>",
200 }
201 
202 static void
203 time_after_monthdays_range(void **state)
204 {
205  run_one_test("2001-06-10",
206  "<" PCMK_XE_DATE_SPEC " "
207  PCMK_XA_ID "='spec' "
208  PCMK_XA_YEARS "='2001' "
209  PCMK_XA_MONTHS "='6' "
210  PCMK_XA_MONTHDAYS "='11-15'/>",
212 }
213 
215  cmocka_unit_test(null_invalid),
216  cmocka_unit_test(spec_id_missing),
217  cmocka_unit_test(invalid_range),
218  cmocka_unit_test(time_satisfies_year_spec),
219  cmocka_unit_test(time_after_year_spec),
220  cmocka_unit_test(time_satisfies_year_range),
221  cmocka_unit_test(time_before_year_range),
222  cmocka_unit_test(time_after_year_range),
223  cmocka_unit_test(range_without_start_year_passes),
224  cmocka_unit_test(range_without_end_year_passes),
225  cmocka_unit_test(yeardays_satisfies),
226  cmocka_unit_test(time_after_yeardays_spec),
227  cmocka_unit_test(yeardays_feb_29_satisfies),
228  cmocka_unit_test(exact_ymd_satisfies),
229  cmocka_unit_test(range_in_month_satisfies),
230  cmocka_unit_test(exact_ymd_after_range),
231  cmocka_unit_test(time_after_monthdays_range))
#define PCMK_XA_YEARS
Definition: xml_names.h:456
G_GNUC_INTERNAL int pcmk__evaluate_date_spec(const xmlNode *date_spec, const crm_time_t *now)
Definition: rules.c:193
#define PCMK_XA_YEARDAYS
Definition: xml_names.h:455
struct crm_time_s crm_time_t
Definition: iso8601.h:32
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
#define PCMK_XE_DATE_SPEC
Definition: xml_names.h:97
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
Wrappers for and extensions to libxml2.
#define PCMK_XA_ID
Definition: xml_names.h:301
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
#define PCMK_XA_MONTHDAYS
Definition: xml_names.h:327
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:112
#define PCMK_XA_MONTHS
Definition: xml_names.h:328
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:150