Skip to content

Commit

Permalink
fix(test/libsinsp_e2e): create new inspector for each test
Browse files Browse the repository at this point in the history
Signed-off-by: Roberto Scolaro <[email protected]>
  • Loading branch information
therealbobo committed Jul 13, 2024
1 parent 93a464c commit f2664f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
66 changes: 30 additions & 36 deletions test/libsinsp_e2e/event_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,28 @@ bool event_capture::inspector_ok = false;

concurrent_object_handle<sinsp> event_capture::get_inspector_handle()
{
return {get_inspector(), m_inspector_mutex};
return {m_inspector.get(), m_inspector_mutex};
}

void event_capture::init_inspector()
{
get_inspector()->m_thread_manager->set_max_thread_table_size(m_max_thread_table_size);
get_inspector()->m_thread_timeout_ns = m_thread_timeout_ns;
get_inspector()->set_auto_threads_purging_interval_s(m_inactive_thread_scan_time_ns);
get_inspector()->set_auto_threads_purging(false);
m_inspector = std::unique_ptr<sinsp>(new sinsp);
m_inspector->m_thread_manager->set_max_thread_table_size(m_max_thread_table_size);
m_inspector->m_thread_timeout_ns = m_thread_timeout_ns;
m_inspector->set_auto_threads_purging_interval_s(m_inactive_thread_scan_time_ns);
m_inspector->set_auto_threads_purging(false);

get_inspector()->set_get_procs_cpu_from_driver(true);
m_inspector->set_get_procs_cpu_from_driver(true);

ASSERT_FALSE(get_inspector()->is_capture());
ASSERT_FALSE(get_inspector()->is_live());
ASSERT_FALSE(get_inspector()->is_nodriver());
ASSERT_FALSE(m_inspector->is_capture());
ASSERT_FALSE(m_inspector->is_live());
ASSERT_FALSE(m_inspector->is_nodriver());

try
{
if (m_mode == SINSP_MODE_NODRIVER)
{
get_inspector()->open_nodriver();
m_inspector->open_nodriver();
}
else
{
Expand All @@ -64,16 +65,16 @@ void event_capture::init_inspector()
m_start_failed = true;
m_start_failure_message =
"couldn't open inspector (maybe driver hasn't been loaded yet?) err=" +
get_inspector()->getlasterr() + " exception=" + e.what();
m_inspector->getlasterr() + " exception=" + e.what();
{
m_capture_started = true;
m_condition_started.notify_one();
}
return;
}

get_inspector()->set_debug_mode(true);
get_inspector()->set_hostname_and_port_resolution_mode(false);
m_inspector->set_debug_mode(true);
m_inspector->set_hostname_and_port_resolution_mode(false);
}

void event_capture::capture()
Expand All @@ -84,30 +85,23 @@ void event_capture::capture()
{
std::scoped_lock init_lock(m_inspector_mutex, m_object_state_mutex);

if(!inspector_ok)
init_inspector();
if(m_start_failed)
{
init_inspector();
if(!m_start_failed)
{
inspector_ok = true;
}
else
{
std::cerr << m_start_failure_message << std::endl;
return;
}
std::cerr << m_start_failure_message << std::endl;
return;
}

m_param.m_inspector = get_inspector();
m_param.m_inspector = m_inspector.get();

m_before_open(get_inspector());
m_before_open(m_inspector.get());

get_inspector()->start_capture();
m_inspector->start_capture();
if (m_mode != SINSP_MODE_NODRIVER)
{
m_dump_filename = std::string(LIBSINSP_TEST_CAPTURES_PATH) + test_info->test_case_name() + "_" +
test_info->name() + ".scap";
dumper = std::make_unique<sinsp_cycledumper>(get_inspector(), m_dump_filename.c_str(),
dumper = std::make_unique<sinsp_cycledumper>(m_inspector.get(), m_dump_filename.c_str(),
0, 0, 0, 0, true);
}
} // End init synchronized section
Expand All @@ -120,7 +114,7 @@ void event_capture::capture()
{
{
std::scoped_lock inspector_next_lock(m_inspector_mutex);
next_result = get_inspector()->next(&event);
next_result = m_inspector->next(&event);
}
if (SCAP_SUCCESS == next_result)
{
Expand All @@ -145,7 +139,7 @@ void event_capture::capture()
{
{
std::scoped_lock inspector_next_lock(m_inspector_mutex);
next_result = get_inspector()->next(&event);
next_result = m_inspector->next(&event);
}
if (next_result == SCAP_TIMEOUT)
{
Expand Down Expand Up @@ -177,7 +171,7 @@ void event_capture::capture()
}
{
std::scoped_lock inspector_next_lock(m_inspector_mutex);
while (SCAP_SUCCESS == get_inspector()->next(&event))
while (SCAP_SUCCESS == m_inspector->next(&event))
{
// just consume the remaining events
}
Expand All @@ -186,9 +180,9 @@ void event_capture::capture()

{ // Begin teardown synchronized section
std::scoped_lock teardown_lock(m_inspector_mutex, m_object_state_mutex);
m_before_close(get_inspector());
m_before_close(m_inspector.get());

get_inspector()->stop_capture();
m_inspector->stop_capture();
if (m_mode != SINSP_MODE_NODRIVER)
{
dumper->close();
Expand Down Expand Up @@ -286,7 +280,7 @@ void event_capture::open_engine(const std::string& engine_string, libsinsp::even
#ifdef HAS_ENGINE_KMOD
else if(!engine_string.compare(KMOD_ENGINE))
{
get_inspector()->open_kmod(m_buffer_dim);
m_inspector->open_kmod(m_buffer_dim);
}
#endif
#ifdef HAS_ENGINE_BPF
Expand All @@ -297,13 +291,13 @@ void event_capture::open_engine(const std::string& engine_string, libsinsp::even
std::cerr << "You must specify the path to the bpf probe if you use the 'bpf' engine" << std::endl;
exit(EXIT_FAILURE);
}
get_inspector()->open_bpf(event_capture::get_engine_path().c_str(), m_buffer_dim);
m_inspector->open_bpf(event_capture::get_engine_path().c_str(), m_buffer_dim);
}
#endif
#ifdef HAS_ENGINE_MODERN_BPF
else if(!engine_string.compare(MODERN_BPF_ENGINE))
{
get_inspector()->open_modern_bpf(m_buffer_dim);
m_inspector->open_modern_bpf(m_buffer_dim);
}
#endif
else
Expand Down
8 changes: 2 additions & 6 deletions test/libsinsp_e2e/event_capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ class event_capture

static bool always_continue() { return true; }

sinsp* get_inspector()
{
static sinsp inspector = sinsp();
return &inspector;
}

static void run(run_callback_t run_function,
captured_event_callback_t captured_event_callback,
event_filter_t filter,
Expand Down Expand Up @@ -175,6 +169,7 @@ class event_capture

if (!capturing.m_start_failed.load())
{
//run_function(capturing.m_inspector);
run_function(capturing.get_inspector_handle());
capturing.stop_capture();
capturing.wait_for_capture_stop();
Expand Down Expand Up @@ -232,6 +227,7 @@ class event_capture
std::string m_start_failure_message;
std::string m_dump_filename;
callback_param m_param;
std::unique_ptr<sinsp> m_inspector;
static bool inspector_ok;
sinsp_mode_t m_mode;
uint64_t m_max_timeouts;
Expand Down

0 comments on commit f2664f8

Please sign in to comment.