Skip to content

Instantly share code, notes, and snippets.

@retr0h
Created September 30, 2012 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retr0h/3808196 to your computer and use it in GitHub Desktop.
Save retr0h/3808196 to your computer and use it in GitHub Desktop.
# In default.rb from cookbook being tested
# node.run_state['reboot'] = true
it "flags system to reboot" do
chef_run.node.run_state['reboot'].should_be true
end
@acrmp
Copy link

acrmp commented Sep 30, 2012

https://www.relishapp.com/rspec/rspec-expectations/v/2-11/docs/built-in-matchers/be-matchers

it "flags system to reboot" do
  chef_run.node.run_state['reboot'].should be_true
end

it "flags system to reboot" do
  chef_run.node.run_state['reboot'].should be
end

@acrmp
Copy link

acrmp commented Sep 30, 2012

it "flags system to reboot" do
  puts chef_run.node.run_state.inspect
end
$ rspec
{:template_cache=>{}, :seen_recipes=>{"foo::default"=>true}, :seen_attributes=>{}, "reboot"=>true}

@retr0h
Copy link
Author

retr0h commented Sep 30, 2012

https://github.com/retr0h/cookbook-sol

emopop:sol(master)% rspec

{:template_cache=>{}, :seen_recipes=>{"sol::default"=>true, "reboot-handler"=>true, "chef_handler"=>true}, :seen_attributes=>{}}

@acrmp
Copy link

acrmp commented Sep 30, 2012

Hi John,

This is because the run_state is modified within a ruby_block. Chefspec treats ruby_block resources the same as any other resource - it doesn't actually apply the resource.

If you specifically want to converge a resource you can do something like the following (there is probably a nicer way of expressing this):

chef_run.resources.find{|r| r.name == 'setting reboot flag'}.old_run_action(:create) 

@retr0h
Copy link
Author

retr0h commented Oct 1, 2012

Awesome! I owe you a beer! :)

@retr0h
Copy link
Author

retr0h commented Oct 1, 2012

That works. Thanks much for your help. I feel like I'm cheating a bit.
It's nice to know my 'update-grub' execute is the one making the call to the 'setting reboot flag' resource.

Maybe, there is no good way to test that.

@k-popov
Copy link

k-popov commented May 12, 2017

With modern versions of ChefSpec the following works:
chef_run.find_resource(:ruby_block, 'my ruby_block name').old_run_action(:create)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment