| 1 |
from datetime import datetime, timezone |
| 2 |
from unittest.mock import Mock, call, patch, sentinel |
| 3 |
|
| 4 |
import pytest |
| 5 |
from billiard.einfo import ExceptionInfo |
| 6 |
from kombu.utils.encoding import bytes_to_str |
| 7 |
|
| 8 |
from celery import states |
| 9 |
|
| 10 |
try: |
| 11 |
from elasticsearch import exceptions |
| 12 |
except ImportError: |
| 13 |
exceptions = None |
| 14 |
|
| 15 |
try: |
| 16 |
from elastic_transport import ApiResponseMeta, HttpHeaders, NodeConfig |
| 17 |
except ImportError: |
| 18 |
ApiResponseMeta = None |
| 19 |
HttpHeaders = None |
| 20 |
NodeConfig = None |
| 21 |
|
| 22 |
from celery.app import backends |
| 23 |
from celery.backends import elasticsearch as module |
| 24 |
from celery.backends.elasticsearch import ElasticsearchBackend |
| 25 |
from celery.exceptions import ImproperlyConfigured |
| 26 |
|
| 27 |
_RESULT_RETRY = ( |
| 28 |
'{"status":"RETRY","result":' |
| 29 |
'{"exc_type":"Exception","exc_message":["failed"],"exc_module":"builtins"}}' |
| 30 |
) |
| 31 |
_RESULT_FAILURE = ( |
| 32 |
'{"status":"FAILURE","result":' |
| 33 |
'{"exc_type":"Exception","exc_message":["failed"],"exc_module":"builtins"}}' |
| 34 |
) |
| 35 |
|
| 36 |
pytest.importorskip('elasticsearch') |
| 37 |
|
| 38 |
|
| 39 |
class test_ElasticsearchBackend: |
| 40 |
|
| 41 |
def setup_method(self): |
| 42 |
self.backend = ElasticsearchBackend(app=self.app) |
| 43 |
|
| 44 |
def test_init_no_elasticsearch(self): |
| 45 |
prev, module.elasticsearch = module.elasticsearch, None |
| 46 |
try: |
| 47 |
with pytest.raises(ImproperlyConfigured): |
| 48 |
ElasticsearchBackend(app=self.app) |
| 49 |
finally: |
| 50 |
module.elasticsearch = prev |
| 51 |
|
| 52 |
def test_get(self): |
| 53 |
x = ElasticsearchBackend(app=self.app) |
| 54 |
x._server = Mock() |
| 55 |
x._server.get = Mock() |
| 56 |
# expected result |
| 57 |
r = {'found': True, '_source': {'result': sentinel.result}} |
| 58 |
x._server.get.return_value = r |
| 59 |
dict_result = x.get(sentinel.task_id) |
| 60 |
|
| 61 |
assert dict_result == sentinel.result |
| 62 |
x._server.get.assert_called_once_with( |
| 63 |
id=sentinel.task_id, |
| 64 |
index=x.index, |
| 65 |
) |
| 66 |
|
| 67 |
def test_get_with_doctype(self): |
| 68 |
x = ElasticsearchBackend(app=self.app) |
| 69 |
x._server = Mock() |
| 70 |
x._server.get = Mock() |
| 71 |
# expected result |
| 72 |
x.doc_type = "_doc" |
| 73 |
r = {'found': True, '_source': {'result': sentinel.result}} |
| 74 |
x._server.get.return_value = r |
| 75 |
dict_result = x.get(sentinel.task_id) |
| 76 |
|
| 77 |
assert dict_result == sentinel.result |
| 78 |
x._server.get.assert_called_once_with( |
| 79 |
id=sentinel.task_id, |
| 80 |
index=x.index, |
| 81 |
doc_type=x.doc_type, |
| 82 |
) |
| 83 |
|
| 84 |
def test_get_none(self): |
| 85 |
x = ElasticsearchBackend(app=self.app) |
| 86 |
x._server = Mock() |
| 87 |
x._server.get = Mock() |
| 88 |
x._server.get.return_value = sentinel.result |
| 89 |
none_result = x.get(sentinel.task_id) |
| 90 |
|
| 91 |
assert none_result is None |
| 92 |
x._server.get.assert_called_once_with( |
| 93 |
id=sentinel.task_id, |
| 94 |
index=x.index, |
| 95 |
) |
| 96 |
|
| 97 |
def test_get_task_not_found(self): |
| 98 |
x = ElasticsearchBackend(app=self.app) |
| 99 |
x._server = Mock() |
| 100 |
x._server.get.side_effect = [ |
| 101 |
exceptions.NotFoundError('{"_index":"celery","_type":"_doc","_id":"toto","found":false}', |
| 102 |
ApiResponseMeta(404, "HTTP/1.1", HttpHeaders(), 0, |
| 103 |
NodeConfig("https", "localhost", 9200)), |
| 104 |
{'_index': 'celery', '_type': '_doc', '_id': 'toto', 'found': False}) |
| 105 |
] |
| 106 |
|
| 107 |
res = x.get(sentinel.task_id) |
| 108 |
assert res is None |
| 109 |
|
| 110 |
def test_get_task_not_found_without_throw(self): |
| 111 |
x = ElasticsearchBackend(app=self.app) |
| 112 |
x._server = Mock() |
| 113 |
# this should not happen as if not found elasticsearch python library |
| 114 |
# will raise elasticsearch.exceptions.NotFoundError. |
| 115 |
x._server.get.return_value = {'_index': 'celery', '_type': '_doc', '_id': 'toto', 'found': False} |
| 116 |
|
| 117 |
res = x.get(sentinel.task_id) |
| 118 |
assert res is None |
| 119 |
|
| 120 |
def test_delete(self): |
| 121 |
x = ElasticsearchBackend(app=self.app) |
| 122 |
x._server = Mock() |
| 123 |
x._server.delete = Mock() |
| 124 |
x._server.delete.return_value = sentinel.result |
| 125 |
|
| 126 |
assert x.delete(sentinel.task_id) is None |
| 127 |
x._server.delete.assert_called_once_with( |
| 128 |
id=sentinel.task_id, |
| 129 |
index=x.index, |
| 130 |
) |
| 131 |
|
| 132 |
def test_delete_with_doctype(self): |
| 133 |
x = ElasticsearchBackend(app=self.app) |
| 134 |
x._server = Mock() |
| 135 |
x._server.delete = Mock() |
| 136 |
x._server.delete.return_value = sentinel.result |
| 137 |
x.doc_type = "_doc" |
| 138 |
assert x.delete(sentinel.task_id) is None |
| 139 |
x._server.delete.assert_called_once_with( |
| 140 |
id=sentinel.task_id, |
| 141 |
index=x.index, |
| 142 |
doc_type=x.doc_type, |
| 143 |
) |
| 144 |
|
| 145 |
def test_backend_by_url(self, url='elasticsearch://localhost:9200/index'): |
| 146 |
backend, url_ = backends.by_url(url, self.app.loader) |
| 147 |
|
| 148 |
assert backend is ElasticsearchBackend |
| 149 |
assert url_ == url |
| 150 |
|
| 151 |
@patch('celery.backends.elasticsearch.datetime') |
| 152 |
def test_index_conflict(self, datetime_mock): |
| 153 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 154 |
datetime_mock.now.return_value = expected_dt |
| 155 |
|
| 156 |
x = ElasticsearchBackend(app=self.app) |
| 157 |
x._server = Mock() |
| 158 |
x._server.index.side_effect = [ |
| 159 |
exceptions.ConflictError("concurrent update", |
| 160 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 161 |
NodeConfig("https", "localhost", 9200)), None) |
| 162 |
] |
| 163 |
|
| 164 |
x._server.get.return_value = { |
| 165 |
'found': True, |
| 166 |
'_source': {"result": _RESULT_RETRY}, |
| 167 |
'_seq_no': 2, |
| 168 |
'_primary_term': 1, |
| 169 |
} |
| 170 |
|
| 171 |
x._server.update.return_value = { |
| 172 |
'result': 'updated' |
| 173 |
} |
| 174 |
|
| 175 |
x._set_with_state(sentinel.task_id, sentinel.result, sentinel.state) |
| 176 |
|
| 177 |
assert x._server.get.call_count == 1 |
| 178 |
x._server.index.assert_called_once_with( |
| 179 |
id=sentinel.task_id, |
| 180 |
index=x.index, |
| 181 |
body={'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}, |
| 182 |
params={'op_type': 'create'}, |
| 183 |
) |
| 184 |
x._server.update.assert_called_once_with( |
| 185 |
id=sentinel.task_id, |
| 186 |
index=x.index, |
| 187 |
body={'doc': {'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}}, |
| 188 |
params={'if_seq_no': 2, 'if_primary_term': 1} |
| 189 |
) |
| 190 |
|
| 191 |
@patch('celery.backends.elasticsearch.datetime') |
| 192 |
def test_index_conflict_with_doctype(self, datetime_mock): |
| 193 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 194 |
datetime_mock.now.return_value = expected_dt |
| 195 |
|
| 196 |
x = ElasticsearchBackend(app=self.app) |
| 197 |
x._server = Mock() |
| 198 |
x._server.index.side_effect = [ |
| 199 |
exceptions.ConflictError("concurrent update", |
| 200 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 201 |
NodeConfig("https", "localhost", 9200)), None) |
| 202 |
] |
| 203 |
x.doc_type = "_doc" |
| 204 |
x._server.get.return_value = { |
| 205 |
'found': True, |
| 206 |
'_source': {"result": _RESULT_RETRY}, |
| 207 |
'_seq_no': 2, |
| 208 |
'_primary_term': 1, |
| 209 |
} |
| 210 |
|
| 211 |
x._server.update.return_value = { |
| 212 |
'result': 'updated' |
| 213 |
} |
| 214 |
|
| 215 |
x._set_with_state(sentinel.task_id, sentinel.result, sentinel.state) |
| 216 |
|
| 217 |
assert x._server.get.call_count == 1 |
| 218 |
x._server.index.assert_called_once_with( |
| 219 |
id=sentinel.task_id, |
| 220 |
index=x.index, |
| 221 |
doc_type=x.doc_type, |
| 222 |
body={'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}, |
| 223 |
params={'op_type': 'create'}, |
| 224 |
) |
| 225 |
x._server.update.assert_called_once_with( |
| 226 |
id=sentinel.task_id, |
| 227 |
index=x.index, |
| 228 |
doc_type=x.doc_type, |
| 229 |
body={'doc': {'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}}, |
| 230 |
params={'if_seq_no': 2, 'if_primary_term': 1} |
| 231 |
) |
| 232 |
|
| 233 |
@patch('celery.backends.elasticsearch.datetime') |
| 234 |
def test_index_conflict_without_state(self, datetime_mock): |
| 235 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 236 |
datetime_mock.now.return_value = expected_dt |
| 237 |
|
| 238 |
x = ElasticsearchBackend(app=self.app) |
| 239 |
x._server = Mock() |
| 240 |
x._server.index.side_effect = [ |
| 241 |
exceptions.ConflictError("concurrent update", |
| 242 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 243 |
NodeConfig("https", "localhost", 9200)), None) |
| 244 |
] |
| 245 |
|
| 246 |
x._server.get.return_value = { |
| 247 |
'found': True, |
| 248 |
'_source': {"result": _RESULT_RETRY}, |
| 249 |
'_seq_no': 2, |
| 250 |
'_primary_term': 1, |
| 251 |
} |
| 252 |
|
| 253 |
x._server.update.return_value = { |
| 254 |
'result': 'updated' |
| 255 |
} |
| 256 |
|
| 257 |
x.set(sentinel.task_id, sentinel.result) |
| 258 |
|
| 259 |
assert x._server.get.call_count == 1 |
| 260 |
x._server.index.assert_called_once_with( |
| 261 |
id=sentinel.task_id, |
| 262 |
index=x.index, |
| 263 |
body={'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}, |
| 264 |
params={'op_type': 'create'}, |
| 265 |
) |
| 266 |
x._server.update.assert_called_once_with( |
| 267 |
id=sentinel.task_id, |
| 268 |
index=x.index, |
| 269 |
body={'doc': {'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}}, |
| 270 |
params={'if_seq_no': 2, 'if_primary_term': 1} |
| 271 |
) |
| 272 |
|
| 273 |
@patch('celery.backends.elasticsearch.datetime') |
| 274 |
def test_index_conflict_with_ready_state_on_backend_without_state(self, datetime_mock): |
| 275 |
"""Even if the backend already have a ready state saved (FAILURE in this test case) |
| 276 |
as we are calling ElasticsearchBackend.set directly, it does not have state, |
| 277 |
so it cannot protect overriding a ready state by any other state. |
| 278 |
As a result, server.update will be called no matter what. |
| 279 |
""" |
| 280 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 281 |
datetime_mock.now.return_value = expected_dt |
| 282 |
|
| 283 |
x = ElasticsearchBackend(app=self.app) |
| 284 |
x._server = Mock() |
| 285 |
x._server.index.side_effect = [ |
| 286 |
exceptions.ConflictError("concurrent update", |
| 287 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 288 |
NodeConfig("https", "localhost", 9200)), None) |
| 289 |
] |
| 290 |
|
| 291 |
x._server.get.return_value = { |
| 292 |
'found': True, |
| 293 |
'_source': {"result": _RESULT_FAILURE}, |
| 294 |
'_seq_no': 2, |
| 295 |
'_primary_term': 1, |
| 296 |
} |
| 297 |
|
| 298 |
x._server.update.return_value = { |
| 299 |
'result': 'updated' |
| 300 |
} |
| 301 |
|
| 302 |
x.set(sentinel.task_id, sentinel.result) |
| 303 |
|
| 304 |
assert x._server.get.call_count == 1 |
| 305 |
x._server.index.assert_called_once_with( |
| 306 |
id=sentinel.task_id, |
| 307 |
index=x.index, |
| 308 |
body={'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}, |
| 309 |
params={'op_type': 'create'}, |
| 310 |
) |
| 311 |
x._server.update.assert_called_once_with( |
| 312 |
id=sentinel.task_id, |
| 313 |
index=x.index, |
| 314 |
body={'doc': {'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}}, |
| 315 |
params={'if_seq_no': 2, 'if_primary_term': 1} |
| 316 |
) |
| 317 |
|
| 318 |
@patch('celery.backends.elasticsearch.datetime') |
| 319 |
def test_index_conflict_with_existing_success(self, datetime_mock): |
| 320 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 321 |
datetime_mock.now.return_value = expected_dt |
| 322 |
|
| 323 |
x = ElasticsearchBackend(app=self.app) |
| 324 |
x._server = Mock() |
| 325 |
x._server.index.side_effect = [ |
| 326 |
exceptions.ConflictError("concurrent update", |
| 327 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 328 |
NodeConfig("https", "localhost", 9200)), None) |
| 329 |
] |
| 330 |
|
| 331 |
x._server.get.return_value = { |
| 332 |
'found': True, |
| 333 |
'_source': { |
| 334 |
'result': """{"status":"SUCCESS","result":42}""" |
| 335 |
}, |
| 336 |
'_seq_no': 2, |
| 337 |
'_primary_term': 1, |
| 338 |
} |
| 339 |
|
| 340 |
x._server.update.return_value = { |
| 341 |
'result': 'updated' |
| 342 |
} |
| 343 |
|
| 344 |
x._set_with_state(sentinel.task_id, sentinel.result, sentinel.state) |
| 345 |
|
| 346 |
assert x._server.get.call_count == 1 |
| 347 |
x._server.index.assert_called_once_with( |
| 348 |
id=sentinel.task_id, |
| 349 |
index=x.index, |
| 350 |
body={'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}, |
| 351 |
params={'op_type': 'create'}, |
| 352 |
) |
| 353 |
x._server.update.assert_not_called() |
| 354 |
|
| 355 |
@patch('celery.backends.elasticsearch.datetime') |
| 356 |
def test_index_conflict_with_existing_ready_state(self, datetime_mock): |
| 357 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 358 |
datetime_mock.now.return_value = expected_dt |
| 359 |
|
| 360 |
x = ElasticsearchBackend(app=self.app) |
| 361 |
x._server = Mock() |
| 362 |
x._server.index.side_effect = [ |
| 363 |
exceptions.ConflictError("concurrent update", |
| 364 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 365 |
NodeConfig("https", "localhost", 9200)), None) |
| 366 |
] |
| 367 |
|
| 368 |
x._server.get.return_value = { |
| 369 |
'found': True, |
| 370 |
'_source': {"result": _RESULT_FAILURE}, |
| 371 |
'_seq_no': 2, |
| 372 |
'_primary_term': 1, |
| 373 |
} |
| 374 |
|
| 375 |
x._server.update.return_value = { |
| 376 |
'result': 'updated' |
| 377 |
} |
| 378 |
|
| 379 |
x._set_with_state(sentinel.task_id, sentinel.result, states.RETRY) |
| 380 |
|
| 381 |
assert x._server.get.call_count == 1 |
| 382 |
x._server.index.assert_called_once_with( |
| 383 |
id=sentinel.task_id, |
| 384 |
index=x.index, |
| 385 |
body={'result': sentinel.result, '@timestamp': expected_dt.isoformat()[:-9] + 'Z'}, |
| 386 |
params={'op_type': 'create'}, |
| 387 |
) |
| 388 |
x._server.update.assert_not_called() |
| 389 |
|
| 390 |
@patch('celery.backends.elasticsearch.datetime') |
| 391 |
@patch('celery.app.base.datetime') |
| 392 |
def test_backend_concurrent_update(self, base_datetime_mock, es_datetime_mock): |
| 393 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 394 |
es_datetime_mock.now.return_value = expected_dt |
| 395 |
|
| 396 |
expected_done_dt = datetime(2020, 6, 1, 18, 45, 34, 654321, timezone.utc) |
| 397 |
base_datetime_mock.now.return_value = expected_done_dt |
| 398 |
|
| 399 |
self.app.conf.result_backend_always_retry, prev = True, self.app.conf.result_backend_always_retry |
| 400 |
x_server_get_side_effect = [ |
| 401 |
{ |
| 402 |
'found': True, |
| 403 |
'_source': {'result': _RESULT_RETRY}, |
| 404 |
'_seq_no': 2, |
| 405 |
'_primary_term': 1, |
| 406 |
}, |
| 407 |
{ |
| 408 |
'found': True, |
| 409 |
'_source': {'result': _RESULT_RETRY}, |
| 410 |
'_seq_no': 2, |
| 411 |
'_primary_term': 1, |
| 412 |
}, |
| 413 |
{ |
| 414 |
'found': True, |
| 415 |
'_source': {'result': _RESULT_FAILURE}, |
| 416 |
'_seq_no': 3, |
| 417 |
'_primary_term': 1, |
| 418 |
}, |
| 419 |
{ |
| 420 |
'found': True, |
| 421 |
'_source': {'result': _RESULT_FAILURE}, |
| 422 |
'_seq_no': 3, |
| 423 |
'_primary_term': 1, |
| 424 |
}, |
| 425 |
] |
| 426 |
|
| 427 |
try: |
| 428 |
x = ElasticsearchBackend(app=self.app) |
| 429 |
|
| 430 |
task_id = str(sentinel.task_id) |
| 431 |
encoded_task_id = bytes_to_str(x.get_key_for_task(task_id)) |
| 432 |
result = str(sentinel.result) |
| 433 |
|
| 434 |
sleep_mock = Mock() |
| 435 |
x._sleep = sleep_mock |
| 436 |
x._server = Mock() |
| 437 |
x._server.index.side_effect = exceptions.ConflictError( |
| 438 |
"concurrent update", |
| 439 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, NodeConfig("https", "localhost", 9200)), |
| 440 |
None) |
| 441 |
x._server.get.side_effect = x_server_get_side_effect |
| 442 |
x._server.update.side_effect = [ |
| 443 |
{'result': 'noop'}, |
| 444 |
{'result': 'updated'} |
| 445 |
] |
| 446 |
result_meta = x._get_result_meta(result, states.SUCCESS, None, None) |
| 447 |
result_meta['task_id'] = bytes_to_str(task_id) |
| 448 |
|
| 449 |
expected_result = x.encode(result_meta) |
| 450 |
|
| 451 |
x.store_result(task_id, result, states.SUCCESS) |
| 452 |
x._server.index.assert_has_calls([ |
| 453 |
call( |
| 454 |
id=encoded_task_id, |
| 455 |
index=x.index, |
| 456 |
body={ |
| 457 |
'result': expected_result, |
| 458 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 459 |
}, |
| 460 |
params={'op_type': 'create'} |
| 461 |
), |
| 462 |
call( |
| 463 |
id=encoded_task_id, |
| 464 |
index=x.index, |
| 465 |
body={ |
| 466 |
'result': expected_result, |
| 467 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 468 |
}, |
| 469 |
params={'op_type': 'create'} |
| 470 |
), |
| 471 |
]) |
| 472 |
x._server.update.assert_has_calls([ |
| 473 |
call( |
| 474 |
id=encoded_task_id, |
| 475 |
index=x.index, |
| 476 |
body={ |
| 477 |
'doc': { |
| 478 |
'result': expected_result, |
| 479 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 480 |
} |
| 481 |
}, |
| 482 |
params={'if_seq_no': 2, 'if_primary_term': 1} |
| 483 |
), |
| 484 |
call( |
| 485 |
id=encoded_task_id, |
| 486 |
index=x.index, |
| 487 |
body={ |
| 488 |
'doc': { |
| 489 |
'result': expected_result, |
| 490 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 491 |
} |
| 492 |
}, |
| 493 |
params={'if_seq_no': 3, 'if_primary_term': 1} |
| 494 |
), |
| 495 |
]) |
| 496 |
|
| 497 |
assert sleep_mock.call_count == 1 |
| 498 |
finally: |
| 499 |
self.app.conf.result_backend_always_retry = prev |
| 500 |
|
| 501 |
@patch('celery.backends.elasticsearch.datetime') |
| 502 |
@patch('celery.app.base.datetime') |
| 503 |
def test_backend_index_conflicting_document_removed(self, base_datetime_mock, es_datetime_mock): |
| 504 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 505 |
es_datetime_mock.now.return_value = expected_dt |
| 506 |
|
| 507 |
expected_done_dt = datetime(2020, 6, 1, 18, 45, 34, 654321, timezone.utc) |
| 508 |
base_datetime_mock.now.return_value = expected_done_dt |
| 509 |
|
| 510 |
self.app.conf.result_backend_always_retry, prev = True, self.app.conf.result_backend_always_retry |
| 511 |
try: |
| 512 |
x = ElasticsearchBackend(app=self.app) |
| 513 |
|
| 514 |
task_id = str(sentinel.task_id) |
| 515 |
encoded_task_id = bytes_to_str(x.get_key_for_task(task_id)) |
| 516 |
result = str(sentinel.result) |
| 517 |
|
| 518 |
sleep_mock = Mock() |
| 519 |
x._sleep = sleep_mock |
| 520 |
x._server = Mock() |
| 521 |
x._server.index.side_effect = [ |
| 522 |
exceptions.ConflictError("concurrent update", |
| 523 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 524 |
NodeConfig("https", "localhost", 9200)), None), |
| 525 |
{'result': 'created'} |
| 526 |
] |
| 527 |
|
| 528 |
x._server.get.side_effect = [ |
| 529 |
{ |
| 530 |
'found': True, |
| 531 |
'_source': {"result": _RESULT_RETRY}, |
| 532 |
'_seq_no': 2, |
| 533 |
'_primary_term': 1, |
| 534 |
}, |
| 535 |
exceptions.NotFoundError('{"_index":"celery","_type":"_doc","_id":"toto","found":false}', |
| 536 |
ApiResponseMeta(404, "HTTP/1.1", HttpHeaders(), 0, |
| 537 |
NodeConfig("https", "localhost", 9200)), |
| 538 |
{'_index': 'celery', '_type': '_doc', '_id': 'toto', 'found': False}), |
| 539 |
] |
| 540 |
|
| 541 |
result_meta = x._get_result_meta(result, states.SUCCESS, None, None) |
| 542 |
result_meta['task_id'] = bytes_to_str(task_id) |
| 543 |
|
| 544 |
expected_result = x.encode(result_meta) |
| 545 |
|
| 546 |
x.store_result(task_id, result, states.SUCCESS) |
| 547 |
x._server.index.assert_has_calls([ |
| 548 |
call( |
| 549 |
id=encoded_task_id, |
| 550 |
index=x.index, |
| 551 |
body={ |
| 552 |
'result': expected_result, |
| 553 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 554 |
}, |
| 555 |
params={'op_type': 'create'} |
| 556 |
), |
| 557 |
call( |
| 558 |
id=encoded_task_id, |
| 559 |
index=x.index, |
| 560 |
body={ |
| 561 |
'result': expected_result, |
| 562 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 563 |
}, |
| 564 |
params={'op_type': 'create'} |
| 565 |
), |
| 566 |
]) |
| 567 |
x._server.update.assert_not_called() |
| 568 |
sleep_mock.assert_not_called() |
| 569 |
finally: |
| 570 |
self.app.conf.result_backend_always_retry = prev |
| 571 |
|
| 572 |
@patch('celery.backends.elasticsearch.datetime') |
| 573 |
@patch('celery.app.base.datetime') |
| 574 |
def test_backend_index_conflicting_document_removed_not_throwing(self, base_datetime_mock, es_datetime_mock): |
| 575 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 576 |
es_datetime_mock.now.return_value = expected_dt |
| 577 |
|
| 578 |
expected_done_dt = datetime(2020, 6, 1, 18, 45, 34, 654321, timezone.utc) |
| 579 |
base_datetime_mock.now.return_value = expected_done_dt |
| 580 |
|
| 581 |
self.app.conf.result_backend_always_retry, prev = True, self.app.conf.result_backend_always_retry |
| 582 |
try: |
| 583 |
x = ElasticsearchBackend(app=self.app) |
| 584 |
|
| 585 |
task_id = str(sentinel.task_id) |
| 586 |
encoded_task_id = bytes_to_str(x.get_key_for_task(task_id)) |
| 587 |
result = str(sentinel.result) |
| 588 |
|
| 589 |
sleep_mock = Mock() |
| 590 |
x._sleep = sleep_mock |
| 591 |
x._server = Mock() |
| 592 |
x._server.index.side_effect = [ |
| 593 |
exceptions.ConflictError("concurrent update", |
| 594 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 595 |
NodeConfig("https", "localhost", 9200)), None), |
| 596 |
{'result': 'created'} |
| 597 |
] |
| 598 |
|
| 599 |
x._server.get.side_effect = [ |
| 600 |
{ |
| 601 |
'found': True, |
| 602 |
'_source': {'result': _RESULT_RETRY}, |
| 603 |
'_seq_no': 2, |
| 604 |
'_primary_term': 1, |
| 605 |
}, |
| 606 |
{'_index': 'celery', '_type': '_doc', '_id': 'toto', 'found': False}, |
| 607 |
] |
| 608 |
|
| 609 |
result_meta = x._get_result_meta(result, states.SUCCESS, None, None) |
| 610 |
result_meta['task_id'] = bytes_to_str(task_id) |
| 611 |
|
| 612 |
expected_result = x.encode(result_meta) |
| 613 |
|
| 614 |
x.store_result(task_id, result, states.SUCCESS) |
| 615 |
x._server.index.assert_has_calls([ |
| 616 |
call( |
| 617 |
id=encoded_task_id, |
| 618 |
index=x.index, |
| 619 |
body={ |
| 620 |
'result': expected_result, |
| 621 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 622 |
}, |
| 623 |
params={'op_type': 'create'} |
| 624 |
), |
| 625 |
call( |
| 626 |
id=encoded_task_id, |
| 627 |
index=x.index, |
| 628 |
body={ |
| 629 |
'result': expected_result, |
| 630 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 631 |
}, |
| 632 |
params={'op_type': 'create'} |
| 633 |
), |
| 634 |
]) |
| 635 |
x._server.update.assert_not_called() |
| 636 |
sleep_mock.assert_not_called() |
| 637 |
finally: |
| 638 |
self.app.conf.result_backend_always_retry = prev |
| 639 |
|
| 640 |
@patch('celery.backends.elasticsearch.datetime') |
| 641 |
@patch('celery.app.base.datetime') |
| 642 |
def test_backend_index_corrupted_conflicting_document(self, base_datetime_mock, es_datetime_mock): |
| 643 |
expected_dt = datetime(2020, 6, 1, 18, 43, 24, 123456, timezone.utc) |
| 644 |
es_datetime_mock.now.return_value = expected_dt |
| 645 |
|
| 646 |
expected_done_dt = datetime(2020, 6, 1, 18, 45, 34, 654321, timezone.utc) |
| 647 |
base_datetime_mock.now.return_value = expected_done_dt |
| 648 |
|
| 649 |
# self.app.conf.result_backend_always_retry, prev = True, self.app.conf.result_backend_always_retry |
| 650 |
# try: |
| 651 |
x = ElasticsearchBackend(app=self.app) |
| 652 |
|
| 653 |
task_id = str(sentinel.task_id) |
| 654 |
encoded_task_id = bytes_to_str(x.get_key_for_task(task_id)) |
| 655 |
result = str(sentinel.result) |
| 656 |
|
| 657 |
sleep_mock = Mock() |
| 658 |
x._sleep = sleep_mock |
| 659 |
x._server = Mock() |
| 660 |
x._server.index.side_effect = [ |
| 661 |
exceptions.ConflictError("concurrent update", |
| 662 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 663 |
NodeConfig("https", "localhost", 9200)), None) |
| 664 |
] |
| 665 |
|
| 666 |
x._server.update.side_effect = [ |
| 667 |
{'result': 'updated'} |
| 668 |
] |
| 669 |
|
| 670 |
x._server.get.return_value = { |
| 671 |
'found': True, |
| 672 |
'_source': {}, |
| 673 |
'_seq_no': 2, |
| 674 |
'_primary_term': 1, |
| 675 |
} |
| 676 |
|
| 677 |
result_meta = x._get_result_meta(result, states.SUCCESS, None, None) |
| 678 |
result_meta['task_id'] = bytes_to_str(task_id) |
| 679 |
|
| 680 |
expected_result = x.encode(result_meta) |
| 681 |
|
| 682 |
x.store_result(task_id, result, states.SUCCESS) |
| 683 |
x._server.index.assert_called_once_with( |
| 684 |
id=encoded_task_id, |
| 685 |
index=x.index, |
| 686 |
body={ |
| 687 |
'result': expected_result, |
| 688 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 689 |
}, |
| 690 |
params={'op_type': 'create'} |
| 691 |
) |
| 692 |
x._server.update.assert_called_once_with( |
| 693 |
id=encoded_task_id, |
| 694 |
index=x.index, |
| 695 |
body={ |
| 696 |
'doc': { |
| 697 |
'result': expected_result, |
| 698 |
'@timestamp': expected_dt.isoformat()[:-9] + 'Z' |
| 699 |
} |
| 700 |
}, |
| 701 |
params={'if_primary_term': 1, 'if_seq_no': 2} |
| 702 |
) |
| 703 |
sleep_mock.assert_not_called() |
| 704 |
|
| 705 |
def test_backend_params_by_url(self): |
| 706 |
url = 'elasticsearch://localhost:9200/index/doc_type' |
| 707 |
with self.Celery(backend=url) as app: |
| 708 |
x = app.backend |
| 709 |
|
| 710 |
assert x.index == 'index' |
| 711 |
assert x.doc_type == "doc_type" |
| 712 |
assert x.scheme == 'http' |
| 713 |
assert x.host == 'localhost' |
| 714 |
assert x.port == 9200 |
| 715 |
|
| 716 |
def test_backend_url_no_params(self): |
| 717 |
url = 'elasticsearch:///' |
| 718 |
with self.Celery(backend=url) as app: |
| 719 |
x = app.backend |
| 720 |
|
| 721 |
assert x.index == 'celery' |
| 722 |
assert x.doc_type is None |
| 723 |
assert x.scheme == 'http' |
| 724 |
assert x.host == 'localhost' |
| 725 |
assert x.port == 9200 |
| 726 |
|
| 727 |
@patch('elasticsearch.Elasticsearch') |
| 728 |
def test_get_server_with_auth(self, mock_es_client): |
| 729 |
url = 'elasticsearch+https://fake_user:fake_pass@localhost:9200/index/doc_type' |
| 730 |
with self.Celery(backend=url) as app: |
| 731 |
x = app.backend |
| 732 |
|
| 733 |
assert x.username == 'fake_user' |
| 734 |
assert x.password == 'fake_pass' |
| 735 |
assert x.scheme == 'https' |
| 736 |
|
| 737 |
x._get_server() |
| 738 |
mock_es_client.assert_called_once_with( |
| 739 |
'https://localhost:9200', |
| 740 |
http_auth=('fake_user', 'fake_pass'), |
| 741 |
max_retries=x.es_max_retries, |
| 742 |
retry_on_timeout=x.es_retry_on_timeout, |
| 743 |
timeout=x.es_timeout, |
| 744 |
) |
| 745 |
|
| 746 |
@patch('elasticsearch.Elasticsearch') |
| 747 |
def test_get_server_without_auth(self, mock_es_client): |
| 748 |
url = 'elasticsearch://localhost:9200/index/doc_type' |
| 749 |
with self.Celery(backend=url) as app: |
| 750 |
x = app.backend |
| 751 |
x._get_server() |
| 752 |
mock_es_client.assert_called_once_with( |
| 753 |
'http://localhost:9200', |
| 754 |
http_auth=None, |
| 755 |
max_retries=x.es_max_retries, |
| 756 |
retry_on_timeout=x.es_retry_on_timeout, |
| 757 |
timeout=x.es_timeout, |
| 758 |
) |
| 759 |
|
| 760 |
def test_index(self): |
| 761 |
x = ElasticsearchBackend(app=self.app) |
| 762 |
x._server = Mock() |
| 763 |
x._server.index = Mock() |
| 764 |
expected_result = { |
| 765 |
'_id': sentinel.task_id, |
| 766 |
'_source': {'result': sentinel.result} |
| 767 |
} |
| 768 |
x._server.index.return_value = expected_result |
| 769 |
|
| 770 |
body = {"field1": "value1"} |
| 771 |
x._index( |
| 772 |
id=str(sentinel.task_id).encode(), |
| 773 |
body=body, |
| 774 |
kwarg1='test1' |
| 775 |
) |
| 776 |
x._server.index.assert_called_once_with( |
| 777 |
id=str(sentinel.task_id), |
| 778 |
index=x.index, |
| 779 |
body=body, |
| 780 |
params={'op_type': 'create'}, |
| 781 |
kwarg1='test1' |
| 782 |
) |
| 783 |
|
| 784 |
def test_index_with_doctype(self): |
| 785 |
x = ElasticsearchBackend(app=self.app) |
| 786 |
x._server = Mock() |
| 787 |
x._server.index = Mock() |
| 788 |
expected_result = { |
| 789 |
'_id': sentinel.task_id, |
| 790 |
'_source': {'result': sentinel.result} |
| 791 |
} |
| 792 |
x._server.index.return_value = expected_result |
| 793 |
x.doc_type = "_doc" |
| 794 |
body = {"field1": "value1"} |
| 795 |
x._index( |
| 796 |
id=str(sentinel.task_id).encode(), |
| 797 |
body=body, |
| 798 |
kwarg1='test1' |
| 799 |
) |
| 800 |
x._server.index.assert_called_once_with( |
| 801 |
id=str(sentinel.task_id), |
| 802 |
index=x.index, |
| 803 |
doc_type=x.doc_type, |
| 804 |
body=body, |
| 805 |
params={'op_type': 'create'}, |
| 806 |
kwarg1='test1' |
| 807 |
) |
| 808 |
|
| 809 |
def test_index_bytes_key(self): |
| 810 |
x = ElasticsearchBackend(app=self.app) |
| 811 |
x._server = Mock() |
| 812 |
x._server.index = Mock() |
| 813 |
expected_result = { |
| 814 |
'_id': sentinel.task_id, |
| 815 |
'_source': {'result': sentinel.result} |
| 816 |
} |
| 817 |
x._server.index.return_value = expected_result |
| 818 |
|
| 819 |
body = {b"field1": "value1"} |
| 820 |
x._index( |
| 821 |
id=str(sentinel.task_id).encode(), |
| 822 |
body=body, |
| 823 |
kwarg1='test1' |
| 824 |
) |
| 825 |
x._server.index.assert_called_once_with( |
| 826 |
id=str(sentinel.task_id), |
| 827 |
index=x.index, |
| 828 |
body={"field1": "value1"}, |
| 829 |
params={'op_type': 'create'}, |
| 830 |
kwarg1='test1' |
| 831 |
) |
| 832 |
|
| 833 |
def test_encode_as_json(self): |
| 834 |
self.app.conf.elasticsearch_save_meta_as_text, prev = False, self.app.conf.elasticsearch_save_meta_as_text |
| 835 |
try: |
| 836 |
x = ElasticsearchBackend(app=self.app) |
| 837 |
result_meta = x._get_result_meta({'solution': 42}, states.SUCCESS, None, None) |
| 838 |
assert x.encode(result_meta) == result_meta |
| 839 |
finally: |
| 840 |
self.app.conf.elasticsearch_save_meta_as_text = prev |
| 841 |
|
| 842 |
def test_encode_none_as_json(self): |
| 843 |
self.app.conf.elasticsearch_save_meta_as_text, prev = False, self.app.conf.elasticsearch_save_meta_as_text |
| 844 |
try: |
| 845 |
x = ElasticsearchBackend(app=self.app) |
| 846 |
result_meta = x._get_result_meta(None, states.SUCCESS, None, None) |
| 847 |
assert x.encode(result_meta) == result_meta |
| 848 |
finally: |
| 849 |
self.app.conf.elasticsearch_save_meta_as_text = prev |
| 850 |
|
| 851 |
def test_encode_exception_as_json(self): |
| 852 |
self.app.conf.elasticsearch_save_meta_as_text, prev = False, self.app.conf.elasticsearch_save_meta_as_text |
| 853 |
try: |
| 854 |
x = ElasticsearchBackend(app=self.app) |
| 855 |
try: |
| 856 |
raise Exception("failed") |
| 857 |
except Exception as exc: |
| 858 |
einfo = ExceptionInfo() |
| 859 |
result_meta = x._get_result_meta( |
| 860 |
x.encode_result(exc, states.FAILURE), |
| 861 |
states.FAILURE, |
| 862 |
einfo.traceback, |
| 863 |
None, |
| 864 |
) |
| 865 |
assert x.encode(result_meta) == result_meta |
| 866 |
finally: |
| 867 |
self.app.conf.elasticsearch_save_meta_as_text = prev |
| 868 |
|
| 869 |
def test_decode_from_json(self): |
| 870 |
self.app.conf.elasticsearch_save_meta_as_text, prev = False, self.app.conf.elasticsearch_save_meta_as_text |
| 871 |
try: |
| 872 |
x = ElasticsearchBackend(app=self.app) |
| 873 |
result_meta = x._get_result_meta({'solution': 42}, states.SUCCESS, None, None) |
| 874 |
result_meta['result'] = x._encode(result_meta['result'])[2] |
| 875 |
assert x.decode(result_meta) == result_meta |
| 876 |
finally: |
| 877 |
self.app.conf.elasticsearch_save_meta_as_text = prev |
| 878 |
|
| 879 |
def test_decode_none_from_json(self): |
| 880 |
self.app.conf.elasticsearch_save_meta_as_text, prev = False, self.app.conf.elasticsearch_save_meta_as_text |
| 881 |
try: |
| 882 |
x = ElasticsearchBackend(app=self.app) |
| 883 |
result_meta = x._get_result_meta(None, states.SUCCESS, None, None) |
| 884 |
# result_meta['result'] = x._encode(result_meta['result'])[2] |
| 885 |
assert x.decode(result_meta) == result_meta |
| 886 |
finally: |
| 887 |
self.app.conf.elasticsearch_save_meta_as_text = prev |
| 888 |
|
| 889 |
def test_decode_encoded_from_json(self): |
| 890 |
self.app.conf.elasticsearch_save_meta_as_text, prev = False, self.app.conf.elasticsearch_save_meta_as_text |
| 891 |
try: |
| 892 |
x = ElasticsearchBackend(app=self.app) |
| 893 |
result_meta = x._get_result_meta({'solution': 42}, states.SUCCESS, None, None) |
| 894 |
assert x.decode(x.encode(result_meta)) == result_meta |
| 895 |
finally: |
| 896 |
self.app.conf.elasticsearch_save_meta_as_text = prev |
| 897 |
|
| 898 |
def test_decode_encoded_exception_as_json(self): |
| 899 |
self.app.conf.elasticsearch_save_meta_as_text, prev = False, self.app.conf.elasticsearch_save_meta_as_text |
| 900 |
try: |
| 901 |
x = ElasticsearchBackend(app=self.app) |
| 902 |
try: |
| 903 |
raise Exception("failed") |
| 904 |
except Exception as exc: |
| 905 |
einfo = ExceptionInfo() |
| 906 |
result_meta = x._get_result_meta( |
| 907 |
x.encode_result(exc, states.FAILURE), |
| 908 |
states.FAILURE, |
| 909 |
einfo.traceback, |
| 910 |
None, |
| 911 |
) |
| 912 |
assert x.decode(x.encode(result_meta)) == result_meta |
| 913 |
finally: |
| 914 |
self.app.conf.elasticsearch_save_meta_as_text = prev |
| 915 |
|
| 916 |
@patch("celery.backends.base.KeyValueStoreBackend.decode") |
| 917 |
def test_decode_not_dict(self, kv_decode_mock): |
| 918 |
self.app.conf.elasticsearch_save_meta_as_text, prev = False, self.app.conf.elasticsearch_save_meta_as_text |
| 919 |
try: |
| 920 |
kv_decode_mock.return_value = sentinel.decoded |
| 921 |
x = ElasticsearchBackend(app=self.app) |
| 922 |
assert x.decode(sentinel.encoded) == sentinel.decoded |
| 923 |
kv_decode_mock.assert_called_once() |
| 924 |
finally: |
| 925 |
self.app.conf.elasticsearch_save_meta_as_text = prev |
| 926 |
|
| 927 |
def test_config_params(self): |
| 928 |
self.app.conf.elasticsearch_max_retries = 10 |
| 929 |
self.app.conf.elasticsearch_timeout = 20.0 |
| 930 |
self.app.conf.elasticsearch_retry_on_timeout = True |
| 931 |
|
| 932 |
self.backend = ElasticsearchBackend(app=self.app) |
| 933 |
|
| 934 |
assert self.backend.es_max_retries == 10 |
| 935 |
assert self.backend.es_timeout == 20.0 |
| 936 |
assert self.backend.es_retry_on_timeout is True |
| 937 |
|
| 938 |
def test_lazy_server_init(self): |
| 939 |
x = ElasticsearchBackend(app=self.app) |
| 940 |
x._get_server = Mock() |
| 941 |
x._get_server.return_value = sentinel.server |
| 942 |
|
| 943 |
assert x.server == sentinel.server |
| 944 |
x._get_server.assert_called_once() |
| 945 |
|
| 946 |
def test_mget(self): |
| 947 |
x = ElasticsearchBackend(app=self.app) |
| 948 |
x._server = Mock() |
| 949 |
x._server.get.side_effect = [ |
| 950 |
{'found': True, '_id': sentinel.task_id1, '_source': {'result': sentinel.result1}}, |
| 951 |
{'found': True, '_id': sentinel.task_id2, '_source': {'result': sentinel.result2}}, |
| 952 |
] |
| 953 |
assert x.mget([sentinel.task_id1, sentinel.task_id2]) == [sentinel.result1, sentinel.result2] |
| 954 |
x._server.get.assert_has_calls([ |
| 955 |
call(index=x.index, id=sentinel.task_id1), |
| 956 |
call(index=x.index, id=sentinel.task_id2), |
| 957 |
]) |
| 958 |
|
| 959 |
def test_exception_safe_to_retry(self): |
| 960 |
x = ElasticsearchBackend(app=self.app) |
| 961 |
assert not x.exception_safe_to_retry(Exception("failed")) |
| 962 |
assert not x.exception_safe_to_retry(BaseException("failed")) |
| 963 |
assert x.exception_safe_to_retry( |
| 964 |
exceptions.ConflictError("concurrent update", |
| 965 |
ApiResponseMeta(409, "HTTP/1.1", HttpHeaders(), 0, |
| 966 |
NodeConfig("https", "localhost", 9200)), None)) |
| 967 |
assert x.exception_safe_to_retry(exceptions.ConnectionError("service unavailable")) |
| 968 |
assert x.exception_safe_to_retry(exceptions.TransportError("too many requests")) |
| 969 |
assert not x.exception_safe_to_retry( |
| 970 |
exceptions.NotFoundError("not found", |
| 971 |
ApiResponseMeta(404, "HTTP/1.1", HttpHeaders(), 0, |
| 972 |
NodeConfig("https", "localhost", 9200)), None)) |