pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk_ticket_constraints_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 static char *cib_path = NULL;
18 
19 static void
20 cib_not_connected(void **state)
21 {
22  xmlNode *xml = NULL;
23 
24  /* Without any special setup, cib_new() in pcmk_ticket_constraints will use the
25  * native CIB which means IPC calls. But there's nothing listening for those
26  * calls, so signon() will return ENOTCONN. Check that we handle that.
27  */
28  assert_int_equal(pcmk_ticket_constraints(&xml, NULL), ENOTCONN);
30  free_xml(xml);
31 }
32 
33 static int
34 setup_test(void **state)
35 {
36  cib_path = pcmk__cib_test_copy_cib("tickets.xml");
37 
38  if (cib_path == NULL) {
39  return -1;
40  }
41 
42  return 0;
43 }
44 
45 static int
46 teardown_test(void **state)
47 {
48  pcmk__cib_test_cleanup(cib_path);
49  cib_path = NULL;
50  return 0;
51 }
52 
53 static void
54 invalid_argument(void **state)
55 {
56  assert_int_equal(pcmk_ticket_constraints(NULL, "ticketA"), EINVAL);
57 }
58 
59 static void
60 unknown_ticket(void **state)
61 {
62  xmlNode *xml = NULL;
63 
64  assert_int_equal(pcmk_ticket_constraints(&xml, "XYZ"), ENXIO);
66  free_xml(xml);
67 }
68 
69 static void
70 ticket_exists(void **state)
71 {
72  xmlNode *xml = NULL;
73  xmlXPathObject *xpath_obj = NULL;
74 
75  assert_int_equal(pcmk_ticket_constraints(&xml, "ticketA"), pcmk_rc_ok);
77 
78  /* Verify that the XML result has only one <ticket>, and that its ID is
79  * what we asked for.
80  */
81  xpath_obj = xpath_search(xml, "//" PCMK_XE_PACEMAKER_RESULT "/" PCMK_XE_TICKETS
82  "/" PCMK_XE_TICKET "[@" PCMK_XA_ID "=\"ticketA\"]");
83 
84  assert_int_equal(numXpathResults(xpath_obj), 1);
85  freeXpathObject(xpath_obj);
86  free_xml(xml);
87 }
88 
89 static void
90 multiple_tickets(void **state)
91 {
92  xmlNode *xml = NULL;
93  xmlNode *ticket_node = NULL;
94  xmlXPathObject *xpath_obj = NULL;
95 
96  assert_int_equal(pcmk_ticket_constraints(&xml, NULL), pcmk_rc_ok);
98 
99  /* Verify that the XML result has two <ticket> elements, and that their
100  * IDs are as expected.
101  */
103 
104  assert_int_equal(numXpathResults(xpath_obj), 2);
105 
106  ticket_node = getXpathResult(xpath_obj, 0);
107  assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketA");
108 
109  ticket_node = getXpathResult(xpath_obj, 1);
110  assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketB");
111 
112  freeXpathObject(xpath_obj);
113  free_xml(xml);
114 }
115 
116 /* There are two kinds of tests in this file:
117  *
118  * (1) Those that test what happens if the CIB is not set up correctly, and
119  * (2) Those that test what happens when run against a CIB.
120  *
121  * Therefore, we need two kinds of setup/teardown functions. We only do
122  * minimal overall setup for the entire group, and then setup the CIB for
123  * those tests that need it.
124  */
126  cmocka_unit_test(cib_not_connected),
127  cmocka_unit_test_setup_teardown(invalid_argument, setup_test, teardown_test),
128  cmocka_unit_test_setup_teardown(unknown_ticket, setup_test, teardown_test),
129  cmocka_unit_test_setup_teardown(ticket_exists, setup_test, teardown_test),
130  cmocka_unit_test_setup_teardown(multiple_tickets, setup_test, teardown_test))
int pcmk_ticket_constraints(xmlNodePtr *xml, const char *ticket_id)
Return constraints that apply to the given ticket.
Definition: pcmk_ticket.c:145
#define PCMK_XE_PACEMAKER_RESULT
Definition: xml_names.h:153
High Level API.
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
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_XA_ID
Definition: xml_names.h:296
void free_xml(xmlNode *child)
Definition: xml.c:867
#define PCMK_XE_TICKETS
Definition: xml_names.h:208
void pcmk__cib_test_cleanup(char *out_path)
Definition: unittest.c:121
#define PCMK_XE_TICKET
Definition: xml_names.h:207
xmlXPathObjectPtr xpath_search(const xmlNode *xml_top, const char *path)
Definition: xpath.c:139
void pcmk__assert_validates(xmlNode *xml)
Definition: unittest.c:20
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