ChefDK and rake

I’m testing a cookbook I set up with mis using the ChefDK environment.

I have done chef shell-init and
which rspec returns

/opt/chefdk/embedded/bin/rspec

This command runs the chefspec tests.

chef exec rspec test/unit/spec

However this rake task fails with error suggesting it’s using the wrong
rspec

require 'rspec/core/rake_task’
desc 'Run ChefSpec unit tests’
RSpec::Core::RakeTask.new(:spec) do |t, _args|
t.rspec_opts = 'test/unit/spec’
end

Is there something obvious I’m missing to make this work?

  • Booker C. Bense

On Friday, August 15, 2014 at 9:39 AM, Booker Bense wrote:

I'm testing a cookbook I set up with mis using the ChefDK environment.

I have done chef shell-init and

Just checking, you did eval "$(chef shell-init bash)” ?

which rspec returns

/opt/chefdk/embedded/bin/rspec

This command runs the chefspec tests.

chef exec rspec test/unit/spec
You don’t need chef exec if you configure your shell via shell-init.

However this rake task fails with error suggesting it's using the wrong rspec
What’s the error?

require 'rspec/core/rake_task'
desc 'Run ChefSpec unit tests'
RSpec::Core::RakeTask.new(:spec) do |t, _args|
t.rspec_opts = 'test/unit/spec'
end

Is there something obvious I'm missing to make this work?

  • Booker C. Bense

--
Daniel DeLeo

On Fri, Aug 15, 2014 at 9:44 AM, Daniel DeLeo dan@kallistec.com wrote:

On Friday, August 15, 2014 at 9:39 AM, Booker Bense wrote:

I'm testing a cookbook I set up with mis using the ChefDK environment.

I have done chef shell-init and

Just checking, you did eval "$(chef shell-init bash)” ?

which rspec returns

/opt/chefdk/embedded/bin/rspec

This command runs the chefspec tests.

chef exec rspec test/unit/spec
You don’t need chef exec if you configure your shell via shell-init

Yes, rspec test/unit/spec on the command line works as well. So the
shell-init part is correct.

I did the obvious after a little googling and added this to the rake file

require 'rspec/core/rake_task'
desc 'Run ChefSpec unit tests'
RSpec::Core::RakeTask.new(:spec) do |t, _args|
t.rspec_opts = 'test/unit/spec'
t.rspec_path = '/opt/chefdk/embedded/bin/rspec'
end

And now I get this error, which seems pretty odd. This is the error I was
originally getting, I thought it meant I was
running a different rspec, but apparently not.

rake spec
/opt/chefdk/embedded/bin/ruby -S /opt/chefdk/embedded/bin/rspec
test/unit/spec
/opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-expectations-2.99.2/lib/rspec/matchers/built_in/raise_error.rb:5:in
<class:RaiseError>': uninitialized constant RSpec::Matchers::BuiltIn::RaiseError::MatchAliases (NameError) from /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-expectations-2.99.2/lib/rspec/matchers/built_in/raise_error.rb:4:in module:BuiltIn'
from
/opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-expectations-2.99.2/lib/rspec/matchers/built_in/raise_error.rb:3:in
<module:Matchers>' from /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-expectations-2.99.2/lib/rspec/matchers/built_in/raise_error.rb:2:in module:RSpec'
from
/opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-expectations-2.99.2/lib/rspec/matchers/built_in/raise_error.rb:1:in
<top (required)>' from /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/chefspec-3.4.0/lib/chefspec/expect_exception.rb:1:in require'
from
/opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/chefspec-3.4.0/lib/chefspec/expect_exception.rb:1:in
<top (required)>' from /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/chefspec-3.4.0/lib/chefspec.rb:26:in require_relative'
from
/opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/chefspec-3.4.0/lib/chefspec.rb:26:in
<top (required)>' from /Users/booker/work/sysman/slac_motd/test/unit/spec/spec_helper.rb:2:in require'
from /Users/booker/work/sysman/slac_motd/test/unit/spec/spec_helper.rb:2:in
<top (required)>' from /Users/booker/work/sysman/slac_motd/test/unit/spec/default_spec.rb:3:in require_relative'
from
/Users/booker/work/sysman/slac_motd/test/unit/spec/default_spec.rb:3:in
<top (required)>' from /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-core-2.99.1/lib/rspec/core/configuration.rb:1065:in load'
from
/opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-core-2.99.1/lib/rspec/core/configuration.rb:1065:in
block in load_spec_files' from /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-core-2.99.1/lib/rspec/core/configuration.rb:1065:in each'
from
/opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-core-2.99.1/lib/rspec/core/configuration.rb:1065:in
load_spec_files' from /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-core-2.99.1/lib/rspec/core/command_line.rb:18:in run'
from
/opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-core-2.99.1/lib/rspec/core/runner.rb:103:in
run' from /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/rspec-core-2.99.1/lib/rspec/core/runner.rb:17:in block in autorun

  • Booker C. Bense

Hi Booker,

ChefDK is new enough that I’m not sure what the ‘ChefDK’ way is. But I’d use bundler and a Gemfile in your project, so that all your gems (rspec, etc) are using the same Gem versions.

I did find this bug:
https://github.com/opscode/chef-dk/issues/18

That suggests you may be able to do ‘chef run rake’ or something like that and get the same rake+rspec you get from running rspec yourself. Or get the chef paths and hooks in front of your rake command by running – eval “$(chef shell-init bash)” – I haven’t tested that, though. It sounds like down the road they plan on wrapping all that with a ‘chef test’ command, too.

Hope this helps,

Martin B. Smith, martin.smith@rackspace.commailto:martin.smith@rackspace.com | 800-961-4454
5000 Walzem Rd. | San Antonio, TX 78218

Rackspace - the open cloud company
Ask about our DevOps Automation Service - www.rackspace.com/devopshttp://www.rackspace.com/devops/


From: Booker Bense [bbense@gmail.com]
Sent: Friday, August 15, 2014 11:39 AM
To: chef@lists.opscode.com
Subject: [chef] ChefDK and rake

I’m testing a cookbook I set up with mis using the ChefDK environment.

I have done chef shell-init and
which rspec returns

/opt/chefdk/embedded/bin/rspec

This command runs the chefspec tests.

chef exec rspec test/unit/spec

However this rake task fails with error suggesting it’s using the wrong rspec

require 'rspec/core/rake_task’
desc 'Run ChefSpec unit tests’
RSpec::Core::RakeTask.new(:spec) do |t, _args|
t.rspec_opts = 'test/unit/spec’
end

Is there something obvious I’m missing to make this work?

  • Booker C. Bense