def test_find_missing_types(self): with mock.patch(( 'searchlight.elasticsearch.plugins.utils.searchlight' '.elasticsearch.get_api')) as mock_api: mock_engine = mock.Mock() mock_api.return_value = mock_engine # Test no mapping exists mock_engine.indices.get_mapping.return_value = {} results = plugin_utils.find_missing_types( { 'index': ['OS::Nova::Server', 'OS::Neutron::Subnet'] } ) mock_engine.indices.get_mapping.assert_called() self.assertEqual( (set([]), set(['OS::Nova::Server', 'OS::Neutron::Subnet'])), results ) mock_engine.reset_mock() # Test no index exists mock_engine.indices.get_mapping.side_effect = \ es_exc.NotFoundError() results = plugin_utils.find_missing_types( { 'searchlight-search': ['OS::Nova::Server'] } ) mock_engine.indices.get_mapping.assert_called() self.assertEqual( (set(['searchlight-search']), set([])), results)
def test_exception_safe_to_retry(self): x = ElasticsearchBackend(app=self.app) assert not x.exception_safe_to_retry(Exception("failed")) assert not x.exception_safe_to_retry(BaseException("failed")) assert x.exception_safe_to_retry(exceptions.ConflictError(409, "concurrent update", {})) assert x.exception_safe_to_retry(exceptions.ConnectionError(503, "service unavailable", {})) assert x.exception_safe_to_retry(exceptions.TransportError(429, "too many requests", {})) assert not x.exception_safe_to_retry(exceptions.NotFoundError(404, "not found", {}))
def test_get_task_not_found(self): x = ElasticsearchBackend(app=self.app) x._server = Mock() x._server.get.side_effect = [ exceptions.NotFoundError(404, '{"_index":"celery","_type":"_doc","_id":"toto","found":false}', {'_index': 'celery', '_type': '_doc', '_id': 'toto', 'found': False}) ] res = x.get(sentinel.task_id) assert res is None
def test_backend_index_conflicting_document_removed( self, base_datetime_mock, es_datetime_mock): expected_dt = datetime.datetime(2020, 6, 1, 18, 43, 24, 123456, None) es_datetime_mock.utcnow.return_value = expected_dt expected_done_dt = datetime.datetime(2020, 6, 1, 18, 45, 34, 654321, None) base_datetime_mock.utcnow.return_value = expected_done_dt self.app.conf.result_backend_always_retry, prev = True, self.app.conf.result_backend_always_retry try: x = ElasticsearchBackend(app=self.app) task_id = str(sentinel.task_id) encoded_task_id = bytes_to_str(x.get_key_for_task(task_id)) result = str(sentinel.result) sleep_mock = Mock() x._sleep = sleep_mock x._server = Mock() x._server.index.side_effect = [ exceptions.ConflictError(409, "concurrent update", {}), { 'result': 'created' } ] x._server.get.side_effect = [ { 'found': True, '_source': { 'result': """{"status":"RETRY","result":{"exc_type":"Exception","exc_message":["failed"],"exc_module":"builtins"}}""" }, '_seq_no': 2, '_primary_term': 1, }, exceptions.NotFoundError( 404, '{"_index":"celery","_type":"_doc","_id":"toto","found":false}', { '_index': 'celery', '_type': '_doc', '_id': 'toto', 'found': False }), ] result_meta = x._get_result_meta(result, states.SUCCESS, None, None) result_meta['task_id'] = bytes_to_str(task_id) expected_result = x.encode(result_meta) x.store_result(task_id, result, states.SUCCESS) x._server.index.assert_has_calls([ call(id=encoded_task_id, index=x.index, doc_type=x.doc_type, body={ 'result': expected_result, '@timestamp': expected_dt.isoformat()[:-3] + 'Z' }, params={'op_type': 'create'}), call(id=encoded_task_id, index=x.index, doc_type=x.doc_type, body={ 'result': expected_result, '@timestamp': expected_dt.isoformat()[:-3] + 'Z' }, params={'op_type': 'create'}), ]) x._server.update.assert_not_called() sleep_mock.assert_not_called() finally: self.app.conf.result_backend_always_retry = prev