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

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

DEFINITIONS

This source file includes following definitions.
  1. assert_rsc_expression
  2. null_invalid
  3. id_missing
  4. fail_standard
  5. fail_provider
  6. fail_agent
  7. no_standard_matches
  8. no_provider_matches
  9. no_agent_matches
  10. no_criteria_matches
  11. all_match

   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 resource expressions
  26     .rsc_standard = PCMK_RESOURCE_CLASS_OCF,
  27     .rsc_provider = "heartbeat",
  28     .rsc_agent = "IPaddr2",
  29 };
  30 
  31 /*!
  32  * \internal
  33  * \brief Run one test, comparing return value
  34  *
  35  * \param[in] xml_string    Resource expression XML as string
  36  * \param[in] reference_rc  Assert that evaluation result equals this
  37  */
  38 static void
  39 assert_rsc_expression(const char *xml_string, int reference_rc)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41     xmlNode *xml = pcmk__xml_parse(xml_string);
  42 
  43     assert_int_equal(pcmk__evaluate_rsc_expression(xml, &rule_input),
  44                      reference_rc);
  45     free_xml(xml);
  46 }
  47 
  48 
  49 /*
  50  * Invalid arguments
  51  */
  52 
  53 #define EXPR_ALL_MATCH                                      \
  54         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
  55         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' "     \
  56         PCMK_XA_PROVIDER "='heartbeat' "                    \
  57         PCMK_XA_TYPE "='IPaddr2' />"
  58 
  59 static void
  60 null_invalid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  61 {
  62     xmlNode *xml = NULL;
  63 
  64     assert_int_equal(pcmk__evaluate_rsc_expression(NULL, NULL), EINVAL);
  65 
  66     xml = pcmk__xml_parse(EXPR_ALL_MATCH);
  67     assert_int_equal(pcmk__evaluate_rsc_expression(xml, NULL), EINVAL);
  68     free_xml(xml);
  69 
  70     assert_rsc_expression(NULL, EINVAL);
  71 }
  72 
  73 
  74 /*
  75  * Test PCMK_XA_ID
  76  */
  77 
  78 #define EXPR_ID_MISSING                                 \
  79         "<" PCMK_XE_RSC_EXPRESSION " "                  \
  80         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' " \
  81         PCMK_XA_PROVIDER "='heartbeat' "                \
  82         PCMK_XA_TYPE "='IPaddr2' />"
  83 
  84 #define EXPR_ID_EMPTY                                       \
  85         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='' "    \
  86         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' "     \
  87         PCMK_XA_PROVIDER "='heartbeat' "                    \
  88         PCMK_XA_TYPE "='IPaddr2' />"
  89 
  90 static void
  91 id_missing(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  92 {
  93     // Currently acceptable
  94     assert_rsc_expression(EXPR_ID_MISSING, pcmk_rc_ok);
  95     assert_rsc_expression(EXPR_ID_EMPTY, pcmk_rc_ok);
  96 }
  97 
  98 
  99 /*
 100  * Test standard, provider, and agent
 101  */
 102 
 103 #define EXPR_FAIL_STANDARD                                  \
 104         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 105         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_LSB "' />"
 106 
 107 #define EXPR_EMPTY_STANDARD                                 \
 108         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 109         PCMK_XA_CLASS "='' />"
 110 
 111 static void
 112 fail_standard(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114     assert_rsc_expression(EXPR_FAIL_STANDARD, pcmk_rc_op_unsatisfied);
 115     assert_rsc_expression(EXPR_EMPTY_STANDARD, pcmk_rc_op_unsatisfied);
 116 
 117     rule_input.rsc_standard = NULL;
 118     assert_rsc_expression(EXPR_FAIL_STANDARD, pcmk_rc_op_unsatisfied);
 119     assert_rsc_expression(EXPR_EMPTY_STANDARD, pcmk_rc_op_unsatisfied);
 120     rule_input.rsc_standard = PCMK_RESOURCE_CLASS_OCF;
 121 }
 122 
 123 #define EXPR_FAIL_PROVIDER                                  \
 124         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 125         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' "     \
 126         PCMK_XA_PROVIDER "='pacemaker' "                    \
 127         PCMK_XA_TYPE "='IPaddr2' />"
 128 
 129 #define EXPR_EMPTY_PROVIDER                                 \
 130         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 131         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' "     \
 132         PCMK_XA_PROVIDER "='' " PCMK_XA_TYPE "='IPaddr2' />"
 133 
 134 static void
 135 fail_provider(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 136 {
 137     assert_rsc_expression(EXPR_FAIL_PROVIDER, pcmk_rc_op_unsatisfied);
 138     assert_rsc_expression(EXPR_EMPTY_PROVIDER, pcmk_rc_op_unsatisfied);
 139 
 140     rule_input.rsc_provider = NULL;
 141     assert_rsc_expression(EXPR_FAIL_PROVIDER, pcmk_rc_op_unsatisfied);
 142     assert_rsc_expression(EXPR_EMPTY_PROVIDER, pcmk_rc_op_unsatisfied);
 143     rule_input.rsc_provider = "heartbeat";
 144 }
 145 
 146 #define EXPR_FAIL_AGENT                                     \
 147         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 148         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' "     \
 149         PCMK_XA_PROVIDER "='heartbeat' "                    \
 150         PCMK_XA_TYPE "='IPaddr3' />"
 151 
 152 #define EXPR_EMPTY_AGENT                                    \
 153         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 154         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' "     \
 155         PCMK_XA_PROVIDER "='heartbeat' " PCMK_XA_TYPE "='' />"
 156 
 157 static void
 158 fail_agent(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 159 {
 160     assert_rsc_expression(EXPR_FAIL_AGENT, pcmk_rc_op_unsatisfied);
 161     assert_rsc_expression(EXPR_EMPTY_AGENT, pcmk_rc_op_unsatisfied);
 162 
 163     rule_input.rsc_agent = NULL;
 164     assert_rsc_expression(EXPR_FAIL_AGENT, pcmk_rc_op_unsatisfied);
 165     assert_rsc_expression(EXPR_EMPTY_AGENT, pcmk_rc_op_unsatisfied);
 166     rule_input.rsc_agent = "IPaddr2";
 167 }
 168 
 169 #define EXPR_NO_STANDARD_MATCHES                            \
 170         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 171         PCMK_XA_PROVIDER "='heartbeat' "                    \
 172         PCMK_XA_TYPE "='IPaddr2' />"
 173 
 174 static void
 175 no_standard_matches(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 176 {
 177     assert_rsc_expression(EXPR_NO_STANDARD_MATCHES, pcmk_rc_ok);
 178 }
 179 
 180 #define EXPR_NO_PROVIDER_MATCHES                            \
 181         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 182         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' "     \
 183         PCMK_XA_TYPE "='IPaddr2' />"
 184 
 185 static void
 186 no_provider_matches(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188     assert_rsc_expression(EXPR_NO_PROVIDER_MATCHES, pcmk_rc_ok);
 189 }
 190 
 191 #define EXPR_NO_AGENT_MATCHES                               \
 192         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' "   \
 193         PCMK_XA_CLASS "='" PCMK_RESOURCE_CLASS_OCF "' "     \
 194         PCMK_XA_PROVIDER "='heartbeat' />"
 195 
 196 static void
 197 no_agent_matches(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199     assert_rsc_expression(EXPR_NO_AGENT_MATCHES, pcmk_rc_ok);
 200 }
 201 
 202 #define EXPR_NO_CRITERIA_MATCHES    \
 203         "<" PCMK_XE_RSC_EXPRESSION " " PCMK_XA_ID "='e' />"
 204 
 205 static void
 206 no_criteria_matches(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208     assert_rsc_expression(EXPR_NO_CRITERIA_MATCHES, pcmk_rc_ok);
 209 }
 210 
 211 static void
 212 all_match(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 213 {
 214     assert_rsc_expression(EXPR_ALL_MATCH, pcmk_rc_ok);
 215 }
 216 
 217 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL,
 218                 cmocka_unit_test(null_invalid),
 219                 cmocka_unit_test(id_missing),
 220                 cmocka_unit_test(fail_standard),
 221                 cmocka_unit_test(fail_provider),
 222                 cmocka_unit_test(fail_agent),
 223                 cmocka_unit_test(no_standard_matches),
 224                 cmocka_unit_test(no_provider_matches),
 225                 cmocka_unit_test(no_agent_matches),
 226                 cmocka_unit_test(no_criteria_matches),
 227                 cmocka_unit_test(all_match))

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