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
wget https://packages.chef.io/files/stable/chefdk/4.7.73/ubuntu/18.04/chefdk_4.7.73-1_amd64.deb
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 https://get.docker.com -o get-docker.sh
sh get-docker.sh
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
---
driver:
  name: docker

provisioner:
  name: chef_zero
  always_update_cookbooks: true

verifier:
  name: inspec

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

transport:
  name: docker

suites:
  - name: default
    run_list:
      - recipe[kitchendockerdemo::default]
    verifier:
      inspec_tests:
        - test/integration/default
    attributes:
  • 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
end

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube