pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__evaluate_op_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 operation expressions
27  .op_interval_ms = 10000,
28 };
29 
37 static void
38 assert_op_expression(const char *xml_string, int reference_rc)
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)
59 {
60  xmlNode *xml = NULL;
61 
62  assert_int_equal(pcmk__evaluate_op_expression(NULL, NULL), EINVAL);
63 
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)
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)
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)
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)
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)
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)
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)
180 {
181  assert_op_expression(EXPR_FAIL_INTERVAL, pcmk_rc_op_unsatisfied);
182 }
183 
184 
185 static void
186 match_both(void **state)
187 {
188  assert_op_expression(EXPR_MATCH_BOTH, pcmk_rc_ok);
189 }
190 
191 static void
192 fail_both(void **state)
193 {
194  assert_op_expression(EXPR_FAIL_BOTH, pcmk_rc_op_unsatisfied);
195 }
196 
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))
Data used to evaluate a rule (any NULL items are ignored)
Definition: rules.h:57
const char * op_name
Definition: rules.h:67
#define PCMK_ACTION_MONITOR
Definition: actions.h:60
G_GNUC_INTERNAL int pcmk__evaluate_op_expression(const xmlNode *expr, const pcmk_rule_input_t *rule_input)
Definition: rules.c:1260
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
#define EXPR_NAME_MISSING
#define EXPR_DEFAULT_INTERVAL
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
Wrappers for and extensions to libxml2.
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_FAIL_INTERVAL
#define EXPR_INVALID_INTERVAL
#define EXPR_EMPTY_INTERVAL