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
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
PDF
Test-Driven Puppet Development - PuppetConf 2014
PDF
Continuously Testing Infrastructure - Beyond Module Testing - PuppetConf 2014
PDF
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
KEY
Some Rough Fibrous Material
PDF
Puppet and Openshift
PDF
Puppet User Group Presentation - 15 March 2012
ODP
Software Testing
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
Test-Driven Puppet Development - PuppetConf 2014
Continuously Testing Infrastructure - Beyond Module Testing - PuppetConf 2014
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
Some Rough Fibrous Material
Puppet and Openshift
Puppet User Group Presentation - 15 March 2012
Software Testing

What's hot (19)

PPTX
Upgrading Puppet Agents
PDF
Lessons I Learned While Scaling to 5000 Puppet Agents
PDF
PuppetConf 2014 Killer R10K Workflow With Notes
PDF
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
PDF
Getting Started with Puppet on Windows - PuppetConf 2014
ODP
Perl - laziness, impatience, hubris, and one liners
PDF
Top 10 Perl Performance Tips
ODP
Build-a-Gem Workshop
PPTX
Kubernetes Probes (Liveness, Readyness, Startup) Introduction
PDF
PL/Perl - New Features in PostgreSQL 9.0 201012
PDF
Git why how when and more
PDF
Concurrency in Python
PDF
Minimal MVC in JavaScript
PDF
PuppetConf 2016: Deconfiguration Management: Making Puppet Clean Up Its Own M...
PPTX
Rails 勉強会#3
PDF
Plumbin Pipelines - A Gulp.js workshop
PDF
What you need to remember when you upload to CPAN
PDF
Advanced JavaScript build pipelines using Gulp.js
PPTX
Cql – cassandra query language
Upgrading Puppet Agents
Lessons I Learned While Scaling to 5000 Puppet Agents
PuppetConf 2014 Killer R10K Workflow With Notes
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
Getting Started with Puppet on Windows - PuppetConf 2014
Perl - laziness, impatience, hubris, and one liners
Top 10 Perl Performance Tips
Build-a-Gem Workshop
Kubernetes Probes (Liveness, Readyness, Startup) Introduction
PL/Perl - New Features in PostgreSQL 9.0 201012
Git why how when and more
Concurrency in Python
Minimal MVC in JavaScript
PuppetConf 2016: Deconfiguration Management: Making Puppet Clean Up Its Own M...
Rails 勉強会#3
Plumbin Pipelines - A Gulp.js workshop
What you need to remember when you upload to CPAN
Advanced JavaScript build pipelines using Gulp.js
Cql – cassandra query language
Ad

Viewers also liked (7)

PDF
Switching from Puppet to Ansible
PDF
Java one 2011 monitoring a large-scale infrastructure with clojure
PDF
Drone your Ansible
PDF
Investigation of testing with ansible
PDF
Testing Ansible with Jenkins and Docker
PDF
System Hardening Using Ansible
PPT
Ansible presentation
Switching from Puppet to Ansible
Java one 2011 monitoring a large-scale infrastructure with clojure
Drone your Ansible
Investigation of testing with ansible
Testing Ansible with Jenkins and Docker
System Hardening Using Ansible
Ansible presentation
Ad

Similar to Puppet loves RSpec, why you should, too (20)

PDF
Orchestrated Functional Testing with Puppet-spec and Mspectator
PDF
Unit Testing Lots of Perl
PDF
Git::Hooks
PPTX
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
PPTX
Puppet camp chicago-automated_testing2
PDF
Test Driven Development with Puppet - PuppetConf 2014
PDF
Beaker: Automated, Cloud-Based Acceptance Testing - PuppetConf 2014
PDF
How I hack on puppet modules
PPTX
Autotesting rails app
PDF
Puppet Camp Paris 2014: Test Driven Development
PDF
20140408 tdd puppetcamp-paris
DOCX
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
PDF
Puppet at GitHub
PDF
Docker perl build
PDF
Puppet at GitHub / ChatOps
PDF
Migrating Legacy Rails Apps to Rails 3
PDF
Ansible roles done right
ODP
YAPC::NA 2007 - An Introduction To Perl Critic
Orchestrated Functional Testing with Puppet-spec and Mspectator
Unit Testing Lots of Perl
Git::Hooks
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
Puppet camp chicago-automated_testing2
Test Driven Development with Puppet - PuppetConf 2014
Beaker: Automated, Cloud-Based Acceptance Testing - PuppetConf 2014
How I hack on puppet modules
Autotesting rails app
Puppet Camp Paris 2014: Test Driven Development
20140408 tdd puppetcamp-paris
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
Puppet at GitHub
Docker perl build
Puppet at GitHub / ChatOps
Migrating Legacy Rails Apps to Rails 3
Ansible roles done right
YAPC::NA 2007 - An Introduction To Perl Critic

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
Teaching material agriculture food technology
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Machine learning based COVID-19 study performance prediction
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Chapter 3 Spatial Domain Image Processing.pdf
Network Security Unit 5.pdf for BCA BBA.
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Advanced methodologies resolving dimensionality complications for autism neur...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Monthly Chronicles - July 2025
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Dropbox Q2 2025 Financial Results & Investor Presentation
Teaching material agriculture food technology
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Machine learning based COVID-19 study performance prediction
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
Chapter 3 Spatial Domain Image Processing.pdf

Puppet loves RSpec, why you should, too