Last Updated: February 25, 2016
· 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.


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

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" do |config| = "opscode-ubuntu-12.04-chef11"
  config.vm.box_url = "" # from :hostonly, ""
  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

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.


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:

over 1 year ago ·