pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__register_formats_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 pcmk__output_t *
16 null_create_fn(char **argv) {
17  return NULL;
18 }
19 
20 static pcmk__output_t *
21 null_create_fn_2(char **argv) {
22  return NULL;
23 }
24 
25 static void
26 no_formats(void **state) {
27  pcmk__register_formats(NULL, NULL);
28  assert_null(pcmk__output_formatters());
29 }
30 
31 static void
32 invalid_entries(void **state) {
33  /* Here, we can only test that an empty name won't be added. A NULL name is
34  * the marker for the end of the format table.
35  */
36  pcmk__supported_format_t formats[] = {
37  { "", null_create_fn, NULL },
38  { NULL },
39  };
40 
42 }
43 
44 static void
45 valid_entries(void **state) {
46  GHashTable *formatters = NULL;
47 
48  pcmk__supported_format_t formats[] = {
49  { "fmt1", null_create_fn, NULL },
50  { "fmt2", null_create_fn_2, NULL },
51  { NULL },
52  };
53 
54  pcmk__register_formats(NULL, formats);
55 
56  formatters = pcmk__output_formatters();
57  assert_int_equal(g_hash_table_size(formatters), 2);
58  assert_ptr_equal(g_hash_table_lookup(formatters, "fmt1"), null_create_fn);
59  assert_ptr_equal(g_hash_table_lookup(formatters, "fmt2"), null_create_fn_2);
60 
62 }
63 
64 static void
65 duplicate_keys(void **state) {
66  GHashTable *formatters = NULL;
67 
68  pcmk__supported_format_t formats[] = {
69  { "fmt1", null_create_fn, NULL },
70  { "fmt1", null_create_fn_2, NULL },
71  { NULL },
72  };
73 
74  pcmk__register_formats(NULL, formats);
75 
76  formatters = pcmk__output_formatters();
77  assert_int_equal(g_hash_table_size(formatters), 1);
78  assert_ptr_equal(g_hash_table_lookup(formatters, "fmt1"), null_create_fn_2);
79 
81 }
82 
83 static void
84 duplicate_values(void **state) {
85  GHashTable *formatters = NULL;
86 
87  pcmk__supported_format_t formats[] = {
88  { "fmt1", null_create_fn, NULL },
89  { "fmt2", null_create_fn, NULL },
90  { NULL },
91  };
92 
93  pcmk__register_formats(NULL, formats);
94 
95  formatters = pcmk__output_formatters();
96  assert_int_equal(g_hash_table_size(formatters), 2);
97  assert_ptr_equal(g_hash_table_lookup(formatters, "fmt1"), null_create_fn);
98  assert_ptr_equal(g_hash_table_lookup(formatters, "fmt2"), null_create_fn);
99 
101 }
102 
103 PCMK__UNIT_TEST(NULL, NULL,
104  cmocka_unit_test(no_formats),
105  cmocka_unit_test(invalid_entries),
106  cmocka_unit_test(valid_entries),
107  cmocka_unit_test(duplicate_keys),
108  cmocka_unit_test(duplicate_values))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
Formatted output for pacemaker tools.
void pcmk__register_formats(GOptionGroup *group, const pcmk__supported_format_t *table)
Definition: output.c:153
#define pcmk__assert_asserts(stmt)
void pcmk__unregister_formats(void)
Definition: output.c:166
This structure contains everything that makes up a single output formatter.