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' dolet(:chef_run) doChefSpec::SoloRunner.new do |node|node.set['core']['userlist'] = 'angela'end.converge(described_recipe)endbefore doallow(Chef::EncryptedDataBagItem).to receive(:load).and_return({"id" => "angela","password" => "London2014"})endcontext 'linux' doit 'stubbed databag does not raise error' doexpect(chef_run).to_not raise_errorendendendWhen I test this file, I get the following error:-FFailures:1) core::local_users_linux linux stubbed databag does not raise errorFailure/Error: expect(chef_run).to_not raise_errorexpected 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 failureFailed examples:rspec ./spec/local_users_linux_spec.rb:25 # core::local_users_linux linux stubbed databag does not raise errorChefSpec Coverage report generated...Total Resources: 12Touched Resources: 0Touch Coverage: 0.0%Untouched Resources:chef_gem[ruby-shadow] C:0package[whois] C:0package[members] C:0group[sysadmin] C:0group[sudo] C:0user[svc_goagent] C:0users_manage[sudo] C:0template[/etc/sudoers.d/svc_goagent.conf] C:0user[jg-admin] C:0template[/etc/sudoers.d/jg-admin.conf] C:0user[svc_appinfo] C:0template[/etc/sudoers.d/svc_appinfo.conf] C:0Should 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!AngelaSent from iCloudAngela,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,BrandonOn Tue, Mar 17, 2015 at 10:09 AM, ANGELA EBIRIM < " target="_blank"> > 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.ThanksSent from iCloudAngela, 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 < " target="_blank"> > wrote:Still getting the same error which means the stub isn't working.thanks anywaySent from iCloudQuoting 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 helpsLe 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.rbrequire 'spec_helper'describe 'core::local_users' dolet(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe)}context 'windows' dolet(:chef_run) dorunner = ChefSpec::SoloRunner.new('platform' => 'windows','version' => '2012')runner.node.set['core']['install_flavor'] = 'windows'runner.converge('core::local_users')endit 'stubbed databag does not raise error' doplain_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_errorendendendWhen I run this, I get the following:-C:\git\cookbook-core>rspec spec/local_users_spec.rbffi-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.