Skip to content

Instantly share code, notes, and snippets.

@mizzy
Last active July 26, 2019 06:18
Show Gist options
  • Save mizzy/5560916 to your computer and use it in GitHub Desktop.
Save mizzy/5560916 to your computer and use it in GitHub Desktop.
How to share serverspec tests among hosts
require 'rake'
require 'rspec/core/rake_task'
hosts = [
{
:name => 'proxy001.example.jp',
:roles => %w( base proxy ),
},
{
:name => 'proxy002.example.jp',
:roles => %w( base proxy ),
},
{
:name => 'app001.example.jp',
:roles => %w( base app ),
},
{
:name => 'app002.example.jp',
:roles => %w( base app ),
},
{
:name => 'db001.example.jp',
:roles => %w( base db ),
},
{
:name => 'db002.example.jp',
:roles => %w( base db ),
},
]
hosts = hosts.map do |host|
{
:name => host[:name],
:short_name => host[:name].split('.')[0],
:roles => host[:roles],
}
end
desc "Run serverspec to all hosts"
task :serverspec => 'serverspec:all'
namespace :serverspec do
task :all => hosts.map {|h| 'serverspec:' + h[:short_name] }
hosts.each do |host|
desc "Run serverspec to #{host[:name]}"
RSpec::Core::RakeTask.new(host[:short_name].to_sym) do |t|
ENV['TARGET_HOST'] = host[:name]
t.pattern = 'spec/{' + host[:roles].join(',') + '}/*_spec.rb'
end
end
end
require 'serverspec'
require 'net/ssh'
include Serverspec::Helper::Ssh
include Serverspec::Helper::DetectOS
RSpec.configure do |c|
c.host = ENV['TARGET_HOST']
options = Net::SSH::Config.for(c.host)
user = options[:user] || Etc.getlogin
c.ssh = Net::SSH.start(c.host, user, options)
c.os = backend(Serverspec::Commands::Base).check_os
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment