6

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

Here is my resource:

actions :install, :uninstall
default_action :install

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

Here is my provider:

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

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

Here is the console output:

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

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

sethvargo
  • 26,739
  • 10
  • 86
  • 156
Robert
  • 10,403
  • 14
  • 67
  • 117

1 Answers1

14

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

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

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

skmichaelson
  • 450
  • 5
  • 7
punkle
  • 952
  • 1
  • 8
  • 17
  • Am I correct in saying the cookbook containing the resource to be tested needs a [test] recipe to converge, in order to be able to step into the resource? The documentation is a little sketchy on that. – Jason Capriotti Feb 09 '16 at 16:50
  • Why are you using `ChefSpec::Runner` and not `ChefSpec::SoloRunner` or `ChefSpec::ServerRunner`? – spuder Mar 27 '16 at 19:05
  • ChefSpec::SoloRunner and ChefSpec::ServerRunner were introduced after this question was asked and answered. They were introduced in version 4.1.0 of ChefSpec. https://github.com/sethvargo/chefspec/blob/master/CHANGELOG.md#410-october-12-2014 – punkle Mar 30 '16 at 10:51
  • How do I add platform and version parameters? For example, I tried ChefSpec::SoloRunner.new(step_into: ['my_apache'], platform: 'mac_os_x', version: '10.10') but got error saying "syntax error near token step_into" – Chris F Sep 06 '17 at 03:33