2018-03-06-chef-13-ruby_block-chefspec.md 1.78 KB
Newer Older
1 2 3
---
title: 'Chef 13 Upgrade: Testing `ruby_block`s with ChefSpec'
description: 'Replace your `block.old_run_action` with `block.block.call` to trigger `ruby_block`s within ChefSpec 7 and Chef 13.'
4 5 6 7 8 9 10 11 12 13 14
categories:
- blogumentation
- chef-13-upgrade
tags:
- blogumentation
- chef-13-upgrade
- chef-13-upgrade-chefspec
- chef
- chefspec
- chef-13
- chefspec-7
15
image: /img/vendor/chef-logo.png
16
date: 2018-03-06T20:34:46+00:00
17 18
license_prose: CC-BY-NC-SA-4.0
license_code: Apache-2.0
19
slug: chef-13-ruby_block-chefspec
20
---
21
{{< partialCached "posts/chef-13/intro.html" >}}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

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)`:

```ruby
# 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`:

```diff
 # 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`][proc-call]).

[proc-call]: https://ruby-doc.org/core-2.4.2/Proc.html#method-i-call