Test suite instructions from @dsimunic

This commit is contained in:
Joe Nelson
2016-11-30 21:21:57 -08:00
parent 5b95d22e2a
commit 8c2a95b98e
+122 -21
View File
@@ -14,6 +14,39 @@ The `release page <https://github.com/begriffs/postgrest/releases/latest>`_ has
# You should see a usage help message
Homebrew
========
You can use the Homebrew package manager to install PostgREST on Mac
.. code-block:: bash
# Ensure brew is up to date
brew update
# Check for any problems with brew's setup
brew doctor
# Install the postgrest package
brew install postgrest
This will automatically install PostgreSQL as a dependency. The process tends to take up to 15 minutes to install the package and its dependencies.
After installation completes, the tool is added to your $PATH and can be used from anywhere with:
.. code-block:: bash
postgrest --help
PostgreSQL dependency
=====================
To use PostgREST you will need an underlying database (PostgreSQL version 9.3 or greater is required). You can use something like Amazon `RDS <https://aws.amazon.com/rds/>`_ but installing your own locally is cheaper and more convenient for development.
* `Instructions for OS X <http://exponential.io/blog/2015/02/21/install-postgresql-on-mac-os-x-via-brew/>`_
* `Instructions for Ubuntu 14.04 <https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04>`_
* `Installer for Windows <http://www.enterprisedb.com/products-services-training/pgdownload#windows>`_
Build from Source
=================
@@ -45,38 +78,106 @@ When a pre-built binary does not exist for your system you can build the project
* Check that the server is installed: :code:`postgrest --help`.
If you want to run the test suite, stack can do that too: :code:`stack test`.
PostgREST Test Suite
--------------------
PostgreSQL dependency
=====================
Creating the Test Database
~~~~~~~~~~~~~~~~~~~~~~~~~~
To use PostgREST you will need an underlying database (PostgreSQL version 9.3 or greater is required). You can use something like Amazon `RDS <https://aws.amazon.com/rds/>`_ but installing your own locally is cheaper and more convenient for development.
To properly run postgrest tests one needs to create a database. To do so, use the test creation script `create_test_database` in the `test/` folder.
* `Instructions for OS X <http://exponential.io/blog/2015/02/21/install-postgresql-on-mac-os-x-via-brew/>`_
* `Instructions for Ubuntu 14.04 <https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04>`_
* `Installer for Windows <http://www.enterprisedb.com/products-services-training/pgdownload#windows>`_
The script expects the following parameters:
Homebrew
========
.. code:: bash
You can use the Homebrew package manager to install PostgREST on Mac
test/create_test_db connection_uri database_name [test_db_user] [test_db_user_password]
.. code-block:: bash
Use the `connection URI <https://www.postgresql.org/docs/current/static/libpq-connect.html#AEN45347>`_ to specify the user, password, host, and port. Do not provide the database in the connection URI. The Postgres role you are using to connect must be capable of creating new databases.
# Ensure brew is up to date
brew update
The `database_name` is the name of the database that `stack test` will connect to. If the database of the same name already exists on the server, the script will first drop it and then re-create it.
# Check for any problems with brew's setup
brew doctor
Optionally, specify the database user `stack test` will use. The user will be given necessary permissions to reset the database after every test run.
# Install the postgrest package
brew install postgrest
If the user is not specified, the script will generate the role name `postgrest_test_` suffixed by the chosen database name, and will generate a random password for it.
This will automatically install PostgreSQL as a dependency. The process tends to take up to 15 minutes to install the package and its dependencies.
Optionally, if specifying an existing user to be used for the test connection, one can specify the password the user has.
After installation completes, the tool is added to your $PATH and can be used from anywhere with:
The script will return the db uri to use in the tests--this uri corresponds to the `db-uri` parameter in the configuration file that one would use in production.
.. code-block:: bash
Generating the user and the password allows one to create the database and run the tests against any postgres server without any modifications to the server. (Such as allowing accounts without a passoword or setting up trust authentication, or requiring the server to be on the same localhost the tests are run from).
postgrest --help
Running the Tests
~~~~~~~~~~~~~~~~~
To run the tests, one must supply the database uri in the environment variable `POSTGREST_TEST_CONNECTION`.
Typically, one would create the database and run the test in the same command line, using the `postgres` superuser:
.. code:: bash
POSTGREST_TEST_CONNECTION=$(test/create_test_db "postgres://postgres:pwd@database-host" test_db) stack test
For repeated runs on the same database, one should export the connection variable:
.. code:: bash
export POSTGREST_TEST_CONNECTION=$(test/create_test_db "postgres://postgres:pwd@database-host" test_db)
stack test
stack test
...
If the environment variable is empty or not specified, then the test runner will default to connection uri
.. code:: bash
postgres://postgrest_test@localhost/postgrest_test
This connection assumes the test server on the `localhost` with the user `postgrest_test` without the password and the database of the same name.
Destroying the Database
~~~~~~~~~~~~~~~~~~~~~~~
The test database will remain after the test, together with four new roles created on the postgres server. To permanently erase the created database and the roles, run the script `test/delete_test_database`, using the same superuser role used for creating the database:
.. code:: bash
test/destroy_test_db connection_uri database_name
Testing with Docker
~~~~~~~~~~~~~~~~~~~
The ability to connect to non-local PostgreSQL simplifies the test setup. One elegant way of testing is to use a disposable PostgreSQL in docker.
For example, if local development is on a mac with Docker for Mac installed:
.. code:: bash
$ docker run --name db-scripting-test -e POSTGRES_PASSWORD=pwd -p 5434:5432 -d postgres
$ POSTGREST_TEST_CONNECTION=$(test/create_test_db "postgres://postgres:pwd@localhost:5434" test_db) stack test
Additionally, if one creates a docker container to run stack test (this is necessary on MacOS Sierra with GHC below 8.0.1, where `stack test` fails), one can run PostgreSQL in a separate linked container, or use the locally installed Postgres.app.
Build the test container with `test/Dockerfile.test`:
.. code:: bash
$ docker build -t pgst-test - < text/Dockerfile.test
$ mkdir .stack-work-docker ~/.stack-linux
The first run of the test container will take a long time while the dependencies get cached. Creating the `~/.stack-linux` folder and mapping it as a volume into the container ensures that we can run the container in disposable mode and not worry about subsequent runs being slow. `.stack-work-docker` is also mapped into the container and must be specified when using stack from Linux, not to interfere with the `.stack-work` for local development. (On Sierra, `stack build` works, while `stack test` fails with GHC 8.0.1).
Linked containers:
.. code:: bash
$ docker run --name pg -e POSTGRES_PASSWORD=pwd -d postgres
$ docker run --rm -it -v `pwd`:`pwd` -v ~/.stack-linux:/root/.stack --link pg:pg -w="`pwd`" -v `pwd`/.stack-work-docker:`pwd`/.stack-work pgst-test bash -c "POSTGREST_TEST_CONNECTION=$(test/create_test_db "postgres://postgres:pwd@pg" test_db) stack test"
Stack test in Docker for Mac, Postgres.app on mac:
.. code:: bash
$ host_ip=$(ifconfig en0 | grep 'inet ' | cut -f 2 -d' ')
$ export POSTGREST_TEST_CONNECTION=$(test/create_test_db "postgres://postgres@$HOST" test_db)
$ docker run --rm -it -v `pwd`:`pwd` -v ~/.stack-linux:/root/.stack -v `pwd`/.stack-work-docker:`pwd`/.stack-work -e "HOST=$host_ip" -e "POSTGREST_TEST_CONNECTION=$POSTGREST_TEST_CONNECTION" -w="`pwd`" pgst-test bash -c "stack test"
$ test/destroy_test_db "postgres://postgres@localhost" test_db