1
0
mirror of https://github.com/falk-werner/webfused synced 2024-10-27 20:44:08 +00:00
falk-werner_webfused/test/daemon.cc
2020-03-20 13:48:58 +01:00

89 lines
1.9 KiB
C++

#include "webfused/daemon.h"
#include "mock/server.hpp"
#include <gtest/gtest.h>
#include <cstdlib>
#include <chrono>
#include <thread>
using ::webfused_test::MockServer;
using ::testing::_;
using ::testing::Return;
TEST(daemon, print_usage)
{
char argv0[] = "daemon";
char argv1[] = "--help";
char * argv[] = { argv0, argv1, NULL};
int exit_code = wfd_daemon_run(2, argv);
ASSERT_EQ(EXIT_SUCCESS, exit_code);
}
TEST(daemon, print_usage_short)
{
char argv0[] = "daemon";
char argv1[] = "-h";
char * argv[] = { argv0, argv1, NULL};
int exit_code = wfd_daemon_run(2, argv);
ASSERT_EQ(EXIT_SUCCESS, exit_code);
}
TEST(daemon, fail_invalid_argument)
{
char argv0[] = "daemon";
char argv1[] = "-x";
char * argv[] = { argv0, argv1, NULL};
int exit_code = wfd_daemon_run(2, argv);
ASSERT_EQ(EXIT_FAILURE, exit_code);
}
TEST(daemon, fail_invalid_config_file)
{
char argv0[] = "daemon";
char argv1[] = "-f";
char argv2[] = "invalid.conf";
char * argv[] = { argv0, argv1, argv2, NULL};
int exit_code = wfd_daemon_run(3, argv);
ASSERT_EQ(EXIT_FAILURE, exit_code);
}
void defered_raise()
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));
raise(SIGINT);
}
TEST(daemon, run)
{
char argv0[] = "daemon";
char argv1[] = "-f";
char argv2[] = "webfused.conf";
char * argv[] = { argv0, argv1, argv2, NULL};
std::thread thread(defered_raise);
int exit_code = wfd_daemon_run(3, argv);
ASSERT_EQ(EXIT_SUCCESS, exit_code);
thread.join();
}
TEST(daemon, run_failed_to_create_server)
{
MockServer server;
EXPECT_CALL(server, wf_server_create(_)).Times(1).WillOnce(Return(nullptr));
char argv0[] = "daemon";
char argv1[] = "-f";
char argv2[] = "webfused.conf";
char * argv[] = { argv0, argv1, argv2, NULL};
int exit_code = wfd_daemon_run(3, argv);
ASSERT_EQ(EXIT_FAILURE, exit_code);
}