def test_no_index(self):
     """
     Test that an error is raised if we don't have an index
     """
     with self.assertRaises(ReindexException) as ex:
         get_conn()
     assert "Unable to find index" in str(ex.exception)
 def test_no_index(self):
     """
     Test that an error is raised if we don't have an index
     """
     with self.assertRaises(ReindexException) as ex:
         get_conn()
     assert "Unable to find index" in str(ex.exception)
 def test_no_index(self):
     """
     Test that an error is raised if we don't have an index
     """
     with self.assertRaises(ReindexException) as ex:
         get_conn()
     assert str(ex.exception) == "Unable to find index {}".format(
         settings.ELASTICSEARCH_INDEX)
    def test_no_mapping(self):
        """
        Test that error is raised if we don't have a mapping
        """
        conn = get_conn(verify=False)
        conn.indices.create(settings.ELASTICSEARCH_INDEX)

        with self.assertRaises(ReindexException) as ex:
            get_conn()
        assert str(
            ex.exception) == "Mapping {} not found".format(USER_DOC_TYPE)
    def test_no_index_not_default(self):
        """
        Test that an error is raised if we don't have an index
        """
        # Reset default index so it does not cause an error
        recreate_index()
        other_index = "other"
        delete_indices()

        with self.assertRaises(ReindexException) as ex:
            get_conn(verify_indices=[other_index])
        assert str(ex.exception) == "Unable to find index {}".format(other_index)
    def test_no_index_not_default(self):
        """
        Test that an error is raised if we don't have an index
        """
        # Reset default index so it does not cause an error
        recreate_index()
        other_index = "other"
        delete_indices()

        with self.assertRaises(ReindexException) as ex:
            get_conn(verify_indices=[other_index])
        assert str(
            ex.exception) == "Unable to find index {}".format(other_index)
Exemple #7
0
def execute_search(search_obj):
    """
    Executes a search against ES after checking the connection

    Args:
        search_obj (Search): elasticsearch_dsl Search object

    Returns:
        elasticsearch_dsl.result.Response: ES response
    """
    # make sure there is a live connection
    get_conn()
    return search_obj.execute()
    def test_update_during_recreate_index(self):
        """
        If an indexing action happens during a recreate_index it should update all active indices
        """
        conn = get_conn(verify=False)
        recreate_index()

        temp_aliases = {}
        index_types = [
            PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE
        ]
        for index_type in index_types:
            # create temporary index
            temp_index = make_backing_index_name()
            temp_alias = make_alias_name(index_type=index_type,
                                         is_reindexing=True)
            clear_and_create_index(temp_index, index_type=index_type)
            conn.indices.put_alias(index=temp_index, name=temp_alias)
            temp_aliases[index_type] = temp_alias

        with patch('search.signals.transaction.on_commit',
                   side_effect=lambda callback: callback()):
            program_enrollment = ProgramEnrollmentFactory.create()

        for index_type in index_types:
            assert_search(es.search(index_type), [program_enrollment],
                          index_type=index_type)

            # Temp alias should get updated
            temp_alias = temp_aliases[index_type]
            refresh_index(temp_alias)
            temp_hits = conn.search(index=temp_alias)['hits']
            assert_search(temp_hits, [program_enrollment],
                          index_type=index_type)
    def test_keep_alias(self, existing_temp_alias, index_type):
        """
        Test that recreate_index will point an existing alias at a new backing index
        """
        conn = get_conn(verify=False)
        default_alias = make_alias_name(index_type, is_reindexing=False)
        temp_alias = make_alias_name(index_type, is_reindexing=True)
        assert conn.indices.exists_alias(name=temp_alias) is False

        if existing_temp_alias:
            # Create a temp alias to assert that it doesn't change anything
            backing_index = "{}_backing".format(temp_alias)
            conn.indices.create(backing_index)
            conn.indices.put_alias(name=temp_alias, index=backing_index)

        old_backing_indexes = list(
            conn.indices.get_alias(name=default_alias).keys())
        assert len(old_backing_indexes) == 1
        recreate_index()
        new_backing_indexes = list(
            conn.indices.get_alias(name=default_alias).keys())
        assert len(new_backing_indexes) == 1
        # Backing index should have changed
        assert old_backing_indexes != new_backing_indexes
        # Temp index should have been deleted
        assert conn.indices.exists_alias(name=temp_alias) is False
    def test_update_during_recreate_index(self):
        """
        If an indexing action happens during a recreate_index it should update all active indices
        """
        conn = get_conn(verify=False)
        recreate_index()

        temp_aliases = {}
        index_types = [PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE]
        for index_type in index_types:
            # create temporary index
            temp_index = make_backing_index_name()
            temp_alias = make_alias_name(index_type=index_type, is_reindexing=True)
            clear_and_create_index(temp_index, index_type=index_type)
            conn.indices.put_alias(index=temp_index, name=temp_alias)
            temp_aliases[index_type] = temp_alias

        with patch('search.signals.transaction.on_commit', side_effect=lambda callback: callback()):
            program_enrollment = ProgramEnrollmentFactory.create()

        for index_type in index_types:
            assert_search(es.search(index_type), [program_enrollment], index_type=index_type)

            # Temp alias should get updated
            temp_alias = temp_aliases[index_type]
            refresh_index(temp_alias)
            temp_hits = conn.search(index=temp_alias)['hits']
            assert_search(temp_hits, [program_enrollment], index_type=index_type)
 def setUp(self):
     """
     Start without any index
     """
     super(RecreateIndexTests, self).setUp()
     conn = get_conn(verify=False)
     index_name = settings.ELASTICSEARCH_INDEX
     if conn.indices.exists(index_name):
         conn.indices.delete(index_name)
    def setUp(self):
        """
        Start without any index
        """
        super(GetConnTests, self).setUp()

        conn = get_conn(verify=False)
        index_name = settings.ELASTICSEARCH_INDEX
        conn.indices.delete(index_name)

        # Clear globals
        from search import indexing_api
        indexing_api._CONN = None  # pylint: disable=protected-access
        indexing_api._CONN_VERIFIED = False  # pylint: disable=protected-access
    def setUp(self):
        """
        Start without any index
        """
        super().setUp()

        conn = get_conn(verify=False)
        for index in conn.indices.get_alias().keys():
            if index.startswith(settings.ELASTICSEARCH_INDEX):
                conn.indices.delete(index)

        # Clear globals
        from search import indexing_api
        indexing_api._CONN = None  # pylint: disable=protected-access
        indexing_api._CONN_VERIFIED = False  # pylint: disable=protected-access
    def setUp(self):
        """
        Start without any index
        """
        super().setUp()

        conn = get_conn(verify=False)
        for index in conn.indices.get_alias().keys():
            if index.startswith(settings.ELASTICSEARCH_INDEX):
                conn.indices.delete(index)

        # Clear globals
        from search import indexing_api
        indexing_api._CONN = None  # pylint: disable=protected-access
        indexing_api._CONN_VERIFIED = False  # pylint: disable=protected-access
    def test_get_aliases(self, is_reindex, index_type, expected_indices):
        """
        We should choose the correct alias and doc type given the circumstances
        """
        conn = get_conn(verify=False)

        alias = make_alias_name(index_type, is_reindexing=False)

        backing_index = make_backing_index_name()
        # Skip the mapping because it's invalid for 2.x schema, and we don't need it here
        clear_and_create_index(backing_index, index_type=index_type, skip_mapping=True)
        conn.indices.put_alias(index=backing_index, name=alias)

        if is_reindex:
            conn.indices.put_alias(index=backing_index, name=make_alias_name(index_type, is_reindexing=True))

        aliases = get_aliases(index_type)
        assert aliases == list(expected_indices)

        assert get_default_alias(index_type) == aliases[0]
    def test_get_aliases(self, is_reindex, index_type, expected_indices):
        """
        We should choose the correct alias and doc type given the circumstances
        """
        conn = get_conn(verify=False)

        alias = make_alias_name(index_type, is_reindexing=False)

        backing_index = make_backing_index_name()
        # Skip the mapping because it's invalid for 2.x schema, and we don't need it here
        clear_and_create_index(backing_index,
                               index_type=index_type,
                               skip_mapping=True)
        conn.indices.put_alias(index=backing_index, name=alias)

        if is_reindex:
            conn.indices.put_alias(index=backing_index,
                                   name=make_alias_name(index_type,
                                                        is_reindexing=True))

        aliases = get_aliases(index_type)
        assert aliases == list(expected_indices)

        assert get_default_alias(index_type) == aliases[0]
    def test_keep_alias(self, existing_temp_alias, index_type):
        """
        Test that recreate_index will point an existing alias at a new backing index
        """
        conn = get_conn(verify=False)
        default_alias = make_alias_name(index_type, is_reindexing=False)
        temp_alias = make_alias_name(index_type, is_reindexing=True)
        assert conn.indices.exists_alias(name=temp_alias) is False

        if existing_temp_alias:
            # Create a temp alias to assert that it doesn't change anything
            backing_index = "{}_backing".format(temp_alias)
            conn.indices.create(backing_index)
            conn.indices.put_alias(name=temp_alias, index=backing_index)

        old_backing_indexes = list(conn.indices.get_alias(name=default_alias).keys())
        assert len(old_backing_indexes) == 1
        recreate_index()
        new_backing_indexes = list(conn.indices.get_alias(name=default_alias).keys())
        assert len(new_backing_indexes) == 1
        # Backing index should have changed
        assert old_backing_indexes != new_backing_indexes
        # Temp index should have been deleted
        assert conn.indices.exists_alias(name=temp_alias) is False
 def __init__(self):
     self.conn = get_conn(verify=False)
    def tearDown(self):
        super().tearDown()

        conn = get_conn(verify=False)
        for index in conn.indices.get_mapping().keys():
            conn.indices.delete(index=index)
 def __init__(self):
     self.conn = get_conn(verify=False)
    def tearDown(self):
        super().tearDown()

        conn = get_conn(verify=False)
        for index in conn.indices.get_mapping().keys():
            conn.indices.delete(index=index)