Skip to content

Commit

Permalink
feat(libsinsp_e2e): more granular test settings
Browse files Browse the repository at this point in the history
Signed-off-by: Roberto Scolaro <[email protected]>
  • Loading branch information
therealbobo committed Dec 16, 2024
1 parent eee55f3 commit 82b4b68
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 28 deletions.
18 changes: 14 additions & 4 deletions test/libsinsp_e2e/container/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ TEST_F(sys_call_test, container_cgroups) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::sinsp_state_sc_set());
});
ASSERT_TRUE(done);
Expand Down Expand Up @@ -180,6 +181,7 @@ TEST_F(sys_call_test, container_clone_nspid) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::sinsp_state_sc_set());
});
ASSERT_TRUE(done);
Expand Down Expand Up @@ -233,6 +235,7 @@ TEST_F(sys_call_test, container_clone_nspid_ioctl) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::sinsp_state_sc_set());
});
ASSERT_TRUE(done);
Expand Down Expand Up @@ -312,6 +315,7 @@ static void run_container_docker_test(bool fork_after_container_start) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::sinsp_state_sc_set());
});
ASSERT_TRUE(done);
Expand Down Expand Up @@ -351,7 +355,7 @@ TEST_F(sys_call_test, container_docker_bad_socket) {
return;
}

before_capture_t setup = [&](sinsp* inspector) {
before_open_t setup = [&](sinsp* inspector) {
inspector->set_docker_socket_path("/invalid/path");
};

Expand Down Expand Up @@ -407,7 +411,9 @@ TEST_F(sys_call_test, container_docker_bad_socket) {
inspector->set_docker_socket_path("/var/run/docker.sock");
};

ASSERT_NO_FATAL_FAILURE({ event_capture::run(test, callback, filter, setup, cleanup); });
ASSERT_NO_FATAL_FAILURE({
event_capture::run(test, callback, filter, setup, event_capture::do_nothing, cleanup);
});
ASSERT_TRUE(done);
}

Expand All @@ -420,7 +426,7 @@ TEST_F(sys_call_test, container_libvirt) {
}

// Setup phase before capture has start, to avoid generating too many events
before_capture_t setup = [](sinsp* inspector) {
before_open_t setup = [](sinsp* inspector) {
FILE* f = fopen("/tmp/conf.xml", "w");
ASSERT_TRUE(f != NULL);
fprintf(f,
Expand Down Expand Up @@ -495,6 +501,7 @@ TEST_F(sys_call_test, container_libvirt) {
callback,
filter,
setup,
event_capture::do_nothing,
cleanup,
libsinsp::events::sinsp_state_sc_set());
});
Expand Down Expand Up @@ -643,6 +650,7 @@ static void healthcheck_helper(
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::sinsp_state_sc_set());
});

Expand Down Expand Up @@ -684,6 +692,7 @@ static void healthcheck_tracefile_helper(
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::sinsp_state_sc_set());
});

Expand Down Expand Up @@ -833,7 +842,7 @@ TEST_F(sys_call_test, docker_container_large_json) {

ASSERT_TRUE(dhelper.build_image() == 0);

before_capture_t before = [&](sinsp* inspector) {
before_open_t before = [&](sinsp* inspector) {
inspector->set_container_labels_max_len(60000);
};

Expand Down Expand Up @@ -892,6 +901,7 @@ TEST_F(sys_call_test, docker_container_large_json) {
callback,
filter,
before,
event_capture::do_nothing,
cleanup,
libsinsp::events::sinsp_state_sc_set());
});
Expand Down
10 changes: 7 additions & 3 deletions test/libsinsp_e2e/event_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ std::string event_capture::s_engine_path;
unsigned long event_capture::s_buffer_dim = DEFAULT_DRIVER_BUFFER_BYTES_DIM * 4;

event_capture::event_capture(captured_event_callback_t captured_event_callback,
before_capture_t before_open,
before_open_t before_open,
before_capture_t before_capture,
after_capture_t before_close,
event_filter_t filter,
uint32_t max_thread_table_size,
uint64_t thread_timeout_ns,
uint64_t inactive_thread_scan_time_ns) {
m_captured_event_callback = std::move(captured_event_callback);
m_before_capture = std::move(before_open);
m_before_open = std::move(before_open);
m_before_capture = std::move(before_capture);
m_after_capture = std::move(before_close);
m_filter = std::move(filter);

Expand Down Expand Up @@ -68,7 +70,7 @@ void event_capture::start(bool dump, libsinsp::events::set<ppm_sc_code>& sc_set)
}
}
}
m_before_capture(m_inspector.get());
m_before_open(m_inspector.get());
open_engine(event_capture::get_engine(), sc_set);

const ::testing::TestInfo* const test_info =
Expand All @@ -84,6 +86,8 @@ void event_capture::start(bool dump, libsinsp::events::set<ppm_sc_code>& sc_set)
0,
true);
}

m_before_capture(m_inspector.get());
m_inspector->start_capture();
}

Expand Down
14 changes: 11 additions & 3 deletions test/libsinsp_e2e/event_capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class callback_param {
sinsp* m_inspector;
};

// Right before inspector->open_*() gets called.
typedef std::function<void(sinsp* inspector)> before_open_t;
// Right before inspector->start_capture() gets called.
// Engine is already opened (thus scap handle is already alive).
typedef std::function<void(sinsp* inspector)> before_capture_t;
Expand All @@ -62,7 +64,8 @@ typedef std::function<void()> run_callback_async_t;
class event_capture {
public:
event_capture(captured_event_callback_t captured_event_callback,
before_capture_t before_open,
before_open_t before_open,
before_capture_t before_capture,
after_capture_t before_close,
event_filter_t filter,
uint32_t max_thread_table_size,
Expand All @@ -87,7 +90,8 @@ class event_capture {
static void run(const run_callback_t& run_function,
captured_event_callback_t captured_event_callback,
event_filter_t filter,
before_capture_t before_open = event_capture::do_nothing,
before_open_t before_open = event_capture::do_nothing,
before_capture_t before_capture = event_capture::do_nothing,
after_capture_t before_close = event_capture::do_nothing,
libsinsp::events::set<ppm_sc_code> sc_set = {},
uint32_t max_thread_table_size = 131072,
Expand All @@ -96,6 +100,7 @@ class event_capture {
bool dump = true) {
event_capture capturing(std::move(captured_event_callback),
std::move(before_open),
std::move(before_capture),
std::move(before_close),
std::move(filter),
max_thread_table_size,
Expand Down Expand Up @@ -125,7 +130,8 @@ class event_capture {
static void run(const run_callback_async_t& run_function,
captured_event_callback_t captured_event_callback,
event_filter_t filter,
before_capture_t before_open = event_capture::do_nothing,
before_open_t before_open = event_capture::do_nothing,
before_capture_t before_capture = event_capture::do_nothing,
after_capture_t before_close = event_capture::do_nothing,
libsinsp::events::set<ppm_sc_code> sc_set = {},
uint32_t max_thread_table_size = 131072,
Expand All @@ -134,6 +140,7 @@ class event_capture {
bool dump = true) {
event_capture capturing(std::move(captured_event_callback),
std::move(before_open),
std::move(before_capture),
std::move(before_close),
std::move(filter),
max_thread_table_size,
Expand Down Expand Up @@ -176,6 +183,7 @@ class event_capture {
std::unique_ptr<sinsp_cycledumper> m_dumper;
event_filter_t m_filter;
captured_event_callback_t m_captured_event_callback;
before_open_t m_before_open;
before_capture_t m_before_capture;
after_capture_t m_after_capture;
callback_param m_param{};
Expand Down
22 changes: 18 additions & 4 deletions test/libsinsp_e2e/forking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ TEST_F(sys_call_test, forking_process_expired) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
{},
131072,
5 * ONE_SECOND_IN_NS,
Expand Down Expand Up @@ -579,8 +580,14 @@ TEST_F(sys_call_test, forking_clone_nofs) {

after_capture_t cleanup = [&](sinsp* inspector) { free(stack); };

ASSERT_NO_FATAL_FAILURE(
{ event_capture::run(test, callback, filter, event_capture::do_nothing, cleanup); });
ASSERT_NO_FATAL_FAILURE({
event_capture::run(test,
callback,
filter,
event_capture::do_nothing,
event_capture::do_nothing,
cleanup);
});

EXPECT_EQ(callnum, 4);
}
Expand Down Expand Up @@ -687,8 +694,14 @@ TEST_F(sys_call_test, forking_clone_cwd) {

after_capture_t cleanup = [&](sinsp* inspector) { free(stack); };

ASSERT_NO_FATAL_FAILURE(
{ event_capture::run(test, callback, filter, event_capture::do_nothing, cleanup); });
ASSERT_NO_FATAL_FAILURE({
event_capture::run(test,
callback,
filter,
event_capture::do_nothing,
event_capture::do_nothing,
cleanup);
});

EXPECT_EQ(3, callnum);
}
Expand Down Expand Up @@ -759,6 +772,7 @@ TEST_F(sys_call_test, forking_main_thread_exit) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::all_sc_set());
});
EXPECT_EQ(3, callnum);
Expand Down
3 changes: 3 additions & 0 deletions test/libsinsp_e2e/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ TEST_F(sys_call_test, fs_readv) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::all_sc_set());
});

Expand Down Expand Up @@ -1248,6 +1249,7 @@ TEST_F(sys_call_test, large_read_write) {
event_capture::run(test,
callback,
filter,
event_capture::do_nothing,
setup,
event_capture::do_nothing,
libsinsp::events::all_sc_set(),
Expand Down Expand Up @@ -1367,6 +1369,7 @@ TEST_F(sys_call_test, large_readv_writev) {
event_capture::run(test,
callback,
filter,
event_capture::do_nothing,
setup,
event_capture::do_nothing,
libsinsp::events::all_sc_set(),
Expand Down
12 changes: 10 additions & 2 deletions test/libsinsp_e2e/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ TEST_F(sys_call_test, process_signalfd_kill) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::all_sc_set());
});

Expand Down Expand Up @@ -349,6 +350,7 @@ TEST_F(sys_call_test, process_inotify) {
filter,
event_capture::do_nothing,
event_capture::do_nothing,
event_capture::do_nothing,
libsinsp::events::all_sc_set());
});

Expand Down Expand Up @@ -569,8 +571,14 @@ TEST_F(sys_call_test, process_prlimit) {
syscall(SYS_prlimit64, getpid(), RLIMIT_NOFILE, &orirl, NULL);
};

ASSERT_NO_FATAL_FAILURE(
{ event_capture::run(test, callback, filter, event_capture::do_nothing, cleanup); });
ASSERT_NO_FATAL_FAILURE({
event_capture::run(test,
callback,
filter,
event_capture::do_nothing,
event_capture::do_nothing,
cleanup);
});

EXPECT_EQ(6, callnum);
}
Expand Down
11 changes: 9 additions & 2 deletions test/libsinsp_e2e/suppress_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ static void test_helper_quotactl(test_helper_args& hargs) {
event_capture::run(test,
callback,
filter,
event_capture::do_nothing,
before_open,
before_close,
{},
Expand Down Expand Up @@ -289,8 +290,14 @@ void suppress_types::run_test(std::vector<std::string> supp_syscalls) {
}
};

ASSERT_NO_FATAL_FAILURE(
{ event_capture::run(test, callback, m_tid_filter, before_open, before_close); });
ASSERT_NO_FATAL_FAILURE({
event_capture::run(test,
callback,
m_tid_filter,
event_capture::do_nothing,
before_open,
before_close);
});
EXPECT_EQ(m_expected_calls, callnum);
}

Expand Down
Loading

0 comments on commit 82b4b68

Please sign in to comment.