4

I'm learning to write integration tests for my Chef cookbooks. Is it possible to reference variables from the attributes folder within my test?

Here's my test to make sure httpd and php are installed properly. However, I have additional packages I want to check for.

test/smoke/default/install.rb

%w(httpd php).each do |rpm_package|
  describe package(rpm_package) do
    it { should be_installed }
  end
end

attributes/default.rb

default['ic_apachephp']['php_packages'] = [
                              'php-mysqlnd',
                              'php-mbstring',
                              'php-gd',
                              'php-xml',
                              'php'
                            ]
sdot257
  • 10,046
  • 26
  • 88
  • 122

1 Answers1

4

Yes, this is possible, however not "directly". Matt Wrock described it in this blog entry.

Necessary steps are:

  • add a cookbook (e.g. called export-node, below test/fixtures/cookbooks/) that includes a recipe with the following content (dumping node attributes into the specified JSON file):

    ruby_block "Save node attributes" do
      block do
        IO.write("/tmp/kitchen_chef_node.json", node.to_json)
      end
    end
    
  • add this recipe to the the run list in .kitchen.yml

  • load the node object in your inspec test using

    node = json('/tmp/kitchen_chef_node.json').params
    

Beware that you manually have to pick from the right attribute precedence level (automatic/default/normal/override), as those are not merged.

You can find an example of mine also in this cookbook: TYPO3-cookbooks/t3-pdns.

EDIT: I forgot the step to tell Berkshelf about that cookbook. Add to your Berksfile:

group :integration do
  cookbook 'export-node', path: 'test/fixtures/cookbooks/export-node'
end
StephenKing
  • 36,187
  • 11
  • 83
  • 112
  • All right, so ... in order to add the cookbook to the `run_list` within `.kitchen.yml`, I had to add `depends export-node` in the cookbook I'm testing. is that right? Without the `depends` clause, it keeps telling me that the `export-node` cookbook can't be found.Once I got past that, I'm greeted with `No such file or directory @ rb_sysopen - /tmp/kitchen/chef_node.json` ... except the file is on the docker container. – sdot257 Aug 04 '17 at 17:21
  • No need to add the dependency (you don't want that cookbook on your production nodes, don't you?). It will be automatically found when it's in `test/fixtures/cookbooks`. Please have a look at the cookbook I've referenced. – StephenKing Aug 04 '17 at 17:35
  • Sorry, forgot the berkshelf part. I've updated the answer. – StephenKing Aug 04 '17 at 19:07
  • Yeah, after hours of banging my head, it was the `Berksfile` i was missing. :) ... all good it works now! – sdot257 Aug 04 '17 at 19:29
  • Just curious, but is there a way to have the `export-node` cookbook available to all cookbooks? If I have twenty cookbooks I want to test, i would need said cookbook under each test folder, no? – sdot257 Aug 04 '17 at 19:53
  • Sure, it just needs to be resolvable by Berkshelf. Actually, this is a pretty good idea. It could even be in the supermarket. Alternatively, anywhere from where Berkshelf can fetch it, e.g. `../node-exporter` or a git repo. – StephenKing Aug 05 '17 at 11:15
  • 1
    I feel stupid after adding such code to 5 of my cookbooks ^^ – StephenKing Aug 05 '17 at 11:16
  • 1
    Damn it, there you go [node-exporter](https://supermarket.chef.io/cookbooks/export-node) by mwrock. I misunderstood the blog article. – StephenKing Aug 05 '17 at 11:18
  • @luckytaxi I am still having issues with the No such file or directory @ rb_sysopen - /tmp/kitchen/chef_node.json even though the file exists im the container. I've checked all comments and everything seems to be up to date but still it throws the message. Did the Berksfile really fix that issue for you? – Kārlis Ābele Aug 07 '20 at 12:28