SlideShare a Scribd company logo
Test Driven 
Development! 
for Puppet! 
Puppet needs software development 
Gareth Rushgrove
Who 
(Who is this person?)
@garethr
Gareth Rushgrove
Gareth Rushgrove
Gareth Rushgrove
The problem 
(This isn’t a rant, but…)
Who here is a 
software developer? 
Gareth Rushgrove
If you’re writing 
Puppet code you’re a 
software developer 
Gareth Rushgrove
As a software 
developer it’s your job 
to learn software 
engineering practices 
Gareth Rushgrove
What is Test Driven 
Development 
(And why should you care)
A common practice in 
software engineering 
Gareth Rushgrove
Not just testing 
Gareth Rushgrove
Encourages simple 
designs and inspires 
confidence 
Gareth Rushgrove
First write an (initially 
failing) automated 
test case 
Gareth Rushgrove
Then produce the 
minimum amount of 
code to pass that test 
Gareth Rushgrove
And finally refactor 
the new code to 
acceptable standards 
Gareth Rushgrove
Test Driven Design 
Gareth Rushgrove
Gareth Rushgrove
Unit testing 
with RSpec 
and Guard 
(Not puppet specific)
A unit is the smallest 
testable part of an 
application 
Gareth Rushgrove
Testing puppet 
requires a little Ruby 
knowledge so we’ll 
use Ruby examples 
Gareth Rushgrove
class Person 
def say(word) 
end 
end 
Gareth Rushgrove
First lets write a test. 
For this we use the 
RSpec testing 
framework 
Gareth Rushgrove
require 'person' 
! 
describe Person, "#say" do 
it "should say something" do 
! 
end 
end 
Gareth Rushgrove
require 'person' 
! 
describe Person, "#say" do 
it "should say something" do 
bob = Person.new 
bob.say("hello").should  
eq("hello everyone") 
end 
end 
Gareth Rushgrove
Now lets run our test. 
It should fail 
Gareth Rushgrove
rspec 
Gareth Rushgrove
Failures: 
1) Person#say should say something 
Failure/Error: bob.say("hello").should 
eq("hello everyone") 
expected: "hello everyone" 
got: nil 
Finished in 0.00171 seconds 
1 example, 1 failure 
Gareth Rushgrove
Now lets write the 
implementation 
Gareth Rushgrove
class Person 
def say(word) 
word + " everyone" 
end 
end 
Gareth Rushgrove
And run our test 
again 
Gareth Rushgrove
Person#say 
should say something 
! 
Finished in 0.00199 seconds 
1 example, 0 failures 
Gareth Rushgrove
Why not have tests 
automatically run 
whenever you 
change the code? 
Gareth Rushgrove
That’s what Guard 
does 
Gareth Rushgrove
guard :rspec, cmd: 'bundle exec rspec' do 
watch(%r{^spec/.+_spec.rb$}) 
watch(%r{^lib/.+.rb$}) { 'spec' } 
end 
Gareth Rushgrove
guard 
Gareth Rushgrove
Lets see a quick 
demo 
Gareth Rushgrove
Why test puppet 
code at all 
(Testing declarative languages)
Modules increasingly 
contain logic 
Gareth Rushgrove
Modules increasingly 
take arguments 
Gareth Rushgrove
Modules increasingly 
have interfaces with 
other modules 
Gareth Rushgrove
Modules increasingly 
used in many 
operating system and 
version combinations 
Gareth Rushgrove
Modules increasingly 
used in many Ruby 
and Puppet version 
combinations 
Gareth Rushgrove
Unit testing 
puppet with 
rspec-puppet 
(Finally some puppet code)
Unit testing 
for Puppet
A very simple puppet 
class 
Gareth Rushgrove
class sample { 
} 
Gareth Rushgrove
First write the test 
Gareth Rushgrove
require 'spec_helper' 
! 
describe "sample" do 
it { should create_file('/tmp/sample')} 
end 
Gareth Rushgrove
Then run the test 
Gareth Rushgrove
sample 
should contain File[/tmp/sample] (FAILED - 1) 
! 
Finished in 0.4584 seconds 
1 example, 1 failure 
Gareth Rushgrove
And then write the 
(puppet) code to 
make the test pass 
Gareth Rushgrove
class sample { 
file { "/tmp/sample": 
ensure => present, 
} 
} 
Gareth Rushgrove
sample 
should contain File[/tmp/sample] 
! 
Finished in 0.3881 seconds 
1 example, 0 failures 
Gareth Rushgrove
Lets run the tests 
automatically 
whenever you 
change anything 
Gareth Rushgrove
guard :rspec, cmd: 'bundle exec rspec' do 
watch(%r{^spec/.+_spec.rb$}) 
watch(%r{^manifests/.+.pp$}) { 'spec' } 
end 
Gareth Rushgrove
Lets see a quick 
demo of that too 
Gareth Rushgrove
You can also test 
hosts, defines, facts, 
functions, hieradata 
Gareth Rushgrove
Syntax checking, 
linting, oh my 
(Creating a build process)
puppet-lint 
Gareth Rushgrove
Puppet! 
style guide
Available! 
as a gem
puppet-lint --with-filename /etc/puppet/modules 
foo/manifests/bar.pp: trailing whitespace found on 
line 1 apache/manifests/server.pp: variable not 
enclosed in {} on line 56 
Gareth Rushgrove
puppet-syntax 
Gareth Rushgrove
Validate Puppet 
and ERB syntax
require 'puppet-syntax/tasks/puppet-syntax' 
Gareth Rushgrove
rake syntax 
---> syntax:manifests 
---> syntax:templates 
---> syntax:hiera:yaml 
Gareth Rushgrove
What is Rake and 
why do we use it 
(Still no puppet)
Rake is a Ruby! 
build tool 
Gareth Rushgrove
It’s like Make but in 
Ruby 
Gareth Rushgrove
It’s very easy to 
distribute Rake tasks 
as Ruby gems 
Gareth Rushgrove
rake 
Gareth Rushgrove
rake <command> 
Gareth Rushgrove
rake -T 
Gareth Rushgrove
Lets make a 
command to run lint, 
syntax and spec 
Gareth Rushgrove
task :test => [ 
:syntax, 
:lint, 
:spec, 
] 
Gareth Rushgrove
rake test 
Gareth Rushgrove
Acceptance 
testing with 
beaker 
(Living on the edge)
Acceptance 
test against 
real systems 
Gareth Rushgrove
Test what actually 
happens, not what is 
meant to happen 
Gareth Rushgrove
Build by 
Gareth Rushgrove
Very new 
Gareth Rushgrove
Test against different 
operating systems 
Gareth Rushgrove
HOSTS: 
ubuntu-server-12042-x64: 
roles: 
- master 
platform: ubuntu-server-12.04-amd64 
box: ubuntu-server-12042-x64-vbox4210-nocm 
box_url: http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-hypervisor: vagrant 
! 
CONFIG: 
log_level: verbose 
type: foss 
Gareth Rushgrove
HOSTS: 
centos-64-x64: 
roles: 
- master 
platform: el-6-x86_64 
box : centos-64-x64-vbox4210-nocm 
box_url : http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-hypervisor : vagrant 
! 
CONFIG: 
log_level: verbose 
type: foss 
Gareth Rushgrove
Supports multiple 
hypervisors 
Gareth Rushgrove
Vagrant 
hypervisor
VSphere 
hypervisor
Helpers to install 
puppet and modules 
Gareth Rushgrove
install_puppet 
Gareth Rushgrove
puppet('module', 'install', 'puppetlabs-stdlib') 
Gareth Rushgrove
Test that Puppet runs 
without errors 
Gareth Rushgrove
context 'default parameters' do 
it 'should work with no errors' do 
pp = "class { 'sample': }" 
apply_manifest(pp, :catch_failures => true) 
end 
end 
Gareth Rushgrove
Test runs are 
idempotent 
Gareth Rushgrove
context 'default parameters' do 
it 'should work with no errors' do 
pp = "class { 'sample': }" 
apply_manifest(pp, :catch_failures => true) 
apply_manifest(pp, :catch_changes => true) 
end 
end 
Gareth Rushgrove
Test that the module 
installs packages, run 
services, etc. 
Gareth Rushgrove
serverspec.org 
Gareth Rushgrove
describe package('nginx') do 
it { should be_installed } 
end 
! 
describe service('nginx') do 
it { should be_enabled } 
it { should be_running } 
end 
! 
describe port(80) do 
it { should be_listening} 
end 
Gareth Rushgrove
Other useful tools 
(and what we’re still missing)
Fixtures, 
matchers
Gareth Rushgrove
Nice continuous 
integration
Test Driven Development with Puppet - PuppetConf 2014
Test pull request 
branches too
--- 
language: ruby 
bundler_args: --without development 
before_install: rm Gemfile.lock || true 
rvm: 
- 1.8.7 
- 1.9.3 
- 2.0.0 
script: bundle exec rake test 
env: 
- PUPPET_VERSION="~> 2.7.0" 
- PUPPET_VERSION="~> 3.1.0" 
- PUPPET_VERSION="~> 3.2.0" 
- PUPPET_VERSION="~> 3.3.0" 
- PUPPET_VERSION="~> 3.4.0" 
Gareth Rushgrove
Official! 
ruby 
support
matrix: 
exclude: 
- rvm: 2.0.0 
env: PUPPET_VERSION="~> 2.7.0" 
- rvm: 2.0.0 
env: PUPPET_VERSION="~> 3.1.0" 
- rvm: 1.9.3 
env: PUPPET_VERSION="~> 2.7.0" 
Gareth Rushgrove
Experimental code 
coverage support in 
rspec-puppet master 
Gareth Rushgrove
at_exit { RSpec::Puppet::Coverage.report! } 
Gareth Rushgrove
Total resources: 24 
Touched resources: 8 
Resource coverage: 33.33% 
! 
Untouched resources: 
Class[Nginx] 
File[preferences.d] 
Anchor[apt::update] 
Class[Apt::Params] 
File[sources.list] 
Exec[Required packages: 'debian-keyring debian-archive-keyring' Anchor[apt::source::nginx] 
Class[Apt::Update] 
File[configure-apt-proxy] 
Apt::Key[Add key: 7BD9BF62 from Apt::Source nginx] 
Anchor[apt::key/Add key: 7BD9BF62 from Apt::Source nginx] 
Anchor[apt::key 7BD9BF62 present] 
File[nginx.list] 
Exec[apt_update] 
Gareth Rushgrove
A puppet module 
skeleton with 
everything working 
out of the box 
Gareth Rushgrove
puppet module 
skeleton
puppet module generate sample 
Gareth Rushgrove
A pretty complete 
example 
(The Docker module)
Gareth Rushgrove
Gareth Rushgrove 
Featured on 
the Forge
Gareth Rushgrove 
100 pull request 
and counting
Gareth Rushgrove 
Contributing 
guidelines
Gareth Rushgrove
Gareth Rushgrove 
Currently has 
121 tests
6 classes, 2 defines, 
413 lines of puppet 
code, 387 lines of test 
code 
Gareth Rushgrove
Take away 
(If all you remember is…)
Infrastructure as 
code 
Gareth Rushgrove
The first test is the 
hardest 
Gareth Rushgrove
Politely demand tests 
for module 
contributions 
Gareth Rushgrove
Test the interface not 
the implementation 
Gareth Rushgrove
Questions? 
(And thanks for listening)

More Related Content

PDF
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
PDF
Beaker: Automated, Cloud-Based Acceptance Testing - PuppetConf 2014
PDF
Test Driven Development with Puppet
PDF
The Puppet Master on the JVM - PuppetConf 2014
PDF
Workshop: Know Before You Push 'Go': Using the Beaker Acceptance Test Framewo...
PDF
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
PDF
Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...
PDF
Continuous Infrastructure: Modern Puppet for the Jenkins Project - PuppetConf...
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
Beaker: Automated, Cloud-Based Acceptance Testing - PuppetConf 2014
Test Driven Development with Puppet
The Puppet Master on the JVM - PuppetConf 2014
Workshop: Know Before You Push 'Go': Using the Beaker Acceptance Test Framewo...
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...
Continuous Infrastructure: Modern Puppet for the Jenkins Project - PuppetConf...

What's hot (19)

PDF
Puppet loves RSpec, why you should, too
PDF
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
PDF
Portland PUG April 2014: Beaker 101: Acceptance Test Everything
PDF
Killer R10K Workflow - PuppetConf 2014
PDF
Puppet Continuous Integration with PE and GitLab
PDF
Puppet evolutions
PDF
OlinData Puppet Presentation for MOSC 2012
PDF
The Challenges of Container Configuration
PDF
Tp install anything
PDF
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
PDF
Test driven infrastructure
PDF
Towards Continuous Deployment with Django
PDF
Bootstrapping Puppet and Application Deployment - PuppetConf 2013
PDF
Zero Downtime Deployment with Ansible
PDF
Take control of your Jenkins jobs via job DSL.
PPTX
Puppet Availability and Performance at 100K Nodes - PuppetConf 2014
PDF
Ansible 實戰:top down 觀點
PDF
Zero Downtime Deployment with Ansible
PDF
Testing Your Automation Code (Docker Version)
Puppet loves RSpec, why you should, too
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Portland PUG April 2014: Beaker 101: Acceptance Test Everything
Killer R10K Workflow - PuppetConf 2014
Puppet Continuous Integration with PE and GitLab
Puppet evolutions
OlinData Puppet Presentation for MOSC 2012
The Challenges of Container Configuration
Tp install anything
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
Test driven infrastructure
Towards Continuous Deployment with Django
Bootstrapping Puppet and Application Deployment - PuppetConf 2013
Zero Downtime Deployment with Ansible
Take control of your Jenkins jobs via job DSL.
Puppet Availability and Performance at 100K Nodes - PuppetConf 2014
Ansible 實戰:top down 觀點
Zero Downtime Deployment with Ansible
Testing Your Automation Code (Docker Version)
Ad

Viewers also liked (18)

PDF
Luke Kanies Keynote: Nearly a Decade of Puppet: What We've Learned and Where ...
PDF
To the Future! - Goals for Puppet 4 - PuppetConf 2014
PDF
Puppet Language 4.0 - PuppetConf 2014
PDF
Using Puppet To Manage Splunk
PPTX
Enjoying the Journey from Puppet 3.x to Puppet 4.x (PuppetConf 2016)
PDF
Ansible and AWS
PPTX
Taking Splunk to the Next Level – Architecture
PPTX
VMware and Puppet: How to Plan, Deploy & Manage Modern Applications
PDF
Building self-service on demand infrastructure with Puppet and VMware
PDF
PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...
PDF
Puppet Design Patterns - PuppetConf
PDF
PuppetConf. 2016: Puppet Best Practices: Roles & Profiles – Gary Larizza, Puppet
PDF
Business Value of Agile Testing: Using TDD, CI, CD, & DevOps
PDF
Ansible 2.0 - How to use Ansible to automate your applications in AWS.
PDF
Using Puppet with Self Service Provisioning
PPTX
Ansible + Hadoop
PDF
Stop using Nagios (so it can die peacefully)
PPTX
Why Docker
Luke Kanies Keynote: Nearly a Decade of Puppet: What We've Learned and Where ...
To the Future! - Goals for Puppet 4 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014
Using Puppet To Manage Splunk
Enjoying the Journey from Puppet 3.x to Puppet 4.x (PuppetConf 2016)
Ansible and AWS
Taking Splunk to the Next Level – Architecture
VMware and Puppet: How to Plan, Deploy & Manage Modern Applications
Building self-service on demand infrastructure with Puppet and VMware
PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...
Puppet Design Patterns - PuppetConf
PuppetConf. 2016: Puppet Best Practices: Roles & Profiles – Gary Larizza, Puppet
Business Value of Agile Testing: Using TDD, CI, CD, & DevOps
Ansible 2.0 - How to use Ansible to automate your applications in AWS.
Using Puppet with Self Service Provisioning
Ansible + Hadoop
Stop using Nagios (so it can die peacefully)
Why Docker
Ad

Similar to Test Driven Development with Puppet - PuppetConf 2014 (20)

PDF
Puppet Module Reusability - What I Learned from Shipping to the Forge
PDF
Continuously Testing Infrastructure - Beyond Module Testing - PuppetConf 2014
PDF
PuppetConf 2016: The Future of Testing Puppet Code – Gareth Rushgrove, Puppet
PDF
PuppetConf 2016: Running Puppet Software in Docker Containers – Gareth Rushgr...
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
PPTX
The GO Language : From Beginners to Gophers
PDF
Puppet Loves RSpec, Why You Should, Too
PDF
DevOps For Small Teams
PDF
Go 1.10 Release Party - PDX Go
PPTX
Toolbox of a Ruby Team
PPTX
The Gradle in Ratpack: Dissected
PDF
How to test code with mruby
PPTX
Control your deployments with Capistrano
PDF
Gareth Rushgrove (Puppet) - Ubiquity at #DOXLON
PDF
Hacking with ruby2ruby
PDF
ZendCon 2015 - DevOps for Small Teams
PPTX
TypeScript - Silver Bullet for the Full-stack Developers
PDF
Madison PHP 2015 - DevOps For Small Teams
PPT
Groovy Introduction - JAX Germany - 2008
PDF
Go 1.8 Release Party
Puppet Module Reusability - What I Learned from Shipping to the Forge
Continuously Testing Infrastructure - Beyond Module Testing - PuppetConf 2014
PuppetConf 2016: The Future of Testing Puppet Code – Gareth Rushgrove, Puppet
PuppetConf 2016: Running Puppet Software in Docker Containers – Gareth Rushgr...
2007 09 10 Fzi Training Groovy Grails V Ws
The GO Language : From Beginners to Gophers
Puppet Loves RSpec, Why You Should, Too
DevOps For Small Teams
Go 1.10 Release Party - PDX Go
Toolbox of a Ruby Team
The Gradle in Ratpack: Dissected
How to test code with mruby
Control your deployments with Capistrano
Gareth Rushgrove (Puppet) - Ubiquity at #DOXLON
Hacking with ruby2ruby
ZendCon 2015 - DevOps for Small Teams
TypeScript - Silver Bullet for the Full-stack Developers
Madison PHP 2015 - DevOps For Small Teams
Groovy Introduction - JAX Germany - 2008
Go 1.8 Release Party

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...
PDF
Puppet camp2021 testing modules and controlrepo
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
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...
Puppet camp2021 testing modules and controlrepo
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

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Machine learning based COVID-19 study performance prediction
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
Teaching material agriculture food technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
A Presentation on Artificial Intelligence
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Per capita expenditure prediction using model stacking based on satellite ima...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Machine learning based COVID-19 study performance prediction
The Rise and Fall of 3GPP – Time for a Sabbatical?
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Teaching material agriculture food technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
A Presentation on Artificial Intelligence
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
Understanding_Digital_Forensics_Presentation.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Empathic Computing: Creating Shared Understanding
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Test Driven Development with Puppet - PuppetConf 2014