News

Welcome to End Point’s blog

Ongoing observations by End Point people

Test::Database Postgres support

At our recent company meeting, we organized a 'hackathon' at which the company was split into small groups to work on specific projects. My group was Postgres-focused and we chose to add Postgres support to the new Perl module Test::Database.

This turned out to be a decent sized task for the few hours we had to accomplish it. The team consisted of myself (Greg Sabino Mullane), Mark Johnson, Selena Deckelmann, and Josh Tolley. While I undertook the task of downloading the latest version and putting it into a local git repository, others were assigned to get an overview of how it worked, examine the API, and start writing some unit tests.

In a nutshell, the Test::Database module allows an easy interface to creating and destroying test databases. This can be a non-trivial task on some systems, so putting it all into a module make sense (as well as the benefits of preventing everyone from reinventing this particular wheel). Once we had a basic understanding of how it worked, we were off.

While all of our tasks overlapped to some degree, we managed to get the job done without too much trouble, and in a fairly efficient manner. We made a new file for Postgres, added in all the required API methods, wrote tests for each one, and documented everything as we went along. The basic method to create a test database is to use the initdb program to create a new Postgres cluster, then modify the cluster to use a local Unix socket in the newly created directory (this side-stepping completely the problem of using an already occupied port). Then we can start up the new cluster via the pg_ctl command, and create a new database.

At the end of the day, we had a working module that passed all of its tests. We combined our git patches into a single one mailed it to the author of the module, so hopefully you'll soon see a new version of Test::Database with Postgres support!

1 comment:

Grzegorz J said...

I have a bash script that does exactly that, but because I need to test remote connections too (and it is easy just to scp from script and than ssh run) - they all connect using tcp/ip.

in essence it looks like that:

#!/bin/bash

TEMP_DIR="/var/tmp/"

if [ $(($#)) -ne 2 ]; then
echo "params: port and data directory name"
exit 1;
fi

if [ $(($1)) -lt 1024 ]; then
echo "port value needs to be between 1024 and 65536. not $(($1))"
exit 1;
fi

export PGPORT=$1
export PGDATA=$TEMP_DIR/database$1

echo "attempting to create database instance $2"

initdb -A trust > /dev/null

if [ $? != 0 ]; then
echo "problems with initdatabase"
exit 1;
fi

echo "starting instance with database \"$2:$PGPORT\""

#enable listening on ethernet interfaces

echo "listen_addresses='*'" >> $PGDATA/postgresql.conf

pg_ctl -w start >/dev/null
if [ $? != 0 ]; then
echo "problems starting up temporary database $2:$PGPORT"
rm -rf $PGDATA
exit 1;
fi

(altho it is a part of much bigger module, and tescase).