diff --git a/requirements-test.txt b/requirements-test.txt --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,3 +1,4 @@ hypothesis pytest +pytest-postgresql celery >= 4 diff --git a/swh/scheduler/tests/conftest.py b/swh/scheduler/tests/conftest.py --- a/swh/scheduler/tests/conftest.py +++ b/swh/scheduler/tests/conftest.py @@ -1,4 +1,17 @@ +import os import pytest +import glob +from datetime import timedelta + +from swh.core.utils import numfile_sortkey as sortkey +from swh.scheduler import get_scheduler +from swh.scheduler.tests import SQL_DIR + +DUMP_FILES = os.path.join(SQL_DIR, '*.sql') + +# celery tasks for testing purpose; tasks themselves should be +# in swh/scheduler/tests/celery_tasks.py +TASK_NAMES = ['ping', 'multiping'] @pytest.fixture(scope='session') @@ -16,7 +29,16 @@ @pytest.fixture(scope='session') def celery_parameters(): return { - 'task_cls': 'swh.scheduler.task:SWHTask', + 'task_cls': 'swh.scheduler.task:SWHTask', + } + + +@pytest.fixture(scope='session') +def celery_config(): + return { + 'accept_content': ['application/x-msgpack', 'application/json'], + 'task_serializer': 'msgpack', + 'result_serializer': 'msgpack', } @@ -28,3 +50,35 @@ import swh.scheduler.celery_backend.config swh.scheduler.celery_backend.config.app = celery_session_app yield celery_session_app + + +@pytest.fixture +def swh_scheduler(request, postgresql_proc, postgresql): + scheduler_config = { + 'scheduling_db': 'postgresql://{user}@{host}:{port}/{dbname}'.format( + host=postgresql_proc.host, + port=postgresql_proc.port, + user='postgres', + dbname='tests') + } + + all_dump_files = sorted(glob.glob(DUMP_FILES), key=sortkey) + + cursor = postgresql.cursor() + for fname in all_dump_files: + with open(fname) as fobj: + cursor.execute(fobj.read()) + postgresql.commit() + + scheduler = get_scheduler('local', scheduler_config) + for taskname in TASK_NAMES: + scheduler.create_task_type({ + 'type': 'swh-test-{}'.format(taskname), + 'description': 'The {} testing task'.format(taskname), + 'backend_name': 'swh.scheduler.tests.tasks.{}'.format(taskname), + 'default_interval': timedelta(days=1), + 'min_interval': timedelta(hours=6), + 'max_interval': timedelta(days=12), + }) + + return scheduler diff --git a/swh/scheduler/tests/test_celery_tasks.py b/swh/scheduler/tests/test_celery_tasks.py --- a/swh/scheduler/tests/test_celery_tasks.py +++ b/swh/scheduler/tests/test_celery_tasks.py @@ -1,5 +1,9 @@ from time import sleep from celery.result import GroupResult +from celery.result import AsyncResult + +from swh.scheduler.utils import create_task_dict +from swh.scheduler.celery_backend.runner import run_ready_tasks def test_ping(swh_app, celery_session_worker): @@ -33,3 +37,19 @@ results = [x.get() for x in promise.results] for i in range(5): assert ("OK (kw={'i': %s})" % i) in results + + +def test_scheduler_fixture(swh_app, celery_session_worker, swh_scheduler): + "Test that the scheduler fixture works properly" + task_type = swh_scheduler.get_task_type('swh-test-ping') + assert task_type + assert task_type['backend_name'] == 'swh.scheduler.tests.tasks.ping' + + swh_scheduler.create_tasks([create_task_dict( + 'swh-test-ping', 'oneshot')]) + + backend_tasks = run_ready_tasks(swh_scheduler, swh_app) + assert backend_tasks + for task in backend_tasks: + # Make sure the task completed + AsyncResult(id=task['backend_id']).get()