Skip to content

Commit

Permalink
Adds tests for parser error handling (#94)
Browse files Browse the repository at this point in the history
* Adds tests for parser error handling

* Docstring typo
  • Loading branch information
djperrefort authored Nov 2, 2023
1 parent 9e997e5 commit 98cb0ce
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 1 addition & 3 deletions shinigami/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ def error(self, message: str) -> None:

if len(sys.argv) == 1:
self.print_help()
super().exit(1)

else:
super().error(message)
raise SystemExit(message)


class Parser(BaseParser):
Expand Down
9 changes: 5 additions & 4 deletions shinigami/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ async def terminate_errant_processes(

# Identify orphaned processes and filter them by the UID whitelist
orphaned = process_df[process_df.PPID == INIT_PROCESS_ID]
terminate = orphaned[orphaned['UID'].apply(id_in_whitelist, whitelist=uid_whitelist)]
whitelist_index = orphaned['UID'].apply(id_in_whitelist, whitelist=uid_whitelist)
to_terminate = orphaned[whitelist_index]

for _, row in terminate.iterrows():
for _, row in to_terminate.iterrows():
logging.info(f'[{node}] Marking for termination {dict(row)}')

if terminate.empty:
if to_terminate.empty:
logging.info(f'[{node}] no processes found')

elif not debug:
proc_id_str = ','.join(terminate.PGID.unique().astype(str))
proc_id_str = ','.join(to_terminate.PGID.unique().astype(str))
logging.info(f"[{node}] Sending termination signal for process groups {proc_id_str}")
await conn.run(f"pkill --signal 9 --pgroup {proc_id_str}", check=True)
20 changes: 16 additions & 4 deletions tests/cli/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@

from unittest import TestCase

from shinigami.cli import Parser
from shinigami.cli import Parser, BaseParser


class ScanParser(TestCase):
class BaseParsing(TestCase):
"""Test custom parsing login encapsulated by the `BaseParser` class"""

def test_error_handling(self) -> None:
"""Test error messages are raised as `SystemExit` instances"""

parser = BaseParser()
error_message = "This is an error message"
with self.assertRaises(SystemExit, msg=error_message):
parser.error(error_message)


class ScanSubParser(TestCase):
"""Test the behavior of the ``scan`` subparser"""

def test_debug_option(self) -> None:
"""Test the ``debug`` argument"""
"""Test parsing of the ``debug`` argument"""

parser = Parser()

Expand Down Expand Up @@ -84,7 +96,7 @@ def test_uid_whitelist_arg(self) -> None:
self.assertSequenceEqual(mixed_out, parser.parse_args(mixed_command).uid_whitelist)


class TerminateParser(TestCase):
class TerminateSubParser(TestCase):
"""Test the behavior of the ``terminate`` subparser"""

def test_debug_option(self) -> None:
Expand Down

0 comments on commit 98cb0ce

Please sign in to comment.