Table of Contents

Serverspec



Serverspec


Brief overview

With Serverspec, you can write RSpec tests for checking your servers are configured correctly.

Installation

Install serverspec

$ gem search  serverspec
or
$ gem search  --remote serverspec
or
$ gem search  serverspec -p http://192.168.0.10:9999
$ gem install  serverspec -p http://192.168.0.10:9999

Install rake

$ gem search rake
or
$ gem search --remote rake
or
$ gem search --remote rake -p http://192.168.0.10:9999
$ gem install rake
or
$ gem install  --remote rake
or
$ gem install  --remote rake -p http://192.168.0.10:9999


Configuration

create key

$ ssh-keygen -t rsa -C "test"
$ ls
id_rsa  id_rsa.pub

Client : configuration of sudo

require sudo password

“SUDO_PASSWORD = xxxxx ” or “ASK_SUDO_PASSWORD = 1”

user01   ALL=(ALL)       ALL


nopassword

user01   ALL=(ALL)       NOPASSWD:ALL

How to create initial configuration files.

$ serverspec-init
Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 1

Vagrant instance y/n: n
Input target host name: www.example.com
 + spec/
 + spec/www.example.com/
 + spec/www.example.com/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile


Execute Serverspec

List all rake commands

$ rake -T

Check server

$ rake spec:www.example.com
$ rspec spec/server1/httpd_spec.rb
$ rake spec:www.example.com ASK_SUDO_PASSWORD=1
Enter sudo password:


Sample Spec Files

Linux OS

Connection check

Programming Language

php_spec.rb
ruby_spec.rb

Middleware

# Is there the file?
describe file('/etc/passwd') do
  it { should be_file } 
end

# Is there the Directory ?
describe file('/var/log/httpd') do
  it { should be_directory }
end
describe command('ls -al /') do
  its(:stdout) { should match /bin/ }
end

describe command('ls /foo') do
  its(:stderr) { should match /No such file or directory/ }
end

describe command('ls /foo') do
  its(:exit_status) { should eq 0 }
end
case check_centos_version
  when '5.4'
    describe file('/etc/ldap.conf') do
    do_some_tests
  end
  when '6.2'
    describe file('/etc/pam_ldap.conf') do
    do_some_tests
  end
else
  puts "Error: Couldn't execute tests. Unknown OS."
  exit 1
end




Serverspec