Building for Beanstalk – Part 2 of 3

Make all the Things – Part 2 of 3 (Testing)

In the first part of our three-part series, we talked about setting up a project and building it with Make. This part will cover testing using our Makefile. The goal will be to deploy our project into Vagrant then execute our test suite against it. Here is part 1 and part 3!

We have a lot of stuff in here so let’s go through it piece by piece. First, the docker target has a dependency of check-docker. Before we execute the docker command, we need to make sure it exists and if it doesn’t, fail gracefully with a message that makes sense. Next, we start moving the pieces we need into the build/docker directory which will be our Docker context. It is important that the Docker context is as small as possible to make building Docker images fast. If you don’t create an optimized context, you could have a Docker context that is easily 200MB to multiple Gigabytes! A typical Docker context for us is about 40–80MB.

The last two lines of the docker target actually build the image and notify the developer that the image is built. Let’s examine the second to last line.

@cd build/docker && docker build -t killerapp .

Each step in a Makefile is executed independently of other steps. Therefore, if we have to change directories, it needs to be done inline in the step. This may seem like a quirk of Make, but I think it makes complete sense and actually prefer it this way. Now let’s run the docker image!

Now we have a running Killerapp container in Docker, let’s execute some tests!

Make Test

If you’re like most of us, you want your integration tests to be easy to understand and reflect the features of your application based on what your client specified. Enter Cucumber, the de facto standard in integration testing. Yes, I know, its not Java or Javascript, which we’ve been using so far. But then again, you know Cucumber is easy to learn and use, and it is the best-of-breed tool you want to use to test against your application. You won’t believe how easy this is!

That’s it! Now we have integration tests running against a Docker image built from our project that’s running in Vagrant when we run make test-latest. In your Cucumber configuration, you just need to make sure you’re pointing it at your Vagrant VM’s IP address and it’ll work. Also, if you change a test and want to re-run only your tests, just run make test.

A Note about Vagrant

You probably noticed we have no vagrant commands. This is because we’re assuming that Vagrant is running already with a box that accepts Docker commands and all that is set up correctly. If you want to know more about a Vagrant box to do that, check out Running CoreOS on Vagrant. It’s what we use and it just works. Now, the Makefile we’ve been creating so far does not have to use Vagrant for Docker; you can host your Docker stuff somewhere else, just make sure your Cucumber configs point to the right place and your tests will work.

Next up, Deploying!

In part 2, we discussed how to use a Makefile to integrate Docker and Cucumber for testing. In part 3, we will finally deploy to AWS Elastic Beanstalk.

PS, our current Makefile

If you’ve been following along, you probably want to see the Makefile we’ve been working on. Here it is.