pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
pcmk__output_new_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 "mock_private.h"
16 
17 static bool init_succeeds = true;
18 
19 static bool
20 fake_text_init(pcmk__output_t *out) {
21  return init_succeeds;
22 }
23 
24 static void
25 fake_text_free_priv(pcmk__output_t *out) {
26  /* This function intentionally left blank */
27 }
28 
29 /* "text" is the default for pcmk__output_new. */
30 static pcmk__output_t *
31 mk_fake_text_output(char **argv) {
32  pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
33 
34  if (retval == NULL) {
35  return NULL;
36  }
37 
38  retval->fmt_name = "text";
39  retval->init = fake_text_init;
40  retval->free_priv = fake_text_free_priv;
41 
43  retval->message = pcmk__call_message;
44 
45  return retval;
46 }
47 
48 static int
49 setup(void **state) {
50  pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
51  return 0;
52 }
53 
54 static int
55 teardown(void **state) {
57  return 0;
58 }
59 
60 static void
61 empty_formatters(void **state) {
62  pcmk__output_t *out = NULL;
63 
64  pcmk__assert_asserts(pcmk__output_new(&out, "fake", NULL, NULL));
65 }
66 
67 static void
68 invalid_params(void **state) {
69  /* This must be called with the setup/teardown functions so formatters is not NULL. */
70  pcmk__assert_asserts(pcmk__output_new(NULL, "fake", NULL, NULL));
71 }
72 
73 static void
74 no_such_format(void **state) {
75  pcmk__output_t *out = NULL;
76 
77  assert_int_equal(pcmk__output_new(&out, "fake", NULL, NULL), pcmk_rc_unknown_format);
78 }
79 
80 static void
81 create_fails(void **state) {
82  pcmk__output_t *out = NULL;
83 
84  pcmk__mock_calloc = true; // calloc() will return NULL
85 
86  expect_value(__wrap_calloc, nmemb, 1);
87  expect_value(__wrap_calloc, size, sizeof(pcmk__output_t));
88  assert_int_equal(pcmk__output_new(&out, "text", NULL, NULL), ENOMEM);
89 
90  pcmk__mock_calloc = false; // Use real calloc()
91 }
92 
93 static void
94 fopen_fails(void **state) {
95  pcmk__output_t *out = NULL;
96 
97  pcmk__mock_fopen = true;
98 #if defined(HAVE_FOPEN64) && defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64) && (SIZEOF_LONG < 8)
99  expect_string(__wrap_fopen64, pathname, "destfile");
100  expect_string(__wrap_fopen64, mode, "w");
101  will_return(__wrap_fopen64, EPERM);
102 #else
103  expect_string(__wrap_fopen, pathname, "destfile");
104  expect_string(__wrap_fopen, mode, "w");
105  will_return(__wrap_fopen, EPERM);
106 #endif
107 
108  assert_int_equal(pcmk__output_new(&out, "text", "destfile", NULL), EPERM);
109 
110  pcmk__mock_fopen = false;
111 }
112 
113 static void
114 init_fails(void **state) {
115  pcmk__output_t *out = NULL;
116 
117  init_succeeds = false;
118  assert_int_equal(pcmk__output_new(&out, "text", NULL, NULL), ENOMEM);
119  init_succeeds = true;
120 }
121 
122 static void
123 everything_succeeds(void **state) {
124  pcmk__output_t *out = NULL;
125 
126  assert_int_equal(pcmk__output_new(&out, "text", NULL, NULL), pcmk_rc_ok);
127  assert_string_equal(out->fmt_name, "text");
128  assert_ptr_equal(out->dest, stdout);
129  assert_false(out->quiet);
130  assert_non_null(out->messages);
131  assert_string_equal(getenv("OCF_OUTPUT_FORMAT"), "text");
132 
133  pcmk__output_free(out);
134 }
135 
136 static void
137 no_fmt_name_given(void **state) {
138  pcmk__output_t *out = NULL;
139 
140  assert_int_equal(pcmk__output_new(&out, NULL, NULL, NULL), pcmk_rc_ok);
141  assert_string_equal(out->fmt_name, "text");
142 
143  pcmk__output_free(out);
144 }
145 
146 PCMK__UNIT_TEST(NULL, NULL,
147  cmocka_unit_test(empty_formatters),
148  cmocka_unit_test_setup_teardown(invalid_params, setup, teardown),
149  cmocka_unit_test_setup_teardown(no_such_format, setup, teardown),
150  cmocka_unit_test_setup_teardown(create_fails, setup, teardown),
151  cmocka_unit_test_setup_teardown(init_fails, setup, teardown),
152  cmocka_unit_test_setup_teardown(fopen_fails, setup, teardown),
153  cmocka_unit_test_setup_teardown(everything_succeeds, setup, teardown),
154  cmocka_unit_test_setup_teardown(no_fmt_name_given, setup, teardown))
uint32_t size
Definition: cpg.c:49
bool pcmk__mock_fopen
Definition: mock.c:271
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.
Formatted output for pacemaker tools.
void(* register_message)(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
bool quiet
Should this formatter supress most output?
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
FILE * dest
Where output should be written.
void pcmk__output_free(pcmk__output_t *out)
Definition: output.c:28
void pcmk__unregister_formats(void)
Definition: output.c:158
FILE * __wrap_fopen(const char *pathname, const char *mode)
Definition: mock.c:274
This structure contains everything that makes up a single output formatter.
bool pcmk__mock_calloc
Definition: mock.c:68
int pcmk__output_new(pcmk__output_t **out, const char *fmt_name, const char *filename, char **argv)
Definition: output.c:111
void * __wrap_calloc(size_t nmemb, size_t size)
Definition: mock.c:71