pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
pe_cron_range_satisfied_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020-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 
12 #include <glib.h>
13 
14 #include <crm/msg_xml.h>
16 #include <crm/common/xml.h>
18 
19 static void
20 run_one_test(const char *t, const char *x, int expected) {
21  crm_time_t *tm = crm_time_new(t);
22  xmlNodePtr xml = string2xml(x);
23 
24  assert_int_equal(pe_cron_range_satisfied(tm, xml), expected);
25 
26  crm_time_free(tm);
27  free_xml(xml);
28 }
29 
30 static void
31 no_time_given(void **state) {
32  assert_int_equal(pe_cron_range_satisfied(NULL, NULL), pcmk_rc_op_unsatisfied);
33 }
34 
35 static void
36 any_time_satisfies_empty_spec(void **state) {
37  crm_time_t *tm = crm_time_new(NULL);
38 
39  assert_int_equal(pe_cron_range_satisfied(tm, NULL), pcmk_rc_ok);
40 
41  crm_time_free(tm);
42 }
43 
44 static void
45 time_satisfies_year_spec(void **state) {
46  run_one_test("2020-01-01",
47  "<date_spec " XML_ATTR_ID "='spec' years='2020'/>",
48  pcmk_rc_ok);
49 }
50 
51 static void
52 time_after_year_spec(void **state) {
53  run_one_test("2020-01-01",
54  "<date_spec " XML_ATTR_ID "='spec' years='2019'/>",
56 }
57 
58 static void
59 time_satisfies_year_range(void **state) {
60  run_one_test("2020-01-01",
61  "<date_spec " XML_ATTR_ID "='spec' years='2010-2030'/>",
62  pcmk_rc_ok);
63 }
64 
65 static void
66 time_before_year_range(void **state) {
67  run_one_test("2000-01-01",
68  "<date_spec " XML_ATTR_ID "='spec' years='2010-2030'/>",
70 }
71 
72 static void
73 time_after_year_range(void **state) {
74  run_one_test("2020-01-01",
75  "<date_spec " XML_ATTR_ID "='spec' years='2010-2015'/>",
77 }
78 
79 static void
80 range_without_start_year_passes(void **state) {
81  run_one_test("2010-01-01",
82  "<date_spec " XML_ATTR_ID "='spec' years='-2020'/>",
83  pcmk_rc_ok);
84 }
85 
86 static void
87 range_without_end_year_passes(void **state) {
88  run_one_test("2010-01-01",
89  "<date_spec " XML_ATTR_ID "='spec' years='2000-'/>",
90  pcmk_rc_ok);
91  run_one_test("2000-10-01",
92  "<date_spec " XML_ATTR_ID "='spec' years='2000-'/>",
93  pcmk_rc_ok);
94 }
95 
96 static void
97 yeardays_satisfies(void **state) {
98  run_one_test("2020-01-30",
99  "<date_spec " XML_ATTR_ID "='spec' yeardays='30'/>",
100  pcmk_rc_ok);
101 }
102 
103 static void
104 time_after_yeardays_spec(void **state) {
105  run_one_test("2020-02-15",
106  "<date_spec " XML_ATTR_ID "='spec' yeardays='40'/>",
108 }
109 
110 static void
111 yeardays_feb_29_satisfies(void **state) {
112  run_one_test("2016-02-29",
113  "<date_spec " XML_ATTR_ID "='spec' yeardays='60'/>",
114  pcmk_rc_ok);
115 }
116 
117 static void
118 exact_ymd_satisfies(void **state) {
119  run_one_test("2001-12-31",
120  "<date_spec " XML_ATTR_ID "='spec' years='2001' months='12' "
121  "monthdays='31'/>",
122  pcmk_rc_ok);
123 }
124 
125 static void
126 range_in_month_satisfies(void **state) {
127  run_one_test("2001-06-10",
128  "<date_spec " XML_ATTR_ID "='spec' years='2001' months='6' "
129  "monthdays='1-10'/>",
130  pcmk_rc_ok);
131 }
132 
133 static void
134 exact_ymd_after_range(void **state) {
135  run_one_test("2001-12-31",
136  "<date_spec " XML_ATTR_ID "='spec' years='2001' months='12' "
137  "monthdays='30'/>",
139 }
140 
141 static void
142 time_after_monthdays_range(void **state) {
143  run_one_test("2001-06-10",
144  "<date_spec " XML_ATTR_ID "='spec' years='2001' months='6' "
145  "monthdays='11-15'/>",
147 }
148 
149 PCMK__UNIT_TEST(NULL, NULL,
150  cmocka_unit_test(no_time_given),
151  cmocka_unit_test(any_time_satisfies_empty_spec),
152  cmocka_unit_test(time_satisfies_year_spec),
153  cmocka_unit_test(time_after_year_spec),
154  cmocka_unit_test(time_satisfies_year_range),
155  cmocka_unit_test(time_before_year_range),
156  cmocka_unit_test(time_after_year_range),
157  cmocka_unit_test(range_without_start_year_passes),
158  cmocka_unit_test(range_without_end_year_passes),
159  cmocka_unit_test(yeardays_satisfies),
160  cmocka_unit_test(time_after_yeardays_spec),
161  cmocka_unit_test(yeardays_feb_29_satisfies),
162  cmocka_unit_test(exact_ymd_satisfies),
163  cmocka_unit_test(range_in_month_satisfies),
164  cmocka_unit_test(exact_ymd_after_range),
165  cmocka_unit_test(time_after_monthdays_range))
struct crm_time_s crm_time_t
Definition: iso8601.h:32
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))
xmlNode * string2xml(const char *input)
Definition: xml.c:800
int pe_cron_range_satisfied(const crm_time_t *now, const xmlNode *cron_spec)
Definition: rules.c:237
#define XML_ATTR_ID
Definition: msg_xml.h:156
Wrappers for and extensions to libxml2.
void free_xml(xmlNode *child)
Definition: xml.c:783
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:109
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:150