using cookbook attribute's inside serverspec testing.

141 views
Skip to first unread message

Prince Paulson

unread,
May 28, 2015, 2:20:27 PM5/28/15
to testing-...@googlegroups.com
Hi All,

Has anybody done integration testing with serverspec using cookbook's attributes files integrated.

Thanks,

Simmendinger, Felix

unread,
May 29, 2015, 3:25:30 AM5/29/15
to testing-...@googlegroups.com, Prince Paulson
Yes, what i do is, i render the nodes attributes during the report phase into a json file on disk and load it later on.  For this purpose i wrote a report handler. 

Handler code in a test fixture cookbook  at test/fixtures/cookbooks/helper/files/default/handler/node_attrs.rb

require 'rubygems'
require
'chef/handler'
module MyHelper
class NodeAttrs < Chef::Handler
def report
attrs = { :ipaddress => @run_status.node.automatic.ipaddress,
:fqdn => @run_status.node.automatic.fqdn }
attrs = Chef::Mixin::DeepMerge.deep_merge(attrs, node.default)
attrs = Chef::Mixin::DeepMerge.deep_merge(attrs, node.normal)
attrs = Chef::Mixin::DeepMerge.deep_merge(attrs, node.override)
`mkdir -p /shared`
File.open('/shared/node.json', 'w') do |f|
f.write(attrs.to_json)
end

end
end
End

This handler i register in a recipe at test/fixtures/cookbooks/helper/recipes/node-attrs.rb

include_recipe 'chef_handler'
remote_directory node['chef_handler']['handler_path'] do
source 'handlers'
mode '0755'
recursive true
action :nothing
end.run_action(:create)
chef_handler MyHelper::NodeAttrs' do
source "#{node['chef_handler']['handler_path']}/node_attrs.rb"
action :nothing
end.run_action(:enable)

And in the Berksfile:

group :integration do

cookbook ‘helper', path: 'test/fixtures/cookbooks/helper'
End

Then you need a rspec helper beside your tests like this:

require 'serverspec'

require 'fileutils'
require 'json'
set :backend, :exec
@chef_node = nil
RSpec.configure do |c|
FileUtils.mkdir_p '/shared'
@chef_node = ::JSON.parse(File.read('/shared/node.json')) unless @chef_node
end

In the Kitchenfile add “reicpe[helper::node-attrs]” to runlist

In your tests you can use then: 

@chef_node[‘fqdn] for example.

If you have a private supermarket, you can easily push a cookbook with this code and then reuse it in all your cookbooks.

Cheers Felix
From: Prince Paulson
Date: Donnerstag, 28. Mai 2015 20:20
To: "testing-...@googlegroups.com"
Subject: using cookbook attribute's inside serverspec testing.

Hi All,

Has anybody done integration testing with serverspec using cookbook's attributes files integrated.

Thanks,

--
You received this message because you are subscribed to the Google Groups "Testing with Chef" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testing-with-c...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Prince Paulson

unread,
May 30, 2015, 3:50:40 AM5/30/15
to testing-...@googlegroups.com
Hi Felix,

Thank you for the quick reply. I will implement this approach and get back to you if any questions.


Thanks,
Prince
Reply all
Reply to author
Forward
0 new messages