Skip to content

Commit

Permalink
feat: Add test for self-restart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
lchico committed Jan 25, 2025
1 parent a5f847f commit bcdf1d4
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 240 deletions.
28 changes: 0 additions & 28 deletions src/agent/restart/CMakeLists.txt

This file was deleted.

19 changes: 0 additions & 19 deletions src/agent/restart/include/restart.hpp

This file was deleted.

117 changes: 0 additions & 117 deletions src/agent/restart/src/restart_unix.cpp

This file was deleted.

46 changes: 0 additions & 46 deletions src/agent/restart/src/restart_unix.hpp

This file was deleted.

15 changes: 0 additions & 15 deletions src/agent/restart/src/restart_win.cpp

This file was deleted.

12 changes: 7 additions & 5 deletions src/agent/restart_handler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ else()
endif()

add_library(RestartHandler ${SOURCES})
target_include_directories(RestartHandler PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(RestartHandler
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(RestartHandler PUBLIC Boost::asio CommandEntry PRIVATE Logger)

include(../../cmake/ConfigureTarget.cmake)
configure_target(RestartHandler)

# if(BUILD_TESTS)
# enable_testing()
# add_subdirectory(tests)
# endif()
if(BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
12 changes: 11 additions & 1 deletion src/agent/restart_handler/include/restart_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@

namespace restart_handler
{
/// @brief Restart class.
/// @brief Class for handling service restarts.
class RestartHandler
{
public:
/// @brief Command-line arguments for the service.
static std::vector<char*> cmd_line;
/// @brief Timeout for the restart operation until force a hard restart (in seconds).
static const int timeout = 30;

explicit RestartHandler();

/// @brief Initializes the RestartHandler with command-line arguments.
/// @param argc Number of arguments.
/// @param argv Array of arguments.
static void Initialize(int argc, char* argv[]);

/// @brief Executes the restart command.
/// @return Result of the restart command execution.
static boost::asio::awaitable<module_command::CommandExecutionResult> RestartCommand();
};
} // namespace restart_handler
10 changes: 3 additions & 7 deletions src/agent/restart_handler/src/restart_handler_unix.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "restart_handler_unix.hpp"
#include <fstream>
#include <logger.hpp>

#include <chrono>
Expand Down Expand Up @@ -41,21 +40,18 @@ namespace restart_handler
}
}

void StopAgent()
void StopAgent(const pid_t pid, const int timeout)
{
const int timeout = 30; // Timeout duration (in seconds) for killing the agent child process
time_t start_time = time(nullptr); // Record the start time to track the timeout duration

pid_t pid = getppid();

// Shutdown Gracefully
kill(pid, SIGTERM);

while (true)
{
if (kill(pid, 0) != 0)
{
LogInfo("Agent gracefully stopped.");
LogInfo("Agent stopped.");
break;
}

Expand All @@ -80,7 +76,7 @@ namespace restart_handler
else if (pid == 0)
{
// Child process
StopAgent();
StopAgent(getppid(), RestartHandler::timeout);

LogInfo("Starting wazuh agent in a new process.");

Expand Down
3 changes: 1 addition & 2 deletions src/agent/restart_handler/src/restart_handler_unix.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include <boost/asio/awaitable.hpp>
#include <command_entry.hpp>
#include <restart_handler.hpp>
#include <vector>

Expand All @@ -17,7 +16,7 @@ namespace restart_handler
///
/// This function sends a SIGTERM signal to the agent process to stop it. If the agent process
/// does not stop within a specified timeout period (30 seconds), it forces termination with a SIGKILL signal.
void StopAgent();
void StopAgent(const pid_t pid, const int timeout);

/// @brief Restarts the module by forking a new process.
///
Expand Down
10 changes: 10 additions & 0 deletions src/agent/restart_handler/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
find_package(GTest CONFIG REQUIRED)

if(UNIX)
add_executable(restart_handler_test restart_handler_tests.cpp)
endif()

configure_target(restart_handler_test)
target_include_directories(restart_handler_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
target_link_libraries(restart_handler_test PRIVATE RestartHandler GTest::gtest GTest::gmock GTest::gmock_main)
add_test(NAME RestartHandler COMMAND restart_handler_test)
Loading

0 comments on commit bcdf1d4

Please sign in to comment.