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

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

DEFINITIONS

This source file includes following definitions.
  1. assert_op_expression
  2. null_invalid
  3. id_missing
  4. name_missing
  5. input_name_missing
  6. fail_name
  7. invalid_interval
  8. default_interval
  9. fail_interval
  10. match_both
  11. fail_both

   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 <stdio.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 /*
  21  * Shared data
  22  */
  23 
  24 static pcmk_rule_input_t rule_input = {
  25     // These are the only members used to evaluate operation expressions
  26     .op_name = PCMK_ACTION_MONITOR,
  27     .op_interval_ms = 10000,
  28 };
  29 
  30 /*!
  31  * \internal
  32  * \brief Run one test, comparing return value
  33  *
  34  * \param[in] xml_string    Operation expression XML as string
  35  * \param[in] reference_rc  Assert that evaluation result equals this
  36  */
  37 static void
  38 assert_op_expression(const char *xml_string, int reference_rc)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40     xmlNode *xml = pcmk__xml_parse(xml_string);
  41 
  42     assert_int_equal(pcmk__evaluate_op_expression(xml, &rule_input),
  43                      reference_rc);
  44     free_xml(xml);
  45 }
  46 
  47 
  48 /*
  49  * Invalid arguments
  50  */
  51 
  52 #define EXPR_FAIL_BOTH                                      \
  53         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
  54         PCMK_XA_NAME "='" PCMK_ACTION_START "' "            \
  55         PCMK_XA_INTERVAL "='0' />"
  56 
  57 static void
  58 null_invalid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60     xmlNode *xml = NULL;
  61 
  62     assert_int_equal(pcmk__evaluate_op_expression(NULL, NULL), EINVAL);
  63 
  64     xml = pcmk__xml_parse(EXPR_FAIL_BOTH);
  65     assert_int_equal(pcmk__evaluate_op_expression(xml, NULL), EINVAL);
  66     free_xml(xml);
  67 
  68     assert_op_expression(NULL, EINVAL);
  69 }
  70 
  71 
  72 /*
  73  * Test PCMK_XA_ID
  74  */
  75 
  76 #define EXPR_ID_MISSING                                 \
  77         "<" PCMK_XE_OP_EXPRESSION " "                   \
  78         PCMK_XA_NAME "='" PCMK_ACTION_MONITOR "' "      \
  79         PCMK_XA_INTERVAL "='10s' />"
  80 
  81 #define EXPR_ID_EMPTY                                   \
  82         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='' " \
  83         PCMK_XA_NAME "='" PCMK_ACTION_MONITOR "' "      \
  84         PCMK_XA_INTERVAL "='10s' />"
  85 
  86 static void
  87 id_missing(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89     // Currently acceptable
  90     assert_op_expression(EXPR_ID_MISSING, pcmk_rc_ok);
  91     assert_op_expression(EXPR_ID_EMPTY, pcmk_rc_ok);
  92 }
  93 
  94 
  95 /*
  96  * Test PCMK_XA_NAME
  97  */
  98 
  99 #define EXPR_NAME_MISSING                                   \
 100         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
 101         PCMK_XA_INTERVAL "='10s' />"
 102 
 103 static void
 104 name_missing(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106     assert_op_expression(EXPR_NAME_MISSING, pcmk_rc_unpack_error);
 107 }
 108 
 109 #define EXPR_MATCH_BOTH                                     \
 110         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
 111         PCMK_XA_NAME "='" PCMK_ACTION_MONITOR "' "          \
 112         PCMK_XA_INTERVAL "='10s' />"
 113 
 114 #define EXPR_EMPTY_NAME                                     \
 115         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
 116         PCMK_XA_NAME "='' " PCMK_XA_INTERVAL "='10s' />"
 117 
 118 static void
 119 input_name_missing(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121     rule_input.op_name = NULL;
 122     assert_op_expression(EXPR_MATCH_BOTH, pcmk_rc_op_unsatisfied);
 123     assert_op_expression(EXPR_EMPTY_NAME, pcmk_rc_op_unsatisfied);
 124     rule_input.op_name = PCMK_ACTION_MONITOR;
 125 }
 126 
 127 #define EXPR_FAIL_NAME                                      \
 128         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
 129         PCMK_XA_NAME "='" PCMK_ACTION_START "' "            \
 130         PCMK_XA_INTERVAL "='10s' />"
 131 
 132 static void
 133 fail_name(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 134 {
 135     assert_op_expression(EXPR_FAIL_NAME, pcmk_rc_op_unsatisfied);
 136 
 137     // An empty name is meaningless but accepted, so not an unpack error
 138     assert_op_expression(EXPR_EMPTY_NAME, pcmk_rc_op_unsatisfied);
 139 }
 140 
 141 
 142 /*
 143  * Test PCMK_XA_INTERVAL
 144  */
 145 
 146 #define EXPR_EMPTY_INTERVAL                                 \
 147         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
 148         PCMK_XA_NAME "='" PCMK_ACTION_MONITOR "' "          \
 149         PCMK_XA_INTERVAL "='' />"
 150 
 151 #define EXPR_INVALID_INTERVAL                               \
 152         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
 153         PCMK_XA_NAME "='" PCMK_ACTION_MONITOR "' "          \
 154         PCMK_XA_INTERVAL "='not-an-interval' />"
 155 
 156 static void
 157 invalid_interval(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159     assert_op_expression(EXPR_EMPTY_INTERVAL, pcmk_rc_unpack_error);
 160     assert_op_expression(EXPR_INVALID_INTERVAL, pcmk_rc_unpack_error);
 161 }
 162 
 163 #define EXPR_DEFAULT_INTERVAL                               \
 164         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
 165         PCMK_XA_NAME "='" PCMK_ACTION_MONITOR "' />"
 166 
 167 static void
 168 default_interval(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170     assert_op_expression(EXPR_DEFAULT_INTERVAL, pcmk_rc_ok);
 171 }
 172 
 173 #define EXPR_FAIL_INTERVAL                                  \
 174         "<" PCMK_XE_OP_EXPRESSION " " PCMK_XA_ID "='e' "    \
 175         PCMK_XA_NAME "='" PCMK_ACTION_MONITOR "' "          \
 176         PCMK_XA_INTERVAL "='9s' />"
 177 
 178 static void
 179 fail_interval(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181     assert_op_expression(EXPR_FAIL_INTERVAL, pcmk_rc_op_unsatisfied);
 182 }
 183 
 184 
 185 static void
 186 match_both(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188     assert_op_expression(EXPR_MATCH_BOTH, pcmk_rc_ok);
 189 }
 190 
 191 static void
 192 fail_both(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 193 {
 194     assert_op_expression(EXPR_FAIL_BOTH, pcmk_rc_op_unsatisfied);
 195 }
 196 
 197 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 198                 cmocka_unit_test(null_invalid),
 199                 cmocka_unit_test(id_missing),
 200                 cmocka_unit_test(name_missing),
 201                 cmocka_unit_test(input_name_missing),
 202                 cmocka_unit_test(fail_name),
 203                 cmocka_unit_test(invalid_interval),
 204                 cmocka_unit_test(default_interval),
 205                 cmocka_unit_test(fail_interval),
 206                 cmocka_unit_test(match_both),
 207                 cmocka_unit_test(fail_both))

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