diff --git a/.gitignore b/.gitignore --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ dist/ version.txt /.hypothesis/ +.tox diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -8,3 +8,63 @@ activities (e.g., loading a specific version of a source package). +# Tests + +## Running test manually + +### Test data + +To be able to run (unit) tests, you need to have the +[[https://forge.softwareheritage.org/source/swh-storage-testdata.git|swh-storage-testdata]] +in the parent directory. If you have set your environment following the +[[ https://docs.softwareheritage.org/devel/getting-started.html#getting-started|Getting started]] +document everythong should be set up just fine. + +Otherwise: + +``` +~/.../swh-scheduler$ git clone https://forge.softwareheritage.org/source/swh-storage-testdata.git ../swh-storage-testdata +``` + +### Required services + +Unit tests may require a running celery broker on your system (rabbitmq 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 +``` + +## Using tox + +You may use the tox file, which does this for you: + +``` +$ tox +GLOB sdist-make: /home/ddouard/src/swh-environment/swh-scheduler/setup.py +py3 inst-nodeps: /home/ddouard/src/swh-environment/swh-scheduler/.tox/dist/swh.scheduler-0.0.31.post18.zip +py3 installed: aiohttp==3.4.4,amqp==2.3.2,arrow==0.12.1,async-timeout==3.0.1,attrs==18.2.0,billiard==3.5.0.4,celery==4.2.1,certifi==2018.8.24,cffi==1.11.5,chardet==3.0.4,Click==7.0,coverage==4.5.1,daiquiri==1.5.0,extras==1.0.0,fixtures==3.0.0,Flask==1.0.2,hypothesis==3.76.0,idna==2.7,idna-ssl==1.1.0,itsdangerous==0.24,Jinja2==2.10,kombu==4.2.1,linecache2==1.0.0,MarkupSafe==1.0,msgpack-python==0.5.6,multidict==4.4.2,nose==1.3.7,pbr==4.3.0,pifpaf==2.1.2,psutil==5.4.7,psycopg2==2.7.5,pycparser==2.19,python-dateutil==2.7.3,python-mimeparse==1.6.0,pytz==2018.5,PyYAML==3.13,requests==2.19.1,six==1.11.0,swh.core==0.0.41.dev13+g509b612.d20181011,swh.scheduler==0.0.31.post18,systemd-python==234,testtools==2.3.0,traceback2==1.4.0,unittest2==1.1.0,urllib3==1.23,vcversioner==2.16.0.0,vine==1.1.4,Werkzeug==0.14.1,xattr==0.9.6,yarl==1.2.6 +py3 run-test-pre: PYTHONHASHSEED='2607285344' +py3 runtests: commands[0] | pifpaf run postgresql -- pifpaf run rabbitmq nosetests -- +..................................... +---------------------------------------------------------------------- +Ran 37 tests in 14.536s + +OK + +``` diff --git a/requirements-test.txt b/requirements-test.txt --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,2 +1,3 @@ hypothesis nose +celery diff --git a/swh/scheduler/tests/celery_testing.py b/swh/scheduler/tests/celery_testing.py new file mode 100644 --- /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 --- a/swh/scheduler/tests/test_task.py +++ b/swh/scheduler/tests/test_task.py @@ -8,9 +8,10 @@ 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): diff --git a/tox.ini b/tox.ini new file mode 100644 --- /dev/null +++ b/tox.ini @@ -0,0 +1,15 @@ +[tox] +envlist=py3 + +[testenv:py3] +deps = + -r requirements-test.txt + pifpaf + coverage +whitelist_commands = + /bin/sh +commands = + /bin/sh -c 'if [ ! -d ../swh-storage-testdata ]; then \ + git clone https://forge.softwareheritage.org/source/swh-storage-testdata.git ../swh-storage-testdata; fi' + pifpaf run postgresql -- \ + pifpaf run rabbitmq nosetests -- {posargs}