pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__register_messages_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022-2023 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 
14 
15 #include "../../crmcommon_private.h"
16 
17 static int
18 null_message_fn(pcmk__output_t *out, va_list args) {
19  return pcmk_rc_ok;
20 }
21 
22 static int
23 null_message_fn_2(pcmk__output_t *out, va_list args) {
24  return pcmk_rc_ok;
25 }
26 
27 static bool
28 fake_text_init(pcmk__output_t *out) {
29  return true;
30 }
31 
32 static void
33 fake_text_free_priv(pcmk__output_t *out) {
34  /* This function intentionally left blank */
35 }
36 
37 static pcmk__output_t *
38 mk_fake_text_output(char **argv) {
39  pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
40 
41  if (retval == NULL) {
42  return NULL;
43  }
44 
45  retval->fmt_name = "text";
46  retval->init = fake_text_init;
47  retval->free_priv = fake_text_free_priv;
48 
50  retval->message = pcmk__call_message;
51 
52  return retval;
53 }
54 
55 static int
56 setup(void **state) {
57  pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
58  return 0;
59 }
60 
61 static int
62 teardown(void **state) {
64  return 0;
65 }
66 
67 static void
68 invalid_entries(void **state) {
69  pcmk__output_t *out = NULL;
70 
71  pcmk__message_entry_t entries[] = {
72  /* We can't test a NULL message_id here because that's the marker for
73  * the end of the table.
74  */
75  { "", "", null_message_fn },
76  { "", NULL, null_message_fn },
77  { "", "text", NULL },
78  { NULL },
79  };
80 
81  pcmk__bare_output_new(&out, "text", NULL, NULL);
82 
84  assert_int_equal(g_hash_table_size(out->messages), 0);
85 
86  pcmk__output_free(out);
87 }
88 
89 static void
90 valid_entries(void **state) {
91  pcmk__output_t *out = NULL;
92 
93  pcmk__message_entry_t entries[] = {
94  { "msg1", "text", null_message_fn },
95  { "msg2", "text", null_message_fn_2 },
96  { NULL },
97  };
98 
99  pcmk__bare_output_new(&out, "text", NULL, NULL);
100 
101  pcmk__register_messages(out, entries);
102  assert_int_equal(g_hash_table_size(out->messages), 2);
103  assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
104  assert_ptr_equal(g_hash_table_lookup(out->messages, "msg2"), null_message_fn_2);
105 
106  pcmk__output_free(out);
107 }
108 
109 static void
110 duplicate_message_ids(void **state) {
111  pcmk__output_t *out = NULL;
112 
113  pcmk__message_entry_t entries[] = {
114  { "msg1", "text", null_message_fn },
115  { "msg1", "text", null_message_fn_2 },
116  { NULL },
117  };
118 
119  pcmk__bare_output_new(&out, "text", NULL, NULL);
120 
121  pcmk__register_messages(out, entries);
122  assert_int_equal(g_hash_table_size(out->messages), 1);
123  assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn_2);
124 
125  pcmk__output_free(out);
126 }
127 
128 static void
129 duplicate_functions(void **state) {
130  pcmk__output_t *out = NULL;
131 
132  pcmk__message_entry_t entries[] = {
133  { "msg1", "text", null_message_fn },
134  { "msg2", "text", null_message_fn },
135  { NULL },
136  };
137 
138  pcmk__bare_output_new(&out, "text", NULL, NULL);
139 
140  pcmk__register_messages(out, entries);
141  assert_int_equal(g_hash_table_size(out->messages), 2);
142  assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
143  assert_ptr_equal(g_hash_table_lookup(out->messages, "msg2"), null_message_fn);
144 
145  pcmk__output_free(out);
146 }
147 
148 static void
149 default_handler(void **state) {
150  pcmk__output_t *out = NULL;
151 
152  pcmk__message_entry_t entries[] = {
153  { "msg1", "default", null_message_fn },
154  { NULL },
155  };
156 
157  pcmk__bare_output_new(&out, "text", NULL, NULL);
158 
159  pcmk__register_messages(out, entries);
160  assert_int_equal(g_hash_table_size(out->messages), 1);
161  assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
162 
163  pcmk__output_free(out);
164 }
165 
166 static void
167 override_default_handler(void **state) {
168  pcmk__output_t *out = NULL;
169 
170  pcmk__message_entry_t entries[] = {
171  { "msg1", "default", null_message_fn },
172  { "msg1", "text", null_message_fn_2 },
173  { NULL },
174  };
175 
176  pcmk__bare_output_new(&out, "text", NULL, NULL);
177 
178  pcmk__register_messages(out, entries);
179  assert_int_equal(g_hash_table_size(out->messages), 1);
180  assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn_2);
181 
182  pcmk__output_free(out);
183 }
184 
185 PCMK__UNIT_TEST(NULL, NULL,
186  cmocka_unit_test_setup_teardown(invalid_entries, setup, teardown),
187  cmocka_unit_test_setup_teardown(valid_entries, setup, teardown),
188  cmocka_unit_test_setup_teardown(duplicate_message_ids, setup, teardown),
189  cmocka_unit_test_setup_teardown(duplicate_functions, setup, teardown),
190  cmocka_unit_test_setup_teardown(default_handler, setup, teardown),
191  cmocka_unit_test_setup_teardown(override_default_handler, setup, teardown))
void pcmk__register_messages(pcmk__output_t *out, const pcmk__message_entry_t *table)
Definition: output.c:204
int(* message)(pcmk__output_t *out, const char *message_id,...)
const char * fmt_name
The name of this output formatter.
void pcmk__register_message(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
Definition: output.c:196
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int pcmk__call_message(pcmk__output_t *out, const char *message_id,...)
Definition: output.c:174
GHashTable * messages
Custom messages that are currently registered on this formatter.
G_GNUC_INTERNAL int pcmk__bare_output_new(pcmk__output_t **out, const char *fmt_name, const char *filename, char **argv)
Definition: output.c:63
Formatted output for pacemaker tools.
void(* register_message)(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
void(* free_priv)(pcmk__output_t *out)
bool(* init)(pcmk__output_t *out)
#define pcmk__assert_asserts(stmt)
int pcmk__register_format(GOptionGroup *group, const char *name, pcmk__output_factory_t create, const GOptionEntry *options)
Definition: output.c:127
void pcmk__output_free(pcmk__output_t *out)
Definition: output.c:30
void pcmk__unregister_formats(void)
Definition: output.c:166
This structure contains everything that makes up a single output formatter.