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

Test Driven Infrastructure with Chef

Test Driven Infrastructure with Chef

These are the slides from my 2013 Velocity talk on Chef Driven Infrastructure with Chef using ChefSpec, Test Kitchen, GitHub, Jenkins, and more!

Seth Vargo

June 18, 2013
Tweet

More Decks by Seth Vargo

Other Decks in Technology

Transcript

  1. Gemfile source 'https://rubygems.org' group :test do gem 'chefspec', '~> 1.3'

    gem 'foodcritic', '~> 2.1' gem 'strainer', '~> 3.0' gem 'test-kitchen', '~> 1.0.0.alpha' gem 'kitchen-lxc', '~> 0.0.1.beta1' gem 'knife-spork', '~> 1.0.17' gem 'hipchat', '~> 0.10.0' gem 'guard', '~> 1.8' gem 'guard-foodcritic', '~> 1.0' gem 'guard-rspec', '~> 3.0' end
  2. spec/default_spec.rb require 'spec_helper' describe 'sethvargo-myface::default' do let(:chef_run) do run =

    ChefSpec::ChefRunner.new(platfrom: 'ubuntu', version: '12.04') run.converge('sethvargo-myface::default') end it 'installs apache2' do expect(chef_run).to install_package('apache2') end # ... end
  3. Guardfile guard :rspec, all_on_start: false do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^(recipes)/(.+)\.rb$}) { |m|

    "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { 'spec' } end guard :foodcritic, cookbook_paths: '.', all_on_start: false do watch(%r{attributes/.+\.rb$}) watch(%r{providers/.+\.rb$}) watch(%r{recipes/.+\.rb$}) watch(%r{resources/.+\.rb$}) end
  4. Strainerfile knife: bundle exec knife cookbook test $COOKBOOK foodcritic: bundle

    exec foodcritic $SANDBOX/$COOKBOOK -f any rspec: (cd $COOKBOOK && bundle exec rspec --color)
  5. 1

  6. 1

  7. 1

  8. S

  9. I, _________, solemnly swear under penalty of my GPU catching

    fire, that I will not touch anyone's build but my own.  NO ASSHOLE PLEDGE
  10. 2

  11. 2

  12. 2

  13. 3

  14. spec/default_spec.rb describe 'sethvargo-myface::default' do # pre-existing tests # ... it

    'creates the default template' do expect(chef_run).to create_file('/var/www/index.html') end it 'creates the site with the correct content' do template = chef_run.template('/var/www/index.html') expect(template.owner).to eq('root') expect(template.group).to eq('root') end end
  15. recipes/default.rb package 'apache2' service 'apache2' do action [:enable, :start] end

    template '/var/www/index.html' do owner 'root' group 'root' mode '0755' source 'index.html.erb' end
  16. templates/default/index.html.erb <html> <head> <title>Welcome to <%= node['fqdn'] %></title> </head> <body>

    <p>Here's everything you need to know about <%= node['fqdn'] %>:</p> <pre><%= JSON.pretty_generate(node.to_hash) %></pre> </body> </html>
  17. c

  18. J

  19. J

  20. J

  21. J

  22. metadata.rb name 'myface' maintainer 'YOUR_COMPANY_NAME' maintainer_email 'YOUR_EMAIL' license 'All rights

    reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.0'
  23. metadata.rb name 'myface' maintainer 'Seth Vargo' maintainer_email '[email protected]' license 'All

    rights reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.0'
  24. S

  25. J

  26. J

  27. J

  28. J

  29. J

  30. J

  31. R

  32. J

  33. J

  34. J

  35. q

  36. q

  37. .kitchen.yml driver_plugin: lxc driver_config: use_sudo: true platforms: - name: ubuntu-12.04

    driver_config: base_container: ubuntu_12.04 username: ubuntu password: ubuntu suites: - name: default run_list: ["recipe[myface]"] attributes: {}
  38. Strainerfile knife: bundle exec knife cookbook test $COOKBOOK foodcritic: bundle

    exec foodcritic $SANDBOX/$COOKBOOK -f any rspec: (cd $COOKBOOK && bundle exec rspec --color) kitchen: (cd $COOKBOOK && bundle exec kitchen test)
  39. metadata.rb name 'myface' maintainer 'Seth Vargo' maintainer_email '[email protected]' license 'All

    rights reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.0'
  40. metadata.rb name 'myface' maintainer 'Seth Vargo' maintainer_email '[email protected]' license 'All

    rights reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.1'
  41. q

  42. environments/production.json { "name" : "production", "description" : "Production cluster in

    EC2", "override_attributes" : { ... }, "default_attributes" : { ... } }
  43. environments/production.json { "name" : "production", "description" : "Production cluster in

    EC2", "override_attributes" : { ... }, "default_attributes" : { ... }, "cookbook_versions" : { "myface": "0.1.0" }, }
  44. CD