Unable to run serverspec files on different hosts

970 Views Asked by At

I'm writing tests per host using serverspec. My file are organized as follows..

|-- Rakefile

`-- spec

    |-- spec_helper.rb

    `-- www.example.jp
            `-- example_spec.rb
            `-- spec_helper.rb

The contents of the Rakefile is as follows

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

The contents of the spec_helper.rb is as follows

require 'highline/import'

if ENV['ASK_LOGIN_PASSWORD']
  options[:password] = ask("\nEnter login password: ") { |q| q.echo = false}
else
  options[:password] = ENV['LOGIN_PASSWORD']
end

set :ssh_options, options

I got the contents of spec_helper from serverspec - Advanced tips

I'm able to run the example_spec.rb on local machine using the command

/usr/bin/ruby -S rspec example_spec.rb

It works fine.

But when I try to run the file on www.example.jp (for example take this as the name of the host) this host, it fails.

I just use the following command to run.

rake spec:www.example.jp

I have the same setup done in the other host also.

When I run the command I get

spec/spec_helper.rb:4:undefined local variable or method `options' for main:Object (NameError)
    from spec/www.example.jp/example_spec.rb:1:in `require'
    from spec/www.example.jp/example_spec.rb:1
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load_spec_files'
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
    from /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/exe/rspec:4
/usr/bin/ruby -I/usr/lib/ruby/gems/1.8/gems/rspec-support-3.2.2/lib:/usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/lib /usr/lib/ruby/gems/1.8/gems/rspec-core-3.2.3/exe/rspec --pattern spec/blr2263728.idc.oracle.com/\*_spec.rb failed

Contents of my example_spec.rb is

require 'spec_helper'

describe command('whoami') do
 its(:stdout) { should match('root')}
 end

Please tell me what I'm doing wrong??

2

There are 2 best solutions below

0
On

I think you should amend your spec_helper.rb like this:

require 'highline/import'

host = ENV['TARGET_HOST']
options = Net::SSH::Config.for(host)

if ENV['ASK_LOGIN_PASSWORD']
  options[:password] = ask("\nEnter login password: ") { |q| q.echo = false}
else
  options[:password] = ENV['LOGIN_PASSWORD']
end

set :ssh_options, options

In order for the above to work, you should either set your login password into the environment variable LOGIN_PASSWORD or set the environment variable ASK_LOGIN_PASSWORD in order to be prompted for one.

0
On

I found the mistake, Earlier I was running with spec_helper

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

But this was not connecting to the host properly. So I added the following lines at the end

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

Now I was able to connect to the remote machine.