Skip to content

Instantly share code, notes, and snippets.

@darkn3rd
Last active September 9, 2021 16:13
Show Gist options
  • Save darkn3rd/a8fbf191c50c2679cfaea8e73e4b7c3c to your computer and use it in GitHub Desktop.
Save darkn3rd/a8fbf191c50c2679cfaea8e73e4b7c3c to your computer and use it in GitHub Desktop.
ServerSpec Spec Helpers

ServerSpec Notes

These are snippets of auto-generated from ServerSpec. The serverspec-init only auto-generates base functionality with cmd, exec, ssh, and winrm backends with some optional vagrant support for dyanmic inventory on ssh backend.

There are additional backends available from SpecInfra, but these are not supported and only documented in O'Reilly book for Serverspec: The Definitive Guide by Gosuke Miyashita (ISBN978-4-87311-709-6), which is in Japanese only:

Spec Helper

Windows CMD

require 'serverspec'
set :backend, :cmd

Unix Exec

require 'serverspec'
set :backend, :exec

Windows WinRM

require 'serverspec'
require 'winrm'

set :backend, :winrm

user = <username>
pass = <password>
endpoint = "http://#{ENV['TARGET_HOST']}:5985/wsman"

winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true)
winrm.set_timeout 300 # 5 minutes max timeout for any operation
Specinfra.configuration.winrm = winrm

SSH w/o Vagrant

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

set :host,        options[:host_name] || host
set :ssh_options, options

SSH w Vagrant

require 'serverspec'
require 'net/ssh'
require 'tempfile'

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']

`vagrant up #{host}`

config = Tempfile.new('', Dir.tmpdir)
config.write(`vagrant ssh-config #{host}`)
config.close

options = Net::SSH::Config.for(host, [config.path])

options[:user] ||= Etc.getlogin

set :host,        options[:host_name] || host
set :ssh_options, options

SSH Unused Options (Default)

set :disable_sudo, true
set :env, :LANG => 'C', :LC_MESSAGES => 'C'
set :path, '/sbin:/usr/local/sbin:$PATH'

Backends Source

No automation at the moment for Docker.

Called From:

Rakefile

require 'rake'
require 'rspec/core/rake_task'

task :spec    => 'spec:all'
task :default => :spec

namespace :spec do
  targets = []
  Dir.glob('./spec/*').each do |dir|
    next unless File.directory?(dir)
    target = File.basename(dir)
    target = "_#{target}" if target == "default"
    targets << target
  end

  task :all     => targets
  task :default => :all

  targets.each do |target|
    original_target = target == "_default" ? target[1..-1] : target
    desc "Run serverspec tests to #{original_target}"
    RSpec::Core::RakeTask.new(target.to_sym) do |t|
      ENV['TARGET_HOST'] = original_target
      t.pattern = "spec/#{original_target}/*_spec.rb"
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment