Short summaries of some popular tools for test-driven infrastructure.

RSpec

Description

Testing tool for the Ruby programming language.

http://rspec.info

Typical Usage

  • Test-driven development for Ruby
  • Design through specification
  • Creation of mocks and stubs

Code Example

# cricket_spec.rb
require 'cricket'

describe Cricket, "#score" do
  it "returns 0 for maiden over" do
    cricket = Cricket.new
    6.times { cricket.run(0) }
    cricket.score.should eq(0)
  end
end

RSpec-Puppet

Description

RSpec test tool for Puppet manifests.

http://rspec-puppet.com

Typical Usage

  • RSpec tests for Puppet modules
  • Specification tests during module development

Code Example

require 'spec_helper'

describe 'logrotate::rule' do
  let(:title) { 'nginx' }

  it { should contain_class('logrotate::rule') }

  context 'with compress => true' do
    let(:params) { {:compress => true} }

    it do
      should contain_file('/etc/logrotate.d/nginx') \
        .with_content(/^\s*compress$/)
    end
  end
end

Beaker

Description

PuppetLabs acceptance testing tool.

https://github.com/puppetlabs/beaker

Typical Usage

  • Puppet acceptance testing
  • Virtual machines or containers can be provisioned for tests
  • Tests run on the server via SSH

Example Scenarios

  • Module should start a service
  • Module should add a file “foo”
  • Module should add a directory “bar”

Code Example

require 'spec_helper_acceptance'

describe 'themodule::custom_file define' do
  context 'invalid file' do
    it 'should not add the file' do
      pp = <<-EOS
        class { 'themodule': }
        themodule::custom_file { 'acceptance_test':
          content => 'IS_INVALID',
        }
      EOS

      apply_manifest(pp, :expect_failures => true)
    end

    describe file("#{$config_dir}/acceptance_test.conf") do
      it { is_expected.not_to be_file }
    end
  end
end

Infrataster

Description

Infrastructure behaviour testing framework

https://github.com/ryotarai/infrataster

Typical Usage

  • System behaviour testing
  • Test the external behaviour of a system
  • Tests run from an external harness

Example Scenarios

  • Valid web service response is returned
  • SQL statement returns the correct results

Code Example

require 'spec_helper'

describe server(:web) do
  describe http('http://web') do
    it "responds content including 'Hello World'" do
      expect(response.body).to include('Hello World')
    end
    it "responds OK 200" do
      expect(response.status).to eq(200)
    end
    it "responds as 'text/html'" do
      expect(response['content-type']).to eq('text/html')
    end
  end
end

ServerSpec

Description

RSpec test tool for checking servers are configured correctly.

http://serverspec.org

Typical Usage

  • Infrastructure installation testing
  • Test the internal state of a configured server
  • Tests run on the server via SSH

Example Scenarios

  • Service installed
  • User present
  • Directory present

Code Example

require 'spec_helper'

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

describe service('apache') do
  it { should be_running   }
end

describe port(80) do
  it { should be_listening }
end

Puppet-Lint

Description

Tests that a Puppet manifest conforms to a style guide

http://puppet-lint.com

Typical Usage

  • Ensure that manifests comply with agreed coding styles

Example Scenarios

  • Check for additional whitespace in code
  • Ensure braces and brackets are correctly positioned
  • Check for trailing newlines