Testing cookbook with Test kitchen
Test Kitchen is Chef's integrated testing framework. It enables writing test recipes, which will run on the VMs once they are instantiated and converged using the cookbook. The test recipes run on that VM and can verify if everything works as expected.
Test Kitchen Example
Let’s do a demo to show you how to apply red, green, refactor.Go to workstation node, here we can use docker platform to use an example, So we can install docker in it.
#yum install -y docker
After that enable the docker service and start service,
#service docker start
#service docker status
#service docker enable
Now we can set an environment variable for kitchen docker,
# chef exec gem install kitchen-docker
Fetching: kitchen-docker-2.7.0.gem (100%)
Successfully installed kitchen-docker-2.7.0
1 gem installed
Generate a cookbook called my cookbook,
# chef generate cookbook my_cookbook
Generating cookbook my_cookbook
- Ensuring correct cookbook file content
- Committing cookbook files to git
- Ensuring delivery configuration
- Ensuring correct delivery build cookbook content
- Adding delivery configuration to feature branch
- Adding build cookbook to feature branch
- Merging delivery content feature branch to master
Your cookbook is ready. Type `cd my_cookbook` to enter it.
There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list.
Why not start by writing a test? Tests for the default recipe are stored at:
test/smoke/default/default_test.rb
If you'd prefer to dive right in, the default recipe can be found at:
recipes/default.rb
Go inside that folder,
#cd my_cookbook/
# ls
Berksfile chefignore LICENSE metadata.rb README.md recipes spec test
You can see there is a no .kitchen.yml file, for that gives a command as .kitchen.yml is a hidden directory
#ls -a
. .. Berksfile chefignore .delivery .git .gitignore .kitchen.yml LICENSE metadata.rb README.md recipes spec test
Now we can edit .kitchen.yml file
---
driver:
name: docker
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
verifier:
name: inspec
platforms:
- name: ubuntu-16.04
- name: centos-7
suites:
- name: default
run_list:
- recipe[my_cookbook::default]
verifier:
inspec_tests:
- test/smoke/default
attributes:
Next, we can run kitchen converge
#kitchen converge
To check kitchen list
#kitchen verify
-----> Starting Kitchen (v1.19.2)
-----> Setting up
Finished setting up
-----> Verifying
Loaded tests from {:path=>".home.zippyops.Downloads.chef-repo.cookbooks.my_cookbook.test.smoke.default"}
Profile: tests from {:path=>"/home/zippyops/Downloads/chef-repo/cookbooks/my_cookbook/test/smoke/default"} (tests from
{:path=>".home.zippyops.Downloads.chef-repo.cookbooks.my_cookbook.test.smoke.default"})
Version: (not specified)
Target: ssh://kitchen@localhost:32768
User root
↺
Port 80
↺
Test Summary: 0 successful, 0 failures, 2 skipped
Finished verifying
-----> Kitchen is finished. (0m10.06s)
This is running not running in port 80, so we can add some changes in it
#cd test
#ls
smoke
#cd default/
To edit default_test.rb
#vi default_test.rb
# # encoding: utf-8
# Inspec test for recipe my_cookbook::default
# The Inspec reference, with examples and extensive documentation, can be
# found at http://inspec.io/docs/reference/resources/
unless os.windows?
# This is an example test, replace with your own test.
describe user('root'), :skip do
it { should exist }
end
end
describe package('cowsay') do
it { should be_installed }
end
Now we can run kitchen verify Come back to the my_cookbook directory and run this command.
# kitchen verify
-----> Starting Kitchen (v1.19.2)
-----> Verifying
Loaded tests from {:path=>".home.zippyops.Downloads.chef-repo.cookbooks.my_cookbook.test.smoke.default"}
Profile: tests from {:path=>"/home/zippyops/Downloads/chef-repo/cookbooks/my_cookbook/test/smoke/default"} (tests from
{:path=>".home.zippyops.Downloads.chef-repo.cookbooks.my_cookbook.test.smoke.default"})
Version: (not specified)
Target: ssh://kitchen@localhost:32768
User root
↺
System Package
∅ undefined method `shouid' for
#<:examplegroups::systempackagecowsay:0x00007f5c14358a00>
Test Summary: 0 successful, 1 failure, 1 skipped
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: 2 actions failed.
>>>>>> Verify failed on instance
>>>>>> Verify failed on instance
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration
Now the resultant states as failure, for that go to recepies and edit the default.rb file as shown:
#cd recipes/
#ls
default.rb
# vi default.rb
#
# Cookbook:: my_cookbook
# Recipe:: default
#
# Copyright:: 2018, The Authors, All Rights Reserved.
package 'cowsay'
We can run all the command in one command
# kitchen test
-----> Starting Kitchen (v1.20.0)
-----> Cleaning up any prior instances of
-----> Destroying
UID PID PPID C STIME
TTY TIME CMD
root 10170 10155 0 10:45
? 00:00:00 /usr/sbin/sshd -D -o UseDNS=no -o UsePAM=no -o
PasswordAuthentication=yes -o UsePrivilegeSeparation=no -o PidFile=/tmp/sshd.pid
b51ff98c20115f12a64024320a6a46e2c1602928008d8f801c7deef722576d02
b51ff98c20115f12a64024320a6a46e2c1602928008d8f801c7deef722576d02
Finished destroying
-----> Testing
-----> Creating
Sending build context to Docker daemon 65.02kB
Step 1/17 : FROM ubuntu:16.04
---> f975c5035748
Step 2/17 : RUN dpkg-divert --local --rename --add /sbin/initctl
---> Using Cache
---> c838b352b3bc
Step 3/17 : RUN ln -sf /bin/true /sbin/initctl
---> Using cache
---> 7ae38ad96159
Step 4/17 : ENV DEBIAN_FRONTEND noninteractive
---> Using cache
---> 90c4c29cd5df
Step 5/17 : ENV container docker
---> Using cache
---> f065390f28c6
Step 6/17 : RUN apt-get update
---> Using cache
---> adaa57ed161e
Step 7/17 : RUN apt-get install -y sudo openssh-server curl lsb-release
---> Using cache
---> 74c71e41f5db
Step 8/17 : RUN if ! getent passwd kitchen; then
It has been run successfully.
Relevant Blogs:
Recent Comments
No comments
Leave a Comment
We will be happy to hear what you think about this post