root/lib/common/tests/rules/pcmk__evaluate_date_spec_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. run_one_test
  2. null_invalid
  3. spec_id_missing
  4. invalid_range
  5. time_satisfies_year_spec
  6. time_after_year_spec
  7. time_satisfies_year_range
  8. time_before_year_range
  9. time_after_year_range
  10. range_without_start_year_passes
  11. range_without_end_year_passes
  12. yeardays_satisfies
  13. time_after_yeardays_spec
  14. yeardays_feb_29_satisfies
  15. exact_ymd_satisfies
  16. range_in_month_satisfies
  17. exact_ymd_after_range
  18. time_after_monthdays_range

   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>
  16 #include <crm/common/rules_internal.h>
  17 #include <crm/common/unittest_internal.h>
  18 #include "crmcommon_private.h"
  19 
  20 static void
  21 run_one_test(const char *t, const char *x, int expected)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  73 {
  74     run_one_test("2020-01-01",
  75                  "<" PCMK_XE_DATE_SPEC " "
  76                      PCMK_XA_ID "='spec' "
  77                      PCMK_XA_YEARS "='2019'/>",
  78                  pcmk_rc_after_range);
  79 }
  80 
  81 static void
  82 time_satisfies_year_range(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94     run_one_test("2000-01-01",
  95                  "<" PCMK_XE_DATE_SPEC " "
  96                      PCMK_XA_ID "='spec' "
  97                      PCMK_XA_YEARS "='2010-2030'/>",
  98                  pcmk_rc_before_range);
  99 }
 100 
 101 static void
 102 time_after_year_range(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104     run_one_test("2020-01-01",
 105                  "<" PCMK_XE_DATE_SPEC " "
 106                      PCMK_XA_ID "='spec' "
 107                      PCMK_XA_YEARS "='2010-2015'/>",
 108                  pcmk_rc_after_range);
 109 }
 110 
 111 static void
 112 range_without_start_year_passes(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 148 {
 149     run_one_test("2020-02-15",
 150                  "<" PCMK_XE_DATE_SPEC " "
 151                      PCMK_XA_ID "='spec' "
 152                      PCMK_XA_YEARDAYS "='40'/>",
 153                  pcmk_rc_after_range);
 154 }
 155 
 156 static void
 157 yeardays_feb_29_satisfies(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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'/>",
 199                  pcmk_rc_after_range);
 200 }
 201 
 202 static void
 203 time_after_monthdays_range(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 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'/>",
 211                  pcmk_rc_before_range);
 212 }
 213 
 214 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 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))

/* [previous][next][first][last][top][bottom][index][help] */