Skip to content

Commit

Permalink
tests: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpramos123 committed Dec 17, 2024
1 parent a83a41d commit 074a9f8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/helpers/mq_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ def assert_delete_notification_is_valid(notification_event_producer, host):
assert host.canonical_facts.get("insights_id") == event["events"][0]["payload"]["insights_id"]


def assert_stale_notification_is_valid(notification_event_producer, host):
event = json.loads(notification_event_producer.event)

assert isinstance(event, dict)

expected_keys = {
"timestamp",
"event_type",
"org_id",
"application",
"bundle",
"context",
"events",
}
assert set(event.keys()) == expected_keys

assert event["event_type"] == "system-became-stale"

assert host.canonical_facts.get("insights_id") == event["events"][0]["payload"]["insights_id"]


def assert_patch_event_is_valid(
host,
event_producer,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_custom_staleness.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
"immutable_time_to_delete": 15552000,
}

CUSTOM_STALENESS_HOST_BECAME_STALE = {
"conventional_time_to_stale": 1,
"conventional_time_to_stale_warning": 604800,
"conventional_time_to_delete": 1209600,
"immutable_time_to_stale": 1,
"immutable_time_to_stale_warning": 10368000,
"immutable_time_to_delete": 15552000,
}


def test_delete_only_immutable_hosts(
flask_app,
Expand Down
44 changes: 44 additions & 0 deletions tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import json
from datetime import datetime
from datetime import timedelta
from unittest import mock
from unittest.mock import patch

import pytest

from app.exceptions import ValidationException
from app.logging import threadctx
from app.models import db
from stale_host_notification import run as run_stale_host_notification
from tests.helpers.db_utils import minimal_db_host
from tests.helpers.mq_utils import assert_stale_notification_is_valid
from tests.helpers.mq_utils import assert_system_registered_notification_is_valid
from tests.helpers.test_utils import SYSTEM_IDENTITY
from tests.helpers.test_utils import generate_uuid
from tests.helpers.test_utils import minimal_host
from tests.test_custom_staleness import CUSTOM_STALENESS_HOST_BECAME_STALE

OWNER_ID = SYSTEM_IDENTITY["system"]["cn"]

Expand Down Expand Up @@ -62,6 +72,40 @@ def test_add_host_fail(mq_create_or_update_host, notification_event_producer_moc


# System Became Stale
def test_host_became_stale(
notification_event_producer_mock,
db_create_staleness_culling,
flask_app,
event_producer_mock,
db_create_host,
db_get_host,
inventory_config,
):
db_create_staleness_culling(**CUSTOM_STALENESS_HOST_BECAME_STALE)

with patch("app.models.datetime") as models_datetime, patch("app.culling.datetime") as culling_datetime:
models_datetime.now.return_value = datetime.now() - timedelta(minutes=5)
culling_datetime.now.return_value = datetime.now()

host = minimal_db_host(reporter="some reporter")
created_host = db_create_host(host=host)
assert db_get_host(created_host.id)

threadctx.request_id = None
run_stale_host_notification(
inventory_config,
mock.Mock(),
db.session,
event_producer=event_producer_mock,
notification_event_producer=notification_event_producer_mock,
shutdown_handler=mock.Mock(**{"shut_down.return_value": False}),
application=flask_app,
)

assert_stale_notification_is_valid(
notification_event_producer=notification_event_producer_mock, host=created_host
)


# System Deleted

Expand Down

0 comments on commit 074a9f8

Please sign in to comment.