Puppet loves RSpec, why you should, too

  • 343 views
Uploaded on

A quick overview of using Puppet and RSpec given at Puppet Camp Austin, April 2013.

A quick overview of using Puppet and RSpec given at Puppet Camp Austin, April 2013.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
343
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
6
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Puppet ❤ RSpecWhy You Should, Too
  • 2. Dennis RoweSenior DevOps EngineerCirrus Logic (cirrus.com)shr3kst3r on Twitter, Github, and Freenode
  • 3. • Setup of Puppet RSpec• Quick RSpec primer• Review some RSpec code• Opinions on testing
  • 4. Testing is like freekittens
  • 5. Not Testing?
  • 6. So, RSpechttps://github.com/rodjek/rspec-puppethttp://rspec-puppet.com/tutorial/https://github.com/puppetlabs/puppetlabs_spec_helper
  • 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. % 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. • http://rubygems.org• Specifies exactly what gems are used• `bundle install` installs the gems inour gemsetGemfile Time
  • 10. Example Gemfilesource :rubygemsgem "puppet", "~> 3.1.1"gem "puppetlabs_spec_helper", "~> 0.4.1"
  • 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. 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. Setup .fixtures.yml% cat .fixtures.ymlfixtures:symlinks:puppet-camp-austin: "#{source_dir}"
  • 14. Quick Overview ofRSpec Constructs
  • 15. describe• Expresses the concept of the block• They can be nested• The first level describe should be aclass, define, function, or host
  • 16. Example describedescribe “myfunction” dodescribe “it should do this” do...enddescribe “it should also do this”...endend
  • 17. letslet(:title) { ‘namevar’ }let(:params) {{‘var1’ => ‘foo’, ...}}let(:node) { ‘ahost.example.com’ }let(:facts) {{:osfamily => Debian, ...}}
  • 18. Matchers• include_class• contain_{package, service, user, etc}
  • 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. Now moving on
  • 21. So, let us add anexample class
  • 22. % cat manifests/example1.ppclass puppet-camp-austin::example1 {}
  • 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. 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. Oh no! We Failed!
  • 26. % cat manifests/example1.ppclass puppet-camp-austin::example1 {package { git:ensure => installed}}
  • 27. % rake spec.Finished in 0.11266 seconds1 example, 0 failures
  • 28. Now we are cookingwith peanut oil!
  • 29. Happy Happy Happy
  • 30. Deep Thoughts
  • 31. “It is what you don’texpect that mostneeds looking for.”-- Neal Stephenson, Anathem
  • 32. What should we test?• Non-obvious things• Complex things• Bugs
  • 33. Time for somethingnot obvious
  • 34. Let us say we decided wewanted to let a puppetforge module install gitfor us. Oh look, there isa module calledpuppetlabs/git!
  • 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. Add the git class% cat manifests/example2.ppclass puppet-camp-austin::example2{include git}
  • 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. 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. What?The git moduledoesn’t install git?Is this crazy??!
  • 40. OK, let us go lookclass git {  package { git-core:    ensure => installed,  }}
  • 41. Should you be testing3rd party modulesthis way?Probably not.It depends.
  • 42. Has it burned you inthe past?It did me :(
  • 43. Let us try this again
  • 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. Maybe we go tolunch, forget aboutour test, come back,write some code...
  • 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. Test it!rspec ./spec/classes/puppet-camp-austin/example3_spec.rb:15 #puppet-camp-austin::example3operating system is MINIX shouldnotify a warning
  • 48. D’oh!
  • 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. It works!% rake spec..Finished in 0.13768 seconds2 examples, 0 failures
  • 51. In Retrospect
  • 52. Puppet + RSpecRocks
  • 53. Why are you notdoing it!!?
  • 54. You should testinteresting things
  • 55. Don’t be scared tobreak it!
  • 56. My contrived examplesare at https://github.com/shr3kst3r/puppet-camp-austin
  • 57. Questions and maybesome answers?