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