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”:

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

For travis-ci you can use this .travis.yml

language: ruby

cache
:
- bundler
- apt

env
:
- BERKSHELF_PATH=/tmp/

before_install
:
- sudo apt-get update -qq
- curl -L https://www.chef.io/chef/install.sh | sudo bash -s -- -v 11.18.6

install
:
- bundle install
- bundle exec berks vendor /tmp/cookbooks
- cp dummy-validation.pem /tmp/validation.pem
- cp dna.json /tmp/dna.json

script
:
- chef-client -z -c client.rb -j dna.json

Your dna.json would look like this, resembling the run_list of your kitchen.yml

{"run_list":["recipe[spacewalk-server]"]}

You will also need this client.rb at your repos root

node_name "ci-ubuntu"
cookbook_path ["/tmp/cookbooks"]
data_bag_path "/tmp/data_bags"
environment_path "/tmp/environments"
node_path "/tmp/nodes"
role_path "/tmp/roles"
client_path "/tmp/clients"
#user_path "/tmp/users"
validation_key "/tmp/validation.pem"
client_key "/tmp/client.pem"
chef_server_url "http://127.0.0.1:8889"

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.

  1. No comments yet.

  1. No trackbacks yet.