pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
pcmk__env_option_enabled_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>
12 
13 #include "mock_private.h"
14 
15 static void
16 disabled_null_value(void **state)
17 {
18  // Return false if option value not found (NULL accomplishes this)
19  assert_false(pcmk__env_option_enabled(NULL, NULL));
20  assert_false(pcmk__env_option_enabled("pacemaker-execd", NULL));
21 }
22 
23 static void
24 enabled_true_value(void **state)
25 {
26  // Return true if option value is true, with or without daemon name
27  pcmk__mock_getenv = true;
28 
29  expect_string(__wrap_getenv, name, "PCMK_env_var");
30  will_return(__wrap_getenv, "true");
31  assert_true(pcmk__env_option_enabled(NULL, "env_var"));
32 
33  expect_string(__wrap_getenv, name, "PCMK_env_var");
34  will_return(__wrap_getenv, "true");
35  assert_true(pcmk__env_option_enabled("pacemaker-execd", "env_var"));
36 
37  pcmk__mock_getenv = false;
38 }
39 
40 static void
41 disabled_false_value(void **state)
42 {
43  // Return false if option value is false (no daemon list)
44  pcmk__mock_getenv = true;
45 
46  expect_string(__wrap_getenv, name, "PCMK_env_var");
47  will_return(__wrap_getenv, "false");
48  assert_false(pcmk__env_option_enabled(NULL, "env_var"));
49 
50  expect_string(__wrap_getenv, name, "PCMK_env_var");
51  will_return(__wrap_getenv, "false");
52  assert_false(pcmk__env_option_enabled("pacemaker-execd", "env_var"));
53 
54  pcmk__mock_getenv = false;
55 }
56 
57 static void
58 enabled_daemon_in_list(void **state)
59 {
60  // Return true if daemon is in the option's value
61  pcmk__mock_getenv = true;
62 
63  expect_string(__wrap_getenv, name, "PCMK_env_var");
64  will_return(__wrap_getenv, "pacemaker-execd");
65  assert_true(pcmk__env_option_enabled("pacemaker-execd", "env_var"));
66 
67  expect_string(__wrap_getenv, name, "PCMK_env_var");
68  will_return(__wrap_getenv, "pacemaker-execd,pacemaker-fenced");
69  assert_true(pcmk__env_option_enabled("pacemaker-execd", "env_var"));
70 
71  expect_string(__wrap_getenv, name, "PCMK_env_var");
72  will_return(__wrap_getenv, "pacemaker-controld,pacemaker-execd");
73  assert_true(pcmk__env_option_enabled("pacemaker-execd", "env_var"));
74 
75  expect_string(__wrap_getenv, name, "PCMK_env_var");
76  will_return(__wrap_getenv,
77  "pacemaker-controld,pacemaker-execd,pacemaker-fenced");
78  assert_true(pcmk__env_option_enabled("pacemaker-execd", "env_var"));
79 
80  pcmk__mock_getenv = false;
81 }
82 
83 static void
84 disabled_daemon_not_in_list(void **state)
85 {
86  // Return false if value is not true and daemon is not in the option's value
87  pcmk__mock_getenv = true;
88 
89  expect_string(__wrap_getenv, name, "PCMK_env_var");
90  will_return(__wrap_getenv, "pacemaker-controld,pacemaker-fenced");
91  assert_false(pcmk__env_option_enabled("pacemaker-execd", "env_var"));
92 
93  pcmk__mock_getenv = false;
94 }
95 
96 PCMK__UNIT_TEST(NULL, NULL,
97  cmocka_unit_test(disabled_null_value),
98  cmocka_unit_test(enabled_true_value),
99  cmocka_unit_test(disabled_false_value),
100  cmocka_unit_test(enabled_daemon_in_list),
101  cmocka_unit_test(disabled_daemon_not_in_list))
const char * name
Definition: cib.c:26
PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(bad_input), cmocka_unit_test(not_found), cmocka_unit_test(find_attrB), cmocka_unit_test(find_attrA_matching))
char * __wrap_getenv(const char *name)
Definition: mock.c:96
bool pcmk__env_option_enabled(const char *daemon, const char *option)
Definition: options.c:165
bool pcmk__mock_getenv
Definition: mock.c:93