def setup_es_test_data(es): try: es.cluster.health() except Exception as e: e.args = tuple([ u"%s (it looks like ES is not running, try starting it or " u"don't run ES tests: make test_no_es)" % e.args[0] ] + list(e.args[1:])) raise aliases_and_indexes = set( list(settings.ES_INDEXES.values()) + list(es.indices.get_alias().keys())) for key in aliases_and_indexes: if key.startswith('test_'): es.indices.delete(key, ignore=[404]) # Figure out the name of the indices we're going to create from the # suffixes generated at import time. Like the aliases later, the name # has been prefixed by pytest, we need to add a suffix that is unique # to this test run. actual_indices = { key: get_es_index_name(key) for key in settings.ES_INDEXES.keys() } # Create new addons and stats indexes with the timestamped name. # This is crucial to set up the correct mappings before we start # indexing things in tests. addons_indexers.create_new_index(index_name=actual_indices['default']) stats_search.create_new_index(index_name=actual_indices['stats']) # Alias it to the name the code is going to use (which is suffixed by # pytest to avoid clashing with the real thing). actions = [{ 'add': { 'index': actual_indices['default'], 'alias': settings.ES_INDEXES['default'] } }, { 'add': { 'index': actual_indices['stats'], 'alias': settings.ES_INDEXES['stats'] } }] es.indices.update_aliases({'actions': actions})
class ESTestCase(TestCase): """Base class for tests that require elasticsearch.""" # ES is slow to set up so this uses class setup/teardown. That happens # outside Django transactions so be careful to clean up afterwards. mock_es = False exempt_from_fixture_bundling = True # ES doesn't support bundling (yet?) @classmethod def setUpClass(cls): cls.es = amo_search.get_es(timeout=settings.ES_TIMEOUT) super(ESTestCase, cls).setUpClass() try: cls.es.cluster.health() except Exception, e: e.args = tuple( [u"%s (it looks like ES is not running, try starting it or " u"don't run ES tests: make test_no_es)" % e.args[0]] + list(e.args[1:])) raise cls._SEARCH_ANALYZER_MAP = amo.SEARCH_ANALYZER_MAP amo.SEARCH_ANALYZER_MAP = { 'english': ['en-us'], 'spanish': ['es'], } for index in set(settings.ES_INDEXES.values()): cls.es.indices.delete(index, ignore=[404]) addons_search.create_new_index() stats_search.create_new_index()
class ESTestCase(TestCase): """Base class for tests that require elasticsearch.""" # ES is slow to set up so this uses class setup/teardown. That happens # outside Django transactions so be careful to clean up afterwards. mock_es = False # We need ES indexes aliases to match prod behaviour, but also we need the # names need to stay consistent during the whole test run, so we generate # them at import time. Note that this works because pytest overrides # ES_INDEXES before the test run even begins - if we were using # override_settings() on ES_INDEXES we'd be in trouble. index_names = {key: timestamp_index(value) for key, value in settings.ES_INDEXES.items()} @classmethod def setUpClass(cls): cls.es = amo_search.get_es(timeout=settings.ES_TIMEOUT) cls._SEARCH_ANALYZER_MAP = amo.SEARCH_ANALYZER_MAP amo.SEARCH_ANALYZER_MAP = { 'english': ['en-us'], 'spanish': ['es'], } super(ESTestCase, cls).setUpClass() @classmethod def setUpTestData(cls): try: cls.es.cluster.health() except Exception, e: e.args = tuple( [u"%s (it looks like ES is not running, try starting it or " u"don't run ES tests: make test_no_es)" % e.args[0]] + list(e.args[1:])) raise aliases_and_indexes = set(settings.ES_INDEXES.values() + cls.es.indices.get_aliases().keys()) for key in aliases_and_indexes: if key.startswith('test_amo'): cls.es.indices.delete(key, ignore=[404]) # Create new search and stats indexes with the timestamped name. # This is crucial to set up the correct mappings before we start # indexing things in tests. search_indexers.create_new_index( index_name=cls.index_names['default']) stats_search.create_new_index(index_name=cls.index_names['stats']) # Alias it to the name the code is going to use (which is suffixed by # pytest to avoid clashing with the real thing). actions = [ {'add': {'index': cls.index_names['default'], 'alias': settings.ES_INDEXES['default']}}, {'add': {'index': cls.index_names['stats'], 'alias': settings.ES_INDEXES['stats']}} ] cls.es.indices.update_aliases({'actions': actions}) super(ESTestCase, cls).setUpTestData()
def setup_es_test_data(es): try: es.cluster.health() except Exception as e: e.args = tuple( [u"%s (it looks like ES is not running, try starting it or " u"don't run ES tests: make test_no_es)" % e.args[0]] + list(e.args[1:])) raise aliases_and_indexes = set(settings.ES_INDEXES.values() + es.indices.get_alias().keys()) for key in aliases_and_indexes: if key.startswith('test_'): es.indices.delete(key, ignore=[404]) # Figure out the name of the indices we're going to create from the # suffixes generated at import time. Like the aliases later, the name # has been prefixed by pytest, we need to add a suffix that is unique # to this test run. actual_indices = {key: get_es_index_name(key) for key in settings.ES_INDEXES.keys()} # Create new search and stats indexes with the timestamped name. # This is crucial to set up the correct mappings before we start # indexing things in tests. search_indexers.create_new_index(index_name=actual_indices['default']) stats_search.create_new_index(index_name=actual_indices['stats']) # Alias it to the name the code is going to use (which is suffixed by # pytest to avoid clashing with the real thing). actions = [ {'add': {'index': actual_indices['default'], 'alias': settings.ES_INDEXES['default']}}, {'add': {'index': actual_indices['stats'], 'alias': settings.ES_INDEXES['stats']}} ] es.indices.update_aliases({'actions': actions})
class ESTestCase(TestCase): # We need ES indexes aliases to match prod behaviour, but also we need the # names need to stay consistent during the whole test run, so we generate # them at import time. Note that this works because pytest overrides # ES_INDEXES before the test run even begins - if we were using # override_settings() on ES_INDEXES we'd be in trouble. index_suffixes = { key: timestamp_index('') for key in settings.ES_INDEXES.keys() } @classmethod def get_index_name(cls, key): """Return the name of the actual index used in tests for a given key taken from settings.ES_INDEXES. Can be used to check whether aliases have been set properly - ES_INDEXES will give the aliases, and this method will give the indices the aliases point to.""" value = settings.ES_INDEXES[key] return '%s%s' % (value, cls.index_suffixes[key]) def setUp(self): stop_es_mocks() @classmethod def setUpClass(cls): stop_es_mocks() cls.es = amo_search.get_es(timeout=settings.ES_TIMEOUT) cls._SEARCH_ANALYZER_MAP = amo.SEARCH_ANALYZER_MAP amo.SEARCH_ANALYZER_MAP = { 'english': ['en-us'], 'spanish': ['es'], } super(ESTestCase, cls).setUpClass() @classmethod def setUpTestData(cls): stop_es_mocks() try: cls.es.cluster.health() except Exception, e: e.args = tuple([ u"%s (it looks like ES is not running, try starting it or " u"don't run ES tests: make test_no_es)" % e.args[0] ] + list(e.args[1:])) raise aliases_and_indexes = set(settings.ES_INDEXES.values() + cls.es.indices.get_aliases().keys()) for key in aliases_and_indexes: if key.startswith('test_amo'): cls.es.indices.delete(key, ignore=[404]) # Figure out the name of the indices we're going to create from the # suffixes generated at import time. Like the aliases later, the name # has been prefixed by pytest, we need to add a suffix that is unique # to this test run. actual_indices = { key: cls.get_index_name(key) for key in settings.ES_INDEXES.keys() } # Create new search and stats indexes with the timestamped name. # This is crucial to set up the correct mappings before we start # indexing things in tests. search_indexers.create_new_index(index_name=actual_indices['default']) stats_search.create_new_index(index_name=actual_indices['stats']) # Alias it to the name the code is going to use (which is suffixed by # pytest to avoid clashing with the real thing). actions = [{ 'add': { 'index': actual_indices['default'], 'alias': settings.ES_INDEXES['default'] } }, { 'add': { 'index': actual_indices['stats'], 'alias': settings.ES_INDEXES['stats'] } }] cls.es.indices.update_aliases({'actions': actions}) super(ESTestCase, cls).setUpTestData()
for key in aliases_and_indexes: if key.startswith('test_amo'): es.indices.delete(key, ignore=[404]) # Figure out the name of the indices we're going to create from the # suffixes generated at import time. Like the aliases later, the name # has been prefixed by pytest, we need to add a suffix that is unique # to this test run. actual_indices = {key: get_es_index_name(key) for key in settings.ES_INDEXES.keys()} # Create new search and stats indexes with the timestamped name. # This is crucial to set up the correct mappings before we start # indexing things in tests. search_indexers.create_new_index(index_name=actual_indices['default']) stats_search.create_new_index(index_name=actual_indices['stats']) # Alias it to the name the code is going to use (which is suffixed by # pytest to avoid clashing with the real thing). actions = [ {'add': {'index': actual_indices['default'], 'alias': settings.ES_INDEXES['default']}}, {'add': {'index': actual_indices['stats'], 'alias': settings.ES_INDEXES['stats']}} ] es.indices.update_aliases({'actions': actions}) def formset(*args, **kw): """