pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk_ticket_get_attr_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  xmlNode *xml = NULL;
43 
44  assert_int_equal(pcmk_ticket_get_attr(NULL, "ticketA", "XYZ", NULL), EINVAL);
45 
46  assert_int_equal(pcmk_ticket_get_attr(&xml, NULL, "attrA", NULL), EINVAL);
48  free_xml(xml);
49  xml = NULL;
50 
51  assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketA", NULL, NULL), EINVAL);
53  free_xml(xml);
54 }
55 
56 static void
57 unknown_ticket(void **state)
58 {
59  xmlNode *xml = NULL;
60 
61  /* Both an unknown ticket and an unknown attribute on a known ticket
62  * return ENXIO so we can't really differentiate between the two here.
63  * Still, we'd better test both.
64  */
65  assert_int_equal(pcmk_ticket_get_attr(&xml, "XYZ", "attrA", NULL), ENXIO);
67  free_xml(xml);
68  xml = NULL;
69 
70  assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketA", "XYZ", NULL), ENXIO);
72  free_xml(xml);
73 }
74 
75 static void
76 verify_results(xmlNode *xml, const char *ticket_id, const char *attr_name,
77  const char *attr_value)
78 {
79  xmlNode *node = NULL;
80  xmlXPathObject *xpath_obj = NULL;
81 
82  /* Verify that the XML result has only one <ticket>, and that its ID is
83  * what we asked for.
84  */
86  assert_int_equal(numXpathResults(xpath_obj), 1);
87 
88  node = getXpathResult(xpath_obj, 0);
89  assert_string_equal(crm_element_value(node, PCMK_XA_ID), ticket_id);
90  freeXpathObject(xpath_obj);
91 
92  /* Verify that it has an <attribute> child whose name and value are what
93  * we expect.
94  */
96  "/" PCMK_XE_ATTRIBUTE);
97  assert_int_equal(numXpathResults(xpath_obj), 1);
98 
99  node = getXpathResult(xpath_obj, 0);
100  assert_string_equal(crm_element_value(node, PCMK_XA_NAME), attr_name);
101  assert_string_equal(crm_element_value(node, PCMK_XA_VALUE), attr_value);
102 
103  freeXpathObject(xpath_obj);
104 }
105 
106 static void
107 attribute_exists(void **state)
108 {
109  xmlNode *xml = NULL;
110 
111  assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketA", "owner", NULL), pcmk_rc_ok);
113 
114  verify_results(xml, "ticketA", "owner", "1");
115 
116  free_xml(xml);
117 }
118 
119 static void
120 default_no_ticket(void **state)
121 {
122  xmlNode *xml = NULL;
123 
124  assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketX", "ABC", "DEFAULT"), pcmk_rc_ok);
126 
127  verify_results(xml, "ticketX", "ABC", "DEFAULT");
128 
129  free_xml(xml);
130 }
131 
132 static void
133 default_no_attribute(void **state)
134 {
135  xmlNode *xml = NULL;
136 
137  assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketA", "ABC", "DEFAULT"), pcmk_rc_ok);
139 
140  verify_results(xml, "ticketA", "ABC", "DEFAULT");
141 
142  free_xml(xml);
143 }
144 
146  cmocka_unit_test_setup_teardown(bad_arguments, setup_test, teardown_test),
147  cmocka_unit_test_setup_teardown(unknown_ticket, setup_test, teardown_test),
148  cmocka_unit_test_setup_teardown(attribute_exists, setup_test, teardown_test),
149  cmocka_unit_test_setup_teardown(default_no_ticket, setup_test, teardown_test),
150  cmocka_unit_test_setup_teardown(default_no_attribute, setup_test, teardown_test))
#define PCMK_XA_NAME
Definition: xml_names.h:325
#define PCMK_XE_PACEMAKER_RESULT
Definition: xml_names.h:153
int pcmk_ticket_get_attr(xmlNodePtr *xml, const char *ticket_id, const char *attr_name, const char *attr_default)
Return the value of a ticket&#39;s attribute.
Definition: pcmk_ticket.c:291
High Level API.
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
#define PCMK_XE_ATTRIBUTE
Definition: xml_names.h:69
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
#define PCMK_XA_VALUE
Definition: xml_names.h:437
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