Creating ChefSpec in Chef

Last updated on Nov 12 2021
Yogesh Ajmera

Table of Contents

Creating ChefSpec in Chef

Chef – ChefSpec

Test Driven Development (TDD) is a way to write unit test before writing any actual recipe code. The test should be real and should validate what a recipe does. It should actually fail as there was no recipe developed. Once the recipe is developed, the test should pass.
ChefSpec is built on the popular RSpec framework and offers a tailored syntax for testing Chef recipe.

Creating ChefSpec

Step 1 − Create a gem file containing the chefSpec gem.
vipin@laptop:~/chef-repo $ subl Gemfile
source 'https://rubygems.org'
gem 'chefspec'
Step 2− Install  the gem.

vipin@laptop:~/chef-repo $ bundler install
Fetching gem metadata from https://rubygems.org/
...TRUNCATED OUTPUT...
Installing chefspec (1.3.1)
Using bundler (1.3.5)
Your bundle is complete!
Step 3 − Create a spec directory.
vipin@laptop:~/chef-repo $ mkdir cookbooks/<Cookbook Name>/spec
Step 4 − Create a Spec
vipin@laptop:~/chef-repo $ subl
cookbooks/my_cookbook/spec/default_spec.rb
require 'chefspec'
describe 'my_cookbook::default' do
let(:chef_run) {
ChefSpec::ChefRunner.new(
platform:'ubuntu', version:'12.04'
).converge(described_recipe)
}

it 'creates a greetings file, containing the platform
name' do
expect(chef_run).to
create_file_with_content('/tmp/greeting.txt','Hello! ubuntu!')
end
end
Step 5 − Validate ChefSpec.
vipin@laptop:~/chef-repo $ rspec cookbooks/<Cookbook Name>/spec/default_spec.rb
F
Failures:
1) <CookBook Name> ::default creates a greetings file, containing the platform name
Failure/Error: expect(chef_run.converge(described_recipe)).to
create_file_with_content('/tmp/greeting.txt','Hello! ubuntu!')
File content:
does not match expected:
Hello! ubuntu!
# ./cookbooks/my_cookbook/spec/default_spec.rb:11:in `block
(2 levels) in <top (required)>'
Finished in 0.11152 seconds
1 example, 1 failure

Failed examples:
rspec ./cookbooks/my_cookbook/spec/default_spec.rb:10 # my_
cookbook::default creates a greetings file, containing the
platform name
Step 6 − Edit Cookbooks default recipe.
vipin@laptop:~/chef-repo $ subl cookbooks/<Cookbook Name>/recipes/default.rb
template '/tmp/greeting.txt' do
variables greeting: 'Hello!'
end
Step 7 − Create a template file.
vipin@laptop:~/chef-repo $ subl cookbooks/< Cookbook Name>/recipes/default.rb
<%= @greeting %> <%= node['platform'] %>!
Step 8 − Run the rspec again.
vipin@laptop:~/chef-repo $ rspec cookbooks/<Cookbook Name>/spec/default_spec.rb
.
Finished in 0.10142 seconds
1 example, 0 failures

How It Works

In order to make it work, we need to first set up the base infrastructure for using RSpec with Chef. Then we need to ChefSpec Ruby gem and the cookbook needs a directory called spec where all the tests will be saved.

So, this brings us to the end of blog. This Tecklearn ‘Creating ChefSpec in Chef’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Chef and build a career in DevOps domain, then check out our interactive, Continuous Delivery using Chef Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/continuous-delivery-using-chef/

Continuous Delivery using Chef Training

About the Course

Tecklearn has specially designed this Continuous Delivery using Chef Training Course to advance your skills for a successful career in this domain. The course will cover different components of Chef and how they are used in software development operations. The course consists of important concepts like Continuous Delivery (CD) using Chef, Chef Framework, How Chef Works, Chef Advantages and Chef Installation. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Chef.

Why Should you take Continuous Delivery using Chef Training?

• The average salary for a Senior Development Operations (DevOps) Engineer with Chef skills is $118, 435. – PayScale.com
• Airbnb, Facebook, Slack, Shopify, Digital Ocean & many other MNC’s worldwide use Chef across industries.
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps
• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation
Continuous Delivery using Chef
• Continuous Delivery
• What is Chef
• Chef Framework
• How Chef Works
• Chef Advantages
• Chef Installation
• Hands on

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Creating ChefSpec in Chef"

Leave a Message

Your email address will not be published. Required fields are marked *