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

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

DEFINITIONS

This source file includes following definitions.
  1. fake_text_init
  2. fake_text_free_priv
  3. mk_fake_text_output
  4. setup
  5. teardown
  6. empty_formatters
  7. invalid_params
  8. no_such_format
  9. create_fails
  10. fopen_fails
  11. init_fails
  12. everything_succeeds
  13. no_fmt_name_given

   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 #include "mock_private.h"
  16 
  17 static bool init_succeeds = true;
  18 
  19 static bool
  20 fake_text_init(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  21     return init_succeeds;
  22 }
  23 
  24 static void
  25 fake_text_free_priv(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  42     retval->register_message = pcmk__register_message;
  43     retval->message = pcmk__call_message;
  44 
  45     return retval;
  46 }
  47 
  48 static int
  49 setup(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  50     pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
  51     return 0;
  52 }
  53 
  54 static int
  55 teardown(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  56     pcmk__unregister_formats();
  57     return 0;
  58 }
  59 
  60 static void
  61 empty_formatters(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  95     pcmk__output_t *out = NULL;
  96 
  97     pcmk__mock_fopen = true;
  98     expect_string(__wrap_fopen, pathname, "destfile");
  99     expect_string(__wrap_fopen, mode, "w");
 100     will_return(__wrap_fopen, EPERM);
 101 
 102     assert_int_equal(pcmk__output_new(&out, "text", "destfile", NULL), EPERM);
 103 
 104     pcmk__mock_fopen = false;
 105 }
 106 
 107 static void
 108 init_fails(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 109     pcmk__output_t *out = NULL;
 110 
 111     init_succeeds = false;
 112     assert_int_equal(pcmk__output_new(&out, "text", NULL, NULL), ENOMEM);
 113     init_succeeds = true;
 114 }
 115 
 116 static void
 117 everything_succeeds(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 118     pcmk__output_t *out = NULL;
 119 
 120     assert_int_equal(pcmk__output_new(&out, "text", NULL, NULL), pcmk_rc_ok);
 121     assert_string_equal(out->fmt_name, "text");
 122     assert_ptr_equal(out->dest, stdout);
 123     assert_false(out->quiet);
 124     assert_non_null(out->messages);
 125     assert_string_equal(getenv("OCF_OUTPUT_FORMAT"), "text");
 126 
 127     pcmk__output_free(out);
 128 }
 129 
 130 static void
 131 no_fmt_name_given(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
 132     pcmk__output_t *out = NULL;
 133 
 134     assert_int_equal(pcmk__output_new(&out, NULL, NULL, NULL), pcmk_rc_ok);
 135     assert_string_equal(out->fmt_name, "text");
 136 
 137     pcmk__output_free(out);
 138 }
 139 
 140 PCMK__UNIT_TEST(NULL, NULL,
 141                 cmocka_unit_test(empty_formatters),
 142                 cmocka_unit_test_setup_teardown(invalid_params, setup, teardown),
 143                 cmocka_unit_test_setup_teardown(no_such_format, setup, teardown),
 144                 cmocka_unit_test_setup_teardown(create_fails, setup, teardown),
 145                 cmocka_unit_test_setup_teardown(init_fails, setup, teardown),
 146                 cmocka_unit_test_setup_teardown(fopen_fails, setup, teardown),
 147                 cmocka_unit_test_setup_teardown(everything_succeeds, setup, teardown),
 148                 cmocka_unit_test_setup_teardown(no_fmt_name_given, setup, teardown))

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