pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk_foreach_active_resource_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 <stdio.h> // NULL
13 #include <glib.h> // GList, TRUE, FALSE
14 
15 #include <crm/common/nodes.h>
16 #include <crm/common/resources.h>
18 
19 static int counter = 1;
20 static int return_false = -1;
21 
22 static char rsc1_id[] = "rsc1";
23 static char rsc2_id[] = "rsc2";
24 static char rsc3_id[] = "rsc3";
25 
26 static pcmk_resource_t rsc1 = {
27  .id = rsc1_id,
28 };
29 static pcmk_resource_t rsc2 = {
30  .id = rsc2_id,
31 };
32 static pcmk_resource_t rsc3 = {
33  .id = rsc3_id,
34 };
35 
36 static bool
37 fn(pcmk_resource_t *rsc, void *user_data)
38 {
39  char *expected_id = crm_strdup_printf("rsc%d", counter);
40 
41  assert_string_equal(rsc->id, expected_id);
42  free(expected_id);
43 
44  return counter++ != return_false;
45 }
46 
47 static void
48 null_args(void **state)
49 {
50  struct pe_node_shared_s shared = {
51  .running_rsc = NULL,
52  };
53  pcmk_node_t node = {
54  .details = &shared,
55  };
56 
57  counter = 1;
58 
59  // These just test that it doesn't crash
60  pcmk_foreach_active_resource(NULL, NULL, NULL);
61  pcmk_foreach_active_resource(&node, NULL, NULL);
62 
63  pcmk_foreach_active_resource(NULL, fn, NULL);
64  assert_int_equal(counter, 1);
65 }
66 
67 static void
68 list_of_0(void **state)
69 {
70  struct pe_node_shared_s shared = {
71  .running_rsc = NULL,
72  };
73  pcmk_node_t node = {
74  .details = &shared,
75  };
76 
77  counter = 1;
78  pcmk_foreach_active_resource(&node, fn, NULL);
79  assert_int_equal(counter, 1);
80 }
81 
82 static void
83 list_of_1(void **state)
84 {
85  struct pe_node_shared_s shared = {
86  .running_rsc = NULL,
87  };
88  pcmk_node_t node = {
89  .details = &shared,
90  };
91 
92  shared.running_rsc = g_list_append(shared.running_rsc, &rsc1);
93 
94  counter = 1;
95  pcmk_foreach_active_resource(&node, fn, NULL);
96  assert_int_equal(counter, 2);
97 
98  g_list_free(shared.running_rsc);
99 }
100 
101 static void
102 list_of_3(void **state)
103 {
104  struct pe_node_shared_s shared = {
105  .running_rsc = NULL,
106  };
107  pcmk_node_t node = {
108  .details = &shared,
109  };
110 
111  shared.running_rsc = g_list_append(shared.running_rsc, &rsc1);
112  shared.running_rsc = g_list_append(shared.running_rsc, &rsc2);
113  shared.running_rsc = g_list_append(shared.running_rsc, &rsc3);
114 
115  counter = 1;
116  pcmk_foreach_active_resource(&node, fn, NULL);
117  assert_int_equal(counter, 4);
118 
119  g_list_free(shared.running_rsc);
120 }
121 
122 static void
123 list_of_3_return_false(void **state)
124 {
125  struct pe_node_shared_s shared = {
126  .running_rsc = NULL,
127  };
128  pcmk_node_t node = {
129  .details = &shared,
130  };
131 
132  shared.running_rsc = g_list_append(shared.running_rsc, &rsc1);
133  shared.running_rsc = g_list_append(shared.running_rsc, &rsc2);
134  shared.running_rsc = g_list_append(shared.running_rsc, &rsc3);
135 
136  counter = 1;
137  return_false = 2;
138  pcmk_foreach_active_resource(&node, fn, NULL);
139  assert_int_equal(counter, 3);
140 
141  g_list_free(shared.running_rsc);
142 }
143 
144 PCMK__UNIT_TEST(NULL, NULL,
145  cmocka_unit_test(null_args),
146  cmocka_unit_test(list_of_0),
147  cmocka_unit_test(list_of_1),
148  cmocka_unit_test(list_of_3),
149  cmocka_unit_test(list_of_3_return_false))
Scheduler API for resources.
pcmk_resource_t rsc2
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
struct pe_node_shared_s * details
Definition: nodes.h:168
pcmk_resource_t rsc1
GList * running_rsc
Definition: nodes.h:140
Scheduler API for nodes.
bool pcmk_foreach_active_resource(pcmk_node_t *node, bool(*fn)(pcmk_resource_t *, void *), void *user_data)
Definition: nodes.c:107