Skip to content

Commit

Permalink
cptests: Initial tests around sending signal
Browse files Browse the repository at this point in the history
Doesn't work currently

Signed-off-by: Mobin "Hojjat" Aydinfar <[email protected]>
  • Loading branch information
mobin-2008 committed Aug 4, 2023
1 parent 87256b5 commit 04ed83c
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 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, "testproc", std::move(command), command_offsets, depends};
init_service_defaults(p);
sset.add_service(&p);

p.start();
sset.process_queues();
assert(p.get_state() == service_state_t::STARTING);
base_process_service_test::exec_succeeded(&p);
sset.process_queues();
assert(p.get_state() == service_state_t::STARTED);
assert(event_loop.active_timers.size() == 0);

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, "testproc", service_state_t::STARTED,
service_state_t::STARTED);

// Issue signal (SIGHUP for example):
std::vector<char> cmd = { DINIT_CP_SIGNAL, SIGHUP };
char * h_cp = reinterpret_cast<char *>(&h);
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,7 @@ 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 04ed83c

Please sign in to comment.