Quick Tip: Serverspec spec_helper in Test Kitchen
Recently, I’ve started refactoring some old cookbooks I wrote ages ago. I’m adding Serverspec coverage that can be run with kitchen verify
. In this quicktip, I’ll describe how to create a spec_helper
that can be used in all the specs. This is a convention used by many in the Ruby community to add configuration for RSpec.
For Chef, we can run integration tests after convergence using Test Kitchen using Serverspec. To do that, we need to require Serverspec, and then set its backend. In some cookbooks, the author/developer may have written spec_helper
files in the various test/integration/SUITE/serverspec/
directories, but this will use a single shared file for them all. Let’s get started.
In the .kitchen.yml
, add the data_path
configuration directive in the provisioner.
provisioner:
name: chef_zero
data_path: test/shared
Then, create the test/shared
directory in the cookbook, and create the spec_helper.rb
in it.
mkdir test/shared
$EDITOR test/shared/spec_helper.rb
Minimally, it should look like this:
require 'serverspec'
set :backend, :exec
Then in your specs, for example test/integration/default/serverspec/default_spec.rb
, require the spec_helper
. On the instances under test, the file will be copied to /tmp/kitchen/data/spec_helper.rb
.
require_relative '../../../kitchen/data/spec_helper'
That’s it, now when running kitchen test
, or kitchen verify
on a converged instance, the helper will be used.