root/lib/common/tests/options/pcmk__env_option_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. empty_input_string
  2. input_too_long_for_both
  3. input_too_long_for_pcmk
  4. value_not_found
  5. value_found_pcmk
  6. value_found_ha

   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 #include <crm/common/unittest_internal.h>
  12 
  13 #include "mock_private.h"
  14 
  15 static void
  16 empty_input_string(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  17 {
  18     pcmk__mock_getenv = true;
  19 
  20     // getenv() not called
  21     assert_null(pcmk__env_option(NULL));
  22     assert_null(pcmk__env_option(""));
  23 
  24     pcmk__mock_getenv = false;
  25 }
  26 
  27 static void
  28 input_too_long_for_both(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30     /* pcmk__env_option() prepends "PCMK_" before lookup. If the option name is
  31      * too long for the buffer or isn't found in the env, then it prepends "HA_"
  32      * and tries again. A string of length (NAME_MAX - 3) will set us just over
  33      * just over the edge for both tries.
  34      */
  35     char long_opt[NAME_MAX - 2];
  36 
  37     for (int i = 0; i < NAME_MAX - 3; i++) {
  38         long_opt[i] = 'a';
  39     }
  40     long_opt[NAME_MAX - 3] = '\0';
  41 
  42     pcmk__mock_getenv = true;
  43 
  44     // getenv() not called
  45     assert_null(pcmk__env_option(long_opt));
  46 
  47     pcmk__mock_getenv = false;
  48 }
  49 
  50 static void
  51 input_too_long_for_pcmk(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  52 {
  53     /* If an input is too long for "PCMK_<option>", make sure we fall through
  54      * to try "HA_<option>".
  55      *
  56      * pcmk__env_option() prepends "PCMK_" first. A string of length
  57      * (NAME_MAX - 5) will set us just over the edge, still short enough for
  58      * "HA_<option>" to fit.
  59      */
  60     char long_opt[NAME_MAX - 4];
  61     char buf[NAME_MAX];
  62 
  63     for (int i = 0; i < NAME_MAX - 5; i++) {
  64         long_opt[i] = 'a';
  65     }
  66     long_opt[NAME_MAX - 5] = '\0';
  67 
  68     pcmk__mock_getenv = true;
  69 
  70     /* NULL/non-NULL retval doesn't really matter here; just testing that we
  71      * call getenv() for "HA_" prefix after too long for "PCMK_".
  72      */
  73     snprintf(buf, NAME_MAX, "HA_%s", long_opt);
  74     expect_string(__wrap_getenv, name, buf);
  75     will_return(__wrap_getenv, "value");
  76     assert_string_equal(pcmk__env_option(long_opt), "value");
  77 
  78     pcmk__mock_getenv = false;
  79 }
  80 
  81 static void
  82 value_not_found(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84     // Value not found using PCMK_ or HA_ prefix. Should return NULL.
  85     pcmk__mock_getenv = true;
  86 
  87     expect_string(__wrap_getenv, name, "PCMK_env_var");
  88     will_return(__wrap_getenv, NULL);
  89 
  90     expect_string(__wrap_getenv, name, "HA_env_var");
  91     will_return(__wrap_getenv, NULL);
  92 
  93     assert_null(pcmk__env_option("env_var"));
  94 
  95     pcmk__mock_getenv = false;
  96 }
  97 
  98 static void
  99 value_found_pcmk(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 100 {
 101     // Value found using PCMK_. Should return value and skip HA_ lookup.
 102     pcmk__mock_getenv = true;
 103 
 104     expect_string(__wrap_getenv, name, "PCMK_env_var");
 105     will_return(__wrap_getenv, "value");
 106     assert_string_equal(pcmk__env_option("env_var"), "value");
 107 
 108     pcmk__mock_getenv = false;
 109 }
 110 
 111 static void
 112 value_found_ha(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114     // Value not found using PCMK_. Move on to HA_ lookup, find, and return.
 115     pcmk__mock_getenv = true;
 116 
 117     expect_string(__wrap_getenv, name, "PCMK_env_var");
 118     will_return(__wrap_getenv, NULL);
 119 
 120     expect_string(__wrap_getenv, name, "HA_env_var");
 121     will_return(__wrap_getenv, "value");
 122 
 123     assert_string_equal(pcmk__env_option("env_var"), "value");
 124 
 125     pcmk__mock_getenv = false;
 126 }
 127 
 128 PCMK__UNIT_TEST(NULL, NULL,
 129                 cmocka_unit_test(empty_input_string),
 130                 cmocka_unit_test(input_too_long_for_both),
 131                 cmocka_unit_test(input_too_long_for_pcmk),
 132                 cmocka_unit_test(value_not_found),
 133                 cmocka_unit_test(value_found_pcmk),
 134                 cmocka_unit_test(value_found_ha))

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