Go: Docker for Golang application tests

Kanan Rahimov
Nov 3, 2019 · 3 min read
Image for post
Image for post
Golang’s Gopher with Docker’s Whale :)

Docker widely used in the development environment, and one of the usages is testing.

This article will focus on two different usages of Docker for Golang tests:

1. Use Docker image to test and build your application

2. Use Go package to set up and run Docker images programmatically

During this article, we will use MongoCLI open-source repository as an example. Consider checking it out.

https://github.com/KenanBek/mongocli

Docker image for your application.

Having a Docker image for your application is quite straightforward and commonly used approach. It gives you isolation from your local machine and can be run anywhere where you have Docker installed. We can apply the same technics to any other programming language. Also, we can have one image for the back-end and another for the front-end.

Here is an example Docker image (also taken from mongocli):

FROM golang:latest
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go test -v ./...
RUN go build -o mongocli mongocli.go
RUN touch ~/mongocli.yml
RUN echo -e "Server: db\nPort: 27017" > ~/mongocli.yml
RUN touch mongocli.yml
ENV PATH="/app:${PATH}"

Here we use an official Go image as a base: golang:latest. Then we set-up our environment and run tests. Once all tests passed, we build our application and save it as the mongocli executable file.

We can build and run this image using the following command:

docker build -f Dockerfile -t mongocli:latest .
docker run mongocli:latest

Also, we can execute our mongocli application from within Docker container:

docker run mongocli:latest mongocli help

Go package dockertest to manage containers during the tests

For this approach, we will need an external package. There are a few of Go packages for this purpose. In this article, I will focus on dockertest:

Let’s start with a code:

Go and MongoDB integration tests using Dockertest

As you can see, we have TestMain as an entry point for our tests. In this function, we initialize an instance of dockertest’s Pool and try to spawn up MongoDB container.

If the initialization of the container went successful, we get the port number for MongoDB and create an instance of our tested object — mc (line 38) — and run test command — Ping (line 39).

Once we have successful preparation, we keep a global instance of the needed object (mc, line 13) and pass execution flow to a required test.

Summary

Both approaches work well. But there is one mention-worthy point. Theoretically, if with case one, you can have a bunch of tests and call them unit tests, however, in case two, it will be very questionable if you can call the same tests unit tests when you have an external dependency like, for example, MongoDB. Details of this statement are probably for the next article. Until then, happy coding!

Engineering Hub

The best way to learn something is to teach it.

Kanan Rahimov

Written by

Software Engineer. https://kenanbek.github.io/ for code snippets and periodic posts. Follow Twitter for updates.

Engineering Hub

The best way to learn something is to teach it. That’s why We, as a Team of Engineers, decided to establish Engineering Hub. Our goal is to share with our readers what We learn and to hone our skills. We are sure You will endeavor from this idea as much as We will.

Kanan Rahimov

Written by

Software Engineer. https://kenanbek.github.io/ for code snippets and periodic posts. Follow Twitter for updates.

Engineering Hub

The best way to learn something is to teach it. That’s why We, as a Team of Engineers, decided to establish Engineering Hub. Our goal is to share with our readers what We learn and to hone our skills. We are sure You will endeavor from this idea as much as We will.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store