diff --git a/test/test_job_routes.py b/test/test_job_routes.py index 7f1a20f1ebd70703116abb8c4f7d9ed9ddeade37..b60756e42569f50eed5dd16c2e4b05a1427827e5 100644 --- a/test/test_job_routes.py +++ b/test/test_job_routes.py @@ -1,7 +1,8 @@ import json import jsonschema -from inventory_provider.tasks.common import DB_LATCH_SCHEMA +from inventory_provider.tasks.common import _get_redis, DB_LATCH_SCHEMA + DEFAULT_REQUEST_HEADERS = { "Content-type": "application/json", "Accept": ["application/json"] @@ -19,20 +20,39 @@ TASK_ID_RESPONSE_SCHEMA = { TASK_STATUS_SCHEMA = { "$schema": "http://json-schema.org/draft-07/schema#", - "type": "object", - "properties": { - "id": {"type": "string"}, - "status": {"type": "string"}, - "exception": {"type": "boolean"}, - "ready": {"type": "boolean"}, - "success": {"type": "boolean"}, - "result": {"type": ["object", "null"]} + "definitions": { + "task": { + "type": "object", + "properties": { + "id": {"type": "string"}, + "status": {"type": "string"}, + "exception": {"type": "boolean"}, + "ready": {"type": "boolean"}, + "success": {"type": "boolean"}, + "result": {"type": ["object", "null"]}, + "parent": {"type": ["string", "null"]} + }, + "required": [ + "id", "status", "exception", "ready", "success", "parent"], + "additionalProperties": False + } }, - "required": ["id", "status", "exception", "ready", "success"], - "additionalProperties": False + + "type": "array", + "items": {"$ref": "#/definitions/task"} } +def backend_db(): + return _get_redis({ + 'redis': { + 'hostname': None, + 'port': None + }, + 'redis-databases': [0, 7] + }).db + + def test_job_update_all(client, mocker): expected_task_id = 'xyz@123#456' launch_refresh_cache_all = mocker.patch( @@ -123,6 +143,42 @@ def test_reload_router_config(client, mocker): assert refresh_task_response['task id'] == 'bogus task id' +def test_check_update_status(client, mocker): + + db = backend_db() + db['classifier-cache:update-task-id'] = 'zz55' + + mocker.patch( + 'inventory_provider.tasks.worker.AsyncResult', MockedAsyncResult) + MockedAsyncResult.status = 'SUCCESS' # celery.states.SUCCESS + MockedAsyncResult.result = {'absab': 1, 'def': 'aaabbb'} + + rv = client.post( + 'jobs/check-update-status', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + result = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(result, TASK_STATUS_SCHEMA) + for status in result: + assert status['id'] == 'zz55' + assert status['status'] == 'SUCCESS' + assert not status['exception'] + assert status['ready'] + assert status['success'] + assert 'result' in status + + +def test_check_update_status_404(client): + + db = backend_db() + db.pop('classifier-cache:update-task-id', None) + + rv = client.post( + 'jobs/check-update-status', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 404 + + def test_check_task_status_success(client, mocker): mocker.patch( 'inventory_provider.tasks.worker.AsyncResult', MockedAsyncResult) @@ -133,14 +189,15 @@ def test_check_task_status_success(client, mocker): 'jobs/check-task-status/abc', headers=DEFAULT_REQUEST_HEADERS) assert rv.status_code == 200 - status = json.loads(rv.data.decode('utf-8')) - jsonschema.validate(status, TASK_STATUS_SCHEMA) - assert status['id'] == 'abc' - assert status['status'] == 'SUCCESS' - assert not status['exception'] - assert status['ready'] - assert status['success'] - assert 'result' in status + result = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(result, TASK_STATUS_SCHEMA) + for status in result: + assert status['id'] == 'abc' + assert status['status'] == 'SUCCESS' + assert not status['exception'] + assert status['ready'] + assert status['success'] + assert 'result' in status def test_check_task_status_custom_status(client, mocker): @@ -153,13 +210,14 @@ def test_check_task_status_custom_status(client, mocker): 'jobs/check-task-status/xyz', headers=DEFAULT_REQUEST_HEADERS) assert rv.status_code == 200 - status = json.loads(rv.data.decode('utf-8')) - jsonschema.validate(status, TASK_STATUS_SCHEMA) - assert status['id'] == 'xyz' - assert status['status'] == 'custom' - assert not status['exception'] - assert not status['ready'] - assert not status['success'] + result = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(result, TASK_STATUS_SCHEMA) + for status in result: + assert status['id'] == 'xyz' + assert status['status'] == 'custom' + assert not status['exception'] + assert not status['ready'] + assert not status['success'] def test_check_task_status_exception(client, mocker): @@ -172,15 +230,16 @@ def test_check_task_status_exception(client, mocker): 'jobs/check-task-status/123-xyz.ABC', headers=DEFAULT_REQUEST_HEADERS) assert rv.status_code == 200 - status = json.loads(rv.data.decode('utf-8')) - jsonschema.validate(status, TASK_STATUS_SCHEMA) - assert status['id'] == '123-xyz.ABC' - assert status['status'] == 'FAILURE' - assert status['exception'] - assert status['ready'] - assert not status['success'] - assert status['result']['error type'] == 'AssertionError' - assert status['result']['message'] == 'test error message' + result = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(result, TASK_STATUS_SCHEMA) + for status in result: + assert status['id'] == '123-xyz.ABC' + assert status['status'] == 'FAILURE' + assert status['exception'] + assert status['ready'] + assert not status['success'] + assert status['result']['error type'] == 'AssertionError' + assert status['result']['message'] == 'test error message' def test_latchdb(client, mocked_redis):