使用服务器规范构建Rakefile以测试端口-获得0个示例,0个失败

时间:2019-02-14 16:04:11

标签: ruby rubygems jenkins-pipeline rakefile serverspec

在测试场景中,我需要测试一组各种OS服务器上的端口状态。我已经按照以下步骤构造了 Rakefile

require 'rake'
require 'rspec/core/rake_task'

hosts = %w(

    //list of Ux servers
    server1
    //list of Win servers
    server2

)

task :spec => 'spec:all'

namespace :spec do
  task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
  hosts.each do |host|
    short_name = host.split('.')[0]
    role       = short_name.match(/[^0-9]+/)[0]

desc "Run serverspec to #{host}"
RSpec::Core::RakeTask.new(short_name) do |t|
  ENV['TARGET_HOST'] = host
  t.pattern = "spec/{base,#{role}}/*_spec.rb"
  end
 end
end

然后我在 spec_helper.rb 中添加了以下内容:

require 'serverspec'
require 'net/ssh'

set :backend, :ssh

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

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

当我执行测试命令 rake spec 输出显示在列表中的所有服务器之间循环后没有找到任何示例。 我在这里做什么错了?

输出:

-bash-4.2$ rake spec
/usr/bin/ruby -I/home/username/.gem/ruby/gems/rspec-core-3.8.0/lib:/home/username/.gem/ruby/gems/rspec-support-3.8.0/lib /home/username/.gem/ruby/gems/rspec-core-3.8.0/exe/rspec --pattern spec/\{base,server1\}/\*_spec.rb
**No examples found.**

Finished in 0.00041 seconds (files took 0.50682 seconds to load)
**0 examples, 0 failures**

/usr/bin/ruby -I/home/username/.gem/ruby/gems/rspec-core-3.8.0/lib:/home/username/.gem/ruby/gems/rspec-support-3.8.0/lib /home/username/.gem/ruby/gems/rspec-core-3.8.0/exe/rspec --pattern spec/\{base,server2\}/\*_spec.rb
**No examples found.**

Finished in 0.00036 seconds (files took 0.05545 seconds to load)
**0 examples, 0 failures**

已安装宝石:

diff-lcs-1.3
multi_json-1.13.1
net-scp-1.2.1
net-ssh-3.2.0
net-telnet-0.1.1
rake-12.3.2
rspec-3.8.0
rspec-core-3.8.0
rspec-expectations-3.8.2
rspec-its-1.2.0
rspec-mocks-3.8.0
rspec-support-3.8.0
serverspec-2.41.3
sfl-2.3
specinfra-2.76.9

1 个答案:

答案 0 :(得分:0)

根据您的Rakefile的设计方式(此处已纠正缩进):

require 'rake'
require 'rspec/core/rake_task'

hosts = %w(
  server1
)

task :spec => 'spec:all'

namespace :spec do
  task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
  hosts.each do |host|
    short_name = host.split('.')[0]
    role       = short_name.match(/[^0-9]+/)[0]

    desc "Run serverspec to #{host}"
    RSpec::Core::RakeTask.new(short_name) do |t|
      ENV['TARGET_HOST'] = host
      t.pattern = "spec/{base,#{role}}/*_spec.rb"
    end
  end
end

在我看来,您不应该在spec_helper.rb中有这些示例,而应该有一个像这样的文件spec/base/base_spec.rb

require 'spec_helper'

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

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

想法是,这些测试将在所有主机上运行,​​然后在spec/role/myrole_spec.rb中可以找到特定于角色的测试。

通常,您不应将示例放在spec/spec_helper.rb中。