Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Simplify use of Docker to run postgres for tests #49

Merged
merged 6 commits into from Oct 5, 2016

Conversation

nexdrew
Copy link
Contributor

@nexdrew nexdrew commented Oct 5, 2016

Ran into a problem with PostGIS again when recently trying to use docker-compose to stand up a postgres container for running tests locally.

E: Version '2.2.2+dfsg-1.pgdg80+1' for 'postgresql-9.4-postgis-2.2' was not found
E: Version '2.2.2+dfsg-1.pgdg80+1' for 'postgis' was not found
ERROR: Service 'postgres' failed to build: The command '/bin/sh -c apt-get update       && apt-get install -y --no-install-recommends            postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR=$POSTGIS_VERSION            postgis=$POSTGIS_VERSION       && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100

This got me interested in finding the simplest possible setup for a postgres container. Since oauth2-server-pg actually doesn't need PostGIS, I was able to completely replace the "docker-compose with custom image" approach with this simple docker run command:

docker run --name oauth2serverpg_postgres_1 -e POSTGRES_DB=oauth2_server -p 5432:5432/tcp -d postgres:9.4 postgres

The standard postgres image will use the POSTGRES_DB env var for the default database, so no need to create the oauth2_server db on/after startup.

So I created an npm run script for this and wrapped it in a conditional pretest hook, which automates the setup and tear down of the postgres docker container locally, but doesn't get in the way when run by Travis. This means all you need to run tests locally is to have docker installed and then run npm t, and it will use a throw-away container for postgres for the duration of the tests.

Also added these handy scripts for convenience:

  • npm run pg-test: start up the dockerized postgres db and run migrations
  • npm run psql: login to the running postgres container to run some manual queries
  • npm run pg-test-down: destroy the running postgres container

@coveralls
Copy link

Coverage Status

Coverage remained the same at 89.583% when pulling be1d5bf on nexdrew/simplify-docker into d6ee89c on master.

@bcoe
Copy link
Contributor

bcoe commented Oct 5, 2016

:shipit: I'm comfortable with giving inlining the docker logic a shot.

@nexdrew nexdrew merged commit d355481 into master Oct 5, 2016
@nexdrew nexdrew deleted the nexdrew/simplify-docker branch October 5, 2016 21:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants