pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk_ticket_info_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 int
20 setup_test(void **state)
21 {
22  cib_path = pcmk__cib_test_copy_cib("tickets.xml");
23 
24  if (cib_path == NULL) {
25  return -1;
26  }
27 
28  return 0;
29 }
30 
31 static int
32 teardown_test(void **state)
33 {
34  pcmk__cib_test_cleanup(cib_path);
35  cib_path = NULL;
36  return 0;
37 }
38 
39 static void
40 bad_arguments(void **state)
41 {
42  assert_int_equal(pcmk_ticket_info(NULL, "ticketA"), EINVAL);
43 }
44 
45 static void
46 unknown_ticket(void **state)
47 {
48  xmlNode *xml = NULL;
49 
50  assert_int_equal(pcmk_ticket_info(&xml, "XYZ"), ENXIO);
52  free_xml(xml);
53 }
54 
55 static void
56 all_tickets(void **state)
57 {
58  xmlNode *node = NULL;
59  xmlXPathObject *xpath_obj = NULL;
60  xmlNode *xml = NULL;
61 
62  assert_int_equal(pcmk_ticket_info(&xml, NULL), pcmk_rc_ok);
64 
65  /* Verify that the XML result has three <ticket> elements, with the attributes
66  * we expect. The input has four tickets, but when they are loaded into the
67  * scheduler's hash table, the duplicate IDs will collide leaving us with
68  * three.
69  */
71  assert_int_equal(numXpathResults(xpath_obj), 3);
72  freeXpathObject(xpath_obj);
73 
74  xpath_obj = xpath_search(xml, "//" PCMK_XE_PACEMAKER_RESULT "/" PCMK_XE_TICKETS
75  "/" PCMK_XE_TICKET "[@" PCMK_XA_ID "=\"ticketA\"]");
76 
77  node = getXpathResult(xpath_obj, 0);
78  assert_string_equal(crm_element_value(node, PCMK_XA_STATUS), PCMK_VALUE_REVOKED);
79  assert_string_equal(crm_element_value(node, PCMK__XA_GRANTED), "false");
80  assert_string_equal(crm_element_value(node, PCMK_XA_STANDBY), PCMK_VALUE_FALSE);
81  assert_string_equal(crm_element_value(node, "owner"), "1");
82  freeXpathObject(xpath_obj);
83 
84  xpath_obj = xpath_search(xml, "//" PCMK_XE_PACEMAKER_RESULT "/" PCMK_XE_TICKETS
85  "/" PCMK_XE_TICKET "[@" PCMK_XA_ID "=\"ticketB\"]");
86 
87  node = getXpathResult(xpath_obj, 0);
88  assert_string_equal(crm_element_value(node, PCMK_XA_STATUS), PCMK_VALUE_GRANTED);
89  assert_string_equal(crm_element_value(node, PCMK__XA_GRANTED), "true");
90  assert_string_equal(crm_element_value(node, PCMK_XA_STANDBY), PCMK_VALUE_FALSE);
91  assert_null(crm_element_value(node, "owner"));
92  freeXpathObject(xpath_obj);
93 
94  xpath_obj = xpath_search(xml, "//" PCMK_XE_PACEMAKER_RESULT "/" PCMK_XE_TICKETS
95  "/" PCMK_XE_TICKET "[@" PCMK_XA_ID "=\"ticketC\"]");
96 
97  node = getXpathResult(xpath_obj, 0);
98  assert_string_equal(crm_element_value(node, PCMK_XA_STATUS), PCMK_VALUE_GRANTED);
99  assert_string_equal(crm_element_value(node, PCMK__XA_GRANTED), "true");
100  assert_string_equal(crm_element_value(node, PCMK_XA_STANDBY), PCMK_VALUE_FALSE);
101  assert_null(crm_element_value(node, "owner"));
102 
103  freeXpathObject(xpath_obj);
104  free_xml(xml);
105 }
106 
107 static void
108 single_ticket(void **state)
109 {
110  xmlNode *node = NULL;
111  xmlXPathObject *xpath_obj = NULL;
112  xmlNode *xml = NULL;
113 
114  assert_int_equal(pcmk_ticket_info(&xml, "ticketA"), pcmk_rc_ok);
116 
117  /* Verify that the XML result has only one <ticket>, with the attributes
118  * we expect.
119  */
120  xpath_obj = xpath_search(xml, "//" PCMK_XE_PACEMAKER_RESULT "/" PCMK_XE_TICKETS
121  "/" PCMK_XE_TICKET "[@" PCMK_XA_ID "=\"ticketA\"]");
122  assert_int_equal(numXpathResults(xpath_obj), 1);
123 
124  node = getXpathResult(xpath_obj, 0);
125  assert_string_equal(crm_element_value(node, PCMK_XA_STATUS), PCMK_VALUE_REVOKED);
126  assert_string_equal(crm_element_value(node, PCMK__XA_GRANTED), "false");
127  assert_string_equal(crm_element_value(node, PCMK_XA_STANDBY), PCMK_VALUE_FALSE);
128  assert_string_equal(crm_element_value(node, "owner"), "1");
129 
130  freeXpathObject(xpath_obj);
131  free_xml(xml);
132 }
133 
135  cmocka_unit_test_setup_teardown(bad_arguments, setup_test, teardown_test),
136  cmocka_unit_test_setup_teardown(unknown_ticket, setup_test, teardown_test),
137  cmocka_unit_test_setup_teardown(all_tickets, setup_test, teardown_test),
138  cmocka_unit_test_setup_teardown(single_ticket, setup_test, teardown_test))
#define PCMK_XE_PACEMAKER_RESULT
Definition: xml_names.h:153
#define PCMK_VALUE_FALSE
Definition: options.h:152
#define PCMK_VALUE_GRANTED
Definition: options.h:156
int pcmk_ticket_info(xmlNodePtr *xml, const char *ticket_id)
Return information about the given ticket.
Definition: pcmk_ticket.c:343
High Level API.
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
#define PCMK_VALUE_REVOKED
Definition: options.h:201
#define PCMK_XA_STANDBY
Definition: xml_names.h:401
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
#define PCMK_XA_STATUS
Definition: xml_names.h:405
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
#define PCMK__XA_GRANTED
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
Definition: xpath.c:58
void freeXpathObject(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:39