- From: AJ Christensen <
>
- To: "
" <
>
- Subject: [chef] Re: Re: Re: Re: ChefSpec question
- Date: Thu, 19 Mar 2015 07:26:38 +1300
Yo,
Try this bad boy, hinted at by the error message in your example: (You
didn't pass a block):
expect { chef_run }.to raise_error(SomeErrorClass)
https://github.com/sethvargo/chefspec/blob/master/examples/expect_exception/spec/compile_error_spec.rb
https://github.com/sethvargo/chefspec/blob/master/examples/expect_exception/spec/converge_error_spec.rb
https://github.com/sethvargo/chefspec/blob/master/examples/expect_exception/spec/no_error_spec.rb
when in doubt, peruse the chefspec examples - they are organized very
intuitively.
cheers,
--aj
On Thu, Mar 19, 2015 at 6:29 AM, ANGELA EBIRIM
<
>
wrote:
>
Hi Brandon,
>
>
This is so frustrating.
>
>
I've been trying all afternoon to successfully stub the
>
Chef::EncryptedDataBagItem.load method without success!
>
>
This is my test file:
>
>
describe 'core::local_users_linux' do
>
>
let(:chef_run) do
>
ChefSpec::SoloRunner.new do |node|
>
node.set['core']['userlist'] = 'angela'
>
end.converge(described_recipe)
>
end
>
>
before do
>
allow(Chef::EncryptedDataBagItem).to receive(:load).and_return({
>
"id" => "angela",
>
"password" => "London2014"
>
})
>
end
>
context 'linux' do
>
it 'stubbed databag does not raise error' do
>
>
expect(chef_run).to_not raise_error
>
end
>
end
>
end
>
>
When I test this file, I get the following error:-
>
F
>
>
Failures:
>
>
1) core::local_users_linux linux stubbed databag does not raise error
>
Failure/Error: expect(chef_run).to_not raise_error
>
expected no Exception but was not given a block
>
# ./spec/local_users_linux_spec.rb:27:in `block (3 levels) in <top
>
(required)>'
>
>
Finished in 11.94 seconds (files took 52.17 seconds to load)
>
1 example, 1 failure
>
>
Failed examples:
>
>
rspec ./spec/local_users_linux_spec.rb:25 # core::local_users_linux linux
>
stubbed databag does not raise error
>
>
ChefSpec Coverage report generated...
>
>
Total Resources: 12
>
Touched Resources: 0
>
Touch Coverage: 0.0%
>
>
Untouched Resources:
>
>
chef_gem[ruby-shadow] C:0
>
package[whois] C:0
>
package[members] C:0
>
group[sysadmin] C:0
>
group[sudo] C:0
>
user[svc_goagent] C:0
>
users_manage[sudo] C:0
>
template[/etc/sudoers.d/svc_goagent.conf] C:0
>
user[jg-admin] C:0
>
template[/etc/sudoers.d/jg-admin.conf] C:0
>
user[svc_appinfo] C:0
>
template[/etc/sudoers.d/svc_appinfo.conf] C:0
>
>
Should it be trying to run the load method in my recipe?. Shouldn't that be
>
stubbed and shouldn't the test be returning my canned hash?
>
>
Please help!
>
>
Angela
>
>
>
Sent from iCloud
>
>
>
On Mar 17, 2015, at 04:14 PM, Brandon Raabe
>
<
>
>
wrote:
>
>
Angela,
>
>
You'll need to stub the data bag calls for each of the users, or better yet,
>
if that list of users is being provided through attributes, stub that
>
attribute with a mock list of user(s). Something like this:
>
>
runner.node.set['my']['userlist'] = ['foo']
>
>
>
You can quickly test this is the issue by adding the following into your
>
before block.
>
>
allow(Chef::EncryptedDataBagItem).to receive(:load).and_return({})
>
>
This will stub the load method regardless of the parameters passed to the
>
load method, and will return an empty hash, which you can update to your
>
liking.
>
>
>
Thanks,
>
>
Brandon
>
>
On Tue, Mar 17, 2015 at 10:09 AM, ANGELA EBIRIM
>
<
>
>
wrote:
>
>
>
> Hi Brandon,
>
>
>
> Yes.
>
>
>
> In the main recipe, it iterates through the userlist and decrypts each
>
> encrypted data bag.
>
>
>
> What I would like to do is to stub the code that performs the decryption
>
> (have refactored my code so have put the decryption into a method) so
>
> essentially pass it a canned encrypted data bag and have it spit out a
>
> canned id and password. This will ensure that this error Failed to read the
>
> private key C:\chef\client.pem: #<Errno::ENOENT: No such file or directory
>
> -
>
> C:\chef\client.pem> doens't arise as the method that calls the decryption
>
> will be stubbed.
>
>
>
> Hope that helps.
>
>
>
> Thanks
>
>
>
> Sent from iCloud
>
>
>
>
>
> On Mar 16, 2015, at 09:45 AM, Brandon Raabe
>
> <
>
>
> wrote:
>
>
>
> Angela, Are you loading more than one encrypted data bag item in that
>
> recipe? From the backtrace, it looks like you might be iterating through a
>
> list of users.
>
>
>
>
>
> On Mon, Mar 16, 2015 at 7:47 AM, ANGELA EBIRIM
>
> <
>
>
> wrote:
>
>>
>
>> Still getting the same error which means the stub isn't working.
>
>>
>
>> thanks anyway
>
>>
>
>>
>
>> Sent from iCloud
>
>>
>
>>
>
>> On Mar 16, 2015, at 06:27 AM, Tensibai
>
>> <
>
>
>> wrote:
>
>>
>
>> Quoting the README of ChefSpec on this subject:
>
>> (https://github.com/sethvargo/chefspec#data-bag--data-bag-item)
>
>>
>
>> If you are using Encrypted Data Bag Items, you'll need to dive into the
>
>> RSpec layer and stub that class method instead:
>
>>
>
>> describe 'example::default' do
>
>> before do
>
>> allow(Chef::EncryptedDataBagItem).to receive(:load).with('users',
>
>> 'svargo').and_return(...)
>
>> end
>
>> end
>
>>
>
>> Seems it had to be done in the before block.
>
>> Another entry of how to: https://github.com/sethvargo/chefspec/issues/249
>
>>
>
>> Hope this helps
>
>>
>
>> Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :
>
>>
>
>> Hello everyone,
>
>>
>
>> Does anyone here use ChefSpec regularly? If so, please help!
>
>>
>
>> I've got a situation where I have encrypted databags which I decrypt to
>
>> find the passwords for users (in my local_users recipe). I have generated
>
>> an
>
>> encrypted_data_bag_secret that I use with my Chef Solo.
>
>>
>
>> This is my spec file:
>
>>
>
>> #local_users_spec.rb
>
>>
>
>> require 'spec_helper'
>
>>
>
>> describe 'core::local_users' do
>
>>
>
>> let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe)}
>
>>
>
>> context 'windows' do
>
>> let(:chef_run) do
>
>> runner = ChefSpec::SoloRunner.new(
>
>>
>
>> 'platform' => 'windows',
>
>> 'version' => '2012'
>
>> )
>
>> runner.node.set['core']['install_flavor'] = 'windows'
>
>> runner.converge('core::local_users')
>
>> end
>
>>
>
>> it 'stubbed databag does not raise error' do
>
>> plain_pass = "London2014"
>
>>
>
>>
>
>> allow(Chef::EncryptedDataBagItem).to
>
>> receive(:load).with("aws-admin-passwords",
>
>> "svc_goagent")["password"].and_return(plain_pass)
>
>> expect(chef_run).to_not raise_error
>
>> end
>
>> end
>
>> end
>
>>
>
>> When I run this, I get the following:-
>
>>
>
>> C:\git\cookbook-core>rspec spec/local_users_spec.rb
>
>> ffi-yajl/json_gem is deprecated, these monkeypatches will be dropped
>
>> shortly
>
>> [2015-03-16T12:24:31+00:00] WARN: Failed to read the private key
>
>> C:\chef\client.pem: #<Errno::ENOENT: No such file or directory -
>
>> C:\chef\client.pem>
>
>> [2015-03-16T12:24:31+00:00] WARN: Can't decrypt password for svc_goagent
>
>> "Plain password for svc_goagent is "
>
>> C:/git/cookbook-core/recipes/local_users.rb:32:in `block in <top
>
>> (required)>': undefined local variable or method `node' for main:Object
>
>> (NameError)
>
>> from C:/git/cookbook-core/recipes/local_users.rb:22:in `each'
>
>> from C:/git/cookbook-core/recipes/local_users.rb:22:in `<top
>
>> (required)>'
>
>> from C:/git/cookbook-core/spec/spec_helper.rb:8:in
>
>> `require_relative'
>
>> from C:/git/cookbook-core/spec/spec_helper.rb:8:in `<top
>
>> (required)>'
>
>> from
>
>> C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
>
>> `require'
>
>> from
>
>> C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
>
>> `require'
>
>> from C:/git/cookbook-core/spec/local_users_spec.rb:1:in `<top
>
>> (required)>'
>
>> ...
>
>>
>
>> I expect the test to pass and not generate an error. However I receive an
>
>> error which indicates the test is not stubbing my encrypted data bag
>
>> (especially the No such file or directory - C:\chef\client.pem> errror).
>
>> Can
>
>> anyone help me with this?
>
>>
>
>> Thanks
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>
>
>
>
Archive powered by MHonArc 2.6.16.