Hudson Calling Selenium Tests

November 24, 2010

I wanted to have Hudson pick a Django project from Mercurial, spin it up using Gunicorn, and then run some selenium tests against it. This is on a Debian server, the same server as the Mercurial repository is located on.

I already had my code coming out of Mercurial to a local workspace in Hudson, but that was all that was happening - no build was being done at all. (I suggest you start with this first!)

To get the build to work, I added the following as the values to execute shell in the Hudson project's build section:

export PYTHONPATH=$PYTHONPATH:${WORKSPACE}/bbr
cd ${WORKSPACE}/bbr/bbr
python manage.py syncdb --settings=settingsstaging
python manage.py migrate --settings=settingsstaging
gunicorn_django -b localhost:8003 -p /tmp/gunicorn-bbr-pid settingsstaging.py &
cd ../selenium/bbr
python bbr.py
kill -QUIT `cat /tmp/gunicorn-bbr-pid`

I think this could probably be simplified, and would be better as a version controlled script in the source, but this at least works.

The script runs the following commands:

First, we add the project in the Hudson workspace onto the PYTHONPATH. Here I have the Hudson project configured to check out into a subdirectory called bbr.

export PYTHONPATH=$PYTHONPATH:${WORKSPACE}/bbr

Next, we change directory to where the settings files are

cd ${WORKSPACE}/bbr/bbr

Then we run a syncdb to add any new database tables

python manage.py syncdb --settings=settingsstaging

Next we run a migrate to pick up any South database shape changes

python manage.py migrate --settings=settingsstaging

Then we spin up gunicorn on port 8003, writing the pid to a file in /tmp and using a special staging settings file.

gunicorn_django -b localhost:8003 -p /tmp/gunicorn-bbr-pid settingsstaging.py &

Then we change directory to where the selenium tests are stored in version control and execute them.

cd ../selenium/bbr
python bbr.py

Finally, we kill off the gunicorn server.

kill -QUIT `cat /tmp/gunicorn-bbr-pid`

This relies on the Django project being able to work out where it is installed, see http://drumcoder.co.uk/blog/2010/nov/24/dynamic-paths-django/.

I'm also using nginx as the web server in front of Gunicorn. This is already configured and running to proxy requests to bbr.apophis through to localhost:8000. To pick up the latest site_media/media files it will need to be set up so that /site_media and /media pass through to files in the Hudson workspace.