示例#1
0
def test_sync_app_logic(monkeypatch):
    """Tests syncing an app to Elasticsearch during a mapping migration."""
    bulk_mock = Mock()
    monkeypatch.setattr('datahub.search.bulk_sync.bulk', bulk_mock)
    search_app = create_mock_search_app(
        current_mapping_hash='mapping-hash',
        target_mapping_hash='mapping-hash',
        read_indices=('index1', 'index2'),
        write_index='index1',
        queryset=MockQuerySet([Mock(id=1), Mock(id=2)]),
    )
    sync_app(search_app, batch_size=1000)
    assert bulk_mock.call_args_list[0][1]['actions'] == [
        {
            '_index': 'index1',
            '_id': 1,
            '_type': 'test-type',
        },
        {
            '_index': 'index1',
            '_id': 2,
            '_type': 'test-type',
        },
    ]
    assert bulk_mock.call_count == 1
示例#2
0
    def test_resync_with_deletion_error(self, monkeypatch, mock_es_client):
        """
        Test that resync_after_migrate() raises an exception when there is an error deleting
        documents.
        """
        index_bulk_mock = Mock()
        delete_bulk_mock = Mock(return_value=(True, ({
            'delete': {
                'status': 500
            }
        }, )))
        monkeypatch.setattr('datahub.search.bulk_sync.bulk', index_bulk_mock)
        monkeypatch.setattr('datahub.search.deletion.bulk', delete_bulk_mock)

        get_aliases_for_index_mock = Mock(return_value=set())
        monkeypatch.setattr(
            'datahub.search.migrate_utils.get_aliases_for_index',
            get_aliases_for_index_mock,
        )

        read_indices = {'index1', 'index2'}
        write_index = 'index1'
        mock_app = create_mock_search_app(
            read_indices=read_indices,
            write_index=write_index,
            queryset=MockQuerySet([Mock(id=1), Mock(id=2)]),
        )

        with pytest.raises(DataHubException):
            resync_after_migrate(mock_app)
示例#3
0
def test_sync_app_with_overridden_batch_size(monkeypatch):
    """Tests syncing an app to OpenSearch with an overridden batch size."""
    bulk_mock = Mock()
    monkeypatch.setattr('datahub.search.bulk_sync.bulk', bulk_mock)

    search_app = create_mock_search_app(queryset=MockQuerySet(
        [Mock(id=1), Mock(id=2)]), )
    sync_app(search_app, batch_size=1)

    assert bulk_mock.call_count == 2
示例#4
0
def test_sync_app_with_default_batch_size(monkeypatch):
    """Tests syncing an app to Elasticsearch with the default batch size."""
    bulk_mock = Mock()
    monkeypatch.setattr('datahub.search.bulk_sync.bulk', bulk_mock)

    search_app = create_mock_search_app(queryset=MockQuerySet(
        [Mock(id=1), Mock(id=2)]), )
    sync_app(search_app)

    assert bulk_mock.call_count == 1
示例#5
0
    def test_normal_resync(self, monkeypatch, mock_es_client):
        """
        Test that resync_after_migrate() resyncs the app, updates the read alias and deletes the
        old index.
        """
        index_bulk_mock = Mock()
        delete_bulk_mock = Mock(return_value=(True, ({
            'delete': {
                'status': 404
            }
        }, )))
        monkeypatch.setattr('datahub.search.bulk_sync.bulk', index_bulk_mock)
        monkeypatch.setattr('datahub.search.deletion.bulk', delete_bulk_mock)

        get_aliases_for_index_mock = Mock(return_value=set())
        monkeypatch.setattr(
            'datahub.search.migrate_utils.get_aliases_for_index',
            get_aliases_for_index_mock,
        )

        mock_client = mock_es_client.return_value
        read_indices = {'index1', 'index2'}
        write_index = 'index1'
        mock_app = create_mock_search_app(
            read_indices=read_indices,
            write_index=write_index,
            queryset=MockQuerySet([Mock(id=1), Mock(id=2)]),
        )

        resync_after_migrate(mock_app)

        assert index_bulk_mock.call_count == 1
        assert index_bulk_mock.call_args_list[0][1]['actions'] == [
            {
                '_index': 'index1',
                '_id': 1,
                '_type': 'test-type',
            },
            {
                '_index': 'index1',
                '_id': 2,
                '_type': 'test-type',
            },
        ]
        assert delete_bulk_mock.call_count == 1
        assert list(delete_bulk_mock.call_args_list[0][1]['actions']) == [
            {
                '_index': 'index2',
                '_id': 1,
                '_op_type': 'delete',
                '_type': 'test-type',
            },
            {
                '_index': 'index2',
                '_id': 2,
                '_op_type': 'delete',
                '_type': 'test-type',
            },
        ]

        mock_client.indices.update_aliases.assert_called_once()

        actions = mock_client.indices.update_aliases.call_args_list[0][1][
            'body']['actions']
        actions[0]['remove']['indices'] = sorted(
            actions[0]['remove']['indices'])

        mock_client.indices.update_aliases.assert_called_once_with(body={
            'actions': [
                {
                    'remove': {
                        'alias': 'test-read-alias',
                        'indices': ANY,
                    },
                },
            ],
        }, )

        actions = mock_client.indices.update_aliases.call_args_list[0][1][
            'body']['actions']
        assert sorted(actions[0]['remove']['indices']) == ['index2']

        assert mock_client.indices.delete.call_count == 1
        mock_client.indices.delete.assert_any_call('index2')