pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
pcmk__register_message_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 null_params(void **state) {
69  pcmk__output_t *out = NULL;
70 
71  pcmk__output_new(&out, "text", NULL, NULL);
72 
73  pcmk__assert_asserts(pcmk__register_message(NULL, "fake", null_message_fn));
74  pcmk__assert_asserts(pcmk__register_message(out, NULL, null_message_fn));
75  pcmk__assert_asserts(pcmk__register_message(out, "", null_message_fn));
76  pcmk__assert_asserts(pcmk__register_message(out, "fake", NULL));
77 
78  pcmk__output_free(out);
79 }
80 
81 static void
82 add_message(void **state) {
83  pcmk__output_t *out = NULL;
84 
85  pcmk__bare_output_new(&out, "text", NULL, NULL);
86 
87  /* For starters, there should be no messages defined. */
88  assert_int_equal(g_hash_table_size(out->messages), 0);
89 
90  /* Add a fake function and check that it's the only item in the hash table. */
91  pcmk__register_message(out, "fake", null_message_fn);
92  assert_int_equal(g_hash_table_size(out->messages), 1);
93  assert_ptr_equal(g_hash_table_lookup(out->messages, "fake"), null_message_fn);
94 
95  /* Add a second fake function which should overwrite the first one, leaving
96  * only one item in the hash table but pointing at the new function.
97  */
98  pcmk__register_message(out, "fake", null_message_fn_2);
99  assert_int_equal(g_hash_table_size(out->messages), 1);
100  assert_ptr_equal(g_hash_table_lookup(out->messages, "fake"), null_message_fn_2);
101 
102  pcmk__output_free(out);
103 }
104 
105 PCMK__UNIT_TEST(NULL, NULL,
106  cmocka_unit_test_setup_teardown(null_params, setup, teardown),
107  cmocka_unit_test_setup_teardown(add_message, setup, teardown))
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:188
PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(bad_input), cmocka_unit_test(not_found), cmocka_unit_test(find_attrB), cmocka_unit_test(find_attrA_matching))
int pcmk__call_message(pcmk__output_t *out, const char *message_id,...)
Definition: output.c:166
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:61
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:126
void pcmk__output_free(pcmk__output_t *out)
Definition: output.c:28
void pcmk__unregister_formats(void)
Definition: output.c:158
This structure contains everything that makes up a single output formatter.
int pcmk__output_new(pcmk__output_t **out, const char *fmt_name, const char *filename, char **argv)
Definition: output.c:111