| layout | layout |
|---|---|
| title | Changes of Version 2 |
This is the documentation of changes in Serverspec/Specinfra v2.
Serverspec v2 supports RSpec 3 and does not support RSpec 2 any more.
SpecInfra, the base library of Serverspec, has been renamed to Specinfra.
Almost all of the Serverspec test code you wrote for v1 should work with v2, but some matchers don't work.
match_md5checksum, match_sha256checksum, return_stdout, return_stderr, return_exit_status matchers are obsoleted
You can't write test code like this anymore.
describe command('ls /tmp') do
it { should return_stdout 'foo' }
it { should return_stderr 'bar' }
it { should return_exit_status 0 }
enddescribe file('/etc/services') do
it { should match_md5checksum('fdcb69b5c0e9beb7d392fbc458bc6beb')
it { should match_sha256sum('17feb8dc0817056a963c2861052d670b642c61f5625fae1fd59a2022be1dbb5b') }
endInstead, you must write test code like this.
describe command('ls /tmp') do
its(:stdout) { should eq "foo\n" }
its(:stderr) { should match /bar/ }
its(:exit_status) { should eq 0 }
enddescribe file('/etc/services') do
its(:md5sum) { should eq 'fdcb69b5c0e9beb7d392fbc458bc6beb' }
its(:sha256sum) { should eq '17feb8dc0817056a963c2861052d670b642c61f5625fae1fd59a2022be1dbb5b' }
endspec_helper.rb does not have backward compatibility. So you should re-generate spec_helper.rb by serverspec-init and check it.
In version 1, you need to include SpecInfra::Helper::_backend_type_ SpecInfra::Helper::DetectOS to detect the os of target hosts like this.
require 'serverspec'
include SpecInfra::Helper::Ssh
include SpecInfra::Helper::DetectOSIn version 2, you can specify backend type with set :backend, :type and auto detection is default behavior, so don't include SpecInfra::Helper::_backend_type_ and Specinfra::Helper::DetectOS.
require 'serverspec'
# Set backend type
set :backend, :ssh
# Don't include Specinfra::Helper::DetectOSSpecinfra sets prompt internally by -p option of sudo command now. So we don't need set sudo_prompt by RSpec.configure or Specinfra.configuration.
The helper command os which returns OS infomation of target hosts has been changed in two point.
- Return OS family in
small letters - Return
release numbercorrectly
In v1, os command returns OS information like this.
irb(main):004:0> os
=> {:family=>"FreeBSD10", :release=>nil, :arch=>"x86_64"}In v2, os command returns OS information like this.
irb(main):004:0> os
=> {:family=>"freebsd", :release=>"10", :arch=>"x86_64"}In v1, $PATH is appended to your path configuration automatically, so you set paths excluding $PATH like this.
RSpec.configure do |c|
c.path = '/sbin:/usr/local/sbin'
end
# Or
Specinfra.configuration.path = '/sbin:/usr/local/sbin'In v2, you must set $PATH in path configuration.
set :path, '/sbin:/usr/local/sbin:$PATH'(This is the syntax sugar of Specinfra.configuration. I will explain it later.)
Thanks to this change, you can change the path order like this.
set :path, '$PATH:/sbin:/usr/local/sbin'In v1, you can set configuration parameters and values like this.
# Through RSpec.configure
RSpec.configure |c|
c.host = 'target-host-name'
end
# Or call Specinfra.configuration directly
Specinfra.configuration.host = 'target-host-name'In v2, you can set configuration parameters and values like this.
set :host, 'target-host-name'In v1, you must create Net::SSH object by yourself and set to c.ssh like this.
RSpec.configure do |c|
c.ssh = Net::SSH.start('host.example.jp', 'user', :port => 2222)
endIn v2, you can set SSH settings like this.
set :host, 'host.example.jp'
set :ssh_options, :user => 'user', :port => 2222You can set any environment variables like this.
set :env, :LANG => 'C', :LC_MESSAGES => 'C'Note: In the case of SSH, environment variables should be accepted by AcceptEnv directive of sshd_config.