unit-testing - 如何使用ChefSpec测试我的LWRP?

标签 unit-testing chef-infra chefspec

我创建了自定义LWRP,但是当我运行ChefSpec单元测试时。它不知道我的LWRP Action 。

这是我的资源:

actions :install, :uninstall
default_action :install

attribute :version, :kind_of => String
attribute :options, :kind_of => String

这是我的提供者:
def whyrun_supported?
  true
end

action :install do
  version = @new_resource.version
  options = @new_resource.options
  e = execute "sudo apt-get install postgresql-#{version} #{options}"
  @new_resource.updated_by_last_action(e.updated_by_last_action?)
end

这是我的ChefSpec单元测试:
require_relative '../spec_helper'

describe 'app_cookbook::postgresql' do

  let(:chef_run) do
    ChefSpec::Runner.new do |node|
      node.set[:app][:postgresql][:database_user] = 'test'
      node.set[:app][:postgresql][:database_password] = 'test'
      node.set[:app][:postgresql][:database_name] = 'testdb'
    end.converge(described_recipe)
  end

  it 'installs postgresql 9.1 package' do
    expect(chef_run).to run_execute('sudo apt-get install postgresql-9.1 -y --force-yes')
  end
end

这是控制台输出:
app_cookbook::postgresql

expected "execute[sudo apt-get install postgresql-9.1 -y --force-yes] with" action :run to be in Chef run. Other execute resources:

./spec/recipes/postgresql_spec.rb:23:in `block (2 levels) in <top (required)>'
  installs postgresql 9.1 package (FAILED - 1)

Failures:

  1) app_cookbook::postgresql installs postgresql 9.1 package
     Failure/Error: expect(chef_run).to run_execute('sudo apt-get install postgresql-9.1 -y --force-yes')
       expected "execute[sudo apt-get install postgresql-9.1 -y --force-yes] with" action :run to be in Chef run. Other execute resources:

     # ./spec/recipes/postgresql_spec.rb:23:in `block (2 levels) in <top (required)>'

1 example, 1 failure, 0 passed

我如何对使用LWRP Action 运行测试的ChefSpec表示看法?

最佳答案

您需要告诉chefspec进入您的资源。您可以按照以下步骤进行操作:

require_relative '../spec_helper'

describe 'app_cookbook::postgresql' do

  let(:chef_run) do
    ChefSpec::Runner.new(step_into: ['my_lwrp']) do |node|
      node.set[:app][:postgresql][:database_user] = 'test'
      node.set[:app][:postgresql][:database_password] = 'test'
      node.set[:app][:postgresql][:database_name] = 'testdb'
    end.converge(described_recipe)
  end

  it 'installs postgresql 9.1 package' do
    expect(chef_run).to run_execute('sudo apt-get install postgresql-9.1 -y --force-yes')
  end
end

您可以将my_lwrp替换为要进入的资源。

有关更多详细信息,请参见Chefspec存储库README中的Testing LWRPS部分。

关于unit-testing - 如何使用ChefSpec测试我的LWRP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562318/

相关文章:

linux - Chef 版本不匹配

chef-infra - 当需要密码访问 URL 时使用 Chef 执行 wget

ruby - chefspec:设置要在配方中的所有测试中使用的全局节点属性

chef-infra - 无方法错误 : undefined method `run_list_for' when testing role with ChefSpec

ruby - 关于 Ruby/ChefSpec 编码风格的反馈

c# - 如何在 Xamarin 中测试共享代码

unit-testing - 在 junit 3 中为测试套件指定测试方法名称前缀

ios - 获取代码覆盖率统计 IOS

mysql - Chef 客户端在处理 MySQL Recipe 期间出现错误

java - 如何模拟 servletContext 而不是 Servlet 或 HttpServletRequest?