sexta-feira, 31 de março de 2017

Running tests with stubs containerized

We've been speaking about tests on other posts and I mentioned about using stubs to run tests with external dependencies.

A good approach is to use docker, I just did some examples of stub services and pushed the images to docker hub. To run my tests, I added a dependency on my docker-compose file for those images and use them to run my tests with docker compose.

It lead me to write this post and share this knowledge with you.

I've created a stub service with NodeJS (using hapi to startup a service and expose my routes), with this code in hands, we can start playing with docker, we'll do some simple steps to publish our image to docker hub and use it on our tests.

Register an account on docker hub

  • Visit Docker Hub and fill the form with dockerhubid, email and password
  • A confirmation email will be sent, and you just have to click a link to activate your account
  • On command line you can run docker login, it'll ask your credentials

Create your image

First of all, with our stub code, we need to create a local docker image, to be able to publish it later.

The first thing to do is to create a DockerFile, which looks like:

FROM node:6.0

RUN mkdir /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app
RUN npm install --dev

COPY . /usr/src/app


CMD npm start

Basically I'm using a node:6.0 image, copying my project and running some commands to install and expose my stub.

To create the image now, we need to execute this DockerFile, and it can be done easily with one command.

On the command line, do the steps below:
  • Navigate to the folder where your DockerFile is
  • Run docker build -t name .

It'll find a DockerFile on the current folder and will build the image with the name informed.

The command "docker build" has other options, in this case I just wanted to keep it simple.

Pushing my image to docker hub

Once I built my DockerFile, I need to push my image to docker hub, and for that we need two steps:

Tag the image

It's a very simple step, we need to tag our image id. This tagging process is to associate the image id you just generated with a name that will be pushed to docker hub.

docker tag imageId dockerHubId/imageName:version

Remember that dockerHubId must be the user logged in (your account).

Push the image

Once we tagged our image, we can now push it to docker hub.
The command to push is very simple.

docker push dockerHubId/imageName:version

Running tests with docker compose

Once we have our image published on docker hub, we can create the docker-compose.yml to run our tests using the stubs from the image just published.

In the example I'll show, it's a NodeJS project running tests with the command "npm test".

Here is the docker-compose.yml file with the dependency of a image with our stub.

  image: mcure/mystub:latest
    - "3002"

  build: .
  command: npm test
    EXTERNAL_URL: "http://mystub:3002"
    - mystub
My code has a dependency on a environment variable for referencing an external API, which I'm calling EXTERNAL_URL.

My project's config file uses this environment variable to set the URL of the external API.

When executing this docker-compose.yml, it'll build the container from the DockerFile of my project, and it will run the "command" specified replacing the environment variable set on the configuration.

For each test execution, it'll create a new container from that image, which means that my test can be ran without any external dependency, and I have now a test more isolated not depending on any real service.

With this post, I hope I helped you to understand a bit more about docker and the strategy of running tests with stubs.

Tweet me if you wanna discuss more about it.

Nenhum comentário:

Postar um comentário