testing docker images with bats

The Bash Automated Testing System (Bats) is a testing framework for Bash I came across recently, in use by the amazing Apline Linux Docker images.

It provides a simple way to verify that the UNIX programs you write behave as expected.

A Bats test file is a Bash script with special syntax for defining test cases. Under the hood, each test case is just a function with a description.

We've started using it to test our Docker images too, see the graze/composer image for an open-source example.

Here's what a typical test looks like:

@test "composer version is correct" {
  run docker run --rm graze/composer:php-7.0 --version --no-ansi
  echo 'status:' $status
  echo 'output:' $output
  version="$(echo $output | awk '{ print $3 }')"
  echo 'version:' $version
  [ "$status" -eq 0 ]
  [ "$version" = "1.0-dev" ]
}

It means we can now apply TDD when making any changes, helping to protect the continuous delivery pipeline.

Going beyond Bats, it looks like RSpec and ServerSpec make for a create combination for testing your images too, with a neat DSL.

Here at graze, we're always looking to hire smart people to join our tech team. If you love solving the types of technical challenges we face, send your CV to jobs@graze.com.

comments powered by Disqus