Skip to content

Commit

Permalink
fix(userspace,unit_tests): fixed bool parsing.
Browse files Browse the repository at this point in the history
Moreover, added some more tests around env vars.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Nov 20, 2023
1 parent 00343d4 commit 4add0ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 22 additions & 2 deletions unit_tests/falco/test_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ TEST(Configuration, configuration_environment_variables)
std::string embedded_env_var_name = "ENV_VAR_EMBEDDED";
SET_ENV_VAR(embedded_env_var_name.c_str(), embedded_env_var_value.c_str());

std::string bool_env_var_value = "true";
std::string bool_env_var_name = "ENV_VAR_BOOL";
SET_ENV_VAR(bool_env_var_name.c_str(), bool_env_var_value.c_str());

std::string int_env_var_value = "12";
std::string int_env_var_name = "ENV_VAR_INT";
SET_ENV_VAR(int_env_var_name.c_str(), int_env_var_value.c_str());

std::string default_value = "default";
std::string env_var_sample_yaml =
"base_value:\n"
Expand All @@ -143,7 +151,10 @@ TEST(Configuration, configuration_environment_variables)
" - $ENV_VAR/foo\n"
" - /foo/${ENV_VAR}/\n"
" - /${ENV_VAR}/${ENV_VAR}${ENV_VAR}/foo\n"
" - ${ENV_VAR_EMBEDDED}/foo\n";
" - ${ENV_VAR_EMBEDDED}/foo\n"
"is_test: ${ENV_VAR_BOOL}\n"
"num_test: ${ENV_VAR_INT}\n";

yaml_helper conf;
conf.load_from_string(env_var_sample_yaml);

Expand Down Expand Up @@ -208,9 +219,18 @@ TEST(Configuration, configuration_environment_variables)
auto path_list_4 = conf.get_scalar<std::string>("paths[4]", default_value);
ASSERT_EQ(path_list_4, env_var_value + "/foo"); // Even when the env var contains another env var, it gets correctly double-expanded

/* Clear the set environment variable after testing */
/* Check that variable expansion is type-aware */
auto boolean = conf.get_scalar<bool>("is_test", false);
ASSERT_EQ(boolean, true);

auto integer = conf.get_scalar<int32_t>("num_test", -1);
ASSERT_EQ(integer, 12);

/* Clear the set environment variables after testing */
SET_ENV_VAR(env_var_name.c_str(), "");
SET_ENV_VAR(embedded_env_var_name.c_str(), "");
SET_ENV_VAR(bool_env_var_name.c_str(), "");
SET_ENV_VAR(int_env_var_name.c_str(), "");
}

TEST(Configuration, configuration_webserver_ip)
Expand Down
2 changes: 1 addition & 1 deletion userspace/falco/yaml_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class yaml_helper
}
std::stringstream ss(str);
T result;
if (ss >> result) return result;
if (ss >> std::boolalpha >> result) return result;
return default_value;
};

Expand Down

0 comments on commit 4add0ee

Please sign in to comment.