Skip to content

Commit

Permalink
cptests: Tests around sending signal
Browse files Browse the repository at this point in the history
Signed-off-by: Mobin "Hojjat" Aydinfar <[email protected]>
  • Loading branch information
mobin-2008 committed Aug 5, 2023
1 parent ee9526c commit 9db381b
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/tests/cptests/cptests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "dinit.h"
#include "service.h"
#include "baseproc-sys.h"
#include "proc-service.h"
#include "control.h"

#include "../test_service.h"
Expand All @@ -22,6 +23,66 @@ class control_conn_t_test
}
};

// Friend interface to access base_process_service private/protected members.
class base_process_service_test
{
public:
static void exec_succeeded(base_process_service *bsp)
{
bsp->waiting_for_execstat = false;
bsp->exec_succeeded();
}

static void exec_failed(base_process_service *bsp, int errcode)
{
run_proc_err err;
err.stage = exec_stage::DO_EXEC;
err.st_errno = errcode;
bsp->waiting_for_execstat = false;
bsp->pid = -1;
bsp->exec_failed(err);
}

static void handle_exit(base_process_service *bsp, int exit_status)
{
bsp->pid = -1;
bsp->handle_exit_status(bp_sys::exit_status(true, false, exit_status));
}

static void handle_signal_exit(base_process_service *bsp, int signo)
{
bsp->pid = -1;
bsp->handle_exit_status(bp_sys::exit_status(false, true, signo));
}

static void handle_stop_exit(process_service *ps, int exit_status)
{
ps->stop_pid = -1;
ps->waiting_for_execstat = false;
ps->stop_status = bp_sys::exit_status(true, false, exit_status);
ps->handle_stop_exit();
}

static int get_notification_fd(base_process_service *bsp)
{
return bsp->notification_fd;
}
};

namespace bp_sys {
// last signal sent:
extern int last_sig_sent;
//extern pid_t last_forked_pid;
}

static time_val default_restart_interval = time_val(0, 200000000); // 200 milliseconds
static void init_service_defaults(base_process_service &ps)
{
ps.set_restart_interval(time_val(10,0), 3);
ps.set_restart_delay(default_restart_interval); // 200 milliseconds
ps.set_stop_timeout(time_val(10,0));
}

// Size of status buffer, as returned in several packet types
constexpr static int STATUS_BUFFER_SIZE = 6 + ((sizeof(pid_t) > sizeof(int)) ? sizeof(pid_t) : sizeof(int));

Expand Down Expand Up @@ -951,6 +1012,57 @@ void cptest_servicestatus()
delete cc;
}

void cptest_sendsignal()
{
using namespace std;

service_set sset;
ha_string command = "test-command";
list<pair<unsigned,unsigned>> command_offsets;
command_offsets.emplace_back(0, command.length());
std::list<prelim_dep> depends;

process_service p {&sset, "test-service", std::move(command), command_offsets, depends};
init_service_defaults(p);
sset.add_service(&p);

p.start();
sset.process_queues();
base_process_service_test::exec_succeeded(&p);
sset.process_queues();

int fd = bp_sys::allocfd();
auto *cc = new control_conn_t(event_loop, &sset, fd);

// Get a service handle:
control_conn_t::handle_t h = find_service(fd, "test-service", service_state_t::STARTED,
service_state_t::STARTED);

// Prepare a signal
uint32_t sig = SIGHUP;

// Issue a signal (SIGHUP for example):
std::vector<char> cmd = { DINIT_CP_SIGNAL };
char * h_cp = reinterpret_cast<char *>(&h);
char * sig_cp = reinterpret_cast<char *>(&sig);
cmd.insert(cmd.end(), sig_cp, sig_cp + sizeof(sig));
cmd.insert(cmd.end(), h_cp, h_cp + sizeof(h));

bp_sys::supply_read_data(fd, std::move(cmd));
event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd);

std::vector<char> wdata;
bp_sys::extract_written_data(fd, wdata);
assert(wdata.size() == 1);
assert(wdata[0] == DINIT_RP_ACK);

assert(bp_sys::last_sig_sent == SIGHUP);

sset.remove_service(&p);

delete cc;
}


#define RUN_TEST(name, spacing) \
std::cout << #name "..." spacing << std::flush; \
Expand All @@ -975,5 +1087,6 @@ int main(int argc, char **argv)
RUN_TEST(cptest_restart, " ");
RUN_TEST(cptest_wake, " ");
RUN_TEST(cptest_servicestatus, " ");
RUN_TEST(cptest_sendsignal, " ");
return 0;
}

0 comments on commit 9db381b

Please sign in to comment.