diff --git a/README.md b/README.md index 71ae55b..6104669 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,33 @@ swh-scheduler ============= Job scheduler for the Software Heritage project. Task manager for asynchronous/delayed tasks, used for both recurrent (e.g., listing a forge, loading new stuff from a Git repository) and one-off activities (e.g., loading a specific version of a source package). +# Tests + +Unit tests may require a running celery broker on your system (rabitmq by +default). You can set the `BROKER_URL` environment variable to specify the url +to be used. + +If you do not want to use your system's broker (or do not want to have one +running), you shold use [[ https://github.com/jd/pifpaf | pifpaf ]] to take +care of that for you. + + +For example: + +``` +$ pifpaf --env-prefix PG run postgresql -- \ + pifpaf --env-prefix AMQP run rabbitmq nosetests + +..................................... +---------------------------------------------------------------------- +Ran 37 tests in 15.578s + +OK +``` diff --git a/swh/scheduler/tests/celery_testing.py b/swh/scheduler/tests/celery_testing.py new file mode 100644 index 0000000..5f0ab61 --- /dev/null +++ b/swh/scheduler/tests/celery_testing.py @@ -0,0 +1,37 @@ +import os +import urllib + +from celery import current_app + + +CELERY_BROKER_PROTOCOLS = ['amqp', 'redis', 'sqs'] + + +def setup_celery(): + broker_url = None + if 'BROKER_URL' in os.environ: + broker_url = os.environ['BROKER_URL'] + + elif 'PIFPAF_URLS' in os.environ: + urls = os.environ['PIFPAF_URLS'].split(';') + urls = [x.replace('rabbit://', 'amqp://') for x in urls] + for url in urls: + scheme = urllib.parse.splittype(url)[0] + if scheme in CELERY_BROKER_PROTOCOLS: + broker_url = url + break + if broker_url: + current_app.conf.broker_url = broker_url + + +class CeleryTestFixture: + """Mix this in a test subject class to get Celery testing support configured + via environment variables, typically set up by pifpaf. + + It expect a connection url to a celery broker either as a BROKER_URL or a + url listed in a PIFPAF_URL environment variable. + """ + + def setUp(sel): + setup_celery() + super().setUp() diff --git a/swh/scheduler/tests/test_task.py b/swh/scheduler/tests/test_task.py index 3d957ba..10131e9 100644 --- a/swh/scheduler/tests/test_task.py +++ b/swh/scheduler/tests/test_task.py @@ -1,31 +1,32 @@ # Copyright (C) 2015 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import unittest from nose.tools import istest from swh.scheduler import task +from .celery_testing import CeleryTestFixture -class Task(unittest.TestCase): +class Task(CeleryTestFixture, unittest.TestCase): @istest def not_implemented_task(self): class NotImplementedTask(task.Task): pass with self.assertRaises(NotImplementedError): NotImplementedTask().run() @istest def add_task(self): class AddTask(task.Task): def run_task(self, x, y): return x + y r = AddTask().apply([2, 3]) self.assertTrue(r.successful()) self.assertEqual(r.result, 5)