Chefspec and lazy

I’m trying to write a unit test for a recipe that uses http://docs.getchef.com/resource_common.html#lazy-attribute-evaluation

It fails with:

Failure/Error: runner.converge(described_recipe)
NoMethodError:
undefined method `lazy’ for Chef::Resource::Execute

Any ideas on how to make this work?

Joe

Can you share a bit more of the code you have under test?

On Tue, Oct 21, 2014 at 5:34 PM, Joe Nuspl nuspl@nvwls.com wrote:

I’m trying to write a unit test for a recipe that uses
Common Resource Functionality

It fails with:

Failure/Error: runner.converge(described_recipe)
NoMethodError:
undefined method `lazy' for Chef::Resource::Execute

Any ideas on how to make this work?

Joe

I am able to reproduce the issue with the following snippet:

execute 'cmd' do
command lazy { node['cmd'] }
end

node.default['cmd'] = '/bin/true'

The corresponding spec is:

describe ‘test::lazy' do
subject do
ChefSpec::Runner.new.converge(described_recipe)
end

it { should run_execute('cmd') }
end

On Oct 21, 2014, at 3:10 PM, Peter Burkholder pburkholder@getchef.com wrote:

Can you share a bit more of the code you have under test?

On Tue, Oct 21, 2014 at 5:34 PM, Joe Nuspl nuspl@nvwls.com wrote:
I’m trying to write a unit test for a recipe that uses Common Resource Functionality

It fails with:

Failure/Error: runner.converge(described_recipe)
NoMethodError:
undefined method `lazy' for Chef::Resource::Execute

Any ideas on how to make this work?

Joe

Joe,

Seem the object node['cmd'] doesn't exist at compile time, so it can't be
deferred for lazy evaluation if it doesn't exist. This would probably work:

node.default['cmd'] = '/bin/false'

execute 'cmd' do
command lazy { node['cmd'] }
end

node.override['cmd'] = '/bin/true'

That said, be careful not to use node attributes as variables. They're
generally intended to be used as an interface to the cookbook.

On Tue, Oct 21, 2014 at 6:48 PM, Joe Nuspl nuspl@nvwls.com wrote:

I am able to reproduce the issue with the following snippet:

execute 'cmd' do
command lazy { node['cmd'] }
end

node.default['cmd'] = '/bin/true'

The corresponding spec is:

describe ‘test::lazy' do
subject do
ChefSpec::Runner.new.converge(described_recipe)
end

it { should run_execute('cmd') }
end

On Oct 21, 2014, at 3:10 PM, Peter Burkholder pburkholder@getchef.com
wrote:

Can you share a bit more of the code you have under test?

On Tue, Oct 21, 2014 at 5:34 PM, Joe Nuspl nuspl@nvwls.com wrote:

I’m trying to write a unit test for a recipe that uses
Common Resource Functionality

It fails with:

Failure/Error: runner.converge(described_recipe)
NoMethodError:
undefined method `lazy' for Chef::Resource::Execute

Any ideas on how to make this work?

Joe

Thanks.

The issue turned out to be a convoluted gem dependency tree that brought in chef-11.4.4 which was before lazy was supported.

Joe