How to setup a Test Kitchen using Chef

How to setup a Test Kitchen using Chef

chef_test_kitchen

Test Environment

Workstation with ChefDK and Virtualization tools installed
Virtualbox
Vagrant

What is Virtualbox

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

What is 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

Step1: Examine the Test Kitchen configuration file

Let’s review a sample test kitchen configuration file which can be used to provision a centos server using vagrat provider for testing the chef recipes.

[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

Now let’s try to list the test environment that are available but not yet created.

[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

Here we will try to create test environment using the configuration file available and check the status of the environment.

[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

Now let’s try to apply the cookbook onto our test environment and check the status.

[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

Here let’s try to validate the cookbook that has been applied to our Test Kitchen environment.

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

Step6: Delete the Test instance

We can delete our Test Kitchen environment once the testing has been completed as shown below.

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