Puppet Loves RSpec, Why You Should, Too

4,419 views

Published on

"Puppet Loves RSpec, Why You Should, Too" by Dennis Rowe at Puppet Camp Austin 2013.

Published in: Technology, Spiritual

Puppet Loves RSpec, Why You Should, Too

  1. 1. Puppet ❤ RSpecWhy You Should, Too
  2. 2. Dennis RoweSenior DevOps EngineerCirrus Logic (cirrus.com)shr3kst3r on Twitter, Github, and Freenode
  3. 3. • Setup of Puppet RSpec• Quick RSpec primer• Review some RSpec code• Opinions on testing
  4. 4. Testing is like freekittens
  5. 5. Not Testing?
  6. 6. So, RSpechttps://github.com/rodjek/rspec-puppethttp://rspec-puppet.com/tutorial/https://github.com/puppetlabs/puppetlabs_spec_helper
  7. 7. First, Install RVM• RVM = Ruby Version Manager• https://rvm.io/• Allows us to manage ruby versions andgemsets• Specify ruby version in repo/.rvmrc• source ~/.rvm/scripts/rvm
  8. 8. % curl -L https://get.rvm.io |bash -s stable --rails --autolibs=enabled% rvm use ruby-1.9.3@puppet-camp-austin --install --create % ruby -vruby 1.9.3p392 [x86_64-darwin12.2.1]
  9. 9. • http://rubygems.org• Specifies exactly what gems are used• `bundle install` installs the gems inour gemsetGemfile Time
  10. 10. Example Gemfilesource :rubygemsgem "puppet", "~> 3.1.1"gem "puppetlabs_spec_helper", "~> 0.4.1"
  11. 11. Gemfile.lockGEMremote: http://rubygems.org/specs:diff-lcs (1.2.4)facter (1.7.0)hiera (1.2.1)json_purejson_pure (1.7.7)puppet (3.1.1)facter (~> 1.6)
  12. 12. RSpec Prep• mkdir -p spec/classes/puppet-camp-austin/• Add Rakefilerequire rubygemsrequire puppetlabs_spec_helper/rake_tasks• Add spec/spec_helper.rbrequire rubygemsrequire puppetlabs_spec_helper/module_spec_helper
  13. 13. Setup .fixtures.yml% cat .fixtures.ymlfixtures:symlinks:puppet-camp-austin: "#{source_dir}"
  14. 14. Quick Overview ofRSpec Constructs
  15. 15. describe• Expresses the concept of the block• They can be nested• The first level describe should be aclass, define, function, or host
  16. 16. Example describedescribe “myfunction” dodescribe “it should do this” do...enddescribe “it should also do this”...endend
  17. 17. letslet(:title) { ‘namevar’ }let(:params) {{‘var1’ => ‘foo’, ...}}let(:node) { ‘ahost.example.com’ }let(:facts) {{:osfamily => Debian, ...}}
  18. 18. Matchers• include_class• contain_{package, service, user, etc}
  19. 19. should or should_notit { should include_class(‘foobarz’) }it { should_not include_class(‘foobarz’)}it { should contain_package(‘fooz’) }it { should contain_user(‘barz’) }
  20. 20. Now moving on
  21. 21. So, let us add anexample class
  22. 22. % cat manifests/example1.ppclass puppet-camp-austin::example1 {}
  23. 23. cat spec/classes/puppet-camp-austin/example1_spec.rbrequire spec_helperdescribe puppet-camp-austin::example1 doit should include git doshould contain_package(git)endend
  24. 24. Let us run RSpec% rake specFFailures:1) puppet-camp-austin::example1 should include gitFailure/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)>
  25. 25. Oh no! We Failed!
  26. 26. % cat manifests/example1.ppclass puppet-camp-austin::example1 {package { git:ensure => installed}}
  27. 27. % rake spec.Finished in 0.11266 seconds1 example, 0 failures
  28. 28. Now we are cookingwith peanut oil!
  29. 29. Happy Happy Happy
  30. 30. Deep Thoughts
  31. 31. “It is what you don’texpect that mostneeds looking for.”-- Neal Stephenson, Anathem
  32. 32. What should we test?• Non-obvious things• Complex things• Bugs
  33. 33. Time for somethingnot obvious
  34. 34. Let us say we decided wewanted to let a puppetforge module install gitfor us. Oh look, there isa module calledpuppetlabs/git!
  35. 35. Let us add the moduleto .fixtures.yml% cat .fixtures.ymlfixtures:repositories:git: git://github.com/puppetlabs/puppetlabs-git.gitsymlinks:puppet-camp-austin: "#{source_dir}"
  36. 36. Add the git class% cat manifests/example2.ppclass puppet-camp-austin::example2{include git}
  37. 37. Add a test% cat spec/classes/puppet-camp-austin/example2_spec.rbrequire spec_helperdescribe puppet-camp-austin::example2 doit should include git doshould contain_package(git)endend
  38. 38. Run the Tests (I am surethey will pass)% rake spec.FFailures:1) puppet-camp-austin::example2 should include gitFailure/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)>
  39. 39. What?The git moduledoesn’t install git?Is this crazy??!
  40. 40. OK, let us go lookclass git {  package { git-core:    ensure => installed,  }}
  41. 41. Should you be testing3rd party modulesthis way?Probably not.It depends.
  42. 42. Has it burned you inthe past?It did me :(
  43. 43. Let us try this again
  44. 44. We write a testdescribe puppet-camp-austin::example3 dodescribe operating system is Debian dolet(:facts) { {:operatingsystem => Debian} }it should include git doshould contain_package(git)endenddescribe operating system is MINIX dolet(:facts) { {:operatingsystem => MINIX} }it should notify a warning doshould contain_notify(git cannot be install for this package)endendend
  45. 45. Maybe we go tolunch, forget aboutour test, come back,write some code...
  46. 46. This is the code we writeclass puppet-camp-austin::example3 {Package { ensure => "installed" }case $operatingsystem {RedHat, CentOS: { package {git: } }/^(Debian|Ubuntu)$/:{ package {git: } }default: { package {git-core: } }}}
  47. 47. Test it!rspec ./spec/classes/puppet-camp-austin/example3_spec.rb:15 #puppet-camp-austin::example3operating system is MINIX shouldnotify a warning
  48. 48. D’oh!
  49. 49. Let us add that notifyclass 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: } }}}
  50. 50. It works!% rake spec..Finished in 0.13768 seconds2 examples, 0 failures
  51. 51. In Retrospect
  52. 52. Puppet + RSpecRocks
  53. 53. Why are you notdoing it!!?
  54. 54. You should testinteresting things
  55. 55. Don’t be scared tobreak it!
  56. 56. My contrived examplesare at https://github.com/shr3kst3r/puppet-camp-austin
  57. 57. Questions and maybesome answers?

×