10-introduction to chefspec

Upload: saranya-thangarajan

Post on 05-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

sdfsd

TRANSCRIPT

PowerPoint Presentation

v1.2.11An Introduction to ChefSpec

Unit Testing Your Cookbooks to Prevent RegressionsCopyright 2015 Chef Software, Inc.10-10-Lesson Objectives2After completing the lesson, you will be able to:Explain what unit testing means for Chef cookbooks and recipesExplain why to write unit tests for Chef recipesUse ChefSpec to create and manage a test suite for your cookbooks10-Problem Statement3Problem: We broke our motd cookbook one too many timesProposed Solution: Use ChefSpec to write tests to ensure the code is valid10-Installing ChefSpec4ChefSpec is already included in the Chef Development Kit (ChefDK)10-ChefSpec is RSpec5ChefSpec is built on-top of RSpecThe standard Ruby testing toolRSpec has a familiar, English-like syntaxChefSpec adds the knowledge of Chef to RSpec10-$ cd cookbooks/motdExercise: Move into cookbook6(No output)10-$ rspec --initExercise: Make a 'spec' Directory7(No output)10-require 'chefspec'

ChefSpec::Coverage.start!

# This file was generated by the `rspec --init`OPEN IN EDITOR: cookbooks/motd/spec/spec_helper.rbSAVE FILE!By convention, test suites have a helperAvoid restating require 'chefspec' over and overCan configure RSpec in here (formatting, enforced style, etc.)8Exercise: Create a Spec Helper10-$ mkdir spec/unit$ mkdir spec/unit/recipesExercise: Make a directory for the tests9(No output)10-General Test Approach10Set up the testMake a Chef run in memorySet up test harness if necessaryMake some assertions (expectations)10-ChefSpec Example11require 'spec_helper'

describe 'cookbook_name::recipe_name' dolet(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

it 'does something' do expect(chef_run).to action_resource_type('NAME OF THE RESOURCE') end

endhttps://github.com/sethvargo/chefspec10-ChefSpec Example12require 'spec_helper'

describe 'cookbook_name::recipe_name' dolet(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

it 'does something' do expect(chef_run).to action_resource_type('NAME OF THE RESOURCE') end endLoads a file that contains common libraries and helper methods that are shared across all tests.10-ChefSpec Example13require 'spec_helper'

describe 'cookbook_name::recipe_name' dolet(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

it 'does something' do expect(chef_run).to action_resource_type('NAME OF THE RESOURCE') end endThis 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-ChefSpec Example14require 'spec_helper'

describe 'cookbook_name::recipe_name' dolet(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

it 'does something' do expect(chef_run).to action_resource_type('NAME OF THE RESOURCE') end endlet sets up a helper 'method' named chef_run that creates our in-memory chef-client run for the cookbook recipe currently under test.10-ChefSpec Example15require 'spec_helper'

describe 'cookbook_name::recipe_name' dolet(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

it 'does something' do expect(chef_run).to action_resource_type('NAME OF THE RESOURCE') end endit defines a single test. The text is used to describe the test. Within the block the expectations are defined. 10-ChefSpec Example16require 'spec_helper'

describe 'cookbook_name::recipe_name' dolet(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

it 'does something' do expect(chef_run).to action_resource_type('NAME OF THE RESOURCE') end endThis is an expectation. Stating an expectation that the chef_run to have a resource take a particular action.10-require 'spec_helper'describe 'motd::default' dolet(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) } it 'creates an motd correctly' end

OPEN IN EDITOR: .../motd/spec/unit/recipes/default_spec.rbExercise: Create a Skeleton Test17Test file name should match recipe name (default) and must end in _spec.rbAn it without a do..end block means the test is pending10-$ rspecExercise: Run rspec From the Cookbook*Pending: motd::default creates an motd correctly # Not yet implemented # ./spec/unit/recipes/default_spec.rb:7

Finished in 0.00033 seconds1 example, 0 failures, 1 pending

No Chef resources found, skipping coverage calculation...1810-Rspec provides a few ways to enable options. The attendees would benefit from learning a few common command-line options (e.g. color, and documentation format)From the command-line:

rspec -c -f d

From thespec_helper.rb:

RSpec.configure do |config| config.color = true # ... end

18OPEN IN EDITOR: .../motd/spec/unit/recipes/default_spec.rbSAVE FILErequire 'spec_helper' describe 'motd::default' dolet(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

it 'creates an motd correctly'doexpect(chef_run).to create_template('/etc/motd').with(:user => 'root',:group => 'root',:mode => '0644')end endExercise: Write a Real Test1910-$ rspecExercise: Run rspecFailures:

1) motd::default creates an motd correctlyFailure/Error: expect(chef_run).to create_template('/etc/motd').with( expected "template[/etc/motd]" to have parameters:

user "root", was nil group "root", was nil# ./spec/unit/recipes/default_spec.rb:7:in `block (2 levels) in 'ubuntu', :version => '14.04'}).converge(described_recipe) end

it 'should install the correct packages'do expect(chef_run).to install_package('mailutils') end endExercise: Write a Real TestSAVE FILE!29Set up a test context for ubuntu10- context 'on CentOS' do let(:chef_run) do ChefSpec::SoloRunner.new({:platform => 'centos', :version => '6.5'}).converge(described_recipe) end

it 'should install the correct packages'do expect(chef_run).to install_package('mailx') end endendExercise: Write a Real TestSAVE FILE!30Set up test context for CentOSOPEN IN EDITOR:.../mailx/spec/unit/recipes/default_spec.rb10-$ rspecExercise: Run rspec..

Finished in 0.18828 seconds (files took 5.17 seconds to load)2 examples, 1 failures

ChefSpec Coverage report generated...

Total Resources: 1 Touched Resources: 1 Touch Coverage: 100.0%

You are awesome and so is your test coverage! Have a fantastic day!

3110-OPEN IN EDITOR: cookbooks/mailx/attributes/default.rbcase node['platform'] when "ubuntu" default['mailx']['mailx-package'] = "mailutils" when "centos" default['mailx']['mailx-package'] = "mailx" endExercise: Add Cross-platform Attributes32SAVE FILE!10-32OPEN IN EDITOR: cookbooks/mailx/recipes/default.rbSAVE FILE!package node['mailx']['mailx-package'] do action :installendExercise: Install the Package3310-$ rspecExercise: Run rspec..

Finished in 0.18828 seconds (files took 5.17 seconds to load)2 examples, 0 failures

ChefSpec Coverage report generated...

Total Resources: 2 Touched Resources: 2 Touch Coverage: 100.0%

You are awesome and so is your test coverage! Have a fantastic day!3410-Review Questions35What 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-