0

I have a successful resource: (converged, worked as expected)

cron_d 'zk_metric' do
  minute '*'
  command “something something"
end

But after adding spec

it 'add cron_d' do
    expect(chef_run).to create_cron_d('zk_metric')
  end

chefspec got the error:

Failures:

  1) myorg::myrecipe add cron_d
     Failure/Error: expect(chef_run).to create_cron_d('zk_metric')
     NoMethodError:
       undefined method `create_cron_d' for #<RSpec::ExampleGroups::Myorgmyrecipe:0x007fa726086e50>
     # ./spec/myrecipe_spec.rb:93:in `block (2 levels) in <top (required)>'

Finished in 12.17 seconds (files took 1.08 seconds to load)

Why is this happening?

The matcher is already defined https://github.com/opscode-cookbooks/cron/blob/master/libraries/matchers.rb

Do I need to include or require something in my spec file (neither worked so far)? Or I need to create my own?

(Edit: stackoverflow autobot asked me to add ruby-on-rails tag, and so I did.)

digit plumber
  • 1,140
  • 2
  • 14
  • 27
  • 1
    Can you show us where you declare the dependency on the cron cookbook, and what version you of that cookbook you are using? Also what version of Chefspec? And show us your full spec_helper.rb and test file, please. There's something not quite right.... – Martin Jul 18 '15 at 01:32

2 Answers2

0

Add below line in your spec_helper.rb or at the beginning of the spec file.

require 'chefspec'
Artur Trzop
  • 336
  • 3
  • 12
  • If this line was not present, no test could happen and the error would be at the defintion of the chef run wich is unknow to rspec. Downvoting – Tensibai Jul 20 '15 at 14:49
0

You're missing a few things, and you have a spacing typo.

Here's how to do it with an example:

  1. Make sure you have a script or something to be run with cron_d (in this example, script.py).
  2. Don't forget to include the cron recipe
  3. If your script is in your cookbook, copy it to the machine like the example below.
  4. Create the cron, like in the example below.

cron_recipe.rb

include_recipe "cron"

cookbook_file '/etc/cron.d/script.py' do
  source 'folder/script.py'
  owner 'root'
  group 'root'
  mode '0777'
  action :create
end

cron_d "example_cron" do
  minute '0'
  hour '0'
  command '/etc/cron.d/script.py'
  user 'root'
end

Now you can create your spec:

  • Make sure you have a spec_helper or other setup to construct your context.

cron_recipe_spec.rb

# encoding: UTF-8
require 'spec_helper'

describe 'cookbook::cron_recipe' do
  CookbookTest.contexts.each do |ctext|
    context '#{ctext[:platform]}-#{ctext[:version]}' do
      cached(:chef_run) do
        CookbookTest.runner(ctext).converge(described_recipe)
      end

      it 'includes the cron_recipe that we are testing' do
        expect(chef_run).to include_recipe(described_recipe)
      end

      it 'includes the cron recipe' do
        expect(chef_run).to include_recipe('cron')
      end

      it 'creates a script.py file' do
        expect(chef_run).to create_cookbook_file('/etc/cron.d/script.py').with(
          :user => 'root',
          :group => 'root',
          :mode => '0777'
        )
      end

      it 'creates the cron' do
        expect(chef_run).to create_cron_d('example_cron').with(
          :command => '/etc/cron.d/script.py',
          :user => 'root',
          :minute => '0',
          :hour => '0'
        )
      end
    end
  end
end
Christine
  • 640
  • 4
  • 9