I'm trying to running my ChefSpec tests.
This is my ChefSpec test:
require_relative '../spec_helper'
describe 'my-demo::basesystem' do
let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe)}
describe 'basesystem' do
it "should be installed" do
expect(chef_run).to install_package('build-essential')
end
end
end
And it is my recipe
include_recipe 'build-essential::default'
It was the error output when I executed my ChefSpec tests
================================================================================
Recipe Compile Error in /tmp/d20140208-11211-1tu0tmq/my-demo/recipes/basesystem.rb
================================================================================
Chef::Exceptions::CookbookNotFound
----------------------------------
Cookbook build-essential:: not found. If you're loading build-essential:: from another cookbook, make sure you configure the dependency in your metadata
Cookbook Trace:
---------------
/tmp/d20140208-11211-1tu0tmq/build-essential/recipes/default.rb:21:in `from_file'
/tmp/d20140208-11211-1tu0tmq/my-demo/recipes/basesystem.rb:2:in `from_file'
Relevant File Content:
----------------------
/tmp/d20140208-11211-1tu0tmq/build-essential/recipes/default.rb:
14: # distributed under the License is distributed on an "AS IS" BASIS,
15: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: # See the License for the specific language governing permissions and
17: # limitations under the License.
18: #
19:
20: begin
21>> include_recipe "build-essential::#{node['platform_family']}"
22: rescue Chef::Exceptions::RecipeNotFound
23: Chef::Log.warn "A build-essential recipe does not exist for the platform_family: #{node['platform_family']}"
24: end
25:
Chef::Exceptions::CookbookNotFound: Cookbook build-essential:: not found. If you're loading build-essential:: from another cookbook, make sure you configure the dependency in your metadata
/tmp/d20140208-11211-1tu0tmq/build-essential/recipes/default.rb:21:in `from_file'
/tmp/d20140208-11211-1tu0tmq/my-demo/recipes/basesystem.rb:2:in `from_file'
./spec/recipes/base_system_spec.rb:5:in `block (2 levels) in <top (required)>'
./spec/recipes/base_system_spec.rb:8:in `block (2 levels) in <top (required)>'
1 example, 1 failure, 0 passed
Finished in 0.062297226 seconds
Process finished with exit code 1
I don't know what's the problem, I thought that Berkshelf could be resolve the cookbooks dependencies.
If you look at the stacktrace:
20: begin
21>> include_recipe "build-essential::#{node['platform_family']}"
22: rescue Chef::Exceptions::RecipeNotFound
23: Chef::Log.warn "A build-essential recipe does not exist for the platform_family: #{node['platform_family']}"
24: end
You'll see on line 21 that the build-essential cookbook is trying to load a recipe that corresponds to the current node's platform family. However, for ChefSpec, that data isn't set unless you specifically tell ChefSpec what kind of node to impersonate. In other words, node['platform_family']
is nil
, so it's trying to include a recipe named build-essential::(nothing)
, and that's not valid.
To fix this, you can either set the value for platform_family:
let(chef_run) do
ChefSpec::Runner.new do |node|
node.automatic['platform_family'] = 'ubuntu'
end.converge(described_recipe)
end
Or (preferred), you can tell ChefSpec to impersonate a node:
let(:chef_run) { ChefSpec::Runner.new(platform: 'ubuntu', version: '12.04').converge(described_recipe) }