new(userspace,unit_tests): return loaded config filenames in config::load_from_file.

Add a debug log with the list of loaded config files.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
This commit is contained in:
Federico Di Pierro 2024-04-08 14:04:16 +02:00 committed by poiana
parent df220e3c3b
commit aac9b550d3
3 changed files with 54 additions and 21 deletions

View File

@ -37,6 +37,8 @@ static std::string sample_yaml =
" - elem2\n" " - elem2\n"
" - elem3\n"; " - elem3\n";
static std::vector<std::string> loaded_conf_files;
TEST(Configuration, configuration_exceptions) TEST(Configuration, configuration_exceptions)
{ {
yaml_helper conf; yaml_helper conf;
@ -136,7 +138,7 @@ TEST(Configuration, configuration_config_files_secondary_fail)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_ANY_THROW(conf.load_from_file("main.yaml")); ASSERT_ANY_THROW(conf.load_from_file("main.yaml", loaded_conf_files));
std::filesystem::remove("main.yaml"); std::filesystem::remove("main.yaml");
std::filesystem::remove("conf_2.yaml"); std::filesystem::remove("conf_2.yaml");
@ -183,7 +185,10 @@ TEST(Configuration, configuration_config_files_ok)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_NO_THROW(conf.load_from_file("main.yaml")); ASSERT_NO_THROW(conf.load_from_file("main.yaml", loaded_conf_files));
// main + conf_2 + conf_3
ASSERT_EQ(loaded_conf_files.size(), 3);
ASSERT_TRUE(conf.is_defined("foo")); ASSERT_TRUE(conf.is_defined("foo"));
ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar"); ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar");
@ -252,7 +257,10 @@ TEST(Configuration, configuration_config_files_relative_main)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_NO_THROW(conf.load_from_file(temp_main.string())); ASSERT_NO_THROW(conf.load_from_file(temp_main.string(), loaded_conf_files));
// main + conf_3
ASSERT_EQ(loaded_conf_files.size(), 2);
ASSERT_TRUE(conf.is_defined("foo")); ASSERT_TRUE(conf.is_defined("foo"));
ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar"); ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar");
@ -302,7 +310,10 @@ TEST(Configuration, configuration_config_files_override)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_NO_THROW(conf.load_from_file("main.yaml")); ASSERT_NO_THROW(conf.load_from_file("main.yaml", loaded_conf_files));
// main + conf_2 + conf_3
ASSERT_EQ(loaded_conf_files.size(), 3);
ASSERT_TRUE(conf.is_defined("foo")); ASSERT_TRUE(conf.is_defined("foo"));
ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar"); ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar");
@ -335,7 +346,10 @@ TEST(Configuration, configuration_config_files_unexistent)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_NO_THROW(conf.load_from_file("main.yaml")); ASSERT_NO_THROW(conf.load_from_file("main.yaml", loaded_conf_files));
// main
ASSERT_EQ(loaded_conf_files.size(), 1);
ASSERT_TRUE(conf.is_defined("base_value.id")); ASSERT_TRUE(conf.is_defined("base_value.id"));
ASSERT_EQ(conf.get_scalar<int>("base_value.id", 0), 1); ASSERT_EQ(conf.get_scalar<int>("base_value.id", 0), 1);
@ -368,7 +382,10 @@ TEST(Configuration, configuration_config_files_scalar_configs_files)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_NO_THROW(conf.load_from_file("main.yaml")); ASSERT_NO_THROW(conf.load_from_file("main.yaml", loaded_conf_files));
// main + conf_2
ASSERT_EQ(loaded_conf_files.size(), 2);
ASSERT_TRUE(conf.is_defined("foo")); ASSERT_TRUE(conf.is_defined("foo"));
ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar"); ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar");
@ -400,7 +417,10 @@ TEST(Configuration, configuration_config_files_empty_configs_files)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_NO_THROW(conf.load_from_file("main.yaml")); ASSERT_NO_THROW(conf.load_from_file("main.yaml", loaded_conf_files));
// main
ASSERT_EQ(loaded_conf_files.size(), 1);
ASSERT_TRUE(conf.is_defined("foo")); ASSERT_TRUE(conf.is_defined("foo"));
ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar"); ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar");
@ -427,7 +447,7 @@ TEST(Configuration, configuration_config_files_self)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_ANY_THROW(conf.load_from_file("main.yaml")); ASSERT_ANY_THROW(conf.load_from_file("main.yaml", loaded_conf_files));
std::filesystem::remove("main.yaml"); std::filesystem::remove("main.yaml");
} }
@ -479,7 +499,10 @@ TEST(Configuration, configuration_config_files_directory)
outfile.close(); outfile.close();
yaml_helper conf; yaml_helper conf;
ASSERT_NO_THROW(conf.load_from_file("main.yaml")); ASSERT_NO_THROW(conf.load_from_file("main.yaml", loaded_conf_files));
// main + conf_2 + conf_3
ASSERT_EQ(loaded_conf_files.size(), 3);
ASSERT_TRUE(conf.is_defined("foo")); ASSERT_TRUE(conf.is_defined("foo"));
ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar"); ASSERT_EQ(conf.get_scalar<std::string>("foo", ""), "bar");

View File

@ -94,9 +94,10 @@ void falco_configuration::init(const std::vector<std::string>& cmdline_options)
void falco_configuration::init(const std::string& conf_filename, const std::vector<std::string> &cmdline_options) void falco_configuration::init(const std::string& conf_filename, const std::vector<std::string> &cmdline_options)
{ {
yaml_helper config; yaml_helper config;
std::vector<std::string> loaded_files;
try try
{ {
config.load_from_file(conf_filename); config.load_from_file(conf_filename, loaded_files);
} }
catch(const std::exception& e) catch(const std::exception& e)
{ {
@ -106,6 +107,13 @@ void falco_configuration::init(const std::string& conf_filename, const std::vect
init_cmdline_options(config, cmdline_options); init_cmdline_options(config, cmdline_options);
load_yaml(conf_filename, config); load_yaml(conf_filename, config);
// Here we have already set up correct logging level
falco_logger::log(falco_logger::level::DEBUG, "Loaded config filenames:\n");
for (const auto& path : loaded_files)
{
falco_logger::log(falco_logger::level::DEBUG, std::string(" ") + path + "\n");
}
} }
void falco_configuration::load_engine_config(const std::string& config_name, const yaml_helper& config) void falco_configuration::load_engine_config(const std::string& config_name, const yaml_helper& config)

View File

@ -92,9 +92,10 @@ public:
/** /**
* Load the YAML document from the given file path. * Load the YAML document from the given file path.
*/ */
void load_from_file(const std::string& path) void load_from_file(const std::string& path, std::vector<std::string>& loaded_config_files)
{ {
m_root = load_from_file_int(path); loaded_config_files.clear();
m_root = load_from_file_int(path, loaded_config_files);
const auto ppath = std::filesystem::path(path); const auto ppath = std::filesystem::path(path);
const auto config_folder = ppath.parent_path(); const auto config_folder = ppath.parent_path();
@ -120,7 +121,7 @@ public:
{ {
if (std::filesystem::is_regular_file(include_file_path)) if (std::filesystem::is_regular_file(include_file_path))
{ {
include_config_file(include_file_path.string()); include_config_file(include_file_path.string(), loaded_config_files);
} }
else if (std::filesystem::is_directory(include_file_path)) else if (std::filesystem::is_directory(include_file_path))
{ {
@ -138,11 +139,11 @@ public:
{ {
falco_logger::log(falco_logger::level::WARNING, "Included config file has wrong type: " + dir_entry.path().string()); falco_logger::log(falco_logger::level::WARNING, "Included config file has wrong type: " + dir_entry.path().string());
} }
std::sort(v.begin(), v.end()); }
for (const auto &f : v) std::sort(v.begin(), v.end());
{ for (const auto &f : v)
include_config_file(f); {
} include_config_file(f, loaded_config_files);
} }
} }
else else
@ -215,16 +216,17 @@ public:
private: private:
YAML::Node m_root; YAML::Node m_root;
YAML::Node load_from_file_int(const std::string& path) YAML::Node load_from_file_int(const std::string& path, std::vector<std::string>& loaded_config_files)
{ {
auto root = YAML::LoadFile(path); auto root = YAML::LoadFile(path);
pre_process_env_vars(root); pre_process_env_vars(root);
loaded_config_files.push_back(path);
return root; return root;
} }
void include_config_file(const std::string& include_file_path) void include_config_file(const std::string& include_file_path, std::vector<std::string>& loaded_config_files)
{ {
auto loaded_nodes = load_from_file_int(include_file_path); auto loaded_nodes = load_from_file_int(include_file_path, loaded_config_files);
for(auto n : loaded_nodes) for(auto n : loaded_nodes)
{ {
/* /*