Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test Driven Development for Puppet

Test Driven Development for Puppet

Puppet Camp London presentation introducing the tools and techniques used for testing Puppet code. Also introduces Test Driven Development, what it is and why it's useful.

Gareth Rushgrove

April 04, 2014
Tweet

More Decks by Gareth Rushgrove

Other Decks in Programming

Transcript

  1. As a software developer it’s your job to learn software

    engineering practices Gareth Rushgrove
  2. First lets write a test. For this we use the

    RSpec testing framework Gareth Rushgrove
  3. require 'person' ! describe Person, "#say" do it "should say

    something" do ! end end Gareth Rushgrove
  4. 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
  5. 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
  6. sample should contain File[/tmp/sample] (FAILED - 1) ! Finished in

    0.4584 seconds 1 example, 1 failure Gareth Rushgrove
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. --- 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
  14. 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
  15. 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
  16. 6 classes, 2 defines, 413 lines of puppet code, 387

    lines of test code Gareth Rushgrove