0

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

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

我是否需要includerequire在我的规范文件中使用某些东西(到目前为止都没有工作)?还是我需要创建自己的?

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

4

2 回答 2

0

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

require 'chefspec'
于 2015-07-17T20:00:18.610 回答
0

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

以下是如何通过示例进行操作

  1. 确保您有一个脚本或要使用 cron_d 运行的东西(在本例中为script.py)。
  2. 不要忘记包括cron食谱
  3. 如果您的脚本在您的食谱中,请将其复制到机器上,如下例所示。
  4. 创建 cron,如下例所示。

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
于 2017-07-04T21:50:06.997 回答