Boston DevOps 11/19/14

Dustin Collins

RSpec tests for your servers configured 
by Puppet, Chef or anything else.

written by Gosuke Miyashita

based on RSpec

serverspec provides resource types to test your infrastructure

cgroup
command
cron
default_gateway
file
group
host
iis_app_pool
iis_website
interface
ipfilter
ipnat
iptables

kernel_module

linux_kernel_parameter

lxc

mail_alias

package

php_config

port

ppa

process

routing_table

selinux

service

user

windows_feature

windows_registry_key

yumrepo

zfs

there are a lot of resource types

require 'spec_helper'

describe 'mycookbook::nginx' do
    it 'should install the nginx package' do
        expect(package('nginx')).to be_installed
    end

    it 'should place the nginx config' do
        nginx_config = file('/etc/nginx/sites-enabled/myapp')

        expect(nginx_config).to be_file
        expect(nginx_config).to be_mode(644)
        expect(nginx_config).to be_owned_by('www-data')
    end

    it 'should enable and start the nginx service' do
        expect(service('nginx')).to be_enabled
        expect(service('nginx')).to be_running
    end

    it 'should listen on port 80' do
        expect(port(80)).to be_listening
    end
end

an example

my setup

via ChefDK

via Vagrant

with kitchen-docker driver

as test-kitchen's busser

my workflow

boot2docker up

kitchen converge <my suite>
kitchen verify <my suite>

* My cookbook needs to change

* I write a serverspec test for the new behavior

kitchen converge <my suite>
kitchen verify <my suite>

* My new tests fail

* I update my cookbook to provide new behavior

kitchen converge <my suite>
kitchen verify <my suite>

* Tests pass! I release new cookbook version

DEMO

wait. i have to use Chef to use serverspec?

SSH

require 'serverspec'
require 'highline/import'

set :backend, :ssh

if ENV['ASK_LOGIN_PASSWORD']
  options[:password] = ask("\nEnter login password: ") { |q| q.echo = false }
else
  options[:password] = ENV['LOGIN_PASSWORD']
end

set :ssh_options, options

not everyone is on Ubuntu

AIX

Arch Linux

Darwin(Mac OS X)

Debian

Fedora/Red Hat/CentOS

FreeBSD

Gentoo Linux

NixOS

OpenBSD

openSUSE

Plamo Linux

SmartOS

Solaris

SUSE

Ubuntu

Windows

serverspec supports*

*Not all OSes are fully supported

installing serverspec

test-kitchen

myproject
-- tests
   -- integration
      -- serverspec
         -- <suite name>
            -- spec_helper.rb
            -- *_spec.rb

standalone

gem install serverspec

serverspec busser is automatically installed before 1st test run

alternatives

Bats (Bash Automated Testing System)

all bash, no cross-platform

RSpec syntax
Access to Chef's run_status, node and run_context

Chef-only

BDD, can handle complicated scenarios

harder to set up, less documented

python, uses fabric

not popular or mature (v0.1.7) yet

and probably more

questions?

Dustin Collins

Made with Slides.com