chef / chef-vault-testfixtures

provides an RSpec shared context for testing Chef cookbooks that use chef-vault
Apache License 2.0
7 stars 10 forks source link

ChefVault::Item.vault? mock doesn't work with String Data Bags #20

Open dougireton opened 8 years ago

dougireton commented 8 years ago

Summary:

ChefVault::Item.vault? mock doesn't work with String Data Bags

With this recipe snippet:

my_local_var = chef_vault_item('MyVault', 'MyVaultId')

And this data bag in `test/integration/data_bags/MyVault/MyVaultId.json

{
  "id": "MyVaultId",
  "username": "my_user",
  "password": "FakePassword"
}

And this ChefSpec test:

Spec.describe 'psp_os_config::default' do
  context 'When all attributes are default' do
    include ChefVault::TestFixtures.rspec_shared_context

    let(:chef_run) do
      runner = ChefSpec::SoloRunner.new(
        platform: 'windows',
        version: '2008R2'
      )
      runner.converge(described_recipe)
    end

    it 'converges successfully' do
      expect { chef_run }.to_not raise_error
    end
  end
end

I get this failure:

Failures:

  1) psp_os_config::default When all attributes are default converges successfully
     Failure/Error: expect { chef_run }.to_not raise_error

       expected no Exception, got #<RSpec::Mocks::MockExpectationError: #<ChefVault::Item (class)> received :vault? with unexpected arguments
         expected: (:MyVault, "myvaultid")
              got: ("MyVault", "MyVaultId")
       Diff:
       @@ -1,2 +1,2 @@
       -[:MyVault, "myvaultid"]
       +["MyVault", "MyVaultId"]

        Please stub a default value first if message might be received with other args as well.
       > with backtrace:
         # /var/folders/0l/d20v6pzx1qlg9wmkszr_v9202dm83c/T/d20160503-87735-ehmzea/cookbooks/chef-vault/libraries/helpers.rb:36:in `chef_vault_item'
         # /var/folders/0l/d20v6pzx1qlg9wmkszr_v9202dm83c/T/d20160503-87735-ehmzea/cookbooks/psp_os_config/recipes/set_posou_env.rb:29:in `from_file'
         # /var/folders/0l/d20v6pzx1qlg9wmkszr_v9202dm83c/T/d20160503-87735-ehmzea/cookbooks/psp_os_config/recipes/default.rb:52:in `from_file'
         # ./spec/unit/recipes/default_spec.rb:18:in `block (3 levels) in <top (required)>'
         # ./spec/unit/recipes/default_spec.rb:31:in `block (4 levels) in <top (required)>'
         # ./spec/unit/recipes/default_spec.rb:31:in `block (3 levels) in <top (required)>'
     # ./spec/unit/recipes/default_spec.rb:31:in `block (3 levels) in <top (required)>'

Finished in 1.17 seconds (files took 2.67 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/unit/recipes/default_spec.rb:30 # psp_os_config::default When all attributes are default converges successfully

Pry Session:

From: /var/folders/0l/d20v6pzx1qlg9wmkszr_v9202dm83c/T/d20160503-85502-1txoi2c/cookbooks/chef-vault/libraries/helpers.rb @ line 37 ChefVaultCookbook#chef_vault_item:

    35: def chef_vault_item(bag, id)
    36:   require 'pry'; binding.pry
 => 37:   if ChefVault::Item.vault?(bag, id)
    38:     ChefVault::Item.load(bag, id)
    39:   elsif node['chef-vault']['databag_fallback']
    40:     Chef::DataBagItem.load(bag, id)
    41:   else
    42:     raise "Trying to load a regular data bag item #{id} from #{bag}, and databag_fallback is disabled"
    43:   end
    44: end

[1] pry(#<Chef::Recipe>)> bag
=> "MyVault"
[2] pry(#<Chef::Recipe>)> id
=> "MyVaultId"
[3] pry(#<Chef::Recipe>)> ChefVault::Item.vault?(bag, id)
RSpec::Mocks::MockExpectationError: #<ChefVault::Item (class)> received :vault? with unexpected arguments
  expected: (:MyVault, "myvaultid")
       got: ("MyVault", "MyVaultId")
Diff:
@@ -1,2 +1,2 @@
-[:StorePasswords, "myvaultid"]
+["StorePasswords", "MyVaultId"]

 Please stub a default value first if message might be received with other args as well.
micmicsuarez commented 8 years ago

hi @dougireton, please never mind my previous comment. I haven't encountered that problem. I tested it in my machine.

micmicsuarez commented 8 years ago

Ok, I simulated the issue.

rneu31 commented 7 years ago

Is there any update on this issue?

I am writing a test for a recipe that uses multiple data bags. All but one databags have one key: value in them, "password": "some-password". There are no errors stubbing these out. The one that errors out as described here is of the structure "production" : { "password": "some-password" }