Serverspec Workshop


serverspec

Now that we know a little about rspec, lets jump right into serverspec.

Installation

Installing serverspec is easy when you have rubygems installed:

NOTE: you may need to use sudo for installation of gems

gem install --no-ri --no-rdoc serverspec rake

Project Setup

Serverspec bundles in a nice utility to generate a few files to help run serverspecs. We should run this to get us started.

$ serverspec-init
Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 2

 + spec/
 + spec/localhost/
 + spec/localhost/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + .rspec

We chose 2) Exec (local) for the backend type to make running these examples easier. We will cover this in more detail later.

We are going to write our specs from scratch, so remove sample_spec.rb:

$ rm spec/localhost/sample_spec.rb

Let's add some serverspec to a file called spec/localhost/example_spec.rb

require 'spec_helper'

set :os, :family => 'ubuntu'

describe package('python') do
  it { should be_installed }
end

describe service('cron') do
  it { should be_enabled.and be_running }
end

describe command('uname -a') do
  its(:stdout) {should match /Linux/ }
end

And to run the server specs:

$ rake spec

For more resource_types click here

Exercises