Skip to content

Commit

Permalink
tests: add tests for preventing enqueuing of request for non-existent…
Browse files Browse the repository at this point in the history
… target
  • Loading branch information
jshimkus-rh committed Jun 11, 2024
1 parent 3eb08bc commit 1554490
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/integration/services/test_restart_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/tasks/test_activation_request_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
[
Expand Down

0 comments on commit 1554490

Please sign in to comment.