Chefspec and accessing a node attribute within EncryptedDataBagItem.load

Hello great chefs!

I’m running into trouble using ChefSpec 3.2.0 and converging a
ChefSpec runner with an encrypted data bag. The data bag I’m trying
to load is referenced by a node attribute, like this:

mysql_passwords =

Chef::EncryptedDataBagItem.load(node[‘percona’][‘encrypted_data_bag’],
“mysql”)

So in my ChefSpec runner, I’ve set that node attribute:

let(:chef_run) do
  ChefSpec::Runner.new do |node|
    node.override["percona"]["encrypted_data_bag"] = 'something'
  end.converge(described_recipe)
end

But converging the ChefSpec running seems to choke on this. Are these
attributes not being assigned in the Runner object? I’ve placed a
puts statement in my recipe code and the tests print out the correct
bogus attribute I defined, “something.”

4>> mysql_passwords =
Chef::EncryptedDataBagItem.load(node[‘percona’][‘encrypted_data_bag’],
“mysql”)

Failures:

  1. prsn_epen2::_deploy_common does something
    Failure/Error: end.converge(described_recipe)
    TypeError:
    no implicit conversion of Array into String

I’m sure this is something obvious, can anyone help me please? I
really appreciate the discussions on this list!

  • Bryan

What I think you want to do is to stub out your databag in the before block
so that chefspec can execute your tests on that data.

Here is an example of what I've done.

More info:

On Fri, Feb 7, 2014 at 12:19 PM, Bryan Bishop bryanjbishop@yahoo.comwrote:

Hello great chefs!

I'm running into trouble using ChefSpec 3.2.0 and converging a
ChefSpec runner with an encrypted data bag. The data bag I'm trying
to load is referenced by a node attribute, like this:

mysql_passwords =

Chef::EncryptedDataBagItem.load(node['percona']['encrypted_data_bag'],
"mysql")

So in my ChefSpec runner, I've set that node attribute:

let(:chef_run) do
  ChefSpec::Runner.new do |node|
    node.override["percona"]["encrypted_data_bag"] = 'something'
  end.converge(described_recipe)
end

But converging the ChefSpec running seems to choke on this. Are these
attributes not being assigned in the Runner object? I've placed a
puts statement in my recipe code and the tests print out the correct
bogus attribute I defined, "something."

4>> mysql_passwords =
Chef::EncryptedDataBagItem.load(node['percona']['encrypted_data_bag'],
"mysql")

Failures:

  1. prsn_epen2::_deploy_common does something
    Failure/Error: end.converge(described_recipe)
    TypeError:
    no implicit conversion of Array into String

I'm sure this is something obvious, can anyone help me please? I
really appreciate the discussions on this list!

  • Bryan

Thank you Pete! This has helped me on my way, I thought my problem
was the node attribute but it turned out to be stubbing the data bag.

On Fri, Feb 7, 2014 at 11:56 AM, Pete Cheslock petecheslock@gmail.com wrote:

What I think you want to do is to stub out your databag in the before block
so that chefspec can execute your tests on that data.

Here is an example of what I've done.
gist:5f6d34ff05680b2b5ec5 · GitHub

More info:
GitHub - chefspec/chefspec: Write RSpec examples and generate coverage reports for Chef recipes!

On Fri, Feb 7, 2014 at 12:19 PM, Bryan Bishop bryanjbishop@yahoo.com
wrote:

Hello great chefs!

I'm running into trouble using ChefSpec 3.2.0 and converging a
ChefSpec runner with an encrypted data bag. The data bag I'm trying
to load is referenced by a node attribute, like this:

mysql_passwords =

Chef::EncryptedDataBagItem.load(node['percona']['encrypted_data_bag'],
"mysql")

So in my ChefSpec runner, I've set that node attribute:

let(:chef_run) do
  ChefSpec::Runner.new do |node|
    node.override["percona"]["encrypted_data_bag"] = 'something'
  end.converge(described_recipe)
end

But converging the ChefSpec running seems to choke on this. Are these
attributes not being assigned in the Runner object? I've placed a
puts statement in my recipe code and the tests print out the correct
bogus attribute I defined, "something."

4>> mysql_passwords =
Chef::EncryptedDataBagItem.load(node['percona']['encrypted_data_bag'],
"mysql")

Failures:

  1. prsn_epen2::_deploy_common does something
    Failure/Error: end.converge(described_recipe)
    TypeError:
    no implicit conversion of Array into String

I'm sure this is something obvious, can anyone help me please? I
really appreciate the discussions on this list!

  • Bryan