Chef 13 Upgrade: Testing ruby_blocks 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_blocks 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).

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #chef-13-upgrade #chef-13-upgrade-chefspec #chef #chefspec #chef-13 #chefspec-7.

This post was filed under articles.

Has this content helped you? Please consider supporting me so I can continue to create content like this!

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.