diff --git a/unit_tests/falco/test_configuration.cpp b/unit_tests/falco/test_configuration.cpp index 174cebd8991..0b7b3fdee0b 100644 --- a/unit_tests/falco/test_configuration.cpp +++ b/unit_tests/falco/test_configuration.cpp @@ -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" @@ -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); @@ -208,9 +219,18 @@ TEST(Configuration, configuration_environment_variables) auto path_list_4 = conf.get_scalar("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("is_test", false); + ASSERT_EQ(boolean, true); + + auto integer = conf.get_scalar("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) diff --git a/userspace/falco/yaml_helper.h b/userspace/falco/yaml_helper.h index 7193a3d220a..3843b03cf7c 100644 --- a/userspace/falco/yaml_helper.h +++ b/userspace/falco/yaml_helper.h @@ -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; };