Populate database with existing data
Preload test data before integration tests
In another example, we saw how to run integration tests with a database such as PostgreSQL. Sometimes however, the integration tests require the database to already have some test data beforehand. With Codefresh you can use the setup block in service containers to preload data to a database.
In this pipeline the database is populated with data from an SQL file.
Example PostgreSQL project
You can see the example project at https://github.com/codefresh-contrib/preload-db-integration-tests. The repository contains a simple integration test and an SQL file that inserts test data.
The SQL file creates a single table in the database:
preload.sql
To work with the project locally, you need to have docker
, golang
and postgres-client
installed on your workstation first.
$ docker run -p 5432:5432 postgres:11.5
Then open another terminal and load the test data:
$ psql -h localhost -U postgres < testdata/preload.sql
A Postgres instance is now running at localhost:5432
and you can run the tests with:
$ go test -v
Create a pipeline the preloads test data to PostgreSQL
Here is the whole pipeline:
codefresh.yml
This pipeline does the following:
- Clones the source code through a Git clone step.
- Compiles the code that runs
go build
through a freestyle step. - Runs the tests while launching a service container for an active PostgreSQL instance. Before tests are run, we launch another container with the
psql
executable to load database data.
TIP
In this simple example, we use psql
to preload the database. In a production application you might also use dedicated db tools such as liquibase or flyway or other command line tools that communicate with your database.
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. The exact order of events is:
- Codefresh launches
postgres:11.5
at port 5432. - It then launches another container in the same network with
pg_isready
in order to wait for the DB to be up. - Then it launches a third container with
psql
to preload data. - Finally, it launches a container with
golang:1.13
to run the actual tests.
All containers are discarded after the pipeline has finished.
Related articles
CI pipeline examples
Integration test example
Integration tests with Postgres
Integration tests with MySQL
Integration tests with Mongo
Integration tests with Redis