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

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

DEFINITIONS

This source file includes following definitions.
  1. default_message_fn
  2. failed_message_fn
  3. message_fn_1
  4. message_fn_2
  5. fake_text_init
  6. fake_text_free_priv
  7. mk_fake_text_output
  8. setup
  9. teardown
  10. no_such_message
  11. message_return_value
  12. wrong_format
  13. default_called

   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 default_message_fn(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  17     function_called();
  18     return pcmk_rc_ok;
  19 }
  20 
  21 static int
  22 failed_message_fn(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  29     function_called();
  30     return pcmk_rc_ok;
  31 }
  32 
  33 static int
  34 message_fn_2(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  35     function_called();
  36     return pcmk_rc_ok;
  37 }
  38 
  39 static bool
  40 fake_text_init(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  41     return true;
  42 }
  43 
  44 static void
  45 fake_text_free_priv(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  46     /* This function intentionally left blank */
  47 }
  48 
  49 static pcmk__output_t *
  50 mk_fake_text_output(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  61     retval->register_message = pcmk__register_message;
  62     retval->message = pcmk__call_message;
  63 
  64     return retval;
  65 }
  66 
  67 static int
  68 setup(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  69     pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
  70     return 0;
  71 }
  72 
  73 static int
  74 teardown(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  75     pcmk__unregister_formats();
  76     return 0;
  77 }
  78 
  79 static void
  80 no_such_message(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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))

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