How to test cookbooks in chef using test kitchen and Docker?

Using Kitchen-Docker Driver to test cookbooks

  • In this series lets understand how to use docker instances to verify chef cookbooks
  • Using docker to test cookbooks drastically simplifies testing various environments and reduces cost
  • So we would be using the test kitchen driver which is a docker
  • For demonstrating this, I am using the ubuntu virtual machine as chef workstation
  • So lets start with installing the chefdk on the ubuntu virtual machine
  • Download the current release of chef dk from here and install it . I will be downloading the deb file since my workstation is ubuntu 18
cd /tmp
sudo dpkg -i /tmp/chefdk_4.7.73-1_amd64.deb
  • one the chefdk is installed, check the installation using version command chef --version Preview
  • Now since we would be using docker as a driver, lets install docker using docker shell script from here
cd /tmp
curl -fsSL -o
sudo usermod -aG docker $USER
  • Now exit and relogin into the linux system.
  • Create a chef cookbook
chef generate cookbook -b kitchendockerdemo
cd kitchendockerdemo\
  • Now install kitchen-docker gem using the following command
chef gem install kitchen-docker
  name: docker

  name: chef_zero
  always_update_cookbooks: true

  name: inspec

  - name: ubuntu
  - name: centos
      image: centos:7
      platform: centos

  name: docker

  - name: default
      - recipe[kitchendockerdemo::default]
        - test/integration/default
  • Now add some code to default.rb in recipes
# Cookbook:: kitchendockerdemo
# Recipe:: default
# Copyright:: 2020, The Authors, All Rights Reserved.
file '/tmp/readme.txt' do
        content 'Created for demo'
        action :create

  • Now execute the following commands
    • List kitchen test environments using the command kitchen list Preview
    • Create test environments using the command kitchen create
    • Now List kitchen test environments using the command to know status kitchen list Preview
    • Converge the test environments using command kitchen converge
    • Now List kitchen test environments using the command to know status kitchen list Preview
    • Once you have finished testing the cookbook, destroy the instances using kitchen destroy

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin