root/lib/common/tests/output/pcmk__register_messages_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. null_message_fn
  2. null_message_fn_2
  3. fake_text_init
  4. fake_text_free_priv
  5. mk_fake_text_output
  6. setup
  7. teardown
  8. invalid_entries
  9. valid_entries
  10. duplicate_message_ids
  11. duplicate_functions
  12. default_handler
  13. override_default_handler

   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 
  12 #include <crm/common/unittest_internal.h>
  13 #include <crm/common/output_internal.h>
  14 
  15 static int
  16 null_message_fn(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  17     return pcmk_rc_ok;
  18 }
  19 
  20 static int
  21 null_message_fn_2(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  22     return pcmk_rc_ok;
  23 }
  24 
  25 static bool
  26 fake_text_init(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  27     return true;
  28 }
  29 
  30 static void
  31 fake_text_free_priv(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  32     /* This function intentionally left blank */
  33 }
  34 
  35 static pcmk__output_t *
  36 mk_fake_text_output(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  47     retval->register_message = pcmk__register_message;
  48     retval->message = pcmk__call_message;
  49 
  50     return retval;
  51 }
  52 
  53 static int
  54 setup(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  55     pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
  56     return 0;
  57 }
  58 
  59 static int
  60 teardown(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  61     pcmk__unregister_formats();
  62     return 0;
  63 }
  64 
  65 static void
  66 invalid_entries(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  67     pcmk__output_t *out = NULL;
  68 
  69     pcmk__message_entry_t entries[] = {
  70         /* We can't test a NULL message_id here because that's the marker for
  71          * the end of the table.
  72          */
  73         { "", "", null_message_fn },
  74         { "", NULL, null_message_fn },
  75         { "", "text", NULL },
  76         { NULL },
  77     };
  78 
  79     pcmk__output_new(&out, "text", NULL, NULL);
  80 
  81     pcmk__assert_asserts(pcmk__register_messages(out, entries));
  82     assert_int_equal(g_hash_table_size(out->messages), 0);
  83 
  84     pcmk__output_free(out);
  85 }
  86 
  87 static void
  88 valid_entries(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  89     pcmk__output_t *out = NULL;
  90 
  91     pcmk__message_entry_t entries[] = {
  92         { "msg1", "text", null_message_fn },
  93         { "msg2", "text", null_message_fn_2 },
  94         { NULL },
  95     };
  96 
  97     pcmk__output_new(&out, "text", NULL, NULL);
  98 
  99     pcmk__register_messages(out, entries);
 100     assert_int_equal(g_hash_table_size(out->messages), 2);
 101     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
 102     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg2"), null_message_fn_2);
 103 
 104     pcmk__output_free(out);
 105 }
 106 
 107 static void
 108 duplicate_message_ids(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 109     pcmk__output_t *out = NULL;
 110 
 111     pcmk__message_entry_t entries[] = {
 112         { "msg1", "text", null_message_fn },
 113         { "msg1", "text", null_message_fn_2 },
 114         { NULL },
 115     };
 116 
 117     pcmk__output_new(&out, "text", NULL, NULL);
 118 
 119     pcmk__register_messages(out, entries);
 120     assert_int_equal(g_hash_table_size(out->messages), 1);
 121     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn_2);
 122 
 123     pcmk__output_free(out);
 124 }
 125 
 126 static void
 127 duplicate_functions(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 128     pcmk__output_t *out = NULL;
 129 
 130     pcmk__message_entry_t entries[] = {
 131         { "msg1", "text", null_message_fn },
 132         { "msg2", "text", null_message_fn },
 133         { NULL },
 134     };
 135 
 136     pcmk__output_new(&out, "text", NULL, NULL);
 137 
 138     pcmk__register_messages(out, entries);
 139     assert_int_equal(g_hash_table_size(out->messages), 2);
 140     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
 141     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg2"), null_message_fn);
 142 
 143     pcmk__output_free(out);
 144 }
 145 
 146 static void
 147 default_handler(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 148     pcmk__output_t *out = NULL;
 149 
 150     pcmk__message_entry_t entries[] = {
 151         { "msg1", "default", null_message_fn },
 152         { NULL },
 153     };
 154 
 155     pcmk__output_new(&out, "text", NULL, NULL);
 156 
 157     pcmk__register_messages(out, entries);
 158     assert_int_equal(g_hash_table_size(out->messages), 1);
 159     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn);
 160 
 161     pcmk__output_free(out);
 162 }
 163 
 164 static void
 165 override_default_handler(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 166     pcmk__output_t *out = NULL;
 167 
 168     pcmk__message_entry_t entries[] = {
 169         { "msg1", "default", null_message_fn },
 170         { "msg1", "text", null_message_fn_2 },
 171         { NULL },
 172     };
 173 
 174     pcmk__output_new(&out, "text", NULL, NULL);
 175 
 176     pcmk__register_messages(out, entries);
 177     assert_int_equal(g_hash_table_size(out->messages), 1);
 178     assert_ptr_equal(g_hash_table_lookup(out->messages, "msg1"), null_message_fn_2);
 179 
 180     pcmk__output_free(out);
 181 }
 182 
 183 PCMK__UNIT_TEST(NULL, NULL,
 184                 cmocka_unit_test_setup_teardown(invalid_entries, setup, teardown),
 185                 cmocka_unit_test_setup_teardown(valid_entries, setup, teardown),
 186                 cmocka_unit_test_setup_teardown(duplicate_message_ids, setup, teardown),
 187                 cmocka_unit_test_setup_teardown(duplicate_functions, setup, teardown),
 188                 cmocka_unit_test_setup_teardown(default_handler, setup, teardown),
 189                 cmocka_unit_test_setup_teardown(override_default_handler, setup, teardown))

/* [previous][next][first][last][top][bottom][index][help] */