pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk_ticket_delete_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
17static char *cib_path = NULL;
18
19static void
20cib_not_connected(void **state)
21{
22 xmlNode *xml = NULL;
23
24 /* Without any special setup, cib_new() in pcmk_ticket_delete 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_delete(&xml, "ticketA", false), ENOTCONN);
30 pcmk__xml_free(xml);
31}
32
33static int
34setup_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
45static int
46teardown_test(void **state)
47{
48 pcmk__cib_test_cleanup(cib_path);
49 cib_path = NULL;
50 return 0;
51}
52
53static void
54bad_arguments(void **state)
55{
56 xmlNode *xml = NULL;
57
58 assert_int_equal(pcmk_ticket_delete(NULL, "ticketA", false), EINVAL);
59
60 assert_int_equal(pcmk_ticket_delete(&xml, NULL, false), EINVAL);
62 pcmk__xml_free(xml);
63}
64
65static void
66unknown_ticket(void **state)
67{
68 xmlNode *xml = NULL;
69
70 assert_int_equal(pcmk_ticket_delete(&xml, "XYZ", false), ENXIO);
72 pcmk__xml_free(xml);
73 xml = NULL;
74
75 assert_int_equal(pcmk_ticket_delete(&xml, "XYZ", true), pcmk_rc_ok);
77 pcmk__xml_free(xml);
78}
79
80static void
81ticket_granted(void **state)
82{
83 xmlNode *xml = NULL;
84
85 assert_int_equal(pcmk_ticket_delete(&xml, "ticketB", false), EACCES);
87 pcmk__xml_free(xml);
88}
89
90static void
91ticket_exists(void **state)
92{
93 xmlNode *xml = NULL;
94 xmlNode *xml_search = NULL;
95 cib_t *cib;
96
97 assert_int_equal(pcmk_ticket_delete(&xml, "ticketA", false), pcmk_rc_ok);
99
100 /* Verify there's no <ticket_state id="ticketA"> */
101 cib = cib_new();
103 cib->cmds->query(cib, "//" PCMK__XE_TICKET_STATE "[@" PCMK_XA_ID "=\"ticketA\"]",
104 &xml_search, cib_xpath);
105 assert_null(xml_search);
106
107 pcmk__xml_free(xml);
109}
110
111static void
112force_delete_ticket(void **state)
113{
114 xmlNode *xml = NULL;
115 xmlNode *xml_search = NULL;
116 cib_t *cib;
117
118 assert_int_equal(pcmk_ticket_delete(&xml, "ticketB", true), pcmk_rc_ok);
120
121 /* Verify there's no <ticket_state id="ticketB"> */
122 cib = cib_new();
124 cib->cmds->query(cib, "//" PCMK__XE_TICKET_STATE "[@" PCMK_XA_ID "=\"ticketB\"]",
125 &xml_search, cib_xpath);
126 assert_null(xml_search);
127
128 pcmk__xml_free(xml);
130}
131
132static void
133duplicate_tickets(void **state)
134{
135 xmlNode *xml = NULL;
136 xmlNode *xml_search = NULL;
137 cib_t *cib;
138
139 assert_int_equal(pcmk_ticket_delete(&xml, "ticketC", true), pcmk_rc_ok);
141
142 /* Verify there's no <ticket_state id="ticketC"> */
143 cib = cib_new();
145 cib->cmds->query(cib, "//" PCMK__XE_TICKET_STATE "[@" PCMK_XA_ID "=\"ticketC\"]",
146 &xml_search, cib_xpath);
147
148 assert_null(xml_search);
149
150 pcmk__xml_free(xml);
152}
153
154/* There are two kinds of tests in this file:
155 *
156 * (1) Those that test what happens if the CIB is not set up correctly, and
157 * (2) Those that test what happens when run against a CIB.
158 *
159 * Therefore, we need two kinds of setup/teardown functions. We only do
160 * minimal overall setup for the entire group, and then setup the CIB for
161 * those tests that need it.
162 */
164 cmocka_unit_test(cib_not_connected),
165 cmocka_unit_test_setup_teardown(bad_arguments, setup_test, teardown_test),
166 cmocka_unit_test_setup_teardown(unknown_ticket, setup_test, teardown_test),
167 cmocka_unit_test_setup_teardown(ticket_granted, setup_test, teardown_test),
168 cmocka_unit_test_setup_teardown(ticket_exists, setup_test, teardown_test),
169 cmocka_unit_test_setup_teardown(force_delete_ticket, setup_test, teardown_test),
170 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
@ cib_xpath
Definition cib_types.h:58
char * crm_system_name
Definition utils.c:45
High Level API.
int pcmk_ticket_delete(xmlNodePtr *xml, const char *ticket_id, bool force)
Delete a ticket's state from the local cluster site.
@ pcmk_rc_ok
Definition results.h:159
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition cib_types.h:124
int(* query)(cib_t *cib, const char *section, xmlNode **output_data, int call_options)
Definition cib_types.h:151
cib_api_operations_t * cmds
Definition cib_types.h:325
void pcmk__cib_test_cleanup(char *out_path)
Definition unittest.c:148
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
void pcmk__assert_validates(xmlNode *xml)
Definition unittest.c:22
char * pcmk__cib_test_copy_cib(const char *in_file)
Definition unittest.c:113
Wrappers for and extensions to libxml2.
void pcmk__xml_free(xmlNode *xml)
Definition xml.c:816
#define PCMK_XA_ID
Definition xml_names.h:301
#define PCMK__XE_TICKET_STATE