Skip to content

Commit

Permalink
new: [ps] exclude_known_goods function
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed Jun 20, 2024
1 parent 12f47fa commit d6fb15e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
21 changes: 21 additions & 0 deletions parsers/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ def parse_ps(filename):
return []


def exclude_known_goods(processes: dict, known_good: dict) -> list[dict]:
"""
Exclude known good processes from the given list of processes.
Args:
processes (dict): The output from parse_ps() to check.
known_good (dict): The output of parse_ps() from a known good.
Returns:
dict: The updated list of processes with known good processes excluded.
"""

known_good_cmd = [x['COMMAND'] for x in known_good]

for proc in processes:
if proc['COMMAND'] in known_good_cmd:
processes.remove(proc)

return processes


"""
Export the process structure to a json file
"""
Expand Down
26 changes: 21 additions & 5 deletions tests/test_parsers_ps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from parsers.ps import parse_path, get_log_files, parse_ps
from parsers import ps
from tests import SysdiagnoseTestCase
import unittest
import tempfile
Expand All @@ -8,10 +8,10 @@ class TestParsersPs(SysdiagnoseTestCase):

def test_parse_ps(self):
for log_root_path in self.log_root_paths:
files = get_log_files(log_root_path)
files = ps.get_log_files(log_root_path)
self.assertTrue(len(files) > 0)
print(f'Parsing {files}')
result = parse_path(log_root_path)
result = ps.parse_path(log_root_path)
if result: # not all logs contain data
for item in result:
self.assertTrue('COMMAND' in item)
Expand All @@ -29,7 +29,7 @@ def test_parse_ps_lower_than_v16(self):
tmp_inputfile = tempfile.NamedTemporaryFile()
with open(tmp_inputfile.name, 'w') as f:
f.write('\n'.join(input))
result = parse_ps(tmp_inputfile.name)
result = ps.parse_ps(tmp_inputfile.name)
tmp_inputfile.close()
self.assertEqual(result, expected_result)

Expand All @@ -44,10 +44,26 @@ def test_parse_ps_newer_than_v16(self):
tmp_inputfile = tempfile.NamedTemporaryFile()
with open(tmp_inputfile.name, 'w') as f:
f.write('\n'.join(input))
result = parse_ps(tmp_inputfile.name)
result = ps.parse_ps(tmp_inputfile.name)
tmp_inputfile.close()
self.assertEqual(result, expected_result)

def test_ps_exclude_known_goods(self):
processes = [
{'COMMAND': 'good', 'PID': 1},
{'COMMAND': 'bad', 'PID': 2},
{'COMMAND': 'unknown', 'PID': 3}
]
known_good = [
{'COMMAND': 'good', 'PID': 1}
]
expected_result = [
{'COMMAND': 'bad', 'PID': 2},
{'COMMAND': 'unknown', 'PID': 3}
]
result = ps.exclude_known_goods(processes, known_good)
self.assertEqual(result, expected_result)


if __name__ == '__main__':
unittest.main()

0 comments on commit d6fb15e

Please sign in to comment.