$30 off During Our Annual Pro Sale. View details »

TDDing tmux

TDDing tmux

Seth Vargo

April 04, 2013
Tweet

More Decks by Seth Vargo

Other Decks in Technology

Transcript

  1. tmux TDDing

  2. ? Ruby

  3. ? Operations

  4. None
  5. http://www.wikihow.com/Make-Friends

  6. How to apply the testing cookbooks to chef know in

    Ruby stuff you already because Chef is just Ruby and you already know Ruby, so strap in
  7. t f g sethvargo

  8. t f g sethvargo

  9. None
  10. opscode.com/careers 8 FSF)JSJOH

  11. Let's talk about testing

  12. It all starts on git clone git://github.com/opscode/chef-repo.git

  13. github.com/opscode/chef-repo

  14. source 'https://rubygems.org' gem 'chef' gem 'chefspec' gem 'fauxhai' gem 'foodcritic'

    gem 'strainer' # Guard gem 'guard' gem 'guard-bundler' gem 'guard-rspec' gem 'rb-fsevent' gem 'terminal-notifier-guard' Gemfile
  15. Chef

  16. ChefSpec

  17. Fauxhai

  18. Foodcritic

  19. Strainer

  20. Guard

  21. 1 Create Cookbook

  22. $ knife cookbook create tmux

  23. $ knife cookbook create tmux ** Creating cookbook tmux **

    Creating README for cookbook: tmux ** Creating CHANGELOG for cookbook: tmux ** Creating metadata for cookbook: tmux
  24. None
  25. None
  26. None
  27. maintainer 'everyone' maintainer_email 'everyone@mountainwest.conf' license 'apache2' description 'Installs and configures

    tmux' long_description # ... version '0.1.0' metadata.rb
  28. 2 Write Specs

  29. require 'chefspec' spec/spec_helper.rb

  30. require 'spec_helper' describe 'tmux::default' do end spec/default_spec.rb

  31. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } end

    spec/default_spec.rb
  32. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } it

    'installs the tmux package' do expect(runner).to install_package('tmux') end end spec/default_spec.rb
  33. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } it

    'installs the tmux package' do expect(runner).to install_package('tmux') end it 'creates the tmux.conf file' do expect(runner).to create_file('/etc/tmux.conf') end end spec/default_spec.rb
  34. Wat?

  35. None
  36. Pour some sugar on me!

  37. Pour some sugar on me! color

  38. --color .rspec

  39. $ rspec cookbooks/tmux/spec/default_spec.rb

  40. $ rspec cookbooks/tmux/spec/default_spec.rb Failures: 1) tmux::default should install the tmux

    package Failure/Error: let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } TypeError: can't convert Symbol into Integer # ./cookbooks/bluepill/attributes/default.rb: 18:in `[]' # ./cookbooks/bluepill/attributes/default.rb: 18:in `from_file' # ./cookbooks/tmux/spec/default_spec.rb:4:in `block (2 levels) in <top (required)>' # ./cookbooks/tmux/spec/default_spec.rb:7:in `block (2 levels) in <top (required)>' 2) tmux::default should create the tmux.conf file Failure/Error: let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') }
  41. None
  42. require 'spec_helper' describe 'tmux::default' do before { Fauxhai.mock(platform: 'ubuntu', version:

    '12.04') } let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } end Before
  43. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new( platform: 'ubuntu',

    version: '12.04' ).converge('tmux::default') } end After
  44. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new( platform: 'ubuntu',

    version: '12.04' ).converge('tmux::default') } it 'installs the tmux package' do expect(runner).to install_package('tmux') end it 'creates the tmux.conf file' do expect(runner).to create_file('/etc/tmux.conf') end end After
  45. $ rspec cookbooks/tmux/spec/default_spec.rb

  46. $ rspec cookbooks/tmux/spec/default_spec.rb Failures: 1) tmux::default should install the tmux

    package Failure/Error: runner.should install_package 'tmux' No package resource named 'tmux' with action :install found. # ./cookbooks/tmux/spec/default_spec.rb:8:in `block (2 levels) in <top (required)>' 2) tmux::default should create the tmux.conf file Failure/Error: runner.should create_file('/etc/ tmux.conf') No file resource named '/etc/tmux.conf' with action :create found. # ./cookbooks/tmux/spec/default_spec.rb:12:in `block (2 levels) in <top (required)>' Finished in 0.14994 seconds 2 examples, 2 failures
  47. 3 Setup Guard

  48. $ guard init

  49. $ guard init Writing new Guardfile to [...]/Guardfile

  50. guard 'rspec', spec_paths: ['spec', 'cookbooks/*/spec'] do end Guardfile

  51. guard 'rspec', spec_paths: ['spec', 'cookbooks/*/spec'] do watch(%r{^spec/.+_spec\.rb$}) end Guardfile

  52. guard 'rspec', spec_paths: ['spec', 'cookbooks/*/spec'] do watch(%r{^spec/.+_spec\.rb$}) watch('spec/spec_helper.rb') { 'spec'

    } end Guardfile
  53. guard 'rspec', spec_paths: ['spec', 'cookbooks/*/spec'] do watch(%r{^spec/.+_spec\.rb$}) watch('spec/spec_helper.rb') { 'spec'

    } watch(%r{^cookbooks/(.+)/recipes/(.+)\.rb$}) do |m| "cookbooks/#{m[1]}/spec/#{m[2]}_spec.rb" end end Guardfile
  54. guard 'rspec', spec_paths: ['spec', 'cookbooks/*/spec'] do watch(%r{^spec/.+_spec\.rb$}) watch('spec/spec_helper.rb') { 'spec'

    } watch(%r{^cookbooks/(.+)/recipes/(.+)\.rb$}) do |m| "cookbooks/#{m[1]}/spec/#{m[2]}_spec.rb" end watch(%r{^cookbooks/([A-Za-z]+)/(.+)(\..*)?$}) do |m| "cookbooks/#{m[1]}/spec/*.rb" end end Guardfile
  55. $ guard

  56. $ guard Guard is now watching at '[...]/chef-repo' Guard::RSpec is

    running Running all specs Failures: 1) tmux::default should install the tmux package Failure/Error: runner.should install_package 'tmux' No package resource named 'tmux' with action :install found. # ./cookbooks/tmux/spec/default_spec.rb:8:in `block (2 levels) in <top (required)>' 2) tmux::default should create the tmux.conf file Failure/Error: runner.should create_file('/etc/ tmux.conf') No file resource named '/etc/tmux.conf' with action :create found. # ./cookbooks/tmux/spec/default_spec.rb:12:in `block (2 levels) in <top (required)>'
  57. recipes/default.rb # Install the tmux package package 'tmux'

  58. # Install the tmux package package 'tmux' # Drop the

    tmux configuration template template '/etc/tmux.conf' do source 'tmux.conf.erb' mode '0644' end recipes/default.rb
  59. $ guard Guard is now watching at '[...]/chef-repo' Guard::RSpec is

    running Running all specs ** Finished in 0.0003 seconds 2 examples, 0 failures
  60. Wat?

  61. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } it

    'installs the tmux package' do expect(runner).to install_package('tmux') end it 'creates the tmux.conf file' do expect(runner).to create_file('/etc/tmux.conf') end end spec/default_spec.rb
  62. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } it

    'installs the tmux package' do expect(runner).to install_package('tmux') end it 'creates the tmux.conf file' do expect(runner).to create_file('/etc/tmux.conf'). with_content('set -g prefix C-a') end end spec/default_spec.rb
  63. $ guard Guard is now watching at '[...]/chef-repo' Guard::RSpec is

    running Running all specs *F Failures: 1) tmux::default should create the tmux.conf file Failure/Error: runner.should create_file('/etc/ tmux.conf') No source template named 'tmux.conf.erb' found in the default locations. # ./cookbooks/tmux/spec/default_spec.rb:12:in `block (2 levels) in <top (required)>' Finished in 0.15691 seconds 2 examples, 1 passed, 1 failures
  64. # Use a better prefix: set -g prefix C-a unbind

    C-b # Change the default delay: set -sg escape-time 1 # Set the window and panes index set -g base-index 1 setw -g pane-base-index 1 # Send prefix to other apps: bind C-a send-prefix # Split windows with more logical keys bind | split-window -h bind - split-window -v # Remap movement keys bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R templates/default/tmux.conf.erb
  65. $ guard Guard is now watching at '[...]/chef-repo' Guard::RSpec is

    running Running all specs ** Finished in 0.0003 seconds 2 examples, 0 failures
  66. 4 Moar Tests

  67. $ foodcritic cookbooks/tmux

  68. $ foodcritic -I my-rules/* cookbooks/tmux

  69. $ foodcritic -I my-rules/* cookbooks/tmux FC045: Consider setting cookbook name

    in metadata: cookbooks/tmux/metadata.rb:1
  70. $ foodcritic -I my-rules/* cookbooks/tmux FC045: Consider setting cookbook name

    in metadata: cookbooks/tmux/metadata.rb:1 $ echo $? 0
  71. $ foodcritic -I my-rules/* -f any cookbooks/tmux FC045: Consider setting

    cookbook name in metadata: cookbooks/tmux/metadata.rb:1 $ echo $? 3
  72. name 'tmux' maintainer 'everyone' maintainer_email 'everyone@mountainwest.conf' license 'apache2' description 'Installs

    and configures tmux' long_description # ... version '0.1.0' metadata.rb
  73. $ knife cookbook test tmux

  74. $ knife cookbook test tmux checking tmux Running syntax check

    on tmux Validating ruby files Validating templates
  75. 5 Simplify

  76. ChefSpec foodcritic knife test

  77. ChefSpec foodcritic knife test Strainer

  78. knife: knife cookbook test $COOKBOOK foodcritic: foodcritic -f any $SANDBOX/$COOKBOOK

    chefspec: bundle exec rspec $SANDBOX/$COOKBOOK Strainerfile
  79. $ strainer test tmux # Straining 'tmux (v0.0.1)' knife |

    knife cookbook test tmux knife | checking tmux knife | Running syntax check on tmux knife | Validating ruby files knife | Validating templates knife | SUCCESS! foodcritic | foodcritic -f any /Users/ sethvargo/Development/chef-is/cookbooks/tmux foodcritic | SUCCESS! chefspec | bundle exec rspec /Users/ sethvargo/Development/chef-is/cookbooks/tmux chefspec | No examples found. chefspec | Finished in 0.00006 seconds chefspec | 0 examples, 0 failures chefspec | SUCCESS!
  80. ? Bonus

  81. #!/bin/bash set -e set +x git submodule update --init 2>&1

    > /dev/null bundle install --quiet rm -Rf .colander strainer test `echo $JOB_NAME | sed 's/_cookbook//'` script/ci
  82. script/ci

  83. sethvargo@opscode.com E b yz

  84. ? Questions