Testing chef cookbooks with Test Kitchen
-
Test Kitchen is a test harness tool to verify/execute the infrastructure as code on one or more platforms
-
Terms:
- Driver: Is used to choose the virtualization platform where infra can be created to test cookbooks
- Driver Plugin: Test kitchen uses driver plugin architecture to run chef cookbooks on cloud providers and virtualization platforms such as Vagrant, Amazon Ec2, Azure, Google Compute Engine, Docker.
- Test Frameworks: chef cookbooks can be tested by writing tests using Chef Inspec.
-
Refer Here for direct devops articles on using test kitchen
-
Refer Here for official docs
-
Along with chefdk the kitchen will be installed on your workstation
-
Now execute the following commands
kitchen --help
- Lets create a new cookbook "kitchenawsdemo" with aws driver Refer Here
chef generate cookbook -b kitchenawsdemo
- In the generated cookbook we will have kitchen.yml file. It would have
- driver: where the tests are supposed to be executed
- provisioner: chef_zero
- verifier: which test framework should be used to verify cookbook
- platforms: different operating systems where you want to execute your cookbook
- suites: These are your test cases
- To know more about kitchen.yaml Refer Here
- The default kitchen.yml file generated will have a vagrant driver configured, but we would like to use aws driver
- For writing yaml of kitchen Refer Docs from here
- Kitchen.yaml written is
---
driver:
name: ec2
aws_ssh_key_id: "terraform"
security_group_ids: ["sg-05bdbb36ca7c53403"]
region: "us-west-2"
availability_zone: "c"
subnet_id: "subnet-f38757ae"
instance_type: "t2.micro"
associate_public_ip: true
transport:
ssh_key: "./terraform.pem"
username: "ubuntu"
## The forwarded_port port feature lets you connect to ports on the VM guest via
## localhost on the host.
## see also: https://www.vagrantup.com/docs/networking/forwarded_ports.html
# network:
# - ["forwarded_port", {guest: 80, host: 8080}]
provisioner:
name: chef_zero
# You may wish to disable always updating cookbooks in CI or other testing environments.
# For example:
# always_update_cookbooks: <%= !ENV['CI'] %>
always_update_cookbooks: true
## product_name and product_version specifies a specific Chef product and version to install.
## see the Chef documentation for more details: https://docs.chef.io/config_yml_kitchen.html
# product_name: chef
# product_version: 15
verifier:
name: inspec
platforms:
- name: ubuntu-18.04
driver:
image_id: "ami-0a634ae95e11c6f91"
#- name: centos-7
suites:
- name: default
run_list:
- recipe[kitchenawsdemo::default]
verifier:
inspec_tests:
- test/integration/default
attributes:
- Lets execute the following commands
berks install
kitchen list
- Now lets create a new recipe called as utils and write resources to install git, tree
#
# Cookbook:: .
# Recipe:: utils
#
# Copyright:: 2020, The Authors, All Rights Reserved.
if platform?('ubuntu')
apt_update 'update packages' do
ignore_failure true
action :update
end
end
utilities = %w(git tree nano)
package utilities do
action :install
end
- Call this recipe from default.rb
include_recipe 'kitchenawsdemo::utils'
- To test this cookbook which we have written, lets execute the following commands
kitchen converge
- To manually verify if the recipe has done its job or not
- Now lets verify the same cookbook on redhat 8, so make changes in kitchen.yaml
---
driver:
name: ec2
aws_ssh_key_id: "terraform"
security_group_ids: ["sg-05bdbb36ca7c53403"]
region: "us-west-2"
availability_zone: "c"
subnet_id: "subnet-f38757ae"
instance_type: "t2.micro"
associate_public_ip: true
transport:
ssh_key: "./terraform.pem"
## The forwarded_port port feature lets you connect to ports on the VM guest via
## localhost on the host.
## see also: https://www.vagrantup.com/docs/networking/forwarded_ports.html
# network:
# - ["forwarded_port", {guest: 80, host: 8080}]
provisioner:
name: chef_zero
# You may wish to disable always updating cookbooks in CI or other testing environments.
# For example:
# always_update_cookbooks: <%= !ENV['CI'] %>
always_update_cookbooks: true
## product_name and product_version specifies a specific Chef product and version to install.
## see the Chef documentation for more details: https://docs.chef.io/config_yml_kitchen.html
# product_name: chef
# product_version: 15
verifier:
name: inspec
platforms:
- name: ubuntu-18.04
driver:
image_id: "ami-0a634ae95e11c6f91"
transport:
username: "ubuntu"
- name: redhat-8
driver:
image_id: ami-02f147dfb8be58a10
transport:
username: "ec2-user"
suites:
- name: default
run_list:
- recipe[kitchenawsdemo::default]
verifier:
inspec_tests:
- test/integration/default
attributes:
-
Now execute the command
kitchen converge
and wait for the command to completed -
So from kitchen i can write cookbooks & verify them on different platforms
-
Now execute
kitchen list
-
Execute
kitchen destroy
to remove the machines created