diff --git a/tests/integration/services/test_restart_helper.py b/tests/integration/services/test_restart_helper.py index a3f6eb2ef..104028e02 100644 --- a/tests/integration/services/test_restart_helper.py +++ b/tests/integration/services/test_restart_helper.py @@ -12,15 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. +import time from unittest import mock import pytest +from django_rq import get_scheduler from aap_eda.core import models from aap_eda.core.enums import ActivationRequest, ProcessParentType from aap_eda.services.activation.restart_helper import ( _queue_auto_start, auto_start_job_id, + system_cancel_restart_activation, system_restart_activation, ) @@ -40,6 +43,33 @@ def activation(): ) +@pytest.mark.django_db +def test_system_cancel_restart_activation(activation): + job_id = auto_start_job_id(ProcessParentType.ACTIVATION, activation.id) + + scheduler = get_scheduler(name="default") + + delay = 5 + system_restart_activation( + ProcessParentType.ACTIVATION, activation.id, delay + ) + + # Sleep for half the delay and verify the job is still in the scheduler. + time.sleep(delay / 2) + assert job_id in scheduler + + # Cancel the job and verify that it's not in the scheduler. + system_cancel_restart_activation( + ProcessParentType.ACTIVATION, activation.id + ) + assert job_id not in scheduler + + # Sleep for half the delay and verify the job didn't somehow run; i.e., we + # really canceled it. + time.sleep(delay / 2) + assert models.ActivationRequestQueue.objects.count() == 0 + + @pytest.mark.django_db @mock.patch("aap_eda.services.activation.restart_helper.enqueue_delay") def test_system_restart_activation(enqueue_mock, activation): diff --git a/tests/integration/tasks/test_activation_request_queue.py b/tests/integration/tasks/test_activation_request_queue.py index 8e6b7a681..3d3bbd56d 100644 --- a/tests/integration/tasks/test_activation_request_queue.py +++ b/tests/integration/tasks/test_activation_request_queue.py @@ -13,10 +13,12 @@ # limitations under the License. import pytest +from django.db.utils import IntegrityError import aap_eda.tasks.activation_request_queue as queue from aap_eda.core import models from aap_eda.core.enums import ActivationRequest, ProcessParentType +from aap_eda.tasks.exceptions import UnknownProcessParentType @pytest.fixture() @@ -76,6 +78,28 @@ def test_queue(activations): ) +@pytest.mark.django_db +def test_queue_push_exceptions(): + parent_type = "unknown" + parent_id = 1 + + with pytest.raises(UnknownProcessParentType) as info: + queue.push(parent_type, parent_id, ActivationRequest.AUTO_START) + assert str(info.value) == f"Unknown parent type {parent_type}" + + with pytest.raises(IntegrityError) as info: + queue.push( + ProcessParentType.ACTIVATION, + parent_id, + ActivationRequest.AUTO_START, + ) + assert ( + str(info.value) + == f"{ProcessParentType.ACTIVATION} {parent_id} no longer exists, " + f"{ActivationRequest.AUTO_START} request will not be processed" + ) + + @pytest.mark.parametrize( "requests", [