SlideShare a Scribd company logo
Puppet ❤ RSpec
Why You Should, Too
Dennis Rowe
Senior DevOps Engineer
Cirrus Logic (cirrus.com)
shr3kst3r on Twitter, Github, and Freenode
• Setup of Puppet RSpec
• Quick RSpec primer
• Review some RSpec code
• Opinions on testing
Testing is like free
kittens
Puppet Loves RSpec, Why You Should, Too
Not Testing?
Puppet Loves RSpec, Why You Should, Too
So, RSpec
https://github.com/rodjek/rspec-puppet
http://rspec-puppet.com/tutorial/
https://github.com/puppetlabs/puppetlabs_spec_helper
First, Install RVM
• RVM = Ruby Version Manager
• https://rvm.io/
• Allows us to manage ruby versions and
gemsets
• Specify ruby version in repo/.rvmrc
• source ~/.rvm/scripts/rvm
% curl -L https://get.rvm.io |
bash -s stable --rails --
autolibs=enabled
% rvm use ruby-1.9.3@puppet-
camp-austin --install --create 
% ruby -v
ruby 1.9.3p392 [x86_64-
darwin12.2.1]
• http://rubygems.org
• Specifies exactly what gems are used
• `bundle install` installs the gems in
our gemset
Gemfile Time
Example Gemfile
source :rubygems
gem "puppet", "~> 3.1.1"
gem "puppetlabs_spec_helper", "~> 0.4.1"
Gemfile.lock
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.2.4)
facter (1.7.0)
hiera (1.2.1)
json_pure
json_pure (1.7.7)
puppet (3.1.1)
facter (~> 1.6)
RSpec Prep
• mkdir -p spec/classes/puppet-camp-austin/
• Add Rakefile
require 'rubygems'
require 'puppetlabs_spec_helper/rake_tasks'
• Add spec/spec_helper.rb
require 'rubygems'
require 'puppetlabs_spec_helper/module_spec_helper'
Setup .fixtures.yml
% cat .fixtures.yml
fixtures:
symlinks:
puppet-camp-austin: "#{source_dir}"
Quick Overview of
RSpec Constructs
describe
• Expresses the concept of the block
• They can be nested
• The first level describe should be a
class, define, function, or host
Example describe
describe “myfunction” do
describe “it should do this” do
...
end
describe “it should also do this”
...
end
end
lets
let(:title) { ‘namevar’ }
let(:params) {{‘var1’ => ‘foo’, ...}}
let(:node) { ‘ahost.example.com’ }
let(:facts) {{:osfamily => 'Debian', ...}}
Matchers
• include_class
• contain_{package, service, user, etc}
should or should_not
it { should include_class(‘foobarz’) }
it { should_not include_class(‘foobarz’)}
it { should contain_package(‘fooz’) }
it { should contain_user(‘barz’) }
Now moving on
So, let us add an
example class
% cat manifests/example1.pp
class puppet-camp-austin::example1 {
}
cat spec/classes/puppet-camp-austin/
example1_spec.rb
require 'spec_helper'
describe 'puppet-camp-austin::example1' do
it 'should include git' do
should contain_package('git')
end
end
Let us run RSpec
% rake spec
F
Failures:
1) puppet-camp-austin::example1 should include git
Failure/Error: should contain_package('git')
expected that the catalogue would contain Package[git]
# ./spec/classes/puppet-camp-austin/example1_spec.rb:5:in
`block (2 levels) in <top (required)>'
Oh no! We Failed!
% cat manifests/example1.pp
class puppet-camp-austin::example1 {
package { 'git':
ensure => installed
}
}
% rake spec
.
Finished in 0.11266 seconds
1 example, 0 failures
Now we are cooking
with peanut oil!
Happy Happy Happy
Deep Thoughts
“It is what you don’t
expect that most
needs looking for.”-- Neal Stephenson, Anathem
What should we test?
• Non-obvious things
• Complex things
• Bugs
Time for something
not obvious
Let us say we decided we
wanted to let a puppet
forge module install git
for us. Oh look, there is
a module called
puppetlabs/git!
Let us add the module
to .fixtures.yml
% cat .fixtures.yml
fixtures:
repositories:
git: git://github.com/puppetlabs/puppetlabs-git.git
symlinks:
puppet-camp-austin: "#{source_dir}"
Add the git class
% cat manifests/example2.pp
class puppet-camp-austin::example2
{
include 'git'
}
Add a test
% cat spec/classes/puppet-camp-austin/example2_spec.rb
require 'spec_helper'
describe 'puppet-camp-austin::example2' do
it 'should include git' do
should contain_package('git')
end
end
Run the Tests (I am sure
they will pass)
% rake spec
.F
Failures:
1) puppet-camp-austin::example2 should include git
Failure/Error: should contain_package('git')
expected that the catalogue would contain Package[git]
# ./spec/classes/puppet-camp-austin/example2_spec.rb:5:in
`block (2 levels) in <top (required)>'
What?
The git module
doesn’t install git?
Is this crazy??!
OK, let us go look
class git {
  package { 'git-core':
    ensure => installed,
  }
}
Should you be testing
3rd party modules
this way?
Probably not.
It depends.
Has it burned you in
the past?
It did me :(
Let us try this again
We write a test
describe 'puppet-camp-austin::example3' do
describe 'operating system is Debian' do
let(:facts) { {:operatingsystem => 'Debian'} }
it 'should include git' do
should contain_package('git')
end
end
describe 'operating system is MINIX' do
let(:facts) { {:operatingsystem => 'MINIX'} }
it 'should notify a warning' do
should contain_notify('git cannot be install for this package')
end
end
end
Maybe we go to
lunch, forget about
our test, come back,
write some code...
This is the code we write
class puppet-camp-austin::example3 {
Package { ensure => "installed" }
case $operatingsystem {
'RedHat', 'CentOS': { package {'git': } }
/^(Debian|Ubuntu)$/:{ package {'git': } }
default: { package {'git-core': } }
}
}
Test it!
rspec ./spec/classes/puppet-camp-
austin/example3_spec.rb:15 #
puppet-camp-austin::example3
operating system is MINIX should
notify a warning
D’oh!
Let us add that notify
class puppet-camp-austin::example3 {
Package { ensure => "installed" }
case $operatingsystem {
'RedHat', 'CentOS': { package {'git': } }
/^(Debian|Ubuntu)$/:{ package {'git': } }
default: { notify {'git cannot be install for this package': } }
}
}
It works!
% rake spec
..
Finished in 0.13768 seconds
2 examples, 0 failures
In Retrospect
Puppet + RSpec
Rocks
Why are you not
doing it!!?
You should test
interesting things
Don’t be scared to
break it!
My contrived examples
are at https://
github.com/shr3kst3r/
puppet-camp-austin
Questions and maybe
some answers?

More Related Content

PDF
Test-Driven Puppet Development - PuppetConf 2014
PDF
Beaker: Automated, Cloud-Based Acceptance Testing - PuppetConf 2014
PDF
Can you upgrade to Puppet 4.x?
PDF
Puppet modules for Fun and Profit
KEY
Introducing Command Line Applications with Ruby
PDF
Puppet loves RSpec, why you should, too
PDF
Unit Testing Lots of Perl
PDF
Puppet @ Seat
Test-Driven Puppet Development - PuppetConf 2014
Beaker: Automated, Cloud-Based Acceptance Testing - PuppetConf 2014
Can you upgrade to Puppet 4.x?
Puppet modules for Fun and Profit
Introducing Command Line Applications with Ruby
Puppet loves RSpec, why you should, too
Unit Testing Lots of Perl
Puppet @ Seat

What's hot (19)

PDF
Shared Object images in Docker: What you need is what you want.
PDF
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
PDF
What you need to remember when you upload to CPAN
KEY
Crafting Beautiful CLI Applications in Ruby
PDF
Testing con spock
PDF
Puppet Module Reusability - What I Learned from Shipping to the Forge
PDF
OlinData Puppet Presentation for MOSC 2012
PDF
Replacing "exec" with a type and provider: Return manifests to a declarative ...
PDF
Puppet Continuous Integration with PE and GitLab
PDF
Doing It Wrong with Puppet -
PDF
Bangpypers april-meetup-2012
PDF
Anatomy of a reusable module
PDF
Puppet modules: An Holistic Approach
PDF
Vagrant + Rouster at salesforce.com - PuppetConf 2013
PDF
Puppet: What _not_ to do
PDF
Using Puppet to Create a Dynamic Network - PuppetConf 2013
PDF
Effective Benchmarks
PDF
Fabric workshop(1) - (MOSG)
PDF
PL/Perl - New Features in PostgreSQL 9.0 201012
Shared Object images in Docker: What you need is what you want.
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
What you need to remember when you upload to CPAN
Crafting Beautiful CLI Applications in Ruby
Testing con spock
Puppet Module Reusability - What I Learned from Shipping to the Forge
OlinData Puppet Presentation for MOSC 2012
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Puppet Continuous Integration with PE and GitLab
Doing It Wrong with Puppet -
Bangpypers april-meetup-2012
Anatomy of a reusable module
Puppet modules: An Holistic Approach
Vagrant + Rouster at salesforce.com - PuppetConf 2013
Puppet: What _not_ to do
Using Puppet to Create a Dynamic Network - PuppetConf 2013
Effective Benchmarks
Fabric workshop(1) - (MOSG)
PL/Perl - New Features in PostgreSQL 9.0 201012
Ad

Similar to Puppet Loves RSpec, Why You Should, Too (20)

PPTX
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
PPTX
Puppet camp chicago-automated_testing2
PDF
Test Driven Development with Puppet - PuppetConf 2014
PDF
20140406 loa days-tdd-with_puppet_tutorial
PDF
Puppet Camp Paris 2014: Test Driven Development
PDF
20140408 tdd puppetcamp-paris
PDF
TDD with Puppet Tutorial presented at Cascadia IT Conference 2014-03-07
PDF
Basic RSpec 2
KEY
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
PDF
Orchestrated Functional Testing with Puppet-spec and Mspectator
PDF
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
PDF
Test Driven Development with Puppet
PDF
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
PDF
Puppet camp2021 testing modules and controlrepo
PDF
PuppetCamp2021-Testing Modules and ControlRepo.pdf
PDF
Testing your infallibleness
PDF
PuppetConf 2016: Enjoying the Journey from Puppet 3.x to 4.x – Rob Nelson, AT&T
PDF
PuppetConf 2016: The Future of Testing Puppet Code – Gareth Rushgrove, Puppet
PPTX
Enjoying the Journey from Puppet 3.x to Puppet 4.x (PuppetConf 2016)
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
Puppet camp chicago-automated_testing2
Test Driven Development with Puppet - PuppetConf 2014
20140406 loa days-tdd-with_puppet_tutorial
Puppet Camp Paris 2014: Test Driven Development
20140408 tdd puppetcamp-paris
TDD with Puppet Tutorial presented at Cascadia IT Conference 2014-03-07
Basic RSpec 2
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
Orchestrated Functional Testing with Puppet-spec and Mspectator
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
Test Driven Development with Puppet
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
Puppet camp2021 testing modules and controlrepo
PuppetCamp2021-Testing Modules and ControlRepo.pdf
Testing your infallibleness
PuppetConf 2016: Enjoying the Journey from Puppet 3.x to 4.x – Rob Nelson, AT&T
PuppetConf 2016: The Future of Testing Puppet Code – Gareth Rushgrove, Puppet
Enjoying the Journey from Puppet 3.x to Puppet 4.x (PuppetConf 2016)
Ad

More from Puppet (20)

PPTX
Puppet Community Day: Planning the Future Together
PPTX
The Evolution of Puppet: Key Changes and Modernization Tips
PPTX
Can You Help Me Upgrade to Puppet 8? Tips, Tools & Best Practices for Your Up...
PPTX
Bolt Dynamic Inventory: Making Puppet Easier
PPTX
Customizing Reporting with the Puppet Report Processor
PPTX
Puppet at ConfigMgmtCamp 2025 Sponsor Deck
PPTX
The State of Puppet in 2025: A Presentation from Developer Relations Lead Dav...
PPTX
Let Red be Red and Green be Green: The Automated Workflow Restarter in GitHub...
PPTX
Puppetcamp r10kyaml
PDF
2021 04-15 operational verification (with notes)
PPTX
Puppet camp vscode
PDF
Modules of the twenties
PDF
Applying Roles and Profiles method to compliance code
PPTX
KGI compliance as-code approach
PDF
Enforce compliance policy with model-driven automation
PDF
Keynote: Puppet camp compliance
PPTX
Automating it management with Puppet + ServiceNow
PPTX
Puppet: The best way to harden Windows
PPTX
Simplified Patch Management with Puppet - Oct. 2020
PPTX
Accelerating azure adoption with puppet
Puppet Community Day: Planning the Future Together
The Evolution of Puppet: Key Changes and Modernization Tips
Can You Help Me Upgrade to Puppet 8? Tips, Tools & Best Practices for Your Up...
Bolt Dynamic Inventory: Making Puppet Easier
Customizing Reporting with the Puppet Report Processor
Puppet at ConfigMgmtCamp 2025 Sponsor Deck
The State of Puppet in 2025: A Presentation from Developer Relations Lead Dav...
Let Red be Red and Green be Green: The Automated Workflow Restarter in GitHub...
Puppetcamp r10kyaml
2021 04-15 operational verification (with notes)
Puppet camp vscode
Modules of the twenties
Applying Roles and Profiles method to compliance code
KGI compliance as-code approach
Enforce compliance policy with model-driven automation
Keynote: Puppet camp compliance
Automating it management with Puppet + ServiceNow
Puppet: The best way to harden Windows
Simplified Patch Management with Puppet - Oct. 2020
Accelerating azure adoption with puppet

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
Teaching material agriculture food technology
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Empathic Computing: Creating Shared Understanding
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Teaching material agriculture food technology
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Understanding_Digital_Forensics_Presentation.pptx

Puppet Loves RSpec, Why You Should, Too