Image for post
Image for post

Integration Testing with ServerSpec

For change automation, integration testing is essential both verifying functionality and testing future changes that could affect a production systems. These changes can be either adding new features, changes the change platform, like Puppet to Chef or Chef to Docker as examples, or other components, e.g. unicorn to puma or passenger, Apache to Nginx, CentOS to Ubuntu, etc. that could affect the service.

One popular tool for this is ServerSpec, a framework that uses RSpec test framework for checking your servers are configured correctly. ServerSpec can execute tests locally, through SSH, WinRM, or using Docker API.

This is not required, but if you have Vagrant and Virtualbox installed, ServerSpec has convenient integration for this.

Starting in a test directory, such as your vagrant directory with a server running, and Ruby previously installed, you can run the following to get started:

gem install serverspec
serverspec-init

If you selected SSH and Vagrant as your environment, this will create similar to the following where $VBOX_HOSTNAME matches name of your vagrant system. Usually, unless you specified this, it will be set to the name ‘default’:

.
├── Rakefile
└─── spec
├── $VBOX_HOSTNAME
│ └── sample_spec.rb
├── README.md
└── spec_helper.rb

As a simple small example, here’s a test that checks for a user and group membership of the user vagrant.

If you have rake gem installed you simply run:

rake spec

Otherwise, if you do not have rake installed, or want to run tests on a single system out of many, you can type:

TARGET_HOST=$VBOX_HOSTNAME rspec

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

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