cron_d的chefspec NoMethodError

时间:2015-07-17 17:47:31

标签: ruby-on-rails chef chef-recipe test-kitchen chefspec

我有一个成功的资源:(融合,按预期工作)

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

但添加规格后

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

chefspec收到错误:

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)

为什么会这样?

匹配器已经定义为https://github.com/opscode-cookbooks/cron/blob/master/libraries/matchers.rb

我的spec文件中是否需要includerequire(到目前为止还没有工作)?或者我需要创建自己的?

(编辑:stackoverflow autobot让我添加ruby-on-rails标签,所以我做了。)

2 个答案:

答案 0 :(得分:0)

spec_helper.rb或规范文件的开头添加以下行。

require 'chefspec'

答案 1 :(得分:0)

你错过了一些东西,而且你有一个间距错字。

以下是如何使用示例

  1. 确保您使用cron_d运行脚本或其他内容(在此示例中为script.py)。
  2. 别忘了加入cron食谱
  3. 如果您的脚本在您的食谱中,请将其复制到机器,如下例所示。
  4. 创建cron,如下例所示。
  5. <强> 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
    

    现在您可以创建规范:

    • 确保您有spec_helper或其他设置来构建您的上下文。

    <强> 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