Instantly share code, notes, and snippets.

Embed
What would you like to do?
serverspec files testing multiple hosts at once. How to get a summary report of host failures?
$ cat Rakefile
require 'rake'
require 'rspec/core/rake_task'
hosts = %w(
atlspf01
atlspf02
atlspf03
atlspf04
)
task :spec => 'spec:all'
namespace :spec do
hosts.each do |host|
begin
RSpec::Core::RakeTask.new(host) do |t|
ENV['TARGET_HOST'] = host
t.pattern = "spec/base,cfengine3/*_spec.rb"
rescue
end
end
end
$ cat spec/spec_helper.rb
require 'serverspec'
require 'net/ssh'
set :backend, :ssh
if ENV['ASK_SUDO_PASSWORD']
begin
require 'highline/import'
rescue LoadError
fail "highline is not available. Try installing it."
end
set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
else
set :sudo_password, ENV['SUDO_PASSWORD']
end
host = ENV['TARGET_HOST']
options = Net::SSH::Config.for(host)
options[:user] ||= Etc.getlogin
host_run_on = options[:host_name] || host
set :host, host_run_on
set :ssh_options, options
RSpec.configure do |c|
c.after(:each) do |example|
if example.exception
puts "Failed on #{host_run_on}"
end
end
end
$ cat spec/cfengine3/common_spec.rb
require 'spec_helper'
require 'socket'
describe "All common tests" do
describe package('cfengine-community') do
it { should be_installed }
end
describe command( '/var/cfengine/bin/cf-promises --version') do
its(:stdout) { should match /3\.6\.5/ }
end
describe service( 'cfengine3' ) do
it { should be_enabled }
end
for proc in [ 'execd', 'serverd', 'monitord' ]
describe process( "cf-#{proc}" ) do
it { should be_running }
end
end
describe port(5308) do
it { should be_listening }
end
end
$ rspec
Password:
..Failed on []
F....
Failures:
1) All common tests Service "cfengine3" should be enabled
Failure/Error: it { should be_enabled }
expected Service "cfengine3" to be enabled
sudo -p 'Password: ' /bin/sh -c chkconfig\ --list\ cfengine3\ \|\ grep\ 3:on
# ./spec/cfengine3/common_spec.rb:14:in `block (3 levels) in <top (required)>'
Finished in 12.96 seconds (files took 1.4 seconds to load)
7 examples, 1 failure
Failed examples:
rspec ./spec/cfengine3/common_spec.rb:14 # All common tests Service "cfengine3" should be enabled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment