Seamless Integration Testing for Your Go Application on GitHub Actions with PostgreSQL
sean
Posted on November 22, 2024
Introduction
Integration testing is crucial for ensuring your Go application works flawlessly with external dependencies like databases. In this blog, we’ll explore how to set up and run integration tests for a Go application using GitHub Actions. We’ll configure a PostgreSQL database within the CI pipeline, streamline the test process, and ensure your codebase is reliable and production-ready with every push. Let’s dive in!.
We created unit test and integrations in a previous article here!. In this article we want to run these tests on all commits to our github repository.
Github Actions
They are a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test and deployment pipeline.
Github Actions lets you run workflows when other events happen in your repository
Github Workflows
A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked in to your repository and will run when triggered by an event in your repository. Workflows are defined in the .github/workfows
.
- Event is a specific activity in a repository that triggers a workflow run. In our case this will be a push to our branch.
- Jobs is a set of steps in a workflow that is executed on the same runner.
- Runners is a server that runs your workflows when they are triggered. Each runner can runner a single job at a time.
Workflow Yaml
- The first step would be to create
.github/workflows
folder where our yaml file will be located. - Next is to create the yaml file in this case we will name it
ci-test.yml
.
name: ci-test
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: Password123
POSTGRES_DB: crud_db
jobs:
build:
name: tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_USER: ${{ env.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
POSTGRES_DB: ${{ env.POSTGRES_DB }}
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.22"
- name: Install dbmate for golang migrations
run: |
sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
sudo chmod +x /usr/local/bin/dbmate
which dbmate
- name: Construct DB URL
id: construct_url
run: echo "DB_URL=postgres://${{ env.POSTGRES_USER }}:${{ env.POSTGRES_PASSWORD }}@localhost:5432/${{ env.POSTGRES_DB }}?sslmode=disable" >> $GITHUB_ENV
- run: env
- name: Make Migrations
run: make migrations URL=${{ env.DB_URL }}
- name: Seed test DB
run: go run db/seed.go
- name: Test
run: make test
Yaml Description
- The first part is to name the action in this case it is ci-test.
Workflow Triggers
- The second section describes triggers. Events that trigger the action. In this file we have two events that will trigger the running of this jobs, pushes and pull requests targeting the
main
branches. This ensures that every code change intended for production is tested before merged, maintaining the integrity of the project.
Environment Variables
Github workflows support global and job-specific environment variables. This variables describe postgres credentials that we will user later in our yaml file.
Job
jobs:
build:
name: tests
runs-on: ubuntu-latest
Here we have assigned a name to the job that will perform the core tasks, which are building and testing our code.
Runner - describes where the workflow will run which will is a Ubuntu viritual machine.
Services
Github Actions workflows allow you to define services. In this case we need a postgres database to run our tests against.
- A PostgreSQL container is created using the official PostgreSQL Docker image.
- The container is configured with environment variables we declared earlier
Workflow Steps
- First step is to checkout the repository code
- uses: actions/checkout@v4
This line fetches the latest version of the repository, providing access to all source files.
- Second step is to setup golang in the runner.
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.22"
- The third step is installing dbmate on our runner. Dbmate is a migration tools that will manage application migrations.
- name: Install dbmate for golang migrations
run: |
sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
sudo chmod +x /usr/local/bin/dbmate
which dbmate
- Fourth is to construct the db url
- name: Construct DB URL
id: construct_url
run: echo "DB_URL=postgres://${{ env.POSTGRES_USER }}:${{ env.POSTGRES_PASSWORD }}@localhost:5432/${{ env.POSTGRES_DB }}?sslmode=disable" >> $GITHUB_ENV
- Fifth is runnig db migrations to setup our relations that will seed with date
- name: Make Migrations
run: make migrations URL=${{ env.DB_URL }}
- The second last action is to seed the database with test data.
- name: Seed test DB
run: go run db/seed.go
The seed.go
file seeds the data ase with test data. Setting up a realistic test environment. To inspect this file further, visit here
The final stage is to execute our go test using the make file
- name: Test
run: make test
This workflow will now run each time we make a pull request or push code to our main branch
Some Advantages of Adopting Github Action.
As we have seen github action allow you to do
- Automated Testing - run tests consistently on every code change.
- Have Database Integretions - provide a real postgres environment for testing, simulating production conditions
- Reproducible Environment - Github action use containerized services and predefined steps to ensure consistent results across all runs.
- Fast Feedback loop - They enable developers to receive quick feedback if something breaks, allowing for faster issue resolution.
- Simplified Collaboration - They ensure all contributors' changes are verified before emerging, maintaining code quality and project stability
Conclusions
By leveraging GitHub Actions, this workflow streamlines testing and database setup, ensuring robust and reliable software development.
Visit the github repository to view the code that is being tested with the action described above.
Posted on November 22, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 22, 2024