Skip to content

Commit

Permalink
fix(test/libsinsp_e2e): make container_clone_nspid related tests more…
Browse files Browse the repository at this point in the history
… reliable.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed Oct 7, 2024
1 parent 03f1d30 commit 2b1e402
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
40 changes: 18 additions & 22 deletions test/libsinsp_e2e/container/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ TEST_F(sys_call_test, container_cgroups) {
}

static int clone_callback(void* arg) {
// Here we need 2 sleeps instead of once because, for some reason,
// we miss the first one. This problem is *probably* related to the
// fact that before we created a brand new inspector for each test but
// not we keep the same and start/stop the capture.
sleep(1);
sleep(1);
return 0;
}
Expand Down Expand Up @@ -195,21 +190,6 @@ TEST_F(sys_call_test, container_clone_nspid_ioctl) {
int flags = CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | SIGCHLD | CLONE_NEWPID;
bool done = false;

const int STACK_SIZE = 65536;
char* stack;
char* stack_top;

stack = (char*)malloc(STACK_SIZE);
if(stack == NULL) {
FAIL();
}
stack_top = stack + STACK_SIZE;

ctid = clone(clone_callback, stack_top, flags, NULL);
if(ctid == -1) {
FAIL();
}

//
// FILTER
//
Expand All @@ -218,7 +198,24 @@ TEST_F(sys_call_test, container_clone_nspid_ioctl) {
//
// TEST CODE
//
run_callback_t test = [&](sinsp* inspector) { waitpid(ctid, NULL, 0); };
run_callback_t test = [&](sinsp* inspector) {
const int STACK_SIZE = 65536;
char* stack;
char* stack_top;

stack = (char*)malloc(STACK_SIZE);
if(stack == NULL) {
FAIL();
}
stack_top = stack + STACK_SIZE;

ctid = clone(clone_callback, stack_top, flags, NULL);
if(ctid == -1) {
FAIL();
}
waitpid(ctid, NULL, 0);
free(stack);
};

//
// OUTPUT VALDATION
Expand All @@ -238,7 +235,6 @@ TEST_F(sys_call_test, container_clone_nspid_ioctl) {
event_capture::do_nothing,
libsinsp::events::sinsp_state_sc_set());
});
free(stack);
ASSERT_TRUE(done);
}

Expand Down
8 changes: 4 additions & 4 deletions test/libsinsp_e2e/forking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ TEST_F(sys_call_test, forking_while_scap_stopped) {
// Wait for 5 seconds to make sure the process will still
// exist when the sinsp will do the lookup to /proc
//
usleep(5000000);
sleep(5);
close(fd);
_exit(xstatus); // child exits with specific return code
} else // fork() returns new pid to the parent process
Expand Down Expand Up @@ -696,7 +696,7 @@ TEST_F(sys_call_test, forking_clone_cwd) {
TEST_F(sys_call_test, forking_main_thread_exit) {
int evtnum = 0;
int callnum = 0;
int fd;
int64_t fd;
pid_t cpid; // parent tid

event_filter_t filter = [&](sinsp_evt* evt) {
Expand Down Expand Up @@ -734,13 +734,13 @@ TEST_F(sys_call_test, forking_main_thread_exit) {
if(param.m_evt->get_type() == PPME_SYSCALL_OPEN_X) {
if(param.m_evt->get_param_value_str("name") == "/etc/passwd") {
EXPECT_EQ("<f>/etc/passwd", param.m_evt->get_param_value_str("fd"));
fd = *(int64_t*)param.m_evt->get_param(0)->m_val;
fd = param.m_evt->get_param(0)->as<int64_t>();
++callnum;
}
} else if(param.m_evt->get_type() == PPME_SYSCALL_OPENAT_2_X) {
if(param.m_evt->get_param_value_str("name") == "/etc/passwd") {
EXPECT_EQ("<f>/etc/passwd", param.m_evt->get_param_value_str("fd"));
memcpy(&fd, (int64_t*)param.m_evt->get_param(0)->m_val, sizeof(fd));
fd = param.m_evt->get_param(0)->as<int64_t>();
++callnum;
}
} else if(param.m_evt->get_type() == PPME_PROCEXIT_1_E && param.m_evt->get_tid() == cpid) {
Expand Down

0 comments on commit 2b1e402

Please sign in to comment.