From 074a9f8ccf8b4366898388cf976131d102ba6d1e Mon Sep 17 00:00:00 2001 From: Joao Paulo Ramos Date: Thu, 12 Dec 2024 18:35:11 -0300 Subject: [PATCH] tests: Add tests --- tests/helpers/mq_utils.py | 21 ++++++++++++++++ tests/test_custom_staleness.py | 9 +++++++ tests/test_notifications.py | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/tests/helpers/mq_utils.py b/tests/helpers/mq_utils.py index 65e101ab71..4785f4687c 100644 --- a/tests/helpers/mq_utils.py +++ b/tests/helpers/mq_utils.py @@ -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, diff --git a/tests/test_custom_staleness.py b/tests/test_custom_staleness.py index 197a975b76..f05f396cc5 100644 --- a/tests/test_custom_staleness.py +++ b/tests/test_custom_staleness.py @@ -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, diff --git a/tests/test_notifications.py b/tests/test_notifications.py index 0d65ad820d..31175b8a06 100644 --- a/tests/test_notifications.py +++ b/tests/test_notifications.py @@ -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"] @@ -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