Wednesday, August 6, 2014

Installing serverspec and running tests on EC2


This is an introduction of installing serverspec and running a test spec file to confirm that nginx and sysstat are installed and running.

Defining hostname of the node to test and specific settings
~/.ssh/config
Host chef-node01
  HostName xxx.xxx.xxx.xxx
  Port 22
  User ec2-user
Installing servespec via bundle
$ gem i bundle
$ mkdir serverspec
~/serverspec/Gemfile
source 'https://rubygems.org'


gem 'serverspec'
gem 'rake'
$ bundle install --path gems
Fetching gem metadata from https://rubygems.org/......
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Installing rake 10.3.2
Installing diff-lcs 1.2.5
Installing highline 1.6.21
Installing net-ssh 2.9.1
Installing rspec-core 2.99.1
Installing rspec-expectations 2.99.2
Installing rspec-mocks 2.99.2
Installing rspec 2.99.0
Installing rspec-its 1.0.1
Installing specinfra 1.24.0
Installing serverspec 1.12.0
Using bundler 1.6.5
Your bundle is complete!
It was installed into ./gems
Initializing serverspec
$ bundle exec 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: chef-node01
 + spec/
 + spec/chef-node01/
 + spec/chef-node01/httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + spec/chef-node01/httpd_spec.rb
$ rm -f spec/chef-node01/httpd_spec.rb
Creating spec file
spec/chef-node01/nginx_spec.rb 
require 'spec_helper'


describe package('nginx') do
  it { should be_installed }
end


describe service('nginx') do
  it { should be_enabled   }
  it { should be_running   }
end


describe port(80) do
  it { should be_listening }
end


describe file('/etc/nginx/nginx.conf') do
  it { should be_file }
end
spec/chef-node01/sysstat_spec.rb 
require 'spec_helper'


describe package('sysstat') do
  it { should be_installed }
end


describe service('sysstat') do
  it { should be_running   }
  it { should be_enabled   }
end
I removed "it { should be_running   }" because sysstat generates pid file but deletes it after generating.
Cf. https://bugzilla.redhat.com/show_bug.cgi?id=820992
/etc/rc.d/init.d/sysstat 
     26 case "$1" in
     27   start)
     28         [ $UID -eq 0 ] || exit 4
     29         echo $$ > $PIDFILE || exit 1
     30         echo -n "Calling the system activity data collector (sadc)... "
     31           /usr/lib64/sa/sa1 --boot
     32         [ $? -eq 0 ] || RETVAL=1
     33         rm -f $PIDFILE
     34         echo
     35         ;;
Running tests
First, move to the directory to run tests. Rakefile sees the spec files that include"*_spec.rb" on its file name.

$ cd spec/chef-node01/
Rakefile 
require 'rake'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec) do |t|
  t.pattern = 'spec/*/*_spec.rb'
end

task :default => :spec
The error message below appears after running the test. It is necessary to add the following entry to require pty.
spec/spec_helper.rb
SpecInfra.configuration.request_pty = true
$ bundle exec rake spec
/usr/bin/ruby2.0 -S rspec spec/chef-node01/nginx_spec.rb
Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
FPlease set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
FPlease set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
FPlease set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
FPlease set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
F

Failures:

  1) Package "nginx" 
     On host `chef-node01`
     Failure/Error: Unable to find matching line from backtrace
     SystemExit: Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
       
       Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
     # (eval):7:in `backend'

  2) Service "nginx" 
     On host `chef-node01`
     Failure/Error: Unable to find matching line from backtrace
     SystemExit: Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
       
       Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
     # (eval):7:in `backend'

  3) Service "nginx" 
     On host `chef-node01`
     Failure/Error: Unable to find matching line from backtrace
     SystemExit: Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
       
       Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
     # (eval):7:in `backend'

  4) Port "80" 
     On host `chef-node01`
     Failure/Error: Unable to find matching line from backtrace
     SystemExit: Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
       
       Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
     # (eval):7:in `backend'

  5) File "/etc/nginx/nginx.conf" 
     On host `chef-node01`
     Failure/Error: Unable to find matching line from backtrace
     SystemExit: Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
       
       Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
     # (eval):7:in `backend'

Finished in 0.21471 seconds
5 examples, 5 failures

Failed examples:

rspec ./spec/chef-node01/nginx_spec.rb:4 # Package "nginx" 
rspec ./spec/chef-node01/nginx_spec.rb:8 # Service "nginx" 
rspec ./spec/chef-node01/nginx_spec.rb:9 # Service "nginx" 
rspec ./spec/chef-node01/nginx_spec.rb:13 # Port "80" 
rspec ./spec/chef-node01/nginx_spec.rb:17 # File "/etc/nginx/nginx.conf" 
/usr/bin/ruby2.0 -S rspec spec/chef-node01/nginx_spec.rb failed
[ec2-user@ip-172-31-5-203 chef-node01]$ bundle exec rake spec
(in /home/ec2-user/serverspec)
/usr/bin/ruby2.0 -S rspec spec/chef-node01/nginx_spec.rb
.....

Finished in 0.34515 seconds
5 examples, 0 failures
$ bundle exec rake spec
(in /home/ec2-user/serverspec)
/usr/bin/ruby2.0 -S rspec spec/chef-node01/nginx_spec.rb spec/chef-node01/sysstat_spec.rb
.......

Finished in 0.36072 seconds
7 examples, 0 failures
Running the specific spec file to run
It is also possible to run the specific spec file by using rspec.
$ bundle exec rspec spec/chef-node01/nginx_spec.rb 
.....

Finished in 0.31992 seconds
5 examples, 0 failures
Displaying the result in detail
It displays the results in detail by adding the entry as follows.
~/.rspec
--format documentation
$ bundle exec rspec spec/chef-node01/nginx_spec.rb 

Package "nginx"
  should be installed

Service "nginx"
  should be enabled
  should be running

Port "80"
  should be listening

File "/etc/nginx/nginx.conf"
  should be file

Finished in 0.32832 seconds
5 examples, 0 failures

Done. we finally confirmed that nginx is installed, running, and activated, and sysstat is installed and activated.


1 comment:

  1. Thanks for providing this informative information you may also refer.
    http://www.s4techno.com/blog/2015/12/21/protect-instances-from-termination-by-auto-scaling/

    ReplyDelete