Serverspec错误:“不知道如何构建任务”

时间:2015-04-28 07:37:22

标签: ruby rspec serverspec

我遇到了Serverspec的问题。我正在尝试使用ansible从CloudBees jenkins构建服务器测试部署在云VM上的基础架构。我使用Serverspec为每个ansible角色创建了一个测试。现在我只是想检查一下我是否可以正确运行测试,并连接到我的VM。

问题是,一旦我运行“rake spec”,我得到一个“不知道如何构建任务'规范:84'”错误,这不在我的代码中。

整个日志都在这里:

########## Testing infrastructure ##########
+ cd openshift-testing/
+ rake spec --trace
** Invoke spec (first_time)
** Invoke spec:all (first_time) rake aborted! Don't know how to build task 'spec:84'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task_manager.rb:62:in
`[]'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:57:in
`lookup_prerequisite'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:53:in
`block in prerequisite_tasks'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:53:in
`map'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:53:in
`prerequisite_tasks'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:199:in
`invoke_prerequisites'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:178:in
`block in invoke_with_call_chain' /usr/share/ruby/monitor.rb:211:in
`mon_synchronize'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:172:in
`invoke_with_call_chain'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:201:in
`block in invoke_prerequisites'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:199:in
`each'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:199:in
`invoke_prerequisites'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:178:in
`block in invoke_with_call_chain' /usr/share/ruby/monitor.rb:211:in
`mon_synchronize'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:172:in
`invoke_with_call_chain'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:165:in
`invoke'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:150:in
`invoke_task'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in
`block (2 levels) in top_level'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in
`each'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in
`block in top_level'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:115:in
`run_with_threads'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:100:in
`top_level'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:78:in
`block in run'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:176:in
`standard_exception_handling'
/home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:75:in
`run' /home/jenkins/.gem/ruby/1.9.1/gems/rake-10.4.2/bin/rake:33:in
`<top (required)>' /usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'

这是我的Rakefile:

  1 require 'rake'
  2 require 'rspec/core/rake_task'
  3 
  4 hosts = %w(
  5   84.39.33.93 
  6 )
  7 
  8 task :spec => 'spec:all'
  9 
 10 namespace :spec do
 11   task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
 12   hosts.each do |host|
 13     short_name = "sca-vm"
 14     role       = "common"
 15 
 16     desc "Run serverspec to #{host}"
 17     RSpec::Core::RakeTask.new(short_name) do |t|
 18       ENV['TARGET_HOST'] = host
 19       t.pattern = "spec/base,#{role}/*_spec.rb"
 20     end
 21   end
 22 end                                                                         
~           

spec_helper.rb:

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

# Disable sudo
# set :disable_sudo, true


# Set environment variables
# set :env, :LANG => 'C', :LC_MESSAGES => 'C' 

# Set PATH
# set :path, '/sbin:/usr/local/sbin:$PATH'

最后,这是我正在使用的test_spec,它只是验证了一个名为now.txt的plaint文本文件的存在:

require 'spec_helper'

describe file('~/now.txt') do
  it { should be_file }
end

我现在真的很困惑。你能帮我理解发生了什么吗?

1 个答案:

答案 0 :(得分:0)

代码段

hosts.map {|h| 'spec:' + h.split('.')[0] }
单独

产生:[“spec:84”]。现在,任务规范:全部取决于该任务。 在以

开头的循环中
hosts.each do |host|

只定义了一个任务,它被称为'scm-va':

RSpec::Core::RakeTask.new(short_name) do |t|
   # ...
end

另请注意,此任务是针对主机的每个元素定义的(在您的示例中只有一个,但您已将hosts作为一个数组出于某种原因,我认为)。

在命令行上尝试rake -T -A列出所有任务,甚至是没有描述的任务,以查看Rake实际知道哪些任务。

换句话说:代码示例声明spec:all依赖于未定义的任务(或任务)。

如果您将行更改为

    RSpec::Core::RakeTask.new('spec:' + host.split('.')[0]) do |t|

代码不会引发异常,但我不确定是否可以解决您的问题。也许重要的是'scm-vm'实际上是定义的任务名称的一部分。

但是,如果这是你想要的,你应该将host.split('.')[0]重构为它自己的方法。