Integration tests with Postgres

Launching a PostgreSQL service container

In this example, we will see a NodeJS project that is using PostgreSQL for data storage. For the integration test phase we will launch an instance of PostgreSQL in order to run a simple integration test.

PostgreSQL integration tests with Codefresh

PostgreSQL integration tests with Codefresh

The integration tests look for a PostgreSQL connection at postgres:5432.

Example NodeJS project

You can see the example project at https://github.com/codefreshdemo/example_nodejs_postgres. The repository contains the NodeJS source code and the simple integration test.

You can play with it locally by using Docker compose to launch both the application and the PostgreSQL Database.

Create a pipeline with PostgreSQL integration tests

Here is the whole pipeline:

codefresh.yml

version: "1.0"
stages:
  - prepare
  - build
  - test
steps:
  main_clone:
    type: "git-clone"
    description: "Cloning main repository..."
    repo: "codefreshdemo/example_nodejs_postgres"
    revision: "master"
    git: github
    stage: prepare
  run_integration_tests:
    title: "Running integration tests"
    stage: test
    image: node:6.9.1
    environment: &test_postgresql_vars
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=admin
      - POSTGRES_DB=todo
    commands:
      # PostgreSQL is certainly up at this point
         - npm install -g gulp
         - npm install
         - npm test
    services:
      composition:
        postgres:
          image: postgres:11.5
          ports:
            - 5432
          environment: *test_postgresql_vars # Same POSTGRES_USER, POSTGRES_PASSWORD etc.
      readiness:
        timeoutSeconds: 30
        periodSeconds: 15
        image: postgres:11.5
        commands:
          - "pg_isready -h postgres"   

This pipeline does the following:

  1. Clones the source code through a Git clone step.
  2. Runs the tests while launching a service container for an active PostgreSQL instance passing the required environment variables (that match what the test is expecting).

Notice that both the DB as well as the tests share a set of variables (POSTGRES_USER, POSTGRES_PASSWORD etc.) and thus we use YAML anchors to avoid duplication.

Notice that we also use the readiness property in the testing phase so that we can verify PostgreSQL is ready and listening, before running the tests.

CI pipeline examples
Integration test example
Integration tests with MySQL
Integration tests with Redis
Integration tests with Mongo
Preload a DB with test data