Пример #1
0
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())
Пример #2
0
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())
Пример #3
0
    def set_up_index_and_aliases(cls):
        """Creates the index and aliases for this model if they don't already exist."""
        if not alias_exists(cls.get_write_alias()):
            index_name = cls.get_target_index_name()
            alias_names = (cls.get_write_alias(), cls.get_read_alias())
            create_index(index_name, cls._doc_type.mapping, alias_names=alias_names)
            return True

        # Should not normally happen
        if not alias_exists(cls.get_read_alias()):
            logger.warning(
                f'Missing read alias {cls.get_read_alias()} detected, recreating the alias...',
            )
            associate_index_with_alias(cls.get_read_alias(), cls.get_write_index())

        return False
def test_alias_exists(mock_es_client, expected):
    """Test alias_exists()."""
    index_name = 'test-index'

    client = mock_es_client.return_value
    client.indices.exists_alias.return_value = expected

    assert elasticsearch.alias_exists(index_name) == expected
    client.indices.exists_alias.assert_called_with(name=index_name)
Пример #5
0
    def set_up_index_and_aliases(cls, force_update_mapping=False):
        """
        Creates the index and aliases for this model if they don't already exist.

        If force_update_mapping is True and the write alias already exists, an attempt
        is made to update to update the existing mapping in place.
        """
        if not alias_exists(cls.get_write_alias()):
            index_name = cls.get_target_index_name()
            alias_names = (cls.get_write_alias(), cls.get_read_alias())
            create_index(index_name,
                         cls._doc_type.mapping,
                         alias_names=alias_names)
        elif force_update_mapping:
            cls.init(cls.get_write_alias())

        # Should not normally happen
        if not alias_exists(cls.get_read_alias()):
            logger.warning(
                f'Missing read alias {cls.get_read_alias()} detected, recreating '
                f'the alias...', )
            associate_index_with_alias(cls.get_read_alias(),
                                       cls.get_write_index())