From a904e2eefc931efce1458611755aba4561271954 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 1 Oct 2024 12:59:49 +0200 Subject: [PATCH] fix(test/libsinsp_e2e): make container_clone_nspid related tests more reliable. Signed-off-by: Federico Di Pierro --- test/libsinsp_e2e/container/container.cpp | 40 ++++++++++------------- test/libsinsp_e2e/forking.cpp | 8 ++--- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/test/libsinsp_e2e/container/container.cpp b/test/libsinsp_e2e/container/container.cpp index 02121f1e2c..7d22be2433 100644 --- a/test/libsinsp_e2e/container/container.cpp +++ b/test/libsinsp_e2e/container/container.cpp @@ -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; } @@ -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 // @@ -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 @@ -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); } diff --git a/test/libsinsp_e2e/forking.cpp b/test/libsinsp_e2e/forking.cpp index 29eada31d0..0035cd676a 100644 --- a/test/libsinsp_e2e/forking.cpp +++ b/test/libsinsp_e2e/forking.cpp @@ -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 @@ -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) { @@ -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("/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(); ++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("/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(); ++callnum; } } else if(param.m_evt->get_type() == PPME_PROCEXIT_1_E && param.m_evt->get_tid() == cpid) {