diff --git a/conftest.py b/conftest.py index 6f870093ed4..bcb756be612 100644 --- a/conftest.py +++ b/conftest.py @@ -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 @@ -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 @@ -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 diff --git a/osf_tests/metrics/utils_for_tests.py b/osf_tests/metrics/utils_for_tests.py new file mode 100644 index 00000000000..12ba1aa1d61 --- /dev/null +++ b/osf_tests/metrics/utils_for_tests.py @@ -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()