pacemaker 2.1.5-a3f44794f94
Scalable High-Availability cluster resource manager
pcmk__register_message_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2022 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 Lesser General Public License
7 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
14
15static int
16null_message_fn(pcmk__output_t *out, va_list args) {
17 return pcmk_rc_ok;
18}
19
20static int
21null_message_fn_2(pcmk__output_t *out, va_list args) {
22 return pcmk_rc_ok;
23}
24
25static bool
26fake_text_init(pcmk__output_t *out) {
27 return true;
28}
29
30static void
31fake_text_free_priv(pcmk__output_t *out) {
32 /* This function intentionally left blank */
33}
34
35static pcmk__output_t *
36mk_fake_text_output(char **argv) {
37 pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
38
39 if (retval == NULL) {
40 return NULL;
41 }
42
43 retval->fmt_name = "text";
44 retval->init = fake_text_init;
45 retval->free_priv = fake_text_free_priv;
46
49
50 return retval;
51}
52
53static int
54setup(void **state) {
55 pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
56 return 0;
57}
58
59static int
60teardown(void **state) {
62 return 0;
63}
64
65static void
66null_params(void **state) {
67 pcmk__output_t *out = NULL;
68
69 pcmk__output_new(&out, "text", NULL, NULL);
70
71 pcmk__assert_asserts(pcmk__register_message(NULL, "fake", null_message_fn));
72 pcmk__assert_asserts(pcmk__register_message(out, NULL, null_message_fn));
73 pcmk__assert_asserts(pcmk__register_message(out, "", null_message_fn));
75
77}
78
79static void
80add_message(void **state) {
81 pcmk__output_t *out = NULL;
82
83 pcmk__output_new(&out, "text", NULL, NULL);
84
85 /* For starters, there should be no messages defined. */
86 assert_int_equal(g_hash_table_size(out->messages), 0);
87
88 /* Add a fake function and check that it's the only item in the hash table. */
89 pcmk__register_message(out, "fake", null_message_fn);
90 assert_int_equal(g_hash_table_size(out->messages), 1);
91 assert_ptr_equal(g_hash_table_lookup(out->messages, "fake"), null_message_fn);
92
93 /* Add a second fake function which should overwrite the first one, leaving
94 * only one item in the hash table but pointing at the new function.
95 */
96 pcmk__register_message(out, "fake", null_message_fn_2);
97 assert_int_equal(g_hash_table_size(out->messages), 1);
98 assert_ptr_equal(g_hash_table_lookup(out->messages, "fake"), null_message_fn_2);
99
101}
102
103PCMK__UNIT_TEST(NULL, NULL,
104 cmocka_unit_test_setup_teardown(null_params, setup, teardown),
105 cmocka_unit_test_setup_teardown(add_message, setup, teardown))
Formatted output for pacemaker tools.
int pcmk__register_format(GOptionGroup *group, const char *name, pcmk__output_factory_t create, const GOptionEntry *options)
Definition: output.c:91
void pcmk__output_free(pcmk__output_t *out)
Definition: output.c:26
int pcmk__output_new(pcmk__output_t **out, const char *fmt_name, const char *filename, char **argv)
Definition: output.c:42
int pcmk__call_message(pcmk__output_t *out, const char *message_id,...)
Definition: output.c:131
void pcmk__register_message(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
Definition: output.c:153
void pcmk__unregister_formats(void)
Definition: output.c:123
@ pcmk_rc_ok
Definition: results.h:148
This structure contains everything that makes up a single output formatter.
int(* message)(pcmk__output_t *out, const char *message_id,...)
GHashTable * messages
Custom messages that are currently registered on this formatter.
const char * fmt_name
The name of this output formatter.
void(* register_message)(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
bool(* init)(pcmk__output_t *out)
void(* free_priv)(pcmk__output_t *out)
#define pcmk__assert_asserts(stmt)
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)