Running ChefSpec without Berkshelf or Librarian


#1

Hi I’m trying to write a unit tests using ChefSpec (and Chef Solo) but I
can’t get even the simplest test to work. I’ve tried following several
examples but always get a Chef::Exceptions::CookbookNotFound error on the
cookbook I’m attempting to test.

For example if I add the following to the ‘java’ cookbook (
http://community.opscode.com/cookbooks/java)

coobooks/java/spec/default_spec.rb

require ‘chefspec’

describe ‘java::default’ do
let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }
it ‘should include the openjdk recipe by default’ do
chef_run.should include_recipe 'java::openjdk’
end
end

When I run rspec I get the following error:

Failures:

  1. java::default should include the openjdk recipe by default
    Failure/Error: let(:chef_run) {
    ChefSpec::Runner.new.converge(described_recipe) }
    Chef::Exceptions::CookbookNotFound:
    Cookbook java not found. If you’re loading java from another
    cookbook, make sure you configure the dependency in your metadata

    ./cookbooks/java/spec/default_spec.rb:4:in `block (2 levels) in <top

(required)>’
# ./cookbooks/java/spec/default_spec.rb:6:in `block (2 levels) in <top
(required)>’

Finished in 0.035 seconds
1 example, 1 failure

Failed examples:

rspec ./cookbooks/java/spec/default_spec.rb:5 # java::default should
include the openjdk recipe by default

I’ve tried running the following commands but they both result in this
error:

rspec cookbooks\java
rspec cookbooks\java\spec\default_spec.rb

I’ve tried adding a .chef/knife.rb as detailed here
https://wiki.opscode.com/display/chef/Troubleshooting+and+Technical+FAQ)
but that didn’t help.

The ChefSpec readme (https://github.com/sethvargo/chefspec) mentions
BerkShelf and Librarian. I don’t currently use either of those tools, do I
need them to run ChefSpec?

Am I missing something obvious (I’m new to Chef and Ruby)?

Thanks, Richard.


#2

Just an update that I finally managed to get a ChefSpec test running! The
problem only occured on windows (on a Ubunutu VM the ChefSpec tests ran
fine). I had to set the cookbook_path as follows in a ChefSpec test in
order to get it working on Windows.

coobooks/java/spec/default_spec.rb

require ‘chefspec’

#set cookbook path relative to this file

cpath = File.expand_path("…/…/…/", FILE)

describe ‘java::default’ do
let(:chef_run) { ChefSpec::Runner.new(cookbook_path:
cpath
).converge(described_recipe)
}
it ‘should include the openjdk recipe by default’ do
chef_run.should include_recipe 'java::openjdk’
end
end

If anyone knows a better way please let me know (I’m going to look into
Berkshelf to see if this helps).

Cheers, Richard.

On 14 February 2014 11:58, Richard Murray rich.j.murray@gmail.com wrote:

Hi I’m trying to write a unit tests using ChefSpec (and Chef Solo) but I
can’t get even the simplest test to work. I’ve tried following several
examples but always get a Chef::Exceptions::CookbookNotFound error on the
cookbook I’m attempting to test.

For example if I add the following to the ‘java’ cookbook (
http://community.opscode.com/cookbooks/java)

coobooks/java/spec/default_spec.rb

require ‘chefspec’

describe ‘java::default’ do
let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }
it ‘should include the openjdk recipe by default’ do
chef_run.should include_recipe 'java::openjdk’
end
end

When I run rspec I get the following error:

Failures:

  1. java::default should include the openjdk recipe by default
    Failure/Error: let(:chef_run) {
    ChefSpec::Runner.new.converge(described_recipe) }
    Chef::Exceptions::CookbookNotFound:
    Cookbook java not found. If you’re loading java from another
    cookbook, make sure you configure the dependency in your metadata

    ./cookbooks/java/spec/default_spec.rb:4:in `block (2 levels) in

<top (required)>’
# ./cookbooks/java/spec/default_spec.rb:6:in `block (2 levels) in
<top (required)>’

Finished in 0.035 seconds
1 example, 1 failure

Failed examples:

rspec ./cookbooks/java/spec/default_spec.rb:5 # java::default should
include the openjdk recipe by default

I’ve tried running the following commands but they both result in this
error:

rspec cookbooks\java
rspec cookbooks\java\spec\default_spec.rb

I’ve tried adding a .chef/knife.rb as detailed here
https://wiki.opscode.com/display/chef/Troubleshooting+and+Technical+FAQ)
but that didn’t help.

The ChefSpec readme (https://github.com/sethvargo/chefspec) mentions
BerkShelf and Librarian. I don’t currently use either of those tools, do I
need them to run ChefSpec?

Am I missing something obvious (I’m new to Chef and Ruby)?

Thanks, Richard.