From 3d9a3786897b0078e0379fb98808fe9f4036e582 Mon Sep 17 00:00:00 2001 From: Ariel Martin Date: Wed, 13 Nov 2024 19:09:21 -0300 Subject: [PATCH] feat: added changes to tests --- .../configuration_parser/tests/CMakeLists.txt | 2 +- .../tests/configuration_parser_test.cpp | 59 +++++++++++-------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/agent/configuration_parser/tests/CMakeLists.txt b/src/agent/configuration_parser/tests/CMakeLists.txt index 42b5e8b7fe..477498d3d8 100644 --- a/src/agent/configuration_parser/tests/CMakeLists.txt +++ b/src/agent/configuration_parser/tests/CMakeLists.txt @@ -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) diff --git a/src/agent/configuration_parser/tests/configuration_parser_test.cpp b/src/agent/configuration_parser/tests/configuration_parser_test.cpp index 3a23b148fa..08d1a03985 100644 --- a/src/agent/configuration_parser/tests/configuration_parser_test.cpp +++ b/src/agent/configuration_parser/tests/configuration_parser_test.cpp @@ -1,9 +1,11 @@ #include +#include #include #include #include +#include #include #include #include @@ -89,7 +91,7 @@ TEST(ConfigurationParser, GetConfigString) string_conf: string )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig("agent", "server_url"); + const auto ret = parserStr->GetConfig("192.168.0.11", "agent", "server_url"); ASSERT_EQ(ret, "192.168.0.11"); } @@ -103,7 +105,8 @@ TEST(ConfigurationParser, GetConfigArrayString) string_conf: string )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig>("agent_array", "array_manager_ip"); + const auto ret = parserStr->GetConfig>( + {"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"); } @@ -118,7 +121,7 @@ TEST(ConfigurationParser, GetConfigInt) int_conf: 10 )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig("agent_array", "int_conf"); + const auto ret = parserStr->GetConfig(10, "agent_array", "int_conf"); ASSERT_EQ(ret, 10); } @@ -132,7 +135,7 @@ TEST(ConfigurationParser, GetConfigFloat) float_conf: 12.34 )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig("agent_array", "float_conf"); + const auto ret = parserStr->GetConfig(12.34f, "agent_array", "float_conf"); EXPECT_FLOAT_EQ(ret, 12.34f); } @@ -146,7 +149,8 @@ TEST(ConfigurationParser, GetConfigNoKey) float_conf: 12.34 )"; const auto parserStr = std::make_unique(strConfig); - EXPECT_ANY_THROW(parserStr->GetConfig("agent_array", "no_key")); + const auto ret = parserStr->GetConfig(12.34f, "agent_array", "no_key"); // NOLINT + EXPECT_FLOAT_EQ(ret, 12.34f); } TEST(ConfigurationParser, GetConfigIntSubTable) @@ -161,7 +165,7 @@ TEST(ConfigurationParser, GetConfigIntSubTable) int_conf: 1234 )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig("agent_array", "sub_table", "int_conf"); + const auto ret = parserStr->GetConfig(1234, "agent_array", "sub_table", "int_conf"); ASSERT_EQ(ret, 1234); } @@ -178,7 +182,7 @@ TEST(ConfigurationParser, GetConfigBoolSubTable) bool_conf: true )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig("agent_array", "sub_table", "bool_conf"); + const auto ret = parserStr->GetConfig(true, "agent_array", "sub_table", "bool_conf"); ASSERT_EQ(ret, true); } @@ -197,7 +201,10 @@ TEST(ConfigurationParser, GetConfigArrayMap) api_token: api_token2 )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig>>("agent_array", "api_auth"); + const auto ret = parserStr->GetConfig>>( + {{{"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"); @@ -212,7 +219,8 @@ TEST(ConfigurationParser, GetConfigMap) string_conf_2: string_2 )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig>("map_string"); + const auto ret = parserStr->GetConfig>( + {{"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"); } @@ -225,7 +233,9 @@ TEST(ConfigurationParser, GetConfigBadCast) int_conf: 10 )"; const auto parserStr = std::make_unique(strConfig); - EXPECT_ANY_THROW(parserStr->GetConfig>("bad_cast_array")); + const auto ret = parserStr->GetConfig>({"dummy", "string"}, "bad_cast_array"); + ASSERT_EQ(ret[0], "dummy"); + ASSERT_EQ(ret[1], "string"); } TEST(ConfigurationParser, GetConfigMultiNode) @@ -245,10 +255,10 @@ TEST(ConfigurationParser, GetConfigMultiNode) file_wait: 500 )"; const auto parserStr = std::make_unique(strConfig); - const auto ret = parserStr->GetConfig>("agent_array", "array_manager_ip"); - const auto retEnabled = parserStr->GetConfig("logcollector", "enabled"); - const auto retFileWait = parserStr->GetConfig("logcollector", "file_wait"); - const auto retLocalFiles = parserStr->GetConfig>("logcollector", "localfiles"); + const auto ret = parserStr->GetConfig>({}, "agent_array", "array_manager_ip"); + const auto retEnabled = parserStr->GetConfig(false, "logcollector", "enabled"); + const auto retFileWait = parserStr->GetConfig(0, "logcollector", "file_wait"); + const auto retLocalFiles = parserStr->GetConfig>({}, "logcollector", "localfiles"); ASSERT_EQ(ret[0], "192.168.0.0"); ASSERT_EQ(ret[1], "192.168.0.1"); ASSERT_TRUE(retEnabled); @@ -273,11 +283,11 @@ TEST_F(ConfigurationParserFileTest, ValidConfigFileLoadsCorrectly) { const auto parser = std::make_unique(m_tempConfigFilePath); - EXPECT_EQ(parser->GetConfig("agent", "server_url"), "https://myserver:28000"); - EXPECT_FALSE(parser->GetConfig("inventory", "enabled")); - EXPECT_EQ(parser->GetConfig("inventory", "interval"), 7200); - EXPECT_FALSE(parser->GetConfig("logcollector", "enabled")); - EXPECT_EQ(parser->GetConfig("logcollector", "file_wait"), 1000); + EXPECT_EQ(parser->GetConfig("", "agent", "server_url"), "https://myserver:28000"); + EXPECT_FALSE(parser->GetConfig(false, "inventory", "enabled")); + EXPECT_EQ(parser->GetConfig(0, "inventory", "interval"), 7200); + EXPECT_FALSE(parser->GetConfig(false, "logcollector", "enabled")); + EXPECT_EQ(parser->GetConfig(0, "logcollector", "file_wait"), 1000); } catch (const std::exception& e) { @@ -293,11 +303,12 @@ TEST_F(ConfigurationParserInvalidYamlFileTest, InvalidConfigFileLoadsDefault) { const auto parser = std::make_unique(m_tempConfigFilePath); - EXPECT_EQ(parser->GetConfig("agent", "server_url"), "https://localhost:27000"); - EXPECT_TRUE(parser->GetConfig("inventory", "enabled")); - EXPECT_EQ(parser->GetConfig("inventory", "interval"), 3600); - EXPECT_TRUE(parser->GetConfig("logcollector", "enabled")); - EXPECT_EQ(parser->GetConfig("logcollector", "file_wait"), 500); + EXPECT_EQ(parser->GetConfig("https://localhost:27000", "agent", "server_url"), + "https://localhost:27000"); + EXPECT_TRUE(parser->GetConfig(true, "inventory", "enabled")); + EXPECT_EQ(parser->GetConfig(3600, "inventory", "interval"), 3600); + EXPECT_TRUE(parser->GetConfig(true, "logcollector", "enabled")); + EXPECT_EQ(parser->GetConfig(500, "logcollector", "file_wait"), 500); } catch (const std::exception& e) {