Skip to content

Commit

Permalink
feat: added changes to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Nov 13, 2024
1 parent e75ca92 commit 3d9a378
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/agent/configuration_parser/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ find_package(GTest CONFIG REQUIRED)

add_executable(ConfigurationParser_test configuration_parser_test.cpp)
configure_target(ConfigurationParser_test)
target_link_libraries(ConfigurationParser_test PUBLIC ConfigurationParser GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main Logger)
target_link_libraries(ConfigurationParser_test PUBLIC ConfigurationParser Config GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main Logger)
add_test(NAME ConfigParserTest COMMAND ConfigurationParser_test)
59 changes: 35 additions & 24 deletions src/agent/configuration_parser/tests/configuration_parser_test.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <configuration_parser.hpp>

#include <config.h>
#include <gtest/gtest.h>

#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <string>
Expand Down Expand Up @@ -89,7 +91,7 @@ TEST(ConfigurationParser, GetConfigString)
string_conf: string
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::string>("agent", "server_url");
const auto ret = parserStr->GetConfig<std::string>("192.168.0.11", "agent", "server_url");
ASSERT_EQ(ret, "192.168.0.11");
}

Expand All @@ -103,7 +105,8 @@ TEST(ConfigurationParser, GetConfigArrayString)
string_conf: string
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip");
const auto ret = parserStr->GetConfig<std::vector<std::string>>(
{"192.168.0.0", "192.168.0.1"}, "agent_array", "array_manager_ip");
ASSERT_EQ(ret[0], "192.168.0.0");
ASSERT_EQ(ret[1], "192.168.0.1");
}
Expand All @@ -118,7 +121,7 @@ TEST(ConfigurationParser, GetConfigInt)
int_conf: 10
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<int>("agent_array", "int_conf");
const auto ret = parserStr->GetConfig<int>(10, "agent_array", "int_conf");
ASSERT_EQ(ret, 10);
}

Expand All @@ -132,7 +135,7 @@ TEST(ConfigurationParser, GetConfigFloat)
float_conf: 12.34
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<float>("agent_array", "float_conf");
const auto ret = parserStr->GetConfig<float>(12.34f, "agent_array", "float_conf");
EXPECT_FLOAT_EQ(ret, 12.34f);
}

Expand All @@ -146,7 +149,8 @@ TEST(ConfigurationParser, GetConfigNoKey)
float_conf: 12.34
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
EXPECT_ANY_THROW(parserStr->GetConfig<float>("agent_array", "no_key"));
const auto ret = parserStr->GetConfig<float>(12.34f, "agent_array", "no_key"); // NOLINT
EXPECT_FLOAT_EQ(ret, 12.34f);
}

TEST(ConfigurationParser, GetConfigIntSubTable)
Expand All @@ -161,7 +165,7 @@ TEST(ConfigurationParser, GetConfigIntSubTable)
int_conf: 1234
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<int>("agent_array", "sub_table", "int_conf");
const auto ret = parserStr->GetConfig<int>(1234, "agent_array", "sub_table", "int_conf");
ASSERT_EQ(ret, 1234);
}

Expand All @@ -178,7 +182,7 @@ TEST(ConfigurationParser, GetConfigBoolSubTable)
bool_conf: true
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<bool>("agent_array", "sub_table", "bool_conf");
const auto ret = parserStr->GetConfig<bool>(true, "agent_array", "sub_table", "bool_conf");
ASSERT_EQ(ret, true);
}

Expand All @@ -197,7 +201,10 @@ TEST(ConfigurationParser, GetConfigArrayMap)
api_token: api_token2
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::vector<std::map<std::string, std::string>>>("agent_array", "api_auth");
const auto ret = parserStr->GetConfig<std::vector<std::map<std::string, std::string>>>(
{{{"org_name", "dummy1"}, {"api_token", "api_token1"}}, {{"org_name", "dummy2"}, {"api_token", "api_token2"}}},
"agent_array",
"api_auth");
ASSERT_EQ(ret[0].at("org_name"), "dummy1");
ASSERT_EQ(ret[0].at("api_token"), "api_token1");
ASSERT_EQ(ret[1].at("org_name"), "dummy2");
Expand All @@ -212,7 +219,8 @@ TEST(ConfigurationParser, GetConfigMap)
string_conf_2: string_2
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::map<std::string, std::string>>("map_string");
const auto ret = parserStr->GetConfig<std::map<std::string, std::string>>(
{{"string_conf_1", "string_1"}, {"string_conf_2", "string_2"}}, "map_string");
ASSERT_EQ(ret.at("string_conf_1"), "string_1");
ASSERT_EQ(ret.at("string_conf_2"), "string_2");
}
Expand All @@ -225,7 +233,9 @@ TEST(ConfigurationParser, GetConfigBadCast)
int_conf: 10
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
EXPECT_ANY_THROW(parserStr->GetConfig<std::vector<std::string>>("bad_cast_array"));
const auto ret = parserStr->GetConfig<std::vector<std::string>>({"dummy", "string"}, "bad_cast_array");
ASSERT_EQ(ret[0], "dummy");
ASSERT_EQ(ret[1], "string");
}

TEST(ConfigurationParser, GetConfigMultiNode)
Expand All @@ -245,10 +255,10 @@ TEST(ConfigurationParser, GetConfigMultiNode)
file_wait: 500
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip");
const auto retEnabled = parserStr->GetConfig<bool>("logcollector", "enabled");
const auto retFileWait = parserStr->GetConfig<int>("logcollector", "file_wait");
const auto retLocalFiles = parserStr->GetConfig<std::vector<std::string>>("logcollector", "localfiles");
const auto ret = parserStr->GetConfig<std::vector<std::string>>({}, "agent_array", "array_manager_ip");
const auto retEnabled = parserStr->GetConfig<bool>(false, "logcollector", "enabled");
const auto retFileWait = parserStr->GetConfig<int>(0, "logcollector", "file_wait");
const auto retLocalFiles = parserStr->GetConfig<std::vector<std::string>>({}, "logcollector", "localfiles");
ASSERT_EQ(ret[0], "192.168.0.0");
ASSERT_EQ(ret[1], "192.168.0.1");
ASSERT_TRUE(retEnabled);
Expand All @@ -273,11 +283,11 @@ TEST_F(ConfigurationParserFileTest, ValidConfigFileLoadsCorrectly)
{
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>(false, "inventory", "enabled"));
EXPECT_EQ(parser->GetConfig<int>(0, "inventory", "interval"), 7200);
EXPECT_FALSE(parser->GetConfig<bool>(false, "logcollector", "enabled"));
EXPECT_EQ(parser->GetConfig<int>(0, "logcollector", "file_wait"), 1000);
}
catch (const std::exception& e)
{
Expand All @@ -293,11 +303,12 @@ TEST_F(ConfigurationParserInvalidYamlFileTest, InvalidConfigFileLoadsDefault)
{
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>("https://localhost:27000", "agent", "server_url"),
"https://localhost:27000");
EXPECT_TRUE(parser->GetConfig<bool>(true, "inventory", "enabled"));
EXPECT_EQ(parser->GetConfig<int>(3600, "inventory", "interval"), 3600);
EXPECT_TRUE(parser->GetConfig<bool>(true, "logcollector", "enabled"));
EXPECT_EQ(parser->GetConfig<int>(500, "logcollector", "file_wait"), 500);
}
catch (const std::exception& e)
{
Expand Down

0 comments on commit 3d9a378

Please sign in to comment.