1

我正在使用 serverspec 为每个主机编写测试。我的文件组织如下..

|-- Rakefile

`-- spec

    |-- spec_helper.rb

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

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

spec_helper.rb的内容如下

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

我从 serverspec 获得了 spec_helper 的内容 - 高级技巧

我可以使用命令在本地机器上运行 example_spec.rb

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

它工作正常。

但是当我尝试在 www.example.jp 上运行文件时(例如,将其作为主机名)这个主机,它失败了。

我只是使用以下命令运行。

rake spec:www.example.jp

我在另一台主机上也进行了相同的设置。

当我运行命令时,我得到

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

我的 example_spec.rb 的内容是

require 'spec_helper'

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

请告诉我我做错了什么??

4

2 回答 2

0

我认为你应该spec_helper.rb像这样修改你的:

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

为了使上述工作正常,您应该将登录密码LOGIN_PASSWORD设置到环境变量中,或者设置环境变量ASK_LOGIN_PASSWORD以提示输入密码。

于 2015-06-20T18:28:48.933 回答
0

我发现了错误,早些时候我正在使用 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

但这没有正确连接到主机。所以我在最后添加了以下几行

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

现在我可以连接到远程机器了。

于 2015-06-22T09:05:39.013 回答