Search code examples
chef-infraserverspec

Chef - ServerSpec - Accessing Node attributes


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"
  }
}

Solution

  • 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.