def _es_session(_es_client): """ Session-scoped fixture that creates Elasticsearch indexes that persist for the entire test session. """ # Create models in the test index for search_app in get_search_apps(): # Clean up in case of any aborted test runs index_name = search_app.es_model.get_target_index_name() read_alias = search_app.es_model.get_read_alias() write_alias = search_app.es_model.get_write_alias() if index_exists(index_name): delete_index(index_name) if alias_exists(read_alias): delete_alias(read_alias) if alias_exists(write_alias): delete_alias(write_alias) # Create indices and aliases alias_names = (read_alias, write_alias) create_index(index_name, search_app.es_model._doc_type.mapping, alias_names=alias_names) yield _es_client for search_app in get_search_apps(): delete_index(search_app.es_model.get_target_index_name())
def _setup_es_indexes(_es_client): """Sets up ES and makes the client available.""" # Create models in the test index for search_app in get_search_apps(): # Clean up in case of any aborted test runs index_name = search_app.es_model.get_target_index_name() read_alias = search_app.es_model.get_read_alias() write_alias = search_app.es_model.get_write_alias() if index_exists(index_name): delete_index(index_name) if alias_exists(read_alias): delete_alias(read_alias) if alias_exists(write_alias): delete_alias(write_alias) # Create indices and aliases alias_names = (read_alias, write_alias) create_index(index_name, search_app.es_model._doc_type.mapping, alias_names=alias_names) yield _es_client for search_app in get_search_apps(): delete_index(search_app.es_model.get_target_index_name())
def _clean_up_aliases_and_indices(search_app): es_model = search_app.es_model read_alias = es_model.get_read_alias() read_indices, write_index = es_model.get_read_and_write_indices() if write_index not in read_indices: raise DataHubException( 'Write index not in read alias, aborting mapping migration...') indices_to_remove = read_indices - {write_index} if indices_to_remove: with start_alias_transaction() as alias_transaction: alias_transaction.dissociate_indices_from_alias( read_alias, indices_to_remove) else: logger.warning(f'No indices to remove for the {read_alias} alias') for index in indices_to_remove: if not get_aliases_for_index(index): delete_index(index)
def test_delete_index(mock_es_client): """Test delete_index().""" index = 'test-index' client = mock_es_client.return_value elasticsearch.delete_index(index) client.indices.delete.assert_called_once_with(index)