chefspec测试中的new_resource赋值

时间:2015-08-21 15:41:36

标签: chef chefspec

我坚持使用chefspec测试,该测试会进入下面的lwrp并断言它会创建一个目录。

describe 'mw-tomcat::chefspec' do

  let(:chef) do
    ChefSpec::SoloRunner.new(step_into: ['mw_tomcat_tree']) do |node|
      node.set['mw']['domain'] = domain
      node.set['mw']['gemrepo'] = gemrepo
    end
  end

  let(:chef_run) { chef.converge(described_recipe) }

  it 'creates the catalina_base directory' do
    expect(chef_run).to create_directory('/apps/bweb/apps/instance1')
  end

done

如果我取消注释下面的行,则测试有效,但在尝试从new_resource.settings获取值时失败

action :create do
  settings = new_resource.settings
  catalina_base=settings['catalina_base']
  #catalina_base='/apps/bweb/apps/instance1'

  directory "#{catalina_base}" do
    mode 0755
    owner settings['user']
    group settings['group']
    recursive true
    action :create
  end

end

在chefspec中有没有办法传递上面的new_resource值?

感谢您的帮助。

爱德华

*更新

它失败并显示以下消息:

[2015-08-21T19:25:02+01:00] DEBUG: resources for generic directory resource enabled on node include: [Chef::Resource::Directory]
[2015-08-21T19:25:02+01:00] DEBUG: resources that survived replacement include: [Chef::Resource::Directory]
[2015-08-21T19:25:02+01:00] DEBUG: resources for generic directory resource enabled on node include: [Chef::Resource::Directory]
[2015-08-21T19:25:02+01:00] DEBUG: resources that survived replacement include: [Chef::Resource::Directory]
[2015-08-21T19:25:02+01:00] DEBUG: resources for generic directory resource enabled on node include: [Chef::Resource::Directory]
[2015-08-21T19:25:02+01:00] DEBUG: resources that survived replacement include: [Chef::Resource::Directory]
[2015-08-21T19:25:02+01:00] DEBUG: resources for generic directory resource enabled on node include: [Chef::Resource::Directory]
[2015-08-21T19:25:02+01:00] DEBUG: resources that survived replacement include: [Chef::Resource::Directory]
[2015-08-21T19:25:02+01:00] DEBUG: resources for generic link resource enabled on node include: [Chef::Resource::Link]
[2015-08-21T19:25:02+01:00] DEBUG: resources that survived replacement include: [Chef::Resource::Link]
[2015-08-21T19:25:02+01:00] DEBUG: resources for generic link resource enabled on node include: [Chef::Resource::Link]
[2015-08-21T19:25:02+01:00] DEBUG: resources that survived replacement include: [Chef::Resource::Link]
[2015-08-21T19:25:02+01:00] INFO: Processing directory[] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 51)
[2015-08-21T19:25:02+01:00] INFO: Processing directory[/bin] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 77)
[2015-08-21T19:25:02+01:00] INFO: Processing directory[/conf] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 77)
[2015-08-21T19:25:02+01:00] INFO: Processing directory[/lib] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 77)
[2015-08-21T19:25:02+01:00] INFO: Processing directory[/logs] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 77)
[2015-08-21T19:25:02+01:00] INFO: Processing directory[/temp] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 77)
[2015-08-21T19:25:02+01:00] INFO: Processing directory[/webapps] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 77)
[2015-08-21T19:25:02+01:00] INFO: Processing directory[/work] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 77)
[2015-08-21T19:25:02+01:00] INFO: Processing link[/lib/BESTFilter] action create (/home/we_edqui/mw-chef-repo/cookbooks/mw-tomcat/providers/tree.rb line 109)
  creates the catalina_base directory (FAILED - 1)

Failures:

  1) mw-tomcat::chefspec creates the catalina_base directory
     Failure/Error: expect(chef_run).to create_directory('/apps/bweb/apps/instance1')
       expected "directory[/apps/bweb/apps/instance1]" with action :create to be in Chef run. Other directory resources:

         directory[]
         directory[/bin]
         directory[/conf]
         directory[/lib]
         directory[/logs]
         directory[/temp]
         directory[/webapps]
         directory[/work]

     # ./spec/default_spec.rb:68:in `block (2 levels) in <top (required)>'

Finished in 0.80061 seconds (files took 4.72 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/default_spec.rb:67 # mw-tomcat::chefspec creates the catalina_base directory

完整输出位于http://fpaste.org/257744/14401829/

1 个答案:

答案 0 :(得分:1)

正如this非常详细的ChefSpec错误报告中所述,您可能无法做到这一点,因为new_resource对象实际上是在运行时动态创建的。

如果您决定使其工作,则有pretty complex workaround,但目前首选的方法是将LWRP转换为HWRP并使用标准RSpec方法模拟输入。