Skip to content

Commit

Permalink
fix: ensure on_delete behavior for process parents in ActivationReque…
Browse files Browse the repository at this point in the history
…stQueue (#937)

Signed-off-by: Alex <aizquier@redhat.com>
  • Loading branch information
Alex-Izquierdo authored Jun 12, 2024
1 parent 2933114 commit cbb6a6f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/aap_eda/core/models/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@

from aap_eda.core.enums import (
ActivationStatus,
ProcessParentType,
RestartPolicy,
RulebookProcessLogLevel,
)
from aap_eda.core.utils import get_default_log_level
from aap_eda.services.activation.engine.common import ContainerableMixin

from .base import BaseOrgModel, UniqueNamedModel
from .mixins import StatusHandlerModelMixin
from .mixins import OnDeleteProcessParentMixin, StatusHandlerModelMixin
from .user import AwxToken, User

__all__ = ("Activation",)


class Activation(
StatusHandlerModelMixin, ContainerableMixin, BaseOrgModel, UniqueNamedModel
StatusHandlerModelMixin,
ContainerableMixin,
OnDeleteProcessParentMixin,
BaseOrgModel,
UniqueNamedModel,
):
class Meta:
db_table = "core_activation"
Expand Down Expand Up @@ -127,3 +132,6 @@ class Meta:
blank=True,
help_text="Name of the kubernetes service",
)

def get_parent_type(self) -> str:
return ProcessParentType.ACTIVATION
11 changes: 9 additions & 2 deletions src/aap_eda/core/models/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@

from aap_eda.core.enums import (
ActivationStatus,
ProcessParentType,
RestartPolicy,
RulebookProcessLogLevel,
)
from aap_eda.core.utils import get_default_log_level
from aap_eda.services.activation.engine.common import ContainerableMixin

from .base import UniqueNamedModel
from .mixins import StatusHandlerModelMixin
from .mixins import OnDeleteProcessParentMixin, StatusHandlerModelMixin


class EventStream(
StatusHandlerModelMixin, ContainerableMixin, UniqueNamedModel
StatusHandlerModelMixin,
ContainerableMixin,
OnDeleteProcessParentMixin,
UniqueNamedModel,
):
"""Model representing an event stream."""

Expand Down Expand Up @@ -107,3 +111,6 @@ def __str__(self) -> str:
def _get_skip_audit_events(self) -> bool:
"""Event stream skips audit events."""
return True

def get_parent_type(self) -> str:
return ProcessParentType.EVENT_STREAM
16 changes: 16 additions & 0 deletions src/aap_eda/core/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import typing as tp

from django.apps import apps
from django.db import models

from aap_eda.core.enums import ACTIVATION_STATUS_MESSAGE_MAP, ActivationStatus
Expand Down Expand Up @@ -98,3 +99,18 @@ def _is_valid_status(self):
ActivationStatus(self.status)
except ValueError as error:
raise UnknownStatusError(error) from None


class OnDeleteProcessParentMixin:
"""Mixin to delete related objects when the parent is deleted."""

def _delete_request_queue(self):
model = apps.get_model("core", "ActivationRequestQueue")
model.objects.filter(
process_parent_id=self.id,
process_parent_type=self.get_parent_type(),
).delete()

def delete(self, *args, **kwargs):
self._delete_request_queue()
super().delete(*args, **kwargs)
29 changes: 28 additions & 1 deletion tests/integration/core/test_process_parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pytest_lazyfixture import lazy_fixture

from aap_eda.core import models
from aap_eda.core.enums import ActivationStatus
from aap_eda.core.enums import ActivationRequest, ActivationStatus


@pytest.mark.parametrize(
Expand Down Expand Up @@ -58,3 +58,30 @@ def test_latest_instance_field(instance):
first_instance.status = ActivationStatus.COMPLETED
first_instance.save(update_fields=["status"])
assert instance.latest_instance == second_instance


@pytest.mark.parametrize(
"instance",
[
pytest.param(
lazy_fixture("new_activation"),
id="activation",
),
pytest.param(
lazy_fixture("new_event_stream"),
id="event_stream",
),
],
)
@pytest.mark.django_db
def test_on_delete_process_parent_mixin(instance):
"""Test on_delete_process_parent_mixin."""
queue_item = models.ActivationRequestQueue.objects.create(
process_parent_type=instance.get_parent_type(),
process_parent_id=instance.id,
request=ActivationRequest.AUTO_START,
)
instance.delete()
assert not models.ActivationRequestQueue.objects.filter(
id=queue_item.id
).exists()

0 comments on commit cbb6a6f

Please sign in to comment.