Slide 1

Slide 1 text

Test Driven Development! for Puppet! Puppet needs software development Gareth Rushgrove

Slide 2

Slide 2 text

Who (Who is this person?)

Slide 3

Slide 3 text

@garethr

Slide 4

Slide 4 text

UK Government Digital Service

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

The problem (This isn’t a rant, but…)

Slide 9

Slide 9 text

Who here is a software developer? Gareth Rushgrove

Slide 10

Slide 10 text

If you’re writing Puppet code you’re a software developer Gareth Rushgrove

Slide 11

Slide 11 text

As a software developer it’s your job to learn software engineering practices Gareth Rushgrove

Slide 12

Slide 12 text

What is Test Driven Development (And why should you care)

Slide 13

Slide 13 text

A common practice in software engineering Gareth Rushgrove

Slide 14

Slide 14 text

Not just testing Gareth Rushgrove

Slide 15

Slide 15 text

Encourages simple designs and inspires confidence Gareth Rushgrove

Slide 16

Slide 16 text

First write an (initially failing) automated test case Gareth Rushgrove

Slide 17

Slide 17 text

Then produce the minimum amount of code to pass that test Gareth Rushgrove

Slide 18

Slide 18 text

And finally refactor the new code to acceptable standards Gareth Rushgrove

Slide 19

Slide 19 text

Test Driven Design Gareth Rushgrove

Slide 20

Slide 20 text

Gareth Rushgrove

Slide 21

Slide 21 text

Unit testing with RSpec and Guard (Not puppet specific)

Slide 22

Slide 22 text

A unit is the smallest testable part of an application Gareth Rushgrove

Slide 23

Slide 23 text

Testing puppet requires a little Ruby knowledge so we’ll use Ruby examples Gareth Rushgrove

Slide 24

Slide 24 text

class Person def say(word) end end Gareth Rushgrove

Slide 25

Slide 25 text

First lets write a test. For this we use the RSpec testing framework Gareth Rushgrove

Slide 26

Slide 26 text

require 'person' ! describe Person, "#say" do it "should say something" do ! end end Gareth Rushgrove

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Now lets run our test. It should fail Gareth Rushgrove

Slide 29

Slide 29 text

rspec Gareth Rushgrove

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Now lets write the implementation Gareth Rushgrove

Slide 32

Slide 32 text

class Person def say(word) word + " everyone" end end Gareth Rushgrove

Slide 33

Slide 33 text

And run our test again Gareth Rushgrove

Slide 34

Slide 34 text

Person#say should say something ! Finished in 0.00199 seconds 1 example, 0 failures Gareth Rushgrove

Slide 35

Slide 35 text

Why not have tests automatically run whenever you change the code? Gareth Rushgrove

Slide 36

Slide 36 text

That’s what Guard does Gareth Rushgrove

Slide 37

Slide 37 text

guard :rspec, cmd: 'bundle exec rspec' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/.+\.rb$}) { 'spec' } end Gareth Rushgrove

Slide 38

Slide 38 text

guard Gareth Rushgrove

Slide 39

Slide 39 text

Lets see a quick demo Gareth Rushgrove

Slide 40

Slide 40 text

Why test puppet code at all (Testing declarative languages)

Slide 41

Slide 41 text

Modules increasingly contain logic Gareth Rushgrove

Slide 42

Slide 42 text

Modules increasingly take arguments Gareth Rushgrove

Slide 43

Slide 43 text

Modules increasingly have interfaces with other modules Gareth Rushgrove

Slide 44

Slide 44 text

Modules increasingly used in many operating system and version combinations Gareth Rushgrove

Slide 45

Slide 45 text

Modules increasingly used in many Ruby and Puppet version combinations Gareth Rushgrove

Slide 46

Slide 46 text

Unit testing puppet with rspec-puppet (Finally some puppet code)

Slide 47

Slide 47 text

Unit testing for Puppet

Slide 48

Slide 48 text

A very simple puppet class Gareth Rushgrove

Slide 49

Slide 49 text

class sample { } Gareth Rushgrove

Slide 50

Slide 50 text

First write the test Gareth Rushgrove

Slide 51

Slide 51 text

require 'spec_helper' ! describe "sample" do it { should create_file('/tmp/sample')} end Gareth Rushgrove

Slide 52

Slide 52 text

Then run the test Gareth Rushgrove

Slide 53

Slide 53 text

sample should contain File[/tmp/sample] (FAILED - 1) ! Finished in 0.4584 seconds 1 example, 1 failure Gareth Rushgrove

Slide 54

Slide 54 text

And then write the (puppet) code to make the test pass Gareth Rushgrove

Slide 55

Slide 55 text

class sample { file { "/tmp/sample": ensure => present, } } Gareth Rushgrove

Slide 56

Slide 56 text

sample should contain File[/tmp/sample] ! Finished in 0.3881 seconds 1 example, 0 failures Gareth Rushgrove

Slide 57

Slide 57 text

Lets run the tests automatically whenever you change anything Gareth Rushgrove

Slide 58

Slide 58 text

guard :rspec, cmd: 'bundle exec rspec' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^manifests/.+\.pp$}) { 'spec' } end Gareth Rushgrove

Slide 59

Slide 59 text

Lets see a quick demo of that too Gareth Rushgrove

Slide 60

Slide 60 text

You can also test hosts, defines, facts, functions, hieradata Gareth Rushgrove

Slide 61

Slide 61 text

Syntax checking, linting, oh my (Creating a build process)

Slide 62

Slide 62 text

puppet-lint Gareth Rushgrove

Slide 63

Slide 63 text

Puppet! style guide

Slide 64

Slide 64 text

Available! as a gem

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

puppet-syntax Gareth Rushgrove

Slide 67

Slide 67 text

Validate Puppet and ERB syntax

Slide 68

Slide 68 text

require 'puppet-syntax/tasks/puppet-syntax' Gareth Rushgrove

Slide 69

Slide 69 text

rake syntax ---> syntax:manifests ---> syntax:templates ---> syntax:hiera:yaml Gareth Rushgrove

Slide 70

Slide 70 text

What is Rake and why do we use it (Still no puppet)

Slide 71

Slide 71 text

Rake is a Ruby! build tool Gareth Rushgrove

Slide 72

Slide 72 text

It’s like Make but in Ruby Gareth Rushgrove

Slide 73

Slide 73 text

It’s very easy to distribute Rake tasks as Ruby gems Gareth Rushgrove

Slide 74

Slide 74 text

rake Gareth Rushgrove

Slide 75

Slide 75 text

rake Gareth Rushgrove

Slide 76

Slide 76 text

rake -T Gareth Rushgrove

Slide 77

Slide 77 text

Lets make a command to run lint, syntax and spec Gareth Rushgrove

Slide 78

Slide 78 text

task :test => [ :syntax, :lint, :spec, ] Gareth Rushgrove

Slide 79

Slide 79 text

rake test Gareth Rushgrove

Slide 80

Slide 80 text

Acceptance testing with beaker (Living on the edge)

Slide 81

Slide 81 text

Acceptance test against real systems Gareth Rushgrove

Slide 82

Slide 82 text

Test what actually happens, not what is meant to happen Gareth Rushgrove

Slide 83

Slide 83 text

Build by Gareth Rushgrove

Slide 84

Slide 84 text

Very new Gareth Rushgrove

Slide 85

Slide 85 text

Test against different operating systems Gareth Rushgrove

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

Supports multiple hypervisors Gareth Rushgrove

Slide 89

Slide 89 text

Vagrant hypervisor

Slide 90

Slide 90 text

VSphere hypervisor

Slide 91

Slide 91 text

Helpers to install puppet and modules Gareth Rushgrove

Slide 92

Slide 92 text

install_puppet Gareth Rushgrove

Slide 93

Slide 93 text

puppet('module', 'install', 'puppetlabs-stdlib') Gareth Rushgrove

Slide 94

Slide 94 text

Test that Puppet runs without errors Gareth Rushgrove

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

Test runs are idempotent Gareth Rushgrove

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

Test that the module installs packages, run services, etc. Gareth Rushgrove

Slide 99

Slide 99 text

Gareth Rushgrove

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

Other useful tools (and what we’re still missing)

Slide 102

Slide 102 text

Fixtures, matchers

Slide 103

Slide 103 text

Gareth Rushgrove

Slide 104

Slide 104 text

Nice continuous integration

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

Test pull request branches too

Slide 107

Slide 107 text

--- 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

Slide 108

Slide 108 text

Official! ruby support

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

Experimental code coverage support in rspec-puppet master Gareth Rushgrove

Slide 111

Slide 111 text

at_exit { RSpec::Puppet::Coverage.report! } Gareth Rushgrove

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

A puppet module skeleton with everything working out of the box Gareth Rushgrove

Slide 114

Slide 114 text

puppet module skeleton

Slide 115

Slide 115 text

puppet module generate sample Gareth Rushgrove

Slide 116

Slide 116 text

A pretty complete example (The Docker module)

Slide 117

Slide 117 text

Gareth Rushgrove

Slide 118

Slide 118 text

Gareth Rushgrove Featured on the Forge

Slide 119

Slide 119 text

Gareth Rushgrove 50 pull request and counting

Slide 120

Slide 120 text

Gareth Rushgrove Contributing guidelines

Slide 121

Slide 121 text

Gareth Rushgrove

Slide 122

Slide 122 text

Gareth Rushgrove Currently has 121 tests

Slide 123

Slide 123 text

6 classes, 2 defines, 413 lines of puppet code, 387 lines of test code Gareth Rushgrove

Slide 124

Slide 124 text

Take away (If all you remember is…)

Slide 125

Slide 125 text

Infrastructure as code Gareth Rushgrove

Slide 126

Slide 126 text

The first test is the hardest Gareth Rushgrove

Slide 127

Slide 127 text

Politely demand tests for contributions Gareth Rushgrove

Slide 128

Slide 128 text

Test the interface not the implementation Gareth Rushgrove

Slide 129

Slide 129 text

Questions? (And thanks for listening)