3

I've run into an issue when using ServerSpec to run integration test on my Chef cookbooks. When I attempted to run the test today, without making any changes, I got the following error:

tl;dr

/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']
> [#] >>>>>> ----------------------

Does anyone know why this is occurring?

As per the comments:

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

That file is as per the instruction laid out for using the application and has previously worked un altered.

localhostv6
  • 47
  • 1
  • 5

3 Answers3

4

If you're using serverspec 2+ you need to remove the SpecInfra lines and replace with the set command:

require 'serverspec'

set :backend, :exec

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

# etc

more info in my latest PR for the example repo on kitchen.ci - https://github.com/test-kitchen/guide-started-git-cookbook/pull/3

Mike Heijmans
  • 439
  • 4
  • 8
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

Julian Dunn
  • 286
  • 1
  • 4
0

The file /tmp/busser/suites/serverspec/spec_helper.rb probably references the constant SpecInfra but you haven't loaded whatever gem or Ruby file actually defines that constant, so the constant is undefined.

I couldn't find much documentation on SpecInfra, but I imagine you just have to run gem install specinfra in a shell to install the gem, and then add require 'specinfra' at the top of the file where the error is occurring. That is the usual way to fix these types of errors.

I'm assuming that spec_helper.rb is a file you wrote at some point, which is getting copied to the server by serverspec, but I've never used that tool so I don't know much about it. You need to ensure the gem gets installed/copied to the server where the tests are running, so you might need to add the gem to some configuration file for serverspec.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Unfortunately that has not solved the issue. Additionally, this was working earlier this week and I made no changes to the code or the test. – localhostv6 Oct 03 '14 at 17:16
  • Well, I suppose there is some chance that an expert in one of these tools comes along and can spot what you're doing wrong from the error message alone, but if not then it would be better for you to post more information about how you have set up everything and post the actual code from `spec_helper.rb`. – David Grayson Oct 03 '14 at 17:18
  • It looks like the gems are copied to `/tmp/busser/gems`; you could poke around in there to see what's available if it's not getting deleted. – David Grayson Oct 03 '14 at 17:20
  • OK, so can you find the specinfra gem in /tmp/busser/gems on your server and see what version it is, and see if its lib directory has a `specinfra.rb` that defines the `SpecInfra` constant? Are there any other files named `specinfra.rb` in other gems that might be masking the real one? – David Grayson Oct 03 '14 at 18:52