How to setup a Test Kitchen – Chef

How to setup a Test Kitchen – Chef


Purpose – Setting up a Test Kitchen – Chef

Pre-requisites
Workstation with ChefDK and Virtualization tools installed like below .

Virtualbox

Vagrant


Virtualbox – It is a hypervisor that is used to manage virtual machine instances. Similar to VMware workstation


Vagrant – It’s a driver that is used to manage the virtual machine. Uses a configuration file called Vagrantfile that is used to describe aspects of the virtual machine such as hostname, memory, cpu settings etc.


Test Kitchen

  • Allows for testing the configuration changes on temporary test instance
  • It runs the infrastructure code in a isolated environment that resembles production environment
  • It builds the Vagrant file for the virtual instance
  • Used to create, destroy and run Chef on the virtual instance
  • Downloads the Vagrant box images


Kitchen – It’s a set of virtual machines that are created by Test kitchen

Test Kitchen Workflow

Create– It creates a virtual instance

Converge– Applies cookbook to the virtual instance

Login– It creates a SSH session to the virtual instance

Verify– Manually verify if virtual instance is configured as required

Destroy– shutdown and destroy the virtual instance


Procedure the Setup a Test Kitchen and validate the Chef Configuration changes



Step1: Examine the Test Kitchen configuration file


[skytap@host-1 learn_chef_httpd]$ cat .kitchen.yml

driver:

name: vagrant

provisioner:

name: chef_zero

verifier:

name: inspec

platforms:

– name: centos-7.2

suites:

– name: default

run_list:

– recipe[learn_chef_httpd::default]

attributes:


When Test Kitchen runs, it downloads the base virtual machine image called box (eg. Bento box from HashiCorp Atlas)


Step2: List the Kitchen virtual instances


[skytap@host-1 learn_chef_httpd]$ kitchen list


Instance Driver Provisioner Verifier Transport Last Action Last Error

default-centos-72 Vagrant ChefZero Inspec Ssh <Not Created> <None>



Step3: Create a kitchen virtual instance


[skytap@host-1 learn_chef_httpd]$ kitchen create

[skytap@host-1 learn_chef_httpd]$ kitchen list

Instance Driver Provisioner Verifier Transport Last Action Last Error

default-centos-72 Vagrant ChefZero Inspec Ssh Created <None>



Step4: Apply the cookbook to the Test Kitchen instance


[skytap@host-1 learn_chef_httpd]$ kitchen converge

[skytap@host-1 learn_chef_httpd]$ kitchen list

Instance Driver Provisioner Verifier Transport Last Action Last Error

default-centos-72 Vagrant ChefZero Inspec Ssh Converged <None>



Step5: Verify the configuration on test instance


[skytap@host-1 learn_chef_httpd]$ kitchen exec -c ‘curl localhost’



Step6: Delete the Test instance


[skytap@host-1 learn_chef_httpd]$ kitchen destroy

[skytap@host-1 learn_chef_httpd]$ kitchen list

Instance Driver Provisioner Verifier Transport Last Action Last Error

default-centos-72 Vagrant ChefZero Inspec Ssh <Not Created> <None>



Hope you enjoyed reading this article. Thank you.