Академический Документы
Профессиональный Документы
Культура Документы
v1.2.1
10-1
Lesson Objectives
After completing the lesson, you will be able to:
Explain what unit testing means for Chef
cookbooks and recipes
Explain why to write unit tests for Chef
recipes
Use ChefSpec to create and manage a test suite
for your cookbooks
10-2
Problem Statement
Problem: We broke our motd cookbook one too
many times
Proposed Solution: Use ChefSpec to write tests to
ensure the code is valid
10-3
Installing ChefSpec
ChefSpec is already included in the Chef
Development Kit (ChefDK)
10-4
ChefSpec is RSpec
ChefSpec is built on-top of RSpec
The standard Ruby testing tool
RSpec has a familiar, English-like syntax
ChefSpec adds the knowledge of Chef to RSpec
10-5
10-6
10-7
require'chefspec'
ChefSpec::Coverage.start!
#Thisfilewasgeneratedbythe`rspecinit`
SAVE FILE!
10-9
10-1
0
ChefSpec Example
spec_helper'
'cookbook_name::recipe_name'do
f_run){ChefSpec::SoloRunner.converge(described_recipe)}
something'do
t(chef_run).toaction_resource_type('NAMEOFTHERESOURCE')
https://github.com/sethvargo/chefspec
10-1
1
ChefSpec Example
spec_helper'
'cookbook_name::recipe_name'do
f_run){ChefSpec::SoloRunner.converge(described_recipe)}
something'do
t(chef_run).toaction_resource_type('NAMEOFTHERESOURCE')
Loads a file that contains common libraries and helper methods that are shared
across all tests.
10-1
2
ChefSpec Example
spec_helper'
'cookbook_name::recipe_name'do
f_run){ChefSpec::SoloRunner.converge(described_recipe)}
something'do
t(chef_run).toaction_resource_type('NAMEOFTHERESOURCE')
This is describing the cookbook's recipe under test. The text here is the name of the
fully-qualified name of the recipe to test.
10-1
3
ChefSpec Example
spec_helper'
'cookbook_name::recipe_name'do
f_run){ChefSpec::SoloRunner.converge(described_recipe)}
something'do
t(chef_run).toaction_resource_type('NAMEOFTHERESOURCE')
let sets up a helper 'method' named chef_run that creates our in-memory chef-client
run for the cookbook recipe currently under test.
10-1
4
ChefSpec Example
spec_helper'
'cookbook_name::recipe_name'do
f_run){ChefSpec::SoloRunner.converge(described_recipe)}
something'do
t(chef_run).toaction_resource_type('NAMEOFTHERESOURCE')
it defines a single test. The text is used to describe the test. Within the block the
expectations are defined.
10-1
5
ChefSpec Example
spec_helper'
'cookbook_name::recipe_name'do
f_run){ChefSpec::SoloRunner.converge(described_recipe)}
something'do
t(chef_run).toaction_resource_type('NAMEOFTHERESOURCE')
it'createsanmotd
correctly'
end
10-1
8
10-2
0
template"/etc/motd"do
source"motd.erb"
mode"0644"
user"root"
group"root"
end
10-2
3
10-2
5
10-2
6
require'chefspec'
ChefSpec::Coverage.start!
SAVE FILE!
10-2
7
10-2
8
10-3
2
packagenode['mailx']['mailxpackage']do
action:install
end
SAVE FILE!
10-3
3
Review Questions
What is ChefSpec used for?
What tool is ChefSpec based on?
What directory does do your tests go into?
Given a recipe named 'backup', what will the
ChefSpec test filename be?
10-3
5