From d6fb15e72ba0c61f1fa59b168bb4341149871279 Mon Sep 17 00:00:00 2001 From: Christophe Vandeplas Date: Thu, 20 Jun 2024 11:48:24 +0200 Subject: [PATCH] new: [ps] exclude_known_goods function --- parsers/ps.py | 21 +++++++++++++++++++++ tests/test_parsers_ps.py | 26 +++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/parsers/ps.py b/parsers/ps.py index 6c982a6..86dc8d8 100644 --- a/parsers/ps.py +++ b/parsers/ps.py @@ -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 """ diff --git a/tests/test_parsers_ps.py b/tests/test_parsers_ps.py index c941d68..0f44656 100644 --- a/tests/test_parsers_ps.py +++ b/tests/test_parsers_ps.py @@ -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 @@ -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) @@ -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) @@ -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()