From 19ea588b7b6fa87de9971cd7572c13c81129b8e7 Mon Sep 17 00:00:00 2001 From: Ariel Martin Date: Fri, 20 Dec 2024 20:49:42 -0300 Subject: [PATCH] refactor: fixes tests after changes --- .../tests/centralized_configuration_tests.cpp | 20 +++-- .../communicator/tests/communicator_test.cpp | 90 ++++++++++++++++--- 2 files changed, 89 insertions(+), 21 deletions(-) diff --git a/src/agent/centralized_configuration/tests/centralized_configuration_tests.cpp b/src/agent/centralized_configuration/tests/centralized_configuration_tests.cpp index a42866be84..c056730de1 100644 --- a/src/agent/centralized_configuration/tests/centralized_configuration_tests.cpp +++ b/src/agent/centralized_configuration/tests/centralized_configuration_tests.cpp @@ -84,8 +84,8 @@ TEST(CentralizedConfiguration, ExecuteCommandReturnsFailureOnParseParameters) { CentralizedConfiguration centralizedConfiguration; centralizedConfiguration.SetGroupIdFunction([](const std::vector&) { return true; }); - centralizedConfiguration.SetDownloadGroupFilesFunction([](const std::string&, const std::string&) - { return true; }); + centralizedConfiguration.SetDownloadGroupFilesFunction( + [](std::string, std::string) -> boost::asio::awaitable { co_return true; }); centralizedConfiguration.ValidateFileFunction([](const std::filesystem::path&) { return true; }); centralizedConfiguration.ReloadModulesFunction([]() {}); @@ -123,8 +123,8 @@ TEST(CentralizedConfiguration, ExecuteCommandHandlesRecognizedCommands) CentralizedConfiguration centralizedConfiguration(std::move(mockFileSystem)); centralizedConfiguration.SetGroupIdFunction([](const std::vector&) { return true; }); centralizedConfiguration.GetGroupIdFunction([]() { return std::vector {"group1", "group2"}; }); - centralizedConfiguration.SetDownloadGroupFilesFunction([](const std::string&, const std::string&) - { return true; }); + centralizedConfiguration.SetDownloadGroupFilesFunction( + [](std::string, std::string) -> boost::asio::awaitable { co_return true; }); centralizedConfiguration.ValidateFileFunction([](const std::filesystem::path&) { return true; }); centralizedConfiguration.ReloadModulesFunction([]() {}); @@ -186,12 +186,14 @@ TEST(CentralizedConfiguration, SetFunctionsAreCalledAndReturnsCorrectResultsForS return true; }); + // NOLINTBEGIN(cppcoreguidelines-avoid-capturing-lambda-coroutines) centralizedConfiguration.SetDownloadGroupFilesFunction( - [&wasDownloadGroupFilesFunctionCalled](const std::string&, const std::string&) + [&wasDownloadGroupFilesFunctionCalled](std::string, std::string) -> boost::asio::awaitable { wasDownloadGroupFilesFunctionCalled = true; - return wasDownloadGroupFilesFunctionCalled; + co_return wasDownloadGroupFilesFunctionCalled; }); + // NOLINTEND(cppcoreguidelines-avoid-capturing-lambda-coroutines) centralizedConfiguration.ValidateFileFunction([](const std::filesystem::path&) { return true; }); centralizedConfiguration.ReloadModulesFunction([]() {}); @@ -244,12 +246,14 @@ TEST(CentralizedConfiguration, SetFunctionsAreCalledAndReturnsCorrectResultsForU return std::vector {"group1", "group2"}; }); + // NOLINTBEGIN(cppcoreguidelines-avoid-capturing-lambda-coroutines) centralizedConfiguration.SetDownloadGroupFilesFunction( - [&wasDownloadGroupFilesFunctionCalled](const std::string&, const std::string&) + [&wasDownloadGroupFilesFunctionCalled](std::string, std::string) -> boost::asio::awaitable { wasDownloadGroupFilesFunctionCalled = true; - return wasDownloadGroupFilesFunctionCalled; + co_return wasDownloadGroupFilesFunctionCalled; }); + // NOLINTEND(cppcoreguidelines-avoid-capturing-lambda-coroutines) centralizedConfiguration.ValidateFileFunction([](const std::filesystem::path&) { return true; }); centralizedConfiguration.ReloadModulesFunction([]() {}); diff --git a/src/agent/communicator/tests/communicator_test.cpp b/src/agent/communicator/tests/communicator_test.cpp index 21493831d1..ebe6d5a190 100644 --- a/src/agent/communicator/tests/communicator_test.cpp +++ b/src/agent/communicator/tests/communicator_test.cpp @@ -215,19 +215,51 @@ TEST(CommunicatorTest, GetGroupConfigurationFromManager_Success) // not really a leak, as its lifetime is managed by the Communicator testing::Mock::AllowLeak(mockHttpClientPtr); - auto communicatorPtr = - std::make_shared(std::move(mockHttpClient), "uuid", "key", nullptr, FUNC); std::string groupName = "group1"; - std::string dstFilePath = "/path/to/file"; + std::string dstFilePath = "./test-output"; boost::beast::http::response mockResponse; mockResponse.result(boost::beast::http::status::ok); - // TODO: Expect call to Co_PerformHttpRequest - // EXPECT_CALL(*mockHttpClientPtr, PerformHttpRequestDownload(_, dstFilePath)).WillOnce(Return(mockResponse)); + // NOLINTBEGIN(cppcoreguidelines-avoid-reference-coroutine-parameters) + auto MockCo_PerformHttpRequest = + [](std::shared_ptr, + const http_client::HttpRequestParams&, + const GetMessagesFuncType&, + const std::function&, + [[maybe_unused]] std::time_t connectionRetry, + [[maybe_unused]] size_t batchSize, + [[maybe_unused]] std::function pOnSuccess, + [[maybe_unused]] std::function loopRequestCondition) -> boost::asio::awaitable + { + pOnSuccess(200, "Dummy response"); // NOLINT(cppcoreguidelines-avoid-magic-numbers) + co_return; + }; + // NOLINTEND(cppcoreguidelines-avoid-reference-coroutine-parameters) + + EXPECT_CALL(*mockHttpClient, Co_PerformHttpRequest(_, _, _, _, _, _, _, _)) + .WillOnce(Invoke(MockCo_PerformHttpRequest)); + + communicator::Communicator communicator(std::move(mockHttpClient), "uuid", "key", nullptr, FUNC); + + std::future result; + + auto task = communicator.GetGroupConfigurationFromManager(groupName, dstFilePath); + boost::asio::io_context ioContext; + boost::asio::co_spawn( + ioContext, + [&]() -> boost::asio::awaitable + { + bool value = co_await communicator.GetGroupConfigurationFromManager(groupName, dstFilePath); + std::promise promise; + promise.set_value(value); + result = promise.get_future(); + }, + boost::asio::detached); - EXPECT_TRUE(communicatorPtr->GetGroupConfigurationFromManager(groupName, dstFilePath)); + ioContext.run(); + EXPECT_TRUE(result.get()); } TEST(CommunicatorTest, GetGroupConfigurationFromManager_Error) @@ -237,19 +269,51 @@ TEST(CommunicatorTest, GetGroupConfigurationFromManager_Error) // not really a leak, as its lifetime is managed by the Communicator testing::Mock::AllowLeak(mockHttpClientPtr); - auto communicatorPtr = - std::make_shared(std::move(mockHttpClient), "uuid", "key", nullptr, FUNC); std::string groupName = "group1"; - std::string dstFilePath = "/path/to/file"; + std::string dstFilePath = "dummy/non/existing/path"; boost::beast::http::response mockResponse; - mockResponse.result(boost::beast::http::status::internal_server_error); + mockResponse.result(boost::beast::http::status::ok); - // TODO: Expect call to Co_PerformHttpRequest - // EXPECT_CALL(*mockHttpClientPtr, PerformHttpRequestDownload(_, dstFilePath)).WillOnce(Return(mockResponse)); + // NOLINTBEGIN(cppcoreguidelines-avoid-reference-coroutine-parameters) + auto MockCo_PerformHttpRequest = + [](std::shared_ptr, + const http_client::HttpRequestParams&, + const GetMessagesFuncType&, + const std::function&, + [[maybe_unused]] std::time_t connectionRetry, + [[maybe_unused]] size_t batchSize, + [[maybe_unused]] std::function pOnSuccess, + [[maybe_unused]] std::function loopRequestCondition) -> boost::asio::awaitable + { + pOnSuccess(200, "Dummy response"); // NOLINT(cppcoreguidelines-avoid-magic-numbers) + co_return; + }; + // NOLINTEND(cppcoreguidelines-avoid-reference-coroutine-parameters) - EXPECT_FALSE(communicatorPtr->GetGroupConfigurationFromManager(groupName, dstFilePath)); + EXPECT_CALL(*mockHttpClient, Co_PerformHttpRequest(_, _, _, _, _, _, _, _)) + .WillOnce(Invoke(MockCo_PerformHttpRequest)); + + communicator::Communicator communicator(std::move(mockHttpClient), "uuid", "key", nullptr, FUNC); + + std::future result; + + auto task = communicator.GetGroupConfigurationFromManager(groupName, dstFilePath); + boost::asio::io_context ioContext; + boost::asio::co_spawn( + ioContext, + [&]() -> boost::asio::awaitable + { + bool value = co_await communicator.GetGroupConfigurationFromManager(groupName, dstFilePath); + std::promise promise; + promise.set_value(value); + result = promise.get_future(); + }, + boost::asio::detached); + + ioContext.run(); + EXPECT_FALSE(result.get()); } int main(int argc, char** argv)