SlideShare una empresa de Scribd logo
1 de 59
Descargar para leer sin conexión
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
Not Testing?
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?

Más contenido relacionado

La actualidad más candente

La actualidad más candente (19)

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

Destacado

Java one 2011 monitoring a large-scale infrastructure with clojure
Java one 2011   monitoring a large-scale infrastructure with clojureJava one 2011   monitoring a large-scale infrastructure with clojure
Java one 2011 monitoring a large-scale infrastructure with clojure
Dennis Rowe
 
Investigation of testing with ansible
Investigation of testing with ansibleInvestigation of testing with ansible
Investigation of testing with ansible
Dennis Rowe
 

Destacado (7)

Switching from Puppet to Ansible
Switching from Puppet to AnsibleSwitching from Puppet to Ansible
Switching from Puppet to Ansible
 
Java one 2011 monitoring a large-scale infrastructure with clojure
Java one 2011   monitoring a large-scale infrastructure with clojureJava one 2011   monitoring a large-scale infrastructure with clojure
Java one 2011 monitoring a large-scale infrastructure with clojure
 
Drone your Ansible
Drone your AnsibleDrone your Ansible
Drone your Ansible
 
Investigation of testing with ansible
Investigation of testing with ansibleInvestigation of testing with ansible
Investigation of testing with ansible
 
Testing Ansible with Jenkins and Docker
Testing Ansible with Jenkins and DockerTesting Ansible with Jenkins and Docker
Testing Ansible with Jenkins and Docker
 
System Hardening Using Ansible
System Hardening Using AnsibleSystem Hardening Using Ansible
System Hardening Using Ansible
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 

Similar a Puppet loves RSpec, why you should, too

Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2
nottings
 
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docxPrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
ChantellPantoja184
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3
Clinton Dreisbach
 

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

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

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Puppet loves RSpec, why you should, too