# Hooking into execute resources in chefspec

**URL:** <https://discourse.chef.io/t/hooking-into-execute-resources-in-chefspec/5868>\
**Category:** Chef Infra (archive)\
**Created:** [October 29, 2014, 10:21pm UTC](https://discourse.chef.io/t/hooking-into-execute-resources-in-chefspec/5868 "2014-10-29T22:21:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Nuspl](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joe_nuspl/32/3243_2.png) [@Joe\_Nuspl](https://discourse.chef.io/u/Joe_Nuspl)\
**Post date:** [October 29, 2014, 10:21pm UTC](https://discourse.chef.io/t/hooking-into-execute-resources-in-chefspec/5868/1 "2014-10-29T22:21:29Z")

</div>

I’m automating some 3rd party code. What I have is something like:

execute ‘refresh’ do  
only\_if { CONDITION }  
end

execute ‘reconfigure’ do  
only\_if { ::File.exists?(GUARD) }  
end

Sometimes refresh creates GUARD. GUARD could also be created by an admin. reconfigure removes GUARD upon success.

I am try to write chefspec tests to cover all the cases:

```
1) no-existing-guard refresh-creates-gaurd
2) no-existing-guard refresh-does-not-create-guard

3) existing-guard refresh-creates-guard
4) existing-guard refresh-does-not-create-guard

5) no-existing-guard no-refresh
6) exisiting-guard no-refresh

```

What I want to do is to “hook” into execute[refresh] and conditionally update the ::File.exists? stub to return true.

If I update the stub before the run, I’ve collapsed #1 and #4 into one case.

Is it possible to hook into a resource? I tried :step\_into but that did not seem to do the trick. Any guidance would be appreciated.

Thanks.

```
Joe
```

---

<div class="post-metadata">

**Author:** ![Ranjib](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ranjib/32/1040_2.png) [@Ranjib](https://discourse.chef.io/u/Ranjib)\
**Post date:** [October 29, 2014, 11:17pm UTC](https://discourse.chef.io/t/hooking-into-execute-resources-in-chefspec/5868/2 "2014-10-29T23:17:30Z")

</div>

chefspec does not allow stubbing block style guards, it does allow command  
/ string style gurards. If you change the guard to use `test -e file` (and  
from your example, it looks you can) then you use stub\_command("test -e  
file").and\_return(true) or false depending upon your use case

On Wed, Oct 29, 2014 at 3:21 PM, Joe Nuspl [nuspl@nvwls.com](mailto:nuspl@nvwls.com) wrote:

> I’m automating some 3rd party code. What I have is something like:
> 
> execute ‘refresh’ do  
> only\_if { CONDITION }  
> end
> 
> execute ‘reconfigure' do  
> only\_if { ::File.exists?(GUARD) }  
> end
> 
> Sometimes refresh creates GUARD. GUARD could also be created by an admin.  
> reconfigure removes GUARD upon success.
> 
> I am try to write chefspec tests to cover all the cases:
> 
> 1. no-existing-guard refresh-creates-gaurd
> 
> 2. no-existing-guard refresh-does-not-create-guard
> 
> 3. existing-guard refresh-creates-guard
> 
> 4. existing-guard refresh-does-not-create-guard
> 
> 5. no-existing-guard no-refresh
> 
> 6. exisiting-guard no-refresh
> 
> What I want to do is to “hook” into execute[refresh] and conditionally  
> update the ::File.exists? stub to return true.
> 
> If I update the stub before the run, I’ve collapsed #1 and #4 into one  
> case.
> 
> Is it possible to hook into a resource? I tried :step\_into but that did  
> not seem to do the trick. Any guidance would be appreciated.
> 
> Thanks.
> 
> Joe

---

<div class="post-metadata">

**Author:** ![Joe\_Nuspl](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joe_nuspl/32/3243_2.png) [@Joe\_Nuspl](https://discourse.chef.io/u/Joe_Nuspl)\
**Post date:** [October 30, 2014, 6:16pm UTC](https://discourse.chef.io/t/hooking-into-execute-resources-in-chefspec/5868/3 "2014-10-30T18:16:51Z")

</div>

Stubbing of block style guards seems to work for me:

before do  
allow(File).to receive(:exists?).and\_call\_original  
allow(File).to receive(:exists?).with('/tmp/guard').and\_return(true)  
end

But that was not this issue I was describing. I was wondering if there was a way to run some code after the runner processed “execute[refresh]” but before “execute[reconfigure]”.

```
Joe

```

On Oct 29, 2014, at 4:17 PM, Ranjib Dey [dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com) wrote:

> chefspec does not allow stubbing block style guards, it does allow command / string style gurards. If you change the guard to use `test -e file` (and from your example, it looks you can) then you use stub\_command("test -e file").and\_return(true) or false depending upon your use case
> 
> On Wed, Oct 29, 2014 at 3:21 PM, Joe Nuspl [nuspl@nvwls.com](mailto:nuspl@nvwls.com) wrote:  
> I’m automating some 3rd party code. What I have is something like:
> 
> execute ‘refresh’ do  
> only\_if { CONDITION }  
> end
> 
> execute ‘reconfigure' do  
> only\_if { ::File.exists?(GUARD) }  
> end
> 
> Sometimes refresh creates GUARD. GUARD could also be created by an admin. reconfigure removes GUARD upon success.
> 
> I am try to write chefspec tests to cover all the cases:
> 
> 1. no-existing-guard refresh-creates-gaurd
> 
> 2. no-existing-guard refresh-does-not-create-guard
> 
> 3. existing-guard refresh-creates-guard
> 
> 4. existing-guard refresh-does-not-create-guard
> 
> 5. no-existing-guard no-refresh
> 
> 6. exisiting-guard no-refresh
> 
> What I want to do is to “hook” into execute[refresh] and conditionally update the ::File.exists? stub to return true.
> 
> If I update the stub before the run, I’ve collapsed #1 and #4 into one case.
> 
> Is it possible to hook into a resource? I tried :step\_into but that did not seem to do the trick. Any guidance would be appreciated.
> 
> Thanks.
> 
> Joe

---

<div class="post-metadata">

**Author:** ![Lamont\_Granquist\_OLD](https://avatars.discourse-cdn.com/v4/letter/l/7feea3/32.png) [@Lamont\_Granquist\_OLD](https://discourse.chef.io/u/Lamont_Granquist_OLD)\
**Post date:** [October 30, 2014, 8:26pm UTC](https://discourse.chef.io/t/hooking-into-execute-resources-in-chefspec/5868/4 "2014-10-30T20:26:53Z")

</div>

you can and\_return(true, false) to return a sequence of a values...

> <https://stackoverflow.com/questions/5947999/can-rspec-stubbed-method-return-different-values-in-sequence/5948282#5948282>

that doesn't answer your question, but i think that's what you're  
trying to do?

On Thu Oct 30 11:16:51 2014, Joe Nuspl wrote:

> Stubbing of block style guards seems to work for me:
> 
> ```
> before do
> allow(File).to receive(:exists?).and_call_original
> allow(File).to receive(:exists?).with('/tmp/guard').and_return(true)
> end
> 
> ```
> 
> But that was not this issue I was describing. I was wondering if  
> there was a way to run some code after the runner processed  
> “execute[refresh]” but before “execute[reconfigure]”.
> 
> Joe
> 
> On Oct 29, 2014, at 4:17 PM, Ranjib Dey \<[dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com)  
> [mailto:dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com)\> wrote:
> 
> > chefspec does not allow stubbing block style guards, it does allow  
> > command / string style gurards. If you change the guard to use `test -e file` (and from your example, it looks you can) then you use  
> > stub\_command("test -e file").and\_return(true) or false depending upon  
> > your use case
> > 
> > On Wed, Oct 29, 2014 at 3:21 PM, Joe Nuspl \<[nuspl@nvwls.com](mailto:nuspl@nvwls.com)  
> > [mailto:nuspl@nvwls.com](mailto:nuspl@nvwls.com)\> wrote:
> > 
> > ```
> > I’m automating some 3rd party code. What I have is something like:
> > 
> > execute ‘refresh’ do
> > only_if { CONDITION }
> > end
> > 
> > execute ‘reconfigure' do
> > only_if { ::File.exists?(GUARD) }
> > end
> > 
> > Sometimes refresh creates GUARD. GUARD could also be created by
> > an admin. reconfigure removes GUARD upon success.
> > 
> > I am try to write chefspec tests to cover all the cases:
> > 
> > 1) no-existing-guard refresh-creates-gaurd
> > 2) no-existing-guard refresh-does-not-create-guard
> > 
> > 3) existing-guard refresh-creates-guard
> > 4) existing-guard refresh-does-not-create-guard
> > 5) no-existing-guard no-refresh
> > 6) exisiting-guard no-refresh
> > 
> > What I want to do is to “hook” into execute[refresh] and
> > conditionally update the ::File.exists? stub to return true.
> > 
> > If I update the stub before the run, I’ve collapsed #1 and #4
> > into one case.
> > 
> > Is it possible to hook into a resource? I tried :step_into but
> > that did not seem to do the trick. Any guidance would be
> > appreciated.
> > 
> > Thanks.
> > 
> > Joe
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![Joe\_Nuspl](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joe_nuspl/32/3243_2.png) [@Joe\_Nuspl](https://discourse.chef.io/u/Joe_Nuspl)\
**Post date:** [October 30, 2014, 8:35pm UTC](https://discourse.chef.io/t/hooking-into-execute-resources-in-chefspec/5868/5 "2014-10-30T20:35:31Z")

</div>

Thanks! I can use that to emulate the test scenarios.

```
Joe

```

On Oct 30, 2014, at 1:26 PM, Lamont Granquist [lamont@opscode.com](mailto:lamont@opscode.com) wrote:

> you can and\_return(true, false) to return a sequence of a values...
> 
> [ruby on rails - Can RSpec stubbed method return different values in sequence? - Stack Overflow](http://stackoverflow.com/a/5948282/506908)
> 
> that doesn't answer your question, but i think that's what you're trying to do?
> 
> On Thu Oct 30 11:16:51 2014, Joe Nuspl wrote:
> 
> > Stubbing of block style guards seems to work for me:
> > 
> > before do  
> > allow(File).to receive(:exists?).and\_call\_original  
> > allow(File).to receive(:exists?).with('/tmp/guard').and\_return(true)  
> > end
> > 
> > But that was not this issue I was describing. I was wondering if  
> > there was a way to run some code after the runner processed  
> > “execute[refresh]” but before “execute[reconfigure]”.
> > 
> > Joe
> > 
> > On Oct 29, 2014, at 4:17 PM, Ranjib Dey \<[dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com)  
> > [mailto:dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com)\> wrote:
> > 
> > > chefspec does not allow stubbing block style guards, it does allow  
> > > command / string style gurards. If you change the guard to use `test -e file` (and from your example, it looks you can) then you use  
> > > stub\_command("test -e file").and\_return(true) or false depending upon  
> > > your use case
> > > 
> > > On Wed, Oct 29, 2014 at 3:21 PM, Joe Nuspl \<[nuspl@nvwls.com](mailto:nuspl@nvwls.com)  
> > > [mailto:nuspl@nvwls.com](mailto:nuspl@nvwls.com)\> wrote:
> > > 
> > > I’m automating some 3rd party code. What I have is something like:
> > > 
> > > ```
> > > execute ‘refresh’ do
> > > only_if { CONDITION }
> > > end
> > > 
> > > execute ‘reconfigure' do
> > > only_if { ::File.exists?(GUARD) }
> > > end
> > > 
> > > ```
> > > 
> > > Sometimes refresh creates GUARD. GUARD could also be created by  
> > > an admin. reconfigure removes GUARD upon success.
> > > 
> > > I am try to write chefspec tests to cover all the cases:
> > > 
> > > 1. no-existing-guard refresh-creates-gaurd
> > > 
> > > 2. no-existing-guard refresh-does-not-create-guard
> > > 
> > > 3. existing-guard refresh-creates-guard
> > > 
> > > 4. existing-guard refresh-does-not-create-guard
> > > 
> > > 5. no-existing-guard no-refresh
> > > 
> > > 6. exisiting-guard no-refresh
> > > 
> > > What I want to do is to “hook” into execute[refresh] and  
> > > conditionally update the ::File.exists? stub to return true.
> > > 
> > > If I update the stub before the run, I’ve collapsed #1 and #4  
> > > into one case.
> > > 
> > > Is it possible to hook into a resource? I tried :step\_into but  
> > > that did not seem to do the trick. Any guidance would be  
> > > appreciated.
> > > 
> > > Thanks.
> > > 
> > > Joe
