From 8d325df6d6f0758578f3f73391a1cb5f43f8f482 Mon Sep 17 00:00:00 2001 From: vakarisz Date: Thu, 9 Dec 2021 18:17:35 +0200 Subject: [PATCH] Island, UT: fix a bug in "is monkey killed" endpoint The bug happened because by default there's no kill event so kill event time is None --- monkey/monkey_island/cc/services/infection_lifecycle.py | 4 +++- .../monkey_island/cc/services/test_infection_lifecycle.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/services/infection_lifecycle.py b/monkey/monkey_island/cc/services/infection_lifecycle.py index e766d2e14..510e3deb6 100644 --- a/monkey/monkey_island/cc/services/infection_lifecycle.py +++ b/monkey/monkey_island/cc/services/infection_lifecycle.py @@ -33,11 +33,13 @@ def _is_monkey_marked_dead(monkey: Monkey) -> bool: def _is_monkey_killed_manually(monkey: Monkey) -> bool: + kill_timestamp = AgentControls.objects.first().last_stop_all + if kill_timestamp is None: + return False if monkey.has_parent(): launch_timestamp = monkey.get_parent().launch_time else: launch_timestamp = monkey.launch_time - kill_timestamp = AgentControls.objects.first().last_stop_all return int(kill_timestamp) >= int(launch_timestamp) diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_infection_lifecycle.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_infection_lifecycle.py index 389bf3c9c..4d4c229c8 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_infection_lifecycle.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_infection_lifecycle.py @@ -30,6 +30,14 @@ def create_monkey(launch_time): return monkey +@pytest.mark.usefixtures("uses_database") +def test_should_agent_die_no_kill_event(): + monkey = create_monkey(launch_time=3) + kill_event = AgentControls() + kill_event.save() + assert not should_agent_die(monkey.guid) + + def create_kill_event(event_time): kill_event = AgentControls(last_stop_all=event_time) kill_event.save()