Last Updated: February 25, 2016
·
10.26K
· logikal

Test your cookbooks with Chef 11 and Vagrant

Chef 11 has come out, and possibly broken a bunch of your carefully crafted cookbooks. How are you going to test them, though? Here's one way, using chef-solo via Vagrant.

Requirements

  • RVM (or rbenv, but this will use RVM)
    • You're using RVM or rbenv to manage your rubies, right? If not, you should be.
  • Virtualbox
  • some cookbooks you wrote and want to test

Set it up

We don't want to mess up our carefully crafted Chef 10.x environment, right? We'll use rvm gemsets to make a disposable set of gems. If something goes wrong, just close the terminal you're in, or run rvm gemset use default. You'll drop back to the default gemset.

cd $HOMEBASE
rvm gemset create chef11
rvm gemset use chef11
gem install chef vagrant

NOTE: update the homebase = to be the full path to your homebase.

cat <<VAGRANTFILE > Vagrantfile
homebase = "/path/to/homebase"
Vagrant::Config.run do |config|
  config.vm.box = "opscode-ubuntu-12.04-chef11"
  config.vm.box_url = "https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_chef-11.2.0.box" # from https://github.com/opscode/bento
  config.vm.network :hostonly, "10.111.222.33"
  config.vm.customize ["modifyvm", :id, "--memory", 2048]
  config.vm.provision :chef_solo do |chef|
    # chef.log_level = :debug
    chef.cookbooks_path = "#{homebase}/cookbooks"
    chef.data_bags_path = "#{homebase}/data_bags"
    chef.roles_path = "#{homebase}/roles"
    # Replace the following lines with your roles/recipes
    chef.add_recipe "apt::default"
    #chef.add_recipe "foo::bar"
    # How to add a role:
    # chef.add_role "foo"
    # How to set node attributes (like an environment)
    chef.json = {
      :load_limit => 42,
      :chunky_bacon => true
    }
  end
end
VAGRANTFILE

Do some magic

vagrant up

At this point, you should have an ubuntu 12.04 machine starting up, using Chef 11.2.0. It'll run the apt::default recipe (assuming you have it), and throw errors if there are any.

Notes

Adding Recipes/Roles

  • chef.add_recipe "foo::bar" to add a recipe
  • chef.add_role "baz" to add a role

Environments/Node Attributes

If you're used to environments, you can run knife environment show <envname> --format json to get an environment as json from your chef-server. You can also do knife node show <nodename> -a node -f j to get all of the attributes from a particular node you already have. This gist can be adapted to get your json into the ruby 'hashrockets' syntax. Then you'll need to update the chef.json in your Vagrantfile with your attributes.

Further reading

1 Response
Add your response

Thanks for the interesting article. We are currently developing ToASTER, a framework for automated testing of Infrastructure-as-Code automations, in particular Chef scripts. The recipes are executed with different configurations in isolated container environments (Docker VMs), and ToASTER reports various metrics such as system state changes, convergence properties, and idempotence issues.

The code is available open source at:
http://cloud-toaster.github.io/

over 1 year ago ·