Chef 13 Upgrade: Testing ruby_block
s with ChefSpec
As part of an upgrade from Chef 12 to Chef 13, this is one of the posts in which I've been detailing the issues I've encountered, and how I've resolved them .
ChefSpec doesn't execute ruby_block
s by default, and instead requires you to manually trigger it within your test. In Chef 12, we would be able to do this by calling block.old_run_action(:run)
:
# recipe
ruby_block "get the '#{key_name}' key" do
block do
node.run_state["public_key/#{safe_key_name}"] = ::File.read("#{node['etc']['passwd'][username]['dir']}/.ssh/#{safe_key_name}.pub")
Chef::Recipe::RubyBlockHelper.run_state_public_key(node, username, safe_key_name)
end
end
# spec
it ' ... ' do
block = chef_run.find_resource('ruby_block', 'get the \'blah blah key\' key')
block.old_run_action(:run)
expect(chef_run.node.run_state['public_key/blah_blah_key']).to eq 'ssh-rsa blah'
end
This was removed in Chef 13, so instead we must use block.block.call
:
# spec
it ' ... ' do
block = chef_run.find_resource('ruby_block', 'get the \'blah blah key\' key')
- block.old_run_action(:run)
+ block.block.call
expect(chef_run.node.run_state['public_key/blah_blah_key']).to eq 'ssh-rsa blah'
end
This works on both Chef 12 and Chef 13, due to the fact that ruby_block
's block
property is of type Proc
(Proc#call
).