Skip to content

Commit

Permalink
fix: renames variables and deletes the temporary test file in case of…
Browse files Browse the repository at this point in the history
… exception.
  • Loading branch information
Nicogp committed Nov 4, 2024
1 parent 49f1f2e commit c5dc02a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace configuration
{
private:
/// @brief Holds the parsed YAML configuration.
YAML::Node config;
YAML::Node m_config;

public:
/// @brief Default constructor. Loads configuration from a default file path.
Expand Down Expand Up @@ -53,7 +53,7 @@ namespace configuration
{
try
{
YAML::Node current = YAML::Clone(config);
YAML::Node current = YAML::Clone(m_config);

(
[&current](const auto& key)
Expand Down
6 changes: 3 additions & 3 deletions src/agent/configuration_parser/src/configuration_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace configuration
{
try
{
config = YAML::LoadFile(configFile.string());
m_config = YAML::LoadFile(configFile.string());
}
catch (const std::exception& e)
{
Expand Down Expand Up @@ -42,7 +42,7 @@ namespace configuration
file_wait: 500
)";

config = YAML::Load(yamlStr);
m_config = YAML::Load(yamlStr);
}
}

Expand All @@ -55,7 +55,7 @@ namespace configuration
{
try
{
config = YAML::Load(stringToParse);
m_config = YAML::Load(stringToParse);
}
catch (const std::exception& e)
{
Expand Down
60 changes: 40 additions & 20 deletions src/agent/configuration_parser/tests/configuration_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ using namespace configuration;
class ConfigurationParserFileTest : public ::testing::Test
{
protected:
std::filesystem::path tempConfigFilePath;
std::filesystem::path m_tempConfigFilePath;

void SetUp() override
{
tempConfigFilePath = "temp_wazuh-agent.yml";
m_tempConfigFilePath = "temp_wazuh-agent.yml";

std::ofstream outFile(tempConfigFilePath);
std::ofstream outFile(m_tempConfigFilePath);
outFile << R"(
agent:
server_url: https://myserver:28000
Expand All @@ -39,20 +39,22 @@ class ConfigurationParserFileTest : public ::testing::Test

void TearDown() override
{
std::filesystem::remove(tempConfigFilePath);
std::filesystem::remove(m_tempConfigFilePath);
}
};

class ConfigurationParserInvalidYamlFileTest : public ::testing::Test
{
protected:
std::filesystem::path tempConfigFilePath;
std::filesystem::path m_tempConfigFilePath;

void SetUp() override
{
tempConfigFilePath = "temp_wazuh-agent.yml";
m_tempConfigFilePath = "temp_wazuh-agent.yml";

std::ofstream outFile(tempConfigFilePath);
std::ofstream outFile(m_tempConfigFilePath);
// This string does not respect the yaml format in the line of the file_wait field, the field is misaligned.
// With this case we want it to fail when parsing the file.
outFile << R"(
agent:
server_url: https://myserver:28000
Expand All @@ -73,7 +75,7 @@ class ConfigurationParserInvalidYamlFileTest : public ::testing::Test

void TearDown() override
{
std::filesystem::remove(tempConfigFilePath);
std::filesystem::remove(m_tempConfigFilePath);
}
};

Expand Down Expand Up @@ -265,24 +267,42 @@ TEST(ConfigurationParser, ConfigurationParserStringMisaligned)

TEST_F(ConfigurationParserFileTest, ValidConfigFileLoadsCorrectly)
{
const auto parser = std::make_unique<configuration::ConfigurationParser>(tempConfigFilePath);
try
{
const auto parser = std::make_unique<configuration::ConfigurationParser>(m_tempConfigFilePath);

EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url"), "https://myserver:28000");
EXPECT_FALSE(parser->GetConfig<bool>("inventory", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval"), 7200);
EXPECT_FALSE(parser->GetConfig<bool>("logcollector", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait"), 1000);
EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url"), "https://myserver:28000");
EXPECT_FALSE(parser->GetConfig<bool>("inventory", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval"), 7200);
EXPECT_FALSE(parser->GetConfig<bool>("logcollector", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait"), 1000);
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << '\n';
std::filesystem::remove(m_tempConfigFilePath);
throw;
}
}

TEST_F(ConfigurationParserInvalidYamlFileTest, InvalidConfigFileLoadsDefault)
{
const auto parser = std::make_unique<configuration::ConfigurationParser>(tempConfigFilePath);
try
{
const auto parser = std::make_unique<configuration::ConfigurationParser>(m_tempConfigFilePath);

EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url"), "https://localhost:27000");
EXPECT_TRUE(parser->GetConfig<bool>("inventory", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval"), 3600);
EXPECT_TRUE(parser->GetConfig<bool>("logcollector", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait"), 500);
EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url"), "https://localhost:27000");
EXPECT_TRUE(parser->GetConfig<bool>("inventory", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval"), 3600);
EXPECT_TRUE(parser->GetConfig<bool>("logcollector", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait"), 500);
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << '\n';
std::filesystem::remove(m_tempConfigFilePath);
throw;
}
}

int main(int argc, char** argv)
Expand Down

0 comments on commit c5dc02a

Please sign in to comment.