我使用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
我从中获取了spec_helper的内容 serverspec - Advanced tips
我可以使用命令
在本地计算机上运行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
请告诉我我做错了什么?
答案 0 :(得分: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
以便提示输入密码。
答案 1 :(得分: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'
现在我能够连接到远程机器。