pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk_resource_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_resource_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_resource_delete(&xml, "rsc", "primitive"), ENOTCONN);
30 pcmk__xml_free(xml);
31}
32
33static int
34setup_test(void **state)
35{
36 cib_path = pcmk__cib_test_copy_cib("crm_mon.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_input(void **state)
55{
56 xmlNode *xml = NULL;
57
58 /* There is a primitive resource named "Fencing", so we're just checking
59 * that it returns EINVAL if both parameters aren't given.
60 */
61 assert_int_equal(pcmk_resource_delete(&xml, "Fencing", NULL), EINVAL);
63 pcmk__xml_free(xml);
64 xml = NULL;
65
66 assert_int_equal(pcmk_resource_delete(&xml, NULL, "primitive"), EINVAL);
68 pcmk__xml_free(xml);
69}
70
71static xmlNode *
72find_rsc(const char *rsc)
73{
74 GString *xpath = g_string_sized_new(1024);
75 xmlNode *xml_search = NULL;
76 cib_t *cib = NULL;
77
78 cib = cib_new();
80
81 pcmk__g_strcat(xpath,
83 "//*[@" PCMK_XA_ID "=\"", rsc, "\"]", NULL);
84
85 cib->cmds->query(cib, (const char *) xpath->str, &xml_search, cib_xpath);
86
87 g_string_free(xpath, TRUE);
89 return xml_search;
90}
91
92static void
93incorrect_type(void **state)
94{
95 xmlNode *xml = NULL;
96 xmlNode *result = NULL;
97
98 /* cib_process_delete returns pcmk_ok even if given the wrong type so
99 * we have to do an xpath query of the CIB to make sure it's still
100 * there.
101 */
102 assert_int_equal(pcmk_resource_delete(&xml, "Fencing", "clone"), pcmk_rc_ok);
104 pcmk__xml_free(xml);
105
106 result = find_rsc("Fencing");
107 assert_non_null(result);
108
110}
111
112static void
113correct_type(void **state)
114{
115 xmlNode *xml = NULL;
116 xmlNode *result = NULL;
117
118 assert_int_equal(pcmk_resource_delete(&xml, "Fencing", "primitive"), pcmk_rc_ok);
120 pcmk__xml_free(xml);
121
122 result = find_rsc("Fencing");
123 assert_null(result);
124
126}
127
128static void
129unknown_resource(void **state)
130{
131 xmlNode *xml = NULL;
132
133 /* cib_process_delete returns pcmk_ok even if asked to delete something
134 * that doesn't exist.
135 */
136 assert_int_equal(pcmk_resource_delete(&xml, "no_such_resource", "primitive"), pcmk_rc_ok);
138 pcmk__xml_free(xml);
139}
140
141/* There are two kinds of tests in this file:
142 *
143 * (1) Those that test what happens if the CIB is not set up correctly, and
144 * (2) Those that test what happens when run against a CIB.
145 *
146 * Therefore, we need two kinds of setup/teardown functions. We only do
147 * minimal overall setup for the entire group, and then setup the CIB for
148 * those tests that need it.
149 */
151 cmocka_unit_test(cib_not_connected),
152 cmocka_unit_test_setup_teardown(bad_input, setup_test, teardown_test),
153 cmocka_unit_test_setup_teardown(incorrect_type, setup_test, teardown_test),
154 cmocka_unit_test_setup_teardown(correct_type, setup_test, teardown_test),
155 cmocka_unit_test_setup_teardown(unknown_resource, 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
const char * pcmk_cib_xpath_for(const char *element_name)
Get the relative XPath needed to find a specified CIB element name.
Definition cib.c:112
char * crm_system_name
Definition utils.c:45
High Level API.
int pcmk_resource_delete(xmlNodePtr *xml, const char *rsc_id, const char *rsc_type)
Remove a resource.
pcmk__action_result_t result
Definition pcmk_fence.c:37
@ pcmk_rc_ok
Definition results.h:159
void pcmk__g_strcat(GString *buffer,...) G_GNUC_NULL_TERMINATED
Definition strings.c:1299
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_RESOURCES
Definition xml_names.h:179