From 20f949df8f676412ea7040b21dc1e879875c02a7 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Mon, 6 Nov 2023 15:03:46 -0300 Subject: [PATCH] Simplify integrations cache on startupprobe (#3274) Revert logic to detect integrations server on startupprobe and simplify logic (and rework failing test having issues with urlpatterns cache and reset), since cache is the same for engine and integrations, and it is populated once. --- engine/engine/tests/test_views.py | 42 +++++++++++-------------------- engine/engine/views.py | 9 +------ 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/engine/engine/tests/test_views.py b/engine/engine/tests/test_views.py index e462c33a..d8e519b0 100644 --- a/engine/engine/tests/test_views.py +++ b/engine/engine/tests/test_views.py @@ -1,36 +1,12 @@ -import sys -from importlib import import_module, reload from unittest.mock import patch import pytest -from django.conf import settings -from django.urls import clear_url_caches from rest_framework import status from rest_framework.test import APIClient -def reload_urlconf(): - clear_url_caches() - if settings.ROOT_URLCONF in sys.modules: - reload(sys.modules[settings.ROOT_URLCONF]) - return import_module(settings.ROOT_URLCONF) - - -@pytest.mark.skip(reason="TODO: This test is currently failing in oncall-private, skipping to unblock release") -@pytest.mark.parametrize( - "detached_integrations,urlconf,is_cache_updated", - [ - (False, None, True), - (True, None, False), - (True, "engine.integrations_urls", True), - ], -) -def test_startupprobe_populates_integrations_cache(settings, detached_integrations, urlconf, is_cache_updated): - settings.DETACHED_INTEGRATIONS_SERVER = detached_integrations - if urlconf: - settings.ROOT_URLCONF = urlconf - reload_urlconf() - +@pytest.mark.urls("engine.integrations_urls") +def test_detached_integrations_startupprobe_populates_integrations_cache(): client = APIClient() with patch( @@ -39,4 +15,16 @@ def test_startupprobe_populates_integrations_cache(settings, detached_integratio response = client.get("/startupprobe/") assert response.status_code == status.HTTP_200_OK - assert mock_update_cache.called == is_cache_updated + mock_update_cache.assert_called_once + + +def test_startupprobe_populates_integrations_cache(): + client = APIClient() + + with patch( + "apps.integrations.mixins.AlertChannelDefiningMixin.update_alert_receive_channel_cache" + ) as mock_update_cache: + response = client.get("/startupprobe/") + + assert response.status_code == status.HTTP_200_OK + mock_update_cache.assert_called_once diff --git a/engine/engine/views.py b/engine/engine/views.py index 0022d02f..7f3e2f5d 100644 --- a/engine/engine/views.py +++ b/engine/engine/views.py @@ -1,4 +1,3 @@ -from django import urls from django.conf import settings from django.core.cache import cache from django.http import HttpResponse, JsonResponse @@ -44,13 +43,7 @@ class StartupProbeView(View): dangerously_bypass_middlewares = True def get(self, request): - # enable integrations cache if current engine instance is serving them - integrations_enabled = True - if settings.DETACHED_INTEGRATIONS_SERVER: - url_resolver = urls.get_resolver(urls.get_urlconf()) - integrations_enabled = url_resolver.namespace_dict.get("integrations") - - if integrations_enabled and cache.get(AlertChannelDefiningMixin.CACHE_KEY_DB_FALLBACK) is None: + if cache.get(AlertChannelDefiningMixin.CACHE_KEY_DB_FALLBACK) is None: AlertChannelDefiningMixin().update_alert_receive_channel_cache() cache.set("healthcheck", "healthcheck", 30) # Checking cache connectivity