Testing Chef cookbooks on travis-ci / drone.io with chef-zero
If you are using your own Chef cookbooks you certainly have some sort of tests for them. Likely running test-kitchen with docker or vagrant driver. That setup works perfectly fine locally or on a self-hosted Jenkins server. However building them on public CI platforms like travis-ci or drone.io is not possible like that. Caused by their virtualization technology, we cant use test-kitchen with docker or vagrant driver there.
What people usually do to solve this: Use a cloud driver like EC2, Cloudstack, Rackspace so travis-ci will spin up a VM there. However I found this very clunky. This require additional accounts and costs with a Cloud provider.
So what I did is mimicing how the chef-zero provisioner for test-kitchen works as seen here https://github.com/test-kitchen/test-kitchen/blob/master/lib/kitchen/provisioner/chef_zero.rb
It basically boils down to installing Chef, copying all dependency cookbooks in place along with some configs and then running chef-client with chef-zero.
Note: drone.io, travis-ci, codeship only support Ubuntu images.
For drone.io, select Ruby as language, set “Environment Variable” to BERKSHELF_PATH=/tmp/
and set this in “Commands”:
curl -L https://www.chef.io/chef/install.sh | sudo bash -s -- -v 11.18.6
bundle exec berks vendor /tmp/cookbooks
cp dummy-validation.pem /tmp/validation.pem
cp dna.json /tmp/dna.json
chef-client -z -c client.rb -j dna.json
For travis-ci you can use this .travis.yml
- sudo apt-get update -qq
- curl -L https://www.chef.io/chef/install.sh | sudo bash -s -- -v 11.18.6
- bundle install
- bundle exec berks vendor /tmp/cookbooks
- cp dummy-validation.pem /tmp/validation.pem
- cp dna.json /tmp/dna.json
- chef-client -z -c client.rb -j dna.json
Your dna.json would look like this, resembling the run_list of your kitchen.yml
You will also need this client.rb at your repos root
Get the dummy-validation.pem from here and also put it in yuor repos root (actually any will work, it wont be verified. This is the one from test-kitchen).
Thats it! Your cookbook will now be run/installed/converged like in test-kitchen.
Next up: Adding unit and integration tests to this.