3

我在使用 ServerSpec 在我的 Chef 食谱上运行集成测试时遇到了问题。当我今天尝试运行测试时,没有进行任何更改,我收到以下错误:

tl;博士

/tmp/busser/suites/serverspec/spec_helper.rb:3:in <top (required)>': uninitialized constant SpecInfra (NameError)

> [#] ---- Begin output of kitchen verify '(default)-.+' -p ----
> [#] STDOUT: -----> Starting Kitchen (v1.2.1)
> [#] -----> Verifying <default-CentOS-70>...
> [#]        Removing /tmp/busser/suites/serverspec
> [#]        Uploading /tmp/busser/suites/serverspec/localhost/nodejs_spec.rb (mode=0644)
> [#]        Uploading /tmp/busser/suites/serverspec/spec_helper.rb (mode=0644)
> [#] -----> Running serverspec test suite
> [#]        /opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -I/tmp/busser/gems/gems/rspec-support-3.0.4/lib:/tmp/busser/gems/gems/rspec-core-3.0.4/lib -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/nodejs_spec.rb --color --format documentation
> [#]        /tmp/busser/suites/serverspec/spec_helper.rb:3:in `<top (required)>': uninitialized constant SpecInfra (NameError)
> [#]           from /opt/chef/embedded/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
> [#]           from /opt/chef/embedded/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
> [#]           from /tmp/busser/suites/serverspec/localhost/nodejs_spec.rb:1:in `<top (required)>'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in load_spec_files'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load_spec_files'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:97:in `setup'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:85:in `run'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:38:in `invoke'
> [#]           from /tmp/busser/gems/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'
> [#]           from /opt/chef/embedded/bin/rspec:23:in `load'
> [#]           from /opt/chef/embedded/bin/rspec:23:in `<main>'
> [#]        /opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -I/tmp/busser/gems/gems/rspec-support-3.0.4/lib:/tmp/busser/gems/gems/rspec-core-3.0.4/lib -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/nodejs_spec.rb --color --format documentation failed
> [#]        Ruby Script [/tmp/busser/gems/gems/busser-serverspec-0.2.7/lib/busser/runner_plugin/../serverspec/runner.rb /tmp/busser/suites/serverspec] exit code was 1
> [#]
> [#] STDERR: >>>>>> Verify failed on instance <default-CentOS-70>.
> [#] >>>>>> Please see .kitchen/logs/default-CentOS-70.log for more details
> [#] >>>>>> ------Exception-------
> [#] >>>>>> Class: Kitchen::ActionFailed
> [#] >>>>>> Message: SSH exited (1) for command: [sh -c 'BUSSER_ROOT="/tmp/busser" GEM_HOME="/tmp/busser/gems" GEM_PATH="/tmp/busser/gems" GEM_CACHE="/tmp/busser/gems/cache" ; export BUSSER_ROOT GEM_HOME GEM_PATH GEM_CACHE; sudo -E /tmp/busser/bin/busser test']
> [#] >>>>>> ----------------------

有谁知道为什么会这样?

根据评论:

require 'serverspec'
# require 'specinfra' #I've tried both with and without this

include SpecInfra::Helper::Exec
include SpecInfra::Helper::DetectOS

RSpec.configure do |c|
  if ENV['ASK_SUDO_PASSWORD']
    require 'highline/import'
    c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
  else
    c.sudo_password = ENV['SUDO_PASSWORD']
  end
end

该文件按照使用该应用程序的说明进行,并且以前未更改。

4

3 回答 3

4

如果您使用的是 serverspec 2+,则需要删除 SpecInfra 行并替换为 set 命令:

require 'serverspec'

set :backend, :exec

RSpec.configure do |c|
  c.before :all do
    c.path = '/sbin:/usr/sbin'
  end
end

# etc

有关 kitchen.ci 上的示例 repo 的最新 PR 中的更多信息 - https://github.com/test-kitchen/guide-started-git-cookbook/pull/3

于 2014-10-09T18:13:09.837 回答
2

Test Kitchen will try to install the latest version of ServerSpec. Unfortunately, there was a major release of ServerSpec recently that might have broken a few things, so you may need to upgrade your tests.

See thread at http://lists.opscode.com/sympa/arc/chef/2014-10/msg00027.html

于 2014-10-06T03:34:21.720 回答
0

该文件/tmp/busser/suites/serverspec/spec_helper.rb可能引用了该常量SpecInfra,但您尚未加载任何实际定义该常量的 gem 或 Ruby 文件,因此该常量未定义。

我在 上找不到太多文档SpecInfra,但我想您只需要gem install specinfra在 shell 中运行即可安装 gem,然后require 'specinfra'在发生错误的文件顶部添加。这是修复这些类型错误的常用方法。

我假设这spec_helper.rb是您在某个时候编写的文件,它被 serverspec 复制到服务器,但我从未使用过该工具,所以我不太了解它。您需要确保 gem 安装/复制到运行测试的服务器,因此您可能需要将 gem 添加到 serverspec 的某些配置文件中。

于 2014-10-03T17:03:36.927 回答