测试 Chef 食谱失败

Testing Chef cookbook failure

我正在编写一本带有自定义资源的食谱,该资源(除其他外)通过 ssh-keygen 验证 SSH 密钥。我需要测试用户输入无效输入和资源应该根据异常引发的场景,所以我正在寻找一种方法来验证 'given following input ... Chef run fails'。

如果我理解正确,Test Kitchen 意味着每次收敛都成功结束,而 ChefSpec 意味着资源永远不会真正执行(所以我的 ssh-keygen 调用根本不会被调用)。

是否有任何常规方法可以测试这种情况?

要测试 custom_resource 的内部位,您必须告诉 ChefSpec step_into 它。你是对的,ChefSpec 在正常情况下不会执行提供程序。

要对失败进行适当的测试,您应该 expect(:chef_run).to raise_error 描述 here in the documentation

step_into 的文档引用:

In order to run the actions exposed by your LWRP, you have to explicitly tell the Runner to step into it:

require 'chefspec'

describe 'foo::default' do
  let(:chef_run) do
    ChefSpec::SoloRunner.new(step_into: ['my_lwrp']).converge('foo::default')
  end

  it 'installs the foo package through my_lwrp' do
    expect(chef_run).to install_package('foo')
  end
end

NOTE: If your cookbook exposes LWRPs, it is highly recommended you also create a libraries/matchers.rb file as outlined below in the "Packaging Custom Matchers" section. You should never step_into an LWRP unless you are testing it. Never step_into an LWRP from another cookbook!