Search code examples
chef-infrachefspec

new_resource assignment in chefspec test


I'm stuck on a chefspec test which steps into my lwrp below and asserts that it creates a directory.

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

The test works if I uncomment the line below, but fails when trying to get the value from 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

Is there a way in chefspec to pass in the new_resource value above?

Thanks for any help.

Edward

*Update

It fails with the following message:

[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

The full output is at http://fpaste.org/257744/14401829/


Solution

  • As outlined in this pretty detailed ChefSpec bug report, what you are trying to do is probably not possible because the new_resource object is actually created dynamically at run time.

    There is a pretty complex workaround for this if you are determined to make it work, however the currently preferred method would be to convert your LWRP to an HWRP and use the standard RSpec methods for simulating input.