pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk__call_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 General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
14 
15 static int
16 default_message_fn(pcmk__output_t *out, va_list args) {
17  function_called();
18  return pcmk_rc_ok;
19 }
20 
21 static int
22 failed_message_fn(pcmk__output_t *out, va_list args) {
23  function_called();
24  return pcmk_rc_no_output;
25 }
26 
27 static int
28 message_fn_1(pcmk__output_t *out, va_list args) {
29  function_called();
30  return pcmk_rc_ok;
31 }
32 
33 static int
34 message_fn_2(pcmk__output_t *out, va_list args) {
35  function_called();
36  return pcmk_rc_ok;
37 }
38 
39 static bool
40 fake_text_init(pcmk__output_t *out) {
41  return true;
42 }
43 
44 static void
45 fake_text_free_priv(pcmk__output_t *out) {
46  /* This function intentionally left blank */
47 }
48 
49 static pcmk__output_t *
50 mk_fake_text_output(char **argv) {
51  pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
52 
53  if (retval == NULL) {
54  return NULL;
55  }
56 
57  retval->fmt_name = "text";
58  retval->init = fake_text_init;
59  retval->free_priv = fake_text_free_priv;
60 
62  retval->message = pcmk__call_message;
63 
64  return retval;
65 }
66 
67 static int
68 setup(void **state) {
69  pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
70  return 0;
71 }
72 
73 static int
74 teardown(void **state) {
76  return 0;
77 }
78 
79 static void
80 no_such_message(void **state) {
81  pcmk__output_t *out = NULL;
82 
83  pcmk__output_new(&out, "text", NULL, NULL);
84 
85  assert_int_equal(out->message(out, "fake"), EINVAL);
86  pcmk__assert_asserts(out->message(out, ""));
87  pcmk__assert_asserts(out->message(out, NULL));
88 
89  pcmk__output_free(out);
90 }
91 
92 static void
93 message_return_value(void **state) {
94  pcmk__output_t *out = NULL;
95 
96  pcmk__message_entry_t entries[] = {
97  { "msg1", "text", message_fn_1 },
98  { "msg2", "text", message_fn_2 },
99  { "fail", "text", failed_message_fn },
100  { NULL },
101  };
102 
103  pcmk__output_new(&out, "text", NULL, NULL);
104  pcmk__register_messages(out, entries);
105 
106  expect_function_call(message_fn_1);
107  assert_int_equal(out->message(out, "msg1"), pcmk_rc_ok);
108  expect_function_call(message_fn_2);
109  assert_int_equal(out->message(out, "msg2"), pcmk_rc_ok);
110  expect_function_call(failed_message_fn);
111  assert_int_equal(out->message(out, "fail"), pcmk_rc_no_output);
112 
113  pcmk__output_free(out);
114 }
115 
116 static void
117 wrong_format(void **state) {
118  pcmk__output_t *out = NULL;
119 
120  pcmk__message_entry_t entries[] = {
121  { "msg1", "xml", message_fn_1 },
122  { NULL },
123  };
124 
125  pcmk__output_new(&out, "text", NULL, NULL);
126  pcmk__register_messages(out, entries);
127 
128  assert_int_equal(out->message(out, "msg1"), EINVAL);
129 
130  pcmk__output_free(out);
131 }
132 
133 static void
134 default_called(void **state) {
135  pcmk__output_t *out = NULL;
136 
137  pcmk__message_entry_t entries[] = {
138  { "msg1", "default", default_message_fn },
139  { "msg1", "xml", message_fn_1 },
140  { NULL },
141  };
142 
143  pcmk__output_new(&out, "text", NULL, NULL);
144  pcmk__register_messages(out, entries);
145 
146  expect_function_call(default_message_fn);
147  assert_int_equal(out->message(out, "msg1"), pcmk_rc_ok);
148 
149  pcmk__output_free(out);
150 }
151 
152 PCMK__UNIT_TEST(NULL, NULL,
153  cmocka_unit_test_setup_teardown(no_such_message, setup, teardown),
154  cmocka_unit_test_setup_teardown(message_return_value, setup, teardown),
155  cmocka_unit_test_setup_teardown(wrong_format, setup, teardown),
156  cmocka_unit_test_setup_teardown(default_called, 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
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.
int pcmk__output_new(pcmk__output_t **out, const char *fmt_name, const char *filename, char **argv)
Definition: output.c:113