Skip to content

Commit

Permalink
make es_metrics test helper usable with unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Oct 1, 2024
1 parent 5bf0022 commit 4732fbc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
36 changes: 2 additions & 34 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import contextlib
from unittest import mock
import logging
import os
import re

from django.core.management import call_command
from django.db import transaction
from elasticsearch import exceptions as es_exceptions
from elasticsearch_dsl.connections import connections
from elasticsearch_metrics.registry import registry as es_metrics_registry
from faker import Factory
import pytest
import responses
Expand All @@ -17,6 +13,7 @@
from api_tests.share import _utils as shtrove_test_utils
from framework.celery_tasks import app as celery_app
from osf.external.spam import tasks as spam_tasks
from osf_tests.metrics.utils_for_tests import es_metrics_temps
from website import settings as website_settings


Expand Down Expand Up @@ -143,37 +140,8 @@ def _es_metrics_marker(request):
marker = request.node.get_closest_marker('es_metrics')
if marker:
es6_client = request.getfixturevalue('es6_client')
_temp_prefix = 'temp_metrics_'
_temp_wildcard = f'{_temp_prefix}*'

def _teardown_es_temps():
es6_client.indices.delete(index=_temp_wildcard)
try:
es6_client.indices.delete_template(_temp_wildcard)
except es_exceptions.NotFoundError:
pass

@contextlib.contextmanager
def _mock_metric_names():
with contextlib.ExitStack() as _exit:
for _metric_class in es_metrics_registry.get_metrics():
_exit.enter_context(mock.patch.object(
_metric_class,
'_template_name', # also used to construct index names
f'{_temp_prefix}{_metric_class._template_name}',
))
_exit.enter_context(mock.patch.object(
_metric_class,
'_template', # a wildcard string for indexes and templates
f'{_temp_prefix}{_metric_class._template}',
))
yield

_teardown_es_temps()
with _mock_metric_names():
call_command('sync_metrics')
with es_metrics_temps(es6_client):
yield
_teardown_es_temps()
else:
yield

Expand Down
47 changes: 47 additions & 0 deletions osf_tests/metrics/utils_for_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import contextlib
from unittest import mock

from django.core.management import call_command
from elasticsearch import exceptions as es_exceptions
from elasticsearch_dsl.connections import connections
from elasticsearch_metrics.registry import registry as es_metrics_registry

from website import settings as website_settings


@contextlib.contextmanager
def es_metrics_temps(es6_client=None):
if es6_client is None:
connections.create_connection(hosts=[website_settings.ELASTIC6_URI])
es6_client = connections.get_connection()
_temp_prefix = 'temp_metrics_'
_temp_wildcard = f'{_temp_prefix}*'

def _teardown_es_temps():
es6_client.indices.delete(index=_temp_wildcard)
try:
es6_client.indices.delete_template(_temp_wildcard)
except es_exceptions.NotFoundError:
pass

@contextlib.contextmanager
def _mock_metric_names():
with contextlib.ExitStack() as _exit:
for _metric_class in es_metrics_registry.get_metrics():
_exit.enter_context(mock.patch.object(
_metric_class,
'_template_name', # also used to construct index names
f'{_temp_prefix}{_metric_class._template_name}',
))
_exit.enter_context(mock.patch.object(
_metric_class,
'_template', # a wildcard string for indexes and templates
f'{_temp_prefix}{_metric_class._template}',
))
yield

_teardown_es_temps()
with _mock_metric_names():
call_command('sync_metrics')
yield
_teardown_es_temps()

0 comments on commit 4732fbc

Please sign in to comment.