Skip to content

Commit

Permalink
Adds tests for filtering orphaned procs
Browse files Browse the repository at this point in the history
  • Loading branch information
djperrefort committed Dec 7, 2023
1 parent cbd9087 commit 3036a53
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/utils/test_filter_orphaned_processes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Tests for the `filter_orphaned_processes` function."""

import unittest

import pandas as pd

from shinigami.utils import filter_orphaned_processes, INIT_PROCESS_ID


class FilterOrphanedProcesses(unittest.TestCase):
"""Test the identification of orphaned processes from a DataFrame of process data"""

def test_processes_are_filtered(self) -> None:
"""Test orphaned processes are returned from a DataFrame of process data"""

input_df = pd.DataFrame({
'PID': [1, 2, 3, 4, 5],
'PPID': [0, INIT_PROCESS_ID, INIT_PROCESS_ID, 2000, 2000],
'PGID': [0, 0, 0, 0, 0],
'UID': [0, 1, 1, 2, 2],
'CMD': ['init', 'process_1', 'process_2', 'process_3', 'process_4']})

# Manually define the data subset (including the index) expected in the function return
expected_output_df = pd.DataFrame({
'PID': [2, 3],
'PPID': [INIT_PROCESS_ID, INIT_PROCESS_ID],
'PGID': [0, 0],
'UID': [1, 1],
'CMD': ['process_1', 'process_2']},
index=[1, 2])

pd.testing.assert_frame_equal(
filter_orphaned_processes(input_df),
expected_output_df)

def test_no_orphaned_processes(self) -> None:
"""Test the returned DataFrame is empty when no orphaned processes are present"""

input_df = pd.DataFrame({
'PID': [1, 2, 3, 4, 5],
'PPID': [0, 2, 3, 4, 4],
'PGID': [0, 0, 0, 0, 0],
'UID': [0, 1, 1, 2, 2],
'CMD': ['init', 'process_1', 'process_2', 'process_3', 'process_4']})

self.assertTrue(filter_orphaned_processes(input_df).empty)

def test_all_orphaned_processes(self) -> None:
"""Test the returned DataFrame matches the input DataFrame when all processes are orphaned"""

input_df = pd.DataFrame({
'PID': [2, 3, 4, 5],
'PPID': [INIT_PROCESS_ID, INIT_PROCESS_ID, INIT_PROCESS_ID, INIT_PROCESS_ID],
'PGID': [0, 0, 0, 0],
'UID': [1, 1, 2, 2],
'CMD': ['process_1', 'process_2', 'process_3', 'process_4']})

returned_df = filter_orphaned_processes(input_df)
pd.testing.assert_frame_equal(returned_df, input_df)

# Make sure the return value is an equivalent copy and not just the original DataFrame
self.assertIsNot(returned_df, input_df)

0 comments on commit 3036a53

Please sign in to comment.