pacemaker  2.1.0-7c3f660
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pe_cron_range_satisfied_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020-2021 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 Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <glib.h>
11 
12 #include <crm/common/xml.h>
14 
15 static void
16 run_one_test(const char *t, const char *x, int expected) {
17  crm_time_t *tm = crm_time_new(t);
18  xmlNodePtr xml = string2xml(x);
19 
20  g_assert_cmpint(pe_cron_range_satisfied(tm, xml), ==, expected);
21 
22  crm_time_free(tm);
23  free_xml(xml);
24 }
25 
26 static void
27 no_time_given(void) {
28  g_assert_cmpint(pe_cron_range_satisfied(NULL, NULL), ==, pcmk_rc_op_unsatisfied);
29 }
30 
31 static void
32 any_time_satisfies_empty_spec(void) {
33  crm_time_t *tm = crm_time_new(NULL);
34 
35  g_assert_cmpint(pe_cron_range_satisfied(tm, NULL), ==, pcmk_rc_ok);
36 
37  crm_time_free(tm);
38 }
39 
40 static void
41 time_satisfies_year_spec(void) {
42  run_one_test("2020-01-01", "<date_spec id='spec' years='2020'/>", pcmk_rc_ok);
43 }
44 
45 static void
46 time_after_year_spec(void) {
47  run_one_test("2020-01-01", "<date_spec id='spec' years='2019'/>", pcmk_rc_after_range);
48 }
49 
50 static void
51 time_satisfies_year_range(void) {
52  run_one_test("2020-01-01", "<date_spec id='spec' years='2010-2030'/>", pcmk_rc_ok);
53 }
54 
55 static void
56 time_before_year_range(void) {
57  run_one_test("2000-01-01", "<date_spec id='spec' years='2010-2030'/>", pcmk_rc_before_range);
58 }
59 
60 static void
61 time_after_year_range(void) {
62  run_one_test("2020-01-01", "<date_spec id='spec' years='2010-2015'/>", pcmk_rc_after_range);
63 }
64 
65 static void
66 range_without_start_year_passes(void) {
67  run_one_test("2010-01-01", "<date_spec id='spec' years='-2020'/>", pcmk_rc_ok);
68 }
69 
70 static void
71 range_without_end_year_passes(void) {
72  run_one_test("2010-01-01", "<date_spec id='spec' years='2000-'/>", pcmk_rc_ok);
73  run_one_test("2000-10-01", "<date_spec id='spec' years='2000-'/>", pcmk_rc_ok);
74 }
75 
76 static void
77 yeardays_satisfies(void) {
78  run_one_test("2020-01-30", "<date_spec id='spec' yeardays='30'/>", pcmk_rc_ok);
79 }
80 
81 static void
82 time_after_yeardays_spec(void) {
83  run_one_test("2020-02-15", "<date_spec id='spec' yeardays='40'/>", pcmk_rc_after_range);
84 }
85 
86 static void
87 yeardays_feb_29_satisfies(void) {
88  run_one_test("2016-02-29", "<date_spec id='spec' yeardays='60'/>", pcmk_rc_ok);
89 }
90 
91 static void
92 exact_ymd_satisfies(void) {
93  run_one_test("2001-12-31", "<date_spec id='spec' years='2001' months='12' monthdays='31'/>", pcmk_rc_ok);
94 }
95 
96 static void
97 range_in_month_satisfies(void) {
98  run_one_test("2001-06-10", "<date_spec id='spec' years='2001' months='6' monthdays='1-10'/>", pcmk_rc_ok);
99 }
100 
101 static void
102 exact_ymd_after_range(void) {
103  run_one_test("2001-12-31", "<date_spec id='spec' years='2001' months='12' monthdays='30'/>", pcmk_rc_after_range);
104 }
105 
106 static void
107 time_after_monthdays_range(void) {
108  run_one_test("2001-06-10", "<date_spec id='spec' years='2001' months='6' monthdays='11-15'/>", pcmk_rc_before_range);
109 }
110 
111 int main(int argc, char **argv) {
112  g_test_init(&argc, &argv, NULL);
113 
114  g_test_add_func("/pengine/rules/cron_range/no_time_given", no_time_given);
115  g_test_add_func("/pengine/rules/cron_range/empty_spec", any_time_satisfies_empty_spec);
116  g_test_add_func("/pengine/rules/cron_range/year/time_satisfies", time_satisfies_year_spec);
117  g_test_add_func("/pengine/rules/cron_range/year/time_after", time_after_year_spec);
118  g_test_add_func("/pengine/rules/cron_range/range/time_satisfies_year", time_satisfies_year_range);
119  g_test_add_func("/pengine/rules/cron_range/range/time_before_year", time_before_year_range);
120  g_test_add_func("/pengine/rules/cron_range/range/time_after_year", time_after_year_range);
121  g_test_add_func("/pengine/rules/cron_range/range/no_start_year_passes", range_without_start_year_passes);
122  g_test_add_func("/pengine/rules/cron_range/range/no_end_year_passes", range_without_end_year_passes);
123 
124  g_test_add_func("/pengine/rules/cron_range/yeardays/satisfies", yeardays_satisfies);
125  g_test_add_func("/pengine/rules/cron_range/yeardays/time_after", time_after_yeardays_spec);
126  g_test_add_func("/pengine/rules/cron_range/yeardays/feb_29_sasitfies", yeardays_feb_29_satisfies);
127 
128  g_test_add_func("/pengine/rules/cron_range/exact/ymd_satisfies", exact_ymd_satisfies);
129  g_test_add_func("/pengine/rules/cron_range/range/in_month_satisfies", range_in_month_satisfies);
130  g_test_add_func("/pengine/rules/cron_range/exact/ymd_after_range", exact_ymd_after_range);
131  g_test_add_func("/pengine/rules/cron_range/range/in_month_after", time_after_monthdays_range);
132  return g_test_run();
133 }
int pe_cron_range_satisfied(crm_time_t *now, xmlNode *cron_spec)
Definition: rules.c:243
struct crm_time_s crm_time_t
Definition: iso8601.h:32
int main(int argc, char **argv)
xmlNode * string2xml(const char *input)
Definition: xml.c:868
Wrappers for and extensions to libxml2.
void free_xml(xmlNode *child)
Definition: xml.c:823
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:93
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:141