pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__evaluate_rsc_expression_test.c
Go to the documentation of this file.
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>
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
27  .rsc_provider = "heartbeat",
28  .rsc_agent = "IPaddr2",
29 };
30 
38 static void
39 assert_rsc_expression(const char *xml_string, int reference_rc)
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)
61 {
62  xmlNode *xml = NULL;
63 
64  assert_int_equal(pcmk__evaluate_rsc_expression(NULL, NULL), EINVAL);
65 
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)
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)
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);
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)
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)
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)
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)
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)
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)
207 {
208  assert_rsc_expression(EXPR_NO_CRITERIA_MATCHES, pcmk_rc_ok);
209 }
210 
211 static void
212 all_match(void **state)
213 {
214  assert_rsc_expression(EXPR_ALL_MATCH, pcmk_rc_ok);
215 }
216 
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))
#define EXPR_NO_STANDARD_MATCHES
Data used to evaluate a rule (any NULL items are ignored)
Definition: rules.h:57
const char * rsc_provider
Resource provider that rule applies to.
Definition: rules.h:63
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
#define PCMK_RESOURCE_CLASS_OCF
Definition: agents.h:27
#define EXPR_NO_PROVIDER_MATCHES
#define EXPR_NO_AGENT_MATCHES
const char * rsc_agent
Resource agent that rule applies to.
Definition: rules.h:64
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
Wrappers for and extensions to libxml2.
const char * rsc_standard
Resource standard that rule applies to.
Definition: rules.h:62
void free_xml(xmlNode *child)
Definition: xml.c:958
int pcmk__xml_test_teardown_group(void **state)
Definition: unittest.c:104
xmlNode * pcmk__xml_parse(const char *input)
Definition: xml_io.c:245
#define EXPR_NO_CRITERIA_MATCHES
G_GNUC_INTERNAL int pcmk__evaluate_rsc_expression(const xmlNode *expr, const pcmk_rule_input_t *rule_input)
Definition: rules.c:1189