如何创建另一个配方中定义的虚拟资源,而不包括测试运行中的其他配方?

时间:2015-03-13 09:48:27

标签: unit-testing rspec chef chef-recipe chefspec

我有以下厨师食谱:

# recipes/default.rb

include_recipe 'my-cookbook::another_recipe'

execute 'Do some stuff' do
  command "echo some stuff"
  action :run
end

template "my_dir/my_file.txt" do
  source "my_file.txt.erb"
  owner 'myuser'
  group 'mygroup'
  notifies :restart, resources(:service => 'my-service'), :delayed
end

和另一个食谱

# recipes/another_recipe.rb

service 'my-service' do
  start_command "my-service start"
  stop_command "my-service stop"
  supports :start => true, :stop => true
  action :nothing
end

现在我想单独为default食谱编写一个Chefspec单元测试。所以我写了这个:

# spec/unit/recipes/default_spec.rb

require 'rspec/core'
require 'chefspec'

describe 'my-cookbook::default' do
  let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

  before do
    allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).with('my-cookbook::another_recipe')
  end

  it "Does the stuff" do
    expect(chef_run).to run_execute("echo some stuff")
  end

end

如何创建another_recipe中定义的服务的虚拟对象以防止发生这种情况:

 11:    group 'mygroup'
 12>>   notifies :restart, resources(:service => 'my-service'), :delayed
 13:  end
...
Failure/Error: let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
Chef::Exceptions::ResourceNotFound:
Cannot find a resource matching service[my-service] (did you define it first?)

我知道这可能是一个糟糕的设计和一个相当简单的新手问题,但我真的被困在这里,我的情况就是这样:

  • 我有几个月的厨师经验,但除此之外没有Ruby经验
  • 这是最初在没有任何单元测试的情况下编写的生产代码
  • 真正的代码包含更多内容,这些代码片段只是特定问题的模型
  • 我有一个修改default食谱的任务,所以我想添加一些单元测试以验证我的修改是否有效并且不会破坏现有功能
  • 此时我想尽量避免修改任何其他文件(即another_recipe
  • 如果我让测试也运行another_recipe那么我需要模拟并设置太多其他需要的东西

谢谢:) k6ps

3 个答案:

答案 0 :(得分:4)

我的观点:

您应该允许another_recipe运行并执行必要的收敛。如果不是,你就不能真正相信你的测试,因为他们没有对抗在跑步中发生的事情。

无论如何解决您的案件的解决方法:

那么你可以添加一个"嘲讽配方"在你的食谱中,你将定义无操作资源,你需要测试你的食谱,而不需要太多的存根/模拟调用。

让我们说它被称为&spec-receipe.rb'它看起来像:

service 'my-service' do
  action :nothing
end

然后你运行你的测试,包括这个' spec-recipe'像这样:

let(:chef_run) { ChefSpec::SoloRunner.converge('my_cookbook::spec-recipe',described_recipe) }

另一种方法可能是include_recipe 'my_cookbook::spec-recipe' if defined?(ChefSpec)所以这个食谱只包含在chefspec运行中而不是正常运行中,你不必在转轮声明中指定它。

答案 1 :(得分:0)

另一种方法是在配方中包含开始/救援,以便在必要时将服务添加到资源集合中。默认操作是:没有任何内容,因此您不需要在救援块中列出它。

begin
  svc = resources('service[my-service]')
rescue
  svc = service 'my-service'
end

template "my_dir/my_file.txt" do
  source "my_file.txt.erb"
  owner 'myuser'
  group 'mygroup'
  notifies :restart, svc, :delayed
end

答案 2 :(得分:0)

>>> print shlex.split(x,posix=False)
['/usr/bin/yum', 'update', '--exclude="foo', 'bar', 'baz*"']
相关问题