"Cookbook apache_wrapper not found" when running chefspec in apache_wrapper cookbook

1.1k views Asked by At

I'm new in chef spec and try to figure out how to use unit testing in my cookbooks. I have installed chefdk(v2 and v3 on different ubuntu instances) and knife spec plugin. After "apache_wrapper" cookbook creation I have changed next files:

spec/spec_helper.rb

require 'chefspec'
require 'chefspec/berkshelf'

RSpec.configure do |config|
config.log_level = :debug
  config.platform = 'ubuntu'
  config.version = '12.04'
end

spec/recipes/default_spec.rb

require_relative '../spec_helper'

describe 'apache_wrapper::default' do
  subject { ChefSpec::Runner.new.converge(described_recipe) }

  it 'includes community cookbook apache2' do
    expect(subject).to include_recipe('apache2')
  end

  it 'creates a template with attributes' do
    expect(subject).to create_template('/var/www/html/index.html').with(
      user:   'root',
      group:  'root',
      backup: true,
    )

    expect(subject).to_not create_template('/var/www/html/index.html').with(
      user:   'bacon',
      group:  'fat',
      backup: true,
    )
  end
end

in my recipe default.eb:

include_recipe 'apache2'

template "/var/www/html/index.html" do
  source "index.html.erb"
  mode 00644
end

But when I invoke rspec I got next:

$ pwd
/tmp/apache_wrapper
$ rspec
...
Failures:

  1) apache_wrapper::default includes community cookbook apache2
     Failure/Error: subject { ChefSpec::Runner.new.converge(described_recipe) }
     Chef::Exceptions::CookbookNotFound:
       Cookbook apache_wrapper not found. If you're loading apache_wrapper from another cookbook, make sure you configure the dependency in your metadata
     # ./spec/recipes/default_spec.rb:24:in `block (2 levels) in <top (required)>'
     # ./spec/recipes/default_spec.rb:27:in `block (2 levels) in <top (required)>'

  2) apache_wrapper::default creates a template with attributes
     Failure/Error: subject { ChefSpec::Runner.new.converge(described_recipe) }
     Chef::Exceptions::CookbookNotFound:
       Cookbook apache_wrapper not found. If you're loading apache_wrapper from another cookbook, make sure you configure the dependency in your metadata
     # ./spec/recipes/default_spec.rb:24:in `block (2 levels) in <top (required)>'
     # ./spec/recipes/default_spec.rb:31:in `block (2 levels) in <top (required)>'

Finished in 0.32355 seconds (files took 2.39 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./spec/recipes/default_spec.rb:26 # apache_wrapper::default includes community cookbook apache2
rspec ./spec/recipes/default_spec.rb:30 # apache_wrapper::default creates a template with attributes

And in cookbook folder Berksfile.lock appeared. Can anybody told me what Im doing wrong?

UPD:

Berksfile:

source "https://supermarket.getchef.com"
cookbook 'apache2', '= 1.9.6'

Berksfile.lock

DEPENDENCIES
  apache2 (= 1.9.6)

GRAPH
  apache2 (1.9.6)
    iptables (>= 0.0.0)
    logrotate (>= 0.0.0)
    pacman (>= 0.0.0)
  iptables (0.14.0)
  logrotate (1.7.0)
  pacman (1.1.1)

UPD2:

cat ../apache_wrapper/metadata.rb | grep -E 'dep|nam'
name             'apache_wrapper'
depends 'apache2'

UPD3:

also I tried to use next

let (:chef_run) { ChefSpec::ServerRunner.new.converge 'apache_wrapper::default' } 

and now receive

Missing Cookbooks:
------------------
No such cookbook: apache_wrapper

Expanded Run List:
------------------
* apache_wrapper::default

F

Failures:

  1) apache_wrapper::default includes community cookbook apache2
     Failure/Error: let (:chef_run) { ChefSpec::ServerRunner.new.converge 'apache_wrapper::default' }
     Net::HTTPServerException:
       412 "Precondition Failed "
     # ./spec/default_spec.rb:23:in `block (2 levels) in <top (required)>'
     # ./spec/default_spec.rb:26:in `block (2 levels) in <top (required)>

I don't understand, what Im doing wrong, today I've started new instans, install there ruby2.1 and all gems like chefspec and others. And now use rake to run tests, but still getting same error

SOLUTION: Just add "metadata" to cookbooks Berksfile

1

There are 1 answers

8
rastasheep On BEST ANSWER

You must specify "apache2" as dependency in your cookbooks metadata.rb :

name             'COOKBOOK NAME'
maintainer       'YOUR NAME'
maintainer_email 'YOUR_EMAIL'
...

version          '0.0.1'

depends          'apache2'

After that you can include recipes from "apache2" in your cookbook.

UPDATE:

Also you need to specify where your "apache_wrapper" cookbook is located, please check chefspec's configuration options for more info

UPDATE 2

Add metadata to your Berksfile, with that in place Berkshelf will add your local cookbook to List of local cookbooks.

From berkshelf.com:

The metadata keyword is like saying gemspec in Bundler’s Gemfile. It says, “There is a metadata.rb file within the same relative path of my Berksfile”. This allows you to resolve a Cookbook’s dependencies that you are currently working on just like you would resolve the dependencies of a Gem that you are currently working on with Bundler.