pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk__get_ticket_state_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2024-2025 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 <crm/cib/internal.h>
14#include <crm/common/xml.h>
15#include <pacemaker.h>
16
17#include <libxml/xpath.h> // xmlXPathObject, etc.
18
19#include <pacemaker-internal.h>
20
21static char *cib_path = NULL;
22
23static void
24cib_not_connected(void **state)
25{
26 xmlNode *xml = NULL;
27 cib_t *cib = cib_new();
28
29 /* Without any special setup, cib_new() here will use the native CIB which
30 * means IPC calls. But there's nothing listening for those calls, so
31 * signon() will return ENOTCONN. Check that we handle that.
32 */
33 assert_int_equal(pcmk__get_ticket_state(cib, "ticketA", &xml), ENOTCONN);
35}
36
37static int
38setup_test(void **state)
39{
40 cib_path = pcmk__cib_test_copy_cib("tickets.xml");
41
42 if (cib_path == NULL) {
43 return -1;
44 }
45
46 return 0;
47}
48
49static int
50teardown_test(void **state)
51{
52 pcmk__cib_test_cleanup(cib_path);
53 cib_path = NULL;
54 return 0;
55}
56
57static void
58bad_arguments(void **state)
59{
60 xmlNode *xml = NULL;
61 cib_t *cib = cib_new();
62
64
65 pcmk__assert_asserts(pcmk__get_ticket_state(NULL, "ticketA", &xml));
66 pcmk__assert_asserts(pcmk__get_ticket_state(cib, "ticketA", NULL));
67
69}
70
71static void
72unknown_ticket(void **state)
73{
74 xmlNode *xml = NULL;
75 cib_t *cib = cib_new();
76
78
79 assert_int_equal(pcmk__get_ticket_state(cib, "XYZ", &xml), ENXIO);
80
81 pcmk__xml_free(xml);
83}
84
85static void
86ticket_exists(void **state)
87{
88 xmlNode *xml = NULL;
89 xmlXPathObject *xpath_obj = NULL;
90 cib_t *cib = cib_new();
91
93
94 assert_int_equal(pcmk__get_ticket_state(cib, "ticketA", &xml), pcmk_rc_ok);
95
96 /* Verify that the XML result has only one <ticket>, and that its ID is
97 * what we asked for.
98 */
99 xpath_obj = pcmk__xpath_search(xml->doc,
101 "[@" PCMK_XA_ID "=\"ticketA\"]");
102 assert_int_equal(pcmk__xpath_num_results(xpath_obj), 1);
103
104 xmlXPathFreeObject(xpath_obj);
105 pcmk__xml_free(xml);
107}
108
109static void
110multiple_tickets(void **state)
111{
112 xmlNode *xml = NULL;
113 xmlNode *ticket_node = NULL;
114 xmlXPathObject *xpath_obj = NULL;
115 cib_t *cib = cib_new();
116
118
119 assert_int_equal(pcmk__get_ticket_state(cib, NULL, &xml), pcmk_rc_ok);
120
121 /* Verify that the XML result has four <ticket> elements, and that their
122 * IDs are as expected.
123 */
124 xpath_obj = pcmk__xpath_search(xml->doc, "//" PCMK__XE_TICKET_STATE);
125
126 assert_int_equal(pcmk__xpath_num_results(xpath_obj), 4);
127
128 ticket_node = pcmk__xpath_result(xpath_obj, 0);
129 assert_non_null(ticket_node);
130 assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketA");
131
132 ticket_node = pcmk__xpath_result(xpath_obj, 1);
133 assert_non_null(ticket_node);
134 assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketB");
135
136 ticket_node = pcmk__xpath_result(xpath_obj, 2);
137 assert_non_null(ticket_node);
138 assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketC");
139
140 ticket_node = pcmk__xpath_result(xpath_obj, 3);
141 assert_non_null(ticket_node);
142 assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketC");
143
144 xmlXPathFreeObject(xpath_obj);
145 pcmk__xml_free(xml);
147}
148
149static void
150duplicate_tickets(void **state)
151{
152 xmlNode *xml = NULL;
153 xmlXPathObject *xpath_obj = NULL;
154 cib_t *cib = cib_new();
155
157
158 assert_int_equal(pcmk__get_ticket_state(cib, "ticketC", &xml), pcmk_rc_duplicate_id);
159
160 /* Verify that the XML result has two <ticket> elements, and that their
161 * IDs are as expected.
162 */
163 xpath_obj = pcmk__xpath_search(xml->doc,
165 "[@" PCMK_XA_ID "=\"ticketC\"]");
166
167 assert_int_equal(pcmk__xpath_num_results(xpath_obj), 2);
168 xmlXPathFreeObject(xpath_obj);
169 pcmk__xml_free(xml);
171}
172
173/* There are two kinds of tests in this file:
174 *
175 * (1) Those that test what happens if the CIB is not set up correctly, and
176 * (2) Those that test what happens when run against a CIB.
177 *
178 * Therefore, we need two kinds of setup/teardown functions. We only do
179 * minimal overall setup for the entire group, and then setup the CIB for
180 * those tests that need it.
181 */
183 cmocka_unit_test_setup_teardown(cib_not_connected, setup_test, teardown_test),
184 cmocka_unit_test_setup_teardown(bad_arguments, setup_test, teardown_test),
185 cmocka_unit_test_setup_teardown(unknown_ticket, setup_test, teardown_test),
186 cmocka_unit_test_setup_teardown(ticket_exists, setup_test, teardown_test),
187 cmocka_unit_test_setup_teardown(multiple_tickets, setup_test, teardown_test),
188 cmocka_unit_test_setup_teardown(duplicate_tickets, setup_test, teardown_test))
int cib__clean_up_connection(cib_t **cib)
Definition cib_utils.c:942
cib_t * cib_new(void)
Create a new CIB connection object.
Definition cib_client.c:562
@ cib_command
Definition cib_types.h:46
char * crm_system_name
Definition utils.c:45
High Level API.
int pcmk__get_ticket_state(cib_t *cib, const char *ticket_id, xmlNode **state)
Definition pcmk_ticket.c:78
@ pcmk_rc_ok
Definition results.h:159
@ pcmk_rc_duplicate_id
Definition results.h:121
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition cib_types.h:124
cib_api_operations_t * cmds
Definition cib_types.h:325
void pcmk__cib_test_cleanup(char *out_path)
Definition unittest.c:148
#define pcmk__assert_asserts(stmt)
int pcmk__xml_test_teardown_group(void **state)
Definition unittest.c:105
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int pcmk__xml_test_setup_group(void **state)
Definition unittest.c:87
char * pcmk__cib_test_copy_cib(const char *in_file)
Definition unittest.c:113
Wrappers for and extensions to libxml2.
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
void pcmk__xml_free(xmlNode *xml)
Definition xml.c:816
#define PCMK_XA_ID
Definition xml_names.h:301
#define PCMK__XE_TICKET_STATE
xmlXPathObject * pcmk__xpath_search(xmlDoc *doc, const char *path)
Definition xpath.c:137
xmlNode * pcmk__xpath_result(xmlXPathObject *xpath_obj, int index)
Definition xpath.c:65