SlideShare a Scribd company logo
Test Driven
Development!
for Puppet!
Puppet needs software development
Gareth Rushgrove
Who
(Who is this person?)
@garethr
UK Government
Digital Service
Test Driven Development with Puppet
Test Driven Development with Puppet
Test Driven Development with Puppet
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/u
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/
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': }”
!
expect(apply_manifest(pp).exit_code).to_not eq(1)
end
end
Gareth Rushgrove
Test runs are
idempotent
Gareth Rushgrove
context 'default parameters' do
it 'should work with no errors' do
pp = “class { 'sample': }”
!
expect(apply_manifest(pp).exit_code).to_not eq(1)
expect(apply_manifest(pp).exit_code).to eq(0)
end
end
Gareth Rushgrove
Test that the module
installs packages, run
services, etc.
Gareth Rushgrove
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
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-arch
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
Anchor[apt::key 7BD9BF62 present]
File[nginx.list]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
50 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 contributions
Gareth Rushgrove
Test the interface not
the implementation
Gareth Rushgrove
Questions?
(And thanks for listening)

More Related Content

PPTX
Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec
PDF
Workshop: Know Before You Push 'Go': Using the Beaker Acceptance Test Framewo...
PDF
London Hashicorp Meetup #8 - Testing Programmable Infrastructure By Matt Long
PDF
Puppet camp2021 testing modules and controlrepo
PDF
PuppetConf 2016: Enjoying the Journey from Puppet 3.x to 4.x – Rob Nelson, AT&T
PDF
Towards Continuous Deployment with Django
PDF
PuppetConf 2016: The Future of Testing Puppet Code – Gareth Rushgrove, Puppet
PDF
Javascript TDD with Jasmine, Karma, and Gulp
Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec
Workshop: Know Before You Push 'Go': Using the Beaker Acceptance Test Framewo...
London Hashicorp Meetup #8 - Testing Programmable Infrastructure By Matt Long
Puppet camp2021 testing modules and controlrepo
PuppetConf 2016: Enjoying the Journey from Puppet 3.x to 4.x – Rob Nelson, AT&T
Towards Continuous Deployment with Django
PuppetConf 2016: The Future of Testing Puppet Code – Gareth Rushgrove, Puppet
Javascript TDD with Jasmine, Karma, and Gulp

What's hot (19)

PDF
20140406 loa days-tdd-with_puppet_tutorial
PDF
Antons Kranga Building Agile Infrastructures
PPTX
Puppetizing Your Organization
PPTX
Troubleshooting Puppet
PDF
Cooking Perl with Chef
PDF
Continuous Infrastructure: Modern Puppet for the Jenkins Project - PuppetConf...
PPTX
Vagrant+Rouster at salesforce.com
PPTX
Cooking the Cake for Nuget packages
PDF
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
PDF
Bootstrapping Puppet and Application Deployment - PuppetConf 2013
KEY
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PDF
Testing with PostgreSQL
PDF
Testing Your Automation Code (Docker Version)
PDF
Modules of the twenties
PPTX
Exploring the Titanium CLI - Codestrong 2012
PPT
Ratpack - Classy and Compact Groovy Web Apps
PPTX
Titanium 3.2 CLI - TiAppCamp2 - 11/2/2013
PDF
Learning Puppet Chapter 1
PPT
Testing of javacript
20140406 loa days-tdd-with_puppet_tutorial
Antons Kranga Building Agile Infrastructures
Puppetizing Your Organization
Troubleshooting Puppet
Cooking Perl with Chef
Continuous Infrastructure: Modern Puppet for the Jenkins Project - PuppetConf...
Vagrant+Rouster at salesforce.com
Cooking the Cake for Nuget packages
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
Bootstrapping Puppet and Application Deployment - PuppetConf 2013
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
Testing with PostgreSQL
Testing Your Automation Code (Docker Version)
Modules of the twenties
Exploring the Titanium CLI - Codestrong 2012
Ratpack - Classy and Compact Groovy Web Apps
Titanium 3.2 CLI - TiAppCamp2 - 11/2/2013
Learning Puppet Chapter 1
Testing of javacript
Ad

Viewers also liked (20)

PPTX
Guess who is this person
PDF
Cloud Native Cost Optimization
PDF
OpenStack Architecture
PDF
Morgue , helping better understand events by building a post mortem tool - Be...
PPTX
presentation on reducing Cost in Cloud Computing
PDF
Black Swan Based VM Placement and Migration Optimizations
PPTX
Final Review
PPTX
The 27th Australasian Conference on Information Systems
PPTX
Netflix Story of Embracing the Cloud
PPT
Cost Optimization
PDF
Data Center Virtualization @ Cisco
PPTX
2016 Utah Cloud Summit: TCO & Cost Optimization
PDF
Gartner 2013 it cost optimization strategy, best practices & risks
PPTX
Cloud computing: cost reduction
PDF
Data Center Architecture Trends
PPTX
Data center network architectures v1.3
PPTX
Optimization of Resource Provisioning Cost in Cloud Computing
PDF
Dell Data Center Networking Overview
PPTX
Cost Optimization as Major Architectural Consideration for Cloud Application
PDF
Goto Berlin - Migrating to Microservices (Fast Delivery)
Guess who is this person
Cloud Native Cost Optimization
OpenStack Architecture
Morgue , helping better understand events by building a post mortem tool - Be...
presentation on reducing Cost in Cloud Computing
Black Swan Based VM Placement and Migration Optimizations
Final Review
The 27th Australasian Conference on Information Systems
Netflix Story of Embracing the Cloud
Cost Optimization
Data Center Virtualization @ Cisco
2016 Utah Cloud Summit: TCO & Cost Optimization
Gartner 2013 it cost optimization strategy, best practices & risks
Cloud computing: cost reduction
Data Center Architecture Trends
Data center network architectures v1.3
Optimization of Resource Provisioning Cost in Cloud Computing
Dell Data Center Networking Overview
Cost Optimization as Major Architectural Consideration for Cloud Application
Goto Berlin - Migrating to Microservices (Fast Delivery)
Ad

Similar to Test Driven Development with Puppet (20)

PDF
Test Driven Development with Puppet - PuppetConf 2014
PDF
Puppet Module Reusability - What I Learned from Shipping to the Forge
PDF
Continuously Testing Infrastructure - Beyond Module Testing - PuppetConf 2014
PDF
The Challenges of Container Configuration
PPT
Core java
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
PPTX
The GO Language : From Beginners to Gophers
PPT
Automated Unit Testing
PDF
maXbox Starter 36 Software Testing
PDF
Unit testing of spark applications
PPT
Boosting Your Testing Productivity with Groovy
PPT
Javaone2008 Bof 5101 Groovytesting
PDF
Developer Test - Things to Know
PDF
Fighting Fear-Driven-Development With PHPUnit
PDF
2014-11-14 - Why Test Driven Development (TDD) Works for Sysadmins @ LISA14
PPT
Groovy Introduction - JAX Germany - 2008
PDF
Puppet Camp Paris 2014: Test Driven Development
PDF
20140408 tdd puppetcamp-paris
PDF
Testing and validating spark programs - Strata SJ 2016
PPT
Introduzione al TDD
Test Driven Development with Puppet - PuppetConf 2014
Puppet Module Reusability - What I Learned from Shipping to the Forge
Continuously Testing Infrastructure - Beyond Module Testing - PuppetConf 2014
The Challenges of Container Configuration
Core java
2007 09 10 Fzi Training Groovy Grails V Ws
The GO Language : From Beginners to Gophers
Automated Unit Testing
maXbox Starter 36 Software Testing
Unit testing of spark applications
Boosting Your Testing Productivity with Groovy
Javaone2008 Bof 5101 Groovytesting
Developer Test - Things to Know
Fighting Fear-Driven-Development With PHPUnit
2014-11-14 - Why Test Driven Development (TDD) Works for Sysadmins @ LISA14
Groovy Introduction - JAX Germany - 2008
Puppet Camp Paris 2014: Test Driven Development
20140408 tdd puppetcamp-paris
Testing and validating spark programs - Strata SJ 2016
Introduzione al TDD

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
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
PDF
Puppet catalog Diff; Raphael Pinson
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
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
Puppet catalog Diff; Raphael Pinson

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Empathic Computing: Creating Shared Understanding
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
KodekX | Application Modernization Development
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Modernizing your data center with Dell and AMD
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Reach Out and Touch Someone: Haptics and Empathic Computing
Empathic Computing: Creating Shared Understanding
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
KodekX | Application Modernization Development
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Advanced methodologies resolving dimensionality complications for autism neur...
20250228 LYD VKU AI Blended-Learning.pptx
Modernizing your data center with Dell and AMD
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx

Test Driven Development with Puppet