From c53ec8616a363563473a78ebe485c2ac79b91452 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 | 62 ++++++++++++------- 2 files changed, 39 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..f0c924a1ea 100644 --- a/src/agent/configuration_parser/tests/configuration_parser_test.cpp +++ b/src/agent/configuration_parser/tests/configuration_parser_test.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -89,7 +90,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("agent", "server_url").value_or("Invalid string"); ASSERT_EQ(ret, "192.168.0.11"); } @@ -103,7 +104,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>("agent_array", "array_manager_ip") + .value_or(std::vector({"Invalid string1", "Invalid string2"})); ASSERT_EQ(ret[0], "192.168.0.0"); ASSERT_EQ(ret[1], "192.168.0.1"); } @@ -118,7 +120,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("agent_array", "int_conf").value_or(1234); ASSERT_EQ(ret, 10); } @@ -132,7 +134,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("agent_array", "float_conf").value_or(1.1f); EXPECT_FLOAT_EQ(ret, 12.34f); } @@ -146,7 +148,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("agent_array", "no_key").value_or(1.1f); // NOLINT + EXPECT_FLOAT_EQ(ret, 1.1f); } TEST(ConfigurationParser, GetConfigIntSubTable) @@ -161,7 +164,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("agent_array", "sub_table", "int_conf").value_or(0); ASSERT_EQ(ret, 1234); } @@ -178,7 +181,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("agent_array", "sub_table", "bool_conf").value_or(false); ASSERT_EQ(ret, true); } @@ -197,7 +200,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>>("agent_array", "api_auth") + .value_or(std::vector> { + {{"org_name", "default1"}, {"api_token", "default_token1"}}, + {{"org_name", "default2"}, {"api_token", "default_token2"}}}); 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 +218,9 @@ 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>("map_string") + .value_or(std::map {{"string_conf_1", "default_1"}, + {"string_conf_2", "default_2"}}); ASSERT_EQ(ret.at("string_conf_1"), "string_1"); ASSERT_EQ(ret.at("string_conf_2"), "string_2"); } @@ -225,7 +233,10 @@ 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>("bad_cast_array") + .value_or(std::vector {"dummy", "string"}); + ASSERT_EQ(ret[0], "dummy"); + ASSERT_EQ(ret[1], "string"); } TEST(ConfigurationParser, GetConfigMultiNode) @@ -245,10 +256,12 @@ 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") + .value_or(std::vector {}); + const auto retEnabled = parserStr->GetConfig("logcollector", "enabled").value_or(false); + const auto retFileWait = parserStr->GetConfig("logcollector", "file_wait").value_or(0); + const auto retLocalFiles = parserStr->GetConfig>("logcollector", "localfiles") + .value_or(std::vector {}); ASSERT_EQ(ret[0], "192.168.0.0"); ASSERT_EQ(ret[1], "192.168.0.1"); ASSERT_TRUE(retEnabled); @@ -273,11 +286,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").value_or(""), "https://myserver:28000"); + EXPECT_FALSE(parser->GetConfig("inventory", "enabled").value_or(true)); + EXPECT_EQ(parser->GetConfig("inventory", "interval").value_or(0), 7200); + EXPECT_FALSE(parser->GetConfig("logcollector", "enabled").value_or(true)); + EXPECT_EQ(parser->GetConfig("logcollector", "file_wait").value_or(0), 1000); } catch (const std::exception& e) { @@ -293,11 +306,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("agent", "server_url").value_or("https://localhost:27000"), + "https://localhost:27000"); + EXPECT_TRUE(parser->GetConfig("inventory", "enabled").value_or(true)); + EXPECT_EQ(parser->GetConfig("inventory", "interval").value_or(3600), 3600); + EXPECT_TRUE(parser->GetConfig("logcollector", "enabled").value_or(true)); + EXPECT_EQ(parser->GetConfig("logcollector", "file_wait").value_or(500), 500); } catch (const std::exception& e) {