Skip to main content

testing Chef 11 with Vagrant

·415 words·2 mins

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.

DO NOT install RVM if you already use rbenv. They’re incompatible, and you don’t want to mess with that.

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.

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.

What about test-kitchen? #

As of this writing test-kitchen’s master branch doesn’t support Chef 11. Once it does, you should definitely use it.

Further reading #