本文介绍了如何使用ChefSpec测试我的LWRP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created my custom LWRP, but when I run the ChefSpec units test. It doesn't know my LWRP actions.

这是我的资源

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单元测试:

And here is my ChefSpec unit test:

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运行测试的ChefSpec进行说明

How can I say to ChefSpec that run the test with the LWRPs actions?

推荐答案

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

You need to tell chefspec to step into your resource. You can do this as follows:

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替换为要插入的资源。

You can replace my_lwrp with the resource which you want to step into.

更多详细信息,请参阅Chefspec存储库自述文件中的部分。

For more details, see the Testing LWRPS section in the Chefspec repo README.

这篇关于如何使用ChefSpec测试我的LWRP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 11:18