Populate a database with existing data
Preloading test data before integration tests
In an another example we have seen how you can 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 db.
In this pipeline the database is populated with data from an SQL file.
The 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 db:
To work with the project locally, you need to have
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:
This pipeline does the following:
- Clones the source code with a Git clone step.
- Compiles the code with a freestyle step that runs
- Runs the tests while launching a service container for an active PostgreSQL instance. Before tests are run we launch another container with the
psqlexecutable to load db data
In this simple example we use
psqlto 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.5at port 5432.
- It then launches another container in the same network with
pg_isreadyin order to wait for the DB to be up.
- Then it launches a third container with
psqlto preload data.
- Finally it launches a container with
golang:1.13to run the actual tests.
All containers are discarded after the pipeline has finished.