2

I am in the process of writing ServerSpec tests for a cookbook I wrote. The tests need node attributes to assert various things through kitchen.

Fortunately, there is a guide here explaining how to achieve this: http://jakshi.com/blog/2014/05/12/accessing-chef-attributes-in-serverspec-tests/

The problem I am having is, that this does not work:

attrs = attrs.deep_merge(node.override_attrs) unless node.override_attrs.empty?

But this works:

attrs = attrs.deep_merge(node.attributes.combined_override) unless node.attributes.combined_override.empty?

My setup is exactly same as described in the blog. Browsing code did not help due to lack of ruby-fu. The chef-client version is 11.14.6 and Test-Kitchen version is 1.3.1

Can someone help please? Has anyone else had this problem? Thanks.

Update: Here are all the attributes from a dummy cookbook I created to simulate this.

cb-under-test/recipes/default.rb
<Nothing>


cb-under-test/test/fixtures/cookbooks/fake/attributes/default.rb
force_override['important_dir'] = 'test_recipe_force_override'


../env/dummy-env.json
{
  "name": "dummy-env",
  "description": "Dummy Env",
  "cookbook_versions": {
  },
  "json_class": "Chef::Environment",
  "chef_type": "environment",
  "override_attributes": {
    "important_dir": "env_override"
  }
}
dvlpr
  • 139
  • 8
  • Add the attribute file of your test cookbook, we can't guess at which level you set them... – Tensibai Oct 19 '15 at 16:00
  • The attributes come from several places including the environment. I do not have any attributes in my test cookbook. – dvlpr Oct 19 '15 at 16:05
  • Anyway, without an extract we can't guess at which levels your attributes are define (default, normal, override, else ?) – Tensibai Oct 19 '15 at 17:32
  • Which version of chef are you using ? (Just to be sure which code I've to check over for the attributes levels accessors) – Tensibai Oct 20 '15 at 07:16
  • On the test VM it is 11.14.6. I am using chef solo through kitchen. – dvlpr Oct 20 '15 at 07:17
  • According to [the code](https://github.com/chef/chef/blob/11.14.6/lib/chef/node/attribute.rb) there's no override_attrs method, here you'll have the cookbook's one under `node.override` and the env's one in `node.env_override`, the `node.combined_override` gives you the resulting attributes after deep merge. The blog post is quite old, you should better use `attrs = node.merged_attributes` to write the json file. – Tensibai Oct 20 '15 at 07:23
  • This makes so much sense and it works too! Thanks! If you could put this in an answer I can accept. – dvlpr Oct 20 '15 at 07:47
  • You're right, I should have write it as an answer at first. – Tensibai Oct 20 '15 at 07:51

1 Answers1

3

According to the code there's no override_attrs method.

Here you'll have the cookbook's attributes under node.override and the environment's attributes in node.env_override, the node.combined_override gives you the resulting attributes after deep merge.

The blog post is quite old, you should better use attrs = node.merged_attributes to write the json file and get the resulting attributes from cookbook, roles and environments, using merged_attributes should avoid the ohai attributes to, keeping the json size low.

Tensibai
  • 15,557
  • 1
  • 37
  • 57
  • I was using a test_helper recipe which stored the node attributes in a file so ServerSpec could test against them, and found that the environment variables were not being merged in at all. Using `node.env_override` solved it for me. Thank you. – jaseeey Dec 08 '15 at 01:17