-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add test for self-restart functionality
- Loading branch information
Showing
5 changed files
with
183 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
find_package(GTest CONFIG REQUIRED) | ||
|
||
|
||
if(UNIX) | ||
add_executable(restart_test unix_restart_tests.cpp) | ||
endif() | ||
|
||
configure_target(restart_test) | ||
target_include_directories(restart_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src) | ||
target_link_libraries(restart_test PRIVATE Restart GTest::gtest GTest::gmock GTest::gmock_main) | ||
add_test(NAME Restart COMMAND restart_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include <restart_unix.hpp> | ||
|
||
#include <boost/asio/awaitable.hpp> | ||
#include <boost/asio/co_spawn.hpp> | ||
#include <boost/asio/detached.hpp> | ||
#include <boost/asio/io_context.hpp> | ||
#include <boost/asio/use_awaitable.hpp> | ||
|
||
#include <chrono> | ||
#include <csignal> | ||
#include <cstdlib> | ||
#include <iostream> | ||
#include <spdlog/spdlog.h> | ||
#include <string> | ||
#include <thread> | ||
#include <vector> | ||
|
||
using namespace testing; | ||
|
||
namespace | ||
{ | ||
// NOLINTBEGIN(cppcoreguidelines-avoid-reference-signal-handler-definitions) | ||
void SigchldHandler(int status) | ||
{ | ||
// Reap the terminated child process | ||
while (waitpid(-1, &status, WNOHANG) > 0) | ||
{ | ||
// The child process has terminated, no action needed here | ||
} | ||
} | ||
|
||
// NOLINTEND(cppcoreguidelines-avoid-reference-signal-handler-definitions) | ||
} // namespace | ||
|
||
TEST(TestGetCommandLineArgs, ReturnsArgumentsWhenFileIsOpen) | ||
{ | ||
std::vector<char*> args = restart::GetCommandLineArgs(); | ||
|
||
EXPECT_GT(args.size(), 0); | ||
} | ||
|
||
TEST(TestUsingSystemctl, ReturnsTrueWhenSystemctlIsAvailable) | ||
{ | ||
setenv("INVOCATION_ID", "testing", 1); | ||
EXPECT_TRUE(restart::UsingSystemctl()); | ||
} | ||
|
||
TEST(TestUsingSystemctl, ReturnsFalseWhenSystemctlIsNotAvailable) | ||
{ | ||
unsetenv("INVOCATION_ID"); | ||
EXPECT_FALSE(restart::UsingSystemctl()); | ||
} | ||
|
||
TEST(StopAgentTest, GratefullyStop) | ||
{ | ||
signal(SIGCHLD, SigchldHandler); // Set up the signal handler for SIGCHLD | ||
|
||
pid_t pid = fork(); | ||
|
||
if (pid < 0) | ||
{ | ||
FAIL() << "Fork failed!"; | ||
} | ||
if (pid == 0) | ||
{ | ||
// Child process (Simulate agent running) | ||
const int timeout = 60; | ||
std::this_thread::sleep_for(std::chrono::seconds(timeout)); | ||
} | ||
|
||
// Parent process | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
|
||
const int timeout = 10; | ||
restart::StopAgent(pid, timeout); | ||
|
||
ASSERT_TRUE(kill(pid, 0)); | ||
} | ||
|
||
TEST(StopAgentTest, ForceStop) | ||
{ | ||
signal(SIGCHLD, SigchldHandler); // Set up the signal handler for SIGCHLD | ||
signal(SIGTERM, SIG_IGN); // Ignore SIGTERM | ||
|
||
pid_t pid = fork(); | ||
|
||
if (pid < 0) | ||
{ | ||
FAIL() << "Fork failed!"; | ||
} | ||
if (pid == 0) | ||
{ | ||
// Child process (Simulate agent running) | ||
const int timeout = 60; | ||
std::this_thread::sleep_for(std::chrono::seconds(timeout)); | ||
} | ||
|
||
// Parent process | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
|
||
const int timeout = 2; | ||
restart::StopAgent(pid, timeout); | ||
|
||
ASSERT_TRUE(kill(pid, 0)); | ||
} | ||
|
||
// TEST(RestartWithForkTest, ShouldRestartUsingFork) | ||
// { | ||
// signal(SIGCHLD, SigchldHandler); // Set up the signal handler for SIGCHLD | ||
|
||
// pid_t pid = fork(); | ||
|
||
// if (pid < 0) | ||
// { | ||
// FAIL() << "Fork failed!"; | ||
// } | ||
// if (pid == 0) | ||
// { | ||
// // Child process (Simulate agent running) | ||
// boost::asio::io_context io_context; | ||
|
||
// boost::asio::co_spawn( | ||
// io_context, | ||
// []() -> boost::asio::awaitable<void> | ||
// { | ||
// std::vector<char*> args; | ||
// std::string executable = "sleep 20"; | ||
// args.push_back(executable.data()); | ||
// args.push_back(nullptr); // Terminate with nullptr | ||
// const auto result = co_await restart::RestartWithFork(std::move(args)); | ||
|
||
// }(), | ||
// boost::asio::detached); | ||
// const int timeout = 30; | ||
// std::this_thread::sleep_for(std::chrono::seconds(timeout)); | ||
|
||
// } | ||
|
||
// // Parent process | ||
// std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
// int status{}; | ||
// waitpid(pid, &status, 0); | ||
// } | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |